fix: handle empty org list gracefully during login#450
Conversation
There was a problem hiding this comment.
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.
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 {21a68d3 to
62c369f
Compare
There was a problem hiding this comment.
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.
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") | ||
| } |
There was a problem hiding this comment.
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.
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
62c369f to
716f5b7
Compare



Summary
depot loginanddepot org switchwould 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. Bothlogin.goandswitch.gowent straight toorg.OrgIdwithout 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.ErrNoOrganizationsand returning it fromSelectOrganization()when the org list is empty.depot loginnow treats the no-org case as a successful auth (token saved) and prints a guidance message, whiledepot org switchreturns 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.