Skip to content

fix(cli): Use the org from an org auth token when none is set (CLI-3B) - #1938

Merged
BYK merged 1 commit into
mainfrom
fix/cli-3b-org-from-org-auth-token
Nov 4, 2026
Merged

BYK merged 1 commit into
mainfrom
fix/cli-3b-org-from-org-auth-token

Conversation

@sentry-junior

@sentry-junior sentry-junior Bot commented Nov 4, 2026

Copy link
Copy Markdown
Contributor

Org auth tokens (sntrys_…) carry their organization. Legacy sentry-cli used it when --org/SENTRY_ORG was missing (Config::get_org), but the new CLI ignores it. @sentry/bundler-plugins 11.x drives sourcemap upload / release create through createSentrySDK({ token, org, project }), and org is often unset because it was never needed with an org token. Those builds now fail.

This is the largest event source in CLI-3B: in the last 7 days, 1,642 of 3,733 events (44%) came from library mode (command:sourcemap.upload / release.create, no user, CLI 0.45.0). 79% of those passed a project but no org.

resolveFromEnvVars() now falls back to the token's org claim right after SENTRY_ORG. An explicit --org/SENTRY_ORG still wins. A token can only reach its own org, so no other org could have worked.

Before

$ SENTRY_AUTH_TOKEN=sntrys_… sentry release create 1.0.0 --project web
Error: Could not auto-detect organization.

Provide it explicitly:
  sentry release create [<org>/]<version>

Or:
  - Run from a directory with a Sentry DSN in source code or .env files
  - Set SENTRY_ORG and SENTRY_PROJECT (or SENTRY_DSN) environment variables
  - Run 'sentry org list' to find your organization slug
  - Run 'sentry project list <org>/' to find project slugs
$ SENTRY_AUTH_TOKEN=sntrys_… SENTRY_PROJECT=web sentry sourcemap upload ./dist --release 1.0.0
Error: Could not auto-detect organization and project.
…

After (token for acme-corp, local mock server)

$ SENTRY_AUTH_TOKEN=sntrys_… sentry release create 1.0.0 --project web
Release Created: 1.0.0
━━━━━━━━━━━━━━━━━━━━━━

╭──────────┬────────────╮
│ Version  │ 1.0.0      │
│ Status   │ Unreleased │
│ Projects │ web        │
╰──────────┴────────────╯

Release created. Finalize with: sentry release finalize 1.0.0
$ SENTRY_AUTH_TOKEN=sntrys_… SENTRY_PROJECT=web sentry sourcemap upload ./dist --release 1.0.0
# resolves to acme-corp/web and calls GET /api/0/organizations/acme-corp/chunk-upload/

Separate from this fix: about 20% of the library-mode events carry --release undefined (the literal string). That comes from the bundler plugin side, not the CLI.

Unit test note: 11 tests in 8 files (completions, oauth, upgrade, errors, …) fail in my sandbox both on clean main and on this branch. They depend on filesystem permissions and the environment. Everything else passes, and pnpm run tsc and pnpm run lint are clean.

via bete.

--

View Session [Sentry]

Org auth tokens (sntrys_) embed their organization, and legacy sentry-cli
used it when --org/SENTRY_ORG was missing. The new CLI ignored it, so
bundler-plugin and CI setups that pass only a token and a project failed
with "Could not auto-detect organization". Fall back to the token's org
right after SENTRY_ORG.

Co-Authored-By: Miguel Betegon <miguel.betegon@sentry.io>
@vercel

vercel Bot commented Nov 4, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
Project Deployment Actions Updated
sentry-local Skipped Skipped Nov 4, 2026 9:48am UTC

Request Review

@github-actions github-actions Bot added the risk: medium PR risk score: medium label Nov 4, 2026

@BYK BYK left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Interesting, I thought I've already done this earlier.

@BYK
BYK merged commit e4c675f into main Nov 4, 2026
27 checks passed
@BYK
BYK deleted the fix/cli-3b-org-from-org-auth-token branch November 4, 2026 14:58
betegon added a commit that referenced this pull request Nov 4, 2026
…-3B) (#1947)

Follow-up to #1938 for the "Could not auto-detect organization" error.

If an account can reach one organization that has several projects,
commands still fail when nothing is detected from the working directory.
The existing fallback only handles one org with one project.

When auto-detect finds nothing and the account has exactly one org:

- `issue list` lists that org's issues, the same as `sentry issue list
<org>/`.
- Org-scoped commands (`explore`, `replay`, …) use that org.

Accounts with several orgs get the same guidance error as before.
Defaults are never saved, and skipped self-hosted DSNs still show their
own error.

### Before

```
$ sentry issue list --limit 5
Error: Organization and project are required.

Specify them using:
  sentry issue list <org>/<project>

Or:
  - Retry example:
    sentry issue list 'acme-corp/web' --limit 5 --sort date --period '90d'
  - Other available projects: acme-corp/api
  - List projects example: sentry project list 'acme-corp/'
```

```
$ sentry explore --limit 3
Error: Organization is required.

Specify it using:
  sentry explore <target>

Or:
  - SENTRY_ORG environment variable
  - sentry cli defaults
```

### After

```
$ sentry issue list --limit 5
Issues in acme-corp:

╭──────────┬────────────────────────┬────────┬───────┬────────┬───────┬────────╮
│ SHORT ID │ ISSUE                  │ SEEN   │ AGE   │ EVENTS │ USERS │ TRIAGE │
├──────────┼────────────────────────┼────────┼───────┼────────┼───────┼────────┤
│ WEB-1A   │ TypeError: Cannot …    │ 1h ago │ Nov 1 │     42 │     7 │ High   │
├──────────┼────────────────────────┼────────┼───────┼────────┼───────┼────────┤
│ API-7    │ KeyError: 'user_id'    │ 1h ago │ Nov 1 │     42 │     7 │ High   │
╰──────────┴────────────────────────┴────────┴───────┴────────┴───────┴────────╯

Showing 2 issues
```

```
$ sentry explore --limit 3
Querying errors in acme-corp:

╭────────────────────────────────────────────────┬─────────╮
│ TITLE                                          │ COUNT() │
├────────────────────────────────────────────────┼─────────┤
│ TypeError: Cannot read properties of undefined │   1,204 │
│ KeyError: 'user_id'                            │     311 │
╰────────────────────────────────────────────────┴─────────╯

Showing 2 results.
```

I captured this output against a local mock server with one org and two
projects.

### Testing

- New tests in `test/lib/context-recovery.test.ts` cover org-wide `issue
list`, an org-only command, and the multi-org case that still errors.
The first two fail without this change.
- `pnpm run tsc` and `pnpm run lint` pass. In the full CLI unit run, the
only failures are 11 that also fail on `main` in my sandbox. They are
permission and environment checks in completions, db/resilient,
code-scanner, errors, file-changes, oauth, auto-login-host-guard, and
upgrade.

<!-- junior-request-attribution:start -->
via **bete**.
<!-- junior-request-attribution:end -->

<!-- junior-session-footer:start -->
<!-- junior-conversation-id:slack%3AC0C615WFS9G%3A1793783849.302359 -->

--

[View
Session](https://junior-prod.sentry.dev/conversations/slack%3AC0C615WFS9G%3A1793783849.302359)
[[Sentry]](https://sentry.sentry.io/explore/conversations/slack%3AC0C615WFS9G%3A1793783849.302359/?project=4510944073809921)

<!-- junior-session-footer:end -->

Co-authored-by: sentry-junior[bot] <264270552+sentry-junior[bot]@users.noreply.github.com>
Co-authored-by: Miguel Betegon <miguel.betegon@sentry.io>

This branch was previously deployed

1 inactive deployment
Preview — 24b9380d Deployed Nov 4, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

risk: medium PR risk score: medium

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant