Skip to content

fix: handle empty org list gracefully during login#450

Open
121watts wants to merge 1 commit intomainfrom
watts/dep-3802-cli-login-nil-org-fix
Open

fix: handle empty org list gracefully during login#450
121watts wants to merge 1 commit intomainfrom
watts/dep-3802-cli-login-nil-org-fix

Conversation

@121watts
Copy link
Contributor

@121watts 121watts commented Mar 12, 2026

Summary

depot login and depot org switch would panic with a nil pointer dereference if the user had no visible organizations.

What was happening

SelectOrganization() returns (nil, nil) when the org list is empty — no error, just a nil pointer. Both login.go and switch.go went straight to org.OrgId without checking for nil → 💥

This affected users whose organizations were cancelled (the API was filtering them out — fixed separately in depot/api#3173) and users with genuinely zero orgs.

What happens now

  • depot login: If no orgs are found, login still succeeds (token is saved) and prints a helpful message: "No organizations found. You can create one at https://depot.dev"
  • depot org switch: Returns a clear error: "no organizations found"

Both paths are now safe from nil pointer panics.

Anything else?

The root cause (API hiding cancelled orgs from ListOrganizations) is fixed in depot/api#3173. This PR is defensive hardening — it prevents the crash regardless of why the org list is empty.

Made with Cursor


Note

Low Risk
Small, defensive CLI error-handling changes with minimal behavioral impact beyond clearer messaging and avoiding a crash.

Overview
Hardens organization selection by introducing helpers.ErrNoOrganizations and returning it from SelectOrganization() when the org list is empty.

depot login now treats the no-org case as a successful auth (token saved) and prints a guidance message, while depot org switch returns a clear "no organizations found" error instead of dereferencing a nil org.

Written by Cursor Bugbot for commit 716f5b7. This will update automatically on new commits. Configure here.

@linear
Copy link

linear bot commented Mar 12, 2026

Copy link

@cursor cursor bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Autofix Details

Bugbot Autofix prepared a fix for the issue found in the latest run.

  • ✅ Fixed: Nil checks are unreachable due to error return
    • Changed SelectOrganization() to return (nil, nil) instead of (nil, error) when no organizations are found, making the nil checks in login.go and switch.go reachable.

Create PR

Or push these changes by commenting:

@cursor push eab8950d75
Preview (eab8950d75)
diff --git a/pkg/helpers/organization.go b/pkg/helpers/organization.go
--- a/pkg/helpers/organization.go
+++ b/pkg/helpers/organization.go
@@ -46,7 +46,7 @@
 	}
 
 	if len(organizations) == 0 {
-		return nil, fmt.Errorf("no organizations found")
+		return nil, nil
 	}
 
 	if len(organizations) == 1 {

@121watts 121watts force-pushed the watts/dep-3802-cli-login-nil-org-fix branch from 21a68d3 to 62c369f Compare March 12, 2026 19:06
Copy link

@cursor cursor bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Bugbot Autofix prepared a fix for the issue found in the latest run.

  • ✅ Fixed: Dead nil check after error already handles case
    • Removed the unreachable nil check since SelectOrganization() never returns (nil, nil) - all nil organization returns are accompanied by non-nil errors.

Create PR

Or push these changes by commenting:

@cursor push 2e50673c4e
Preview (2e50673c4e)
diff --git a/pkg/cmd/org/switch.go b/pkg/cmd/org/switch.go
--- a/pkg/cmd/org/switch.go
+++ b/pkg/cmd/org/switch.go
@@ -23,9 +23,6 @@
 				if err != nil {
 					return err
 				}
-				if org == nil {
-					return fmt.Errorf("no organizations found")
-				}
 				orgId = org.OrgId
 			}

}
if org == nil {
return fmt.Errorf("no organizations found")
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dead nil check after error already handles case

Low Severity

The if org == nil check on lines 26–28 is unreachable dead code. SelectOrganization() never returns (nil, nil) — every code path that returns a nil *Organization also returns a non-nil error, which is already caught and returned by the if err != nil block on lines 23–25. This misleadingly suggests a (nil, nil) return is possible, which could confuse future readers about the API contract.

Fix in Cursor Fix in Web

SelectOrganization() returns ErrNoOrganizations when the org list is
empty (e.g. all orgs cancelled or user has no orgs). The login command
now catches this sentinel error specifically and treats it as non-fatal:
the token is saved, a helpful message is printed directing the user to
depot.dev to create or reactivate an organization, and login exits
successfully.

Also introduces a sentinel error (ErrNoOrganizations) to replace the
untyped fmt.Errorf, enabling callers to distinguish "no orgs" from
real errors like network failures.

Made-with: Cursor
@121watts 121watts force-pushed the watts/dep-3802-cli-login-nil-org-fix branch from 62c369f to 716f5b7 Compare March 12, 2026 22:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant