diff --git a/packages/cli/src/lib/auth-hint.ts b/packages/cli/src/lib/auth-hint.ts index 6ceacd433..9e263824a 100644 --- a/packages/cli/src/lib/auth-hint.ts +++ b/packages/cli/src/lib/auth-hint.ts @@ -1,13 +1,14 @@ /** * One-shot env-token-ignored hint. * - * When a user sets SENTRY_AUTH_TOKEN (or SENTRY_TOKEN) but also has a - * stored OAuth login from `sentry auth login`, the CLI silently prefers - * the stored login. Users then wonder why their valid provisioned - * token isn't being used — this was the most painful item in the CLI - * UX feedback issue (getsentry/cli#785 #4). The hint surfaces the - * collision on stderr the first time the CLI reaches for auth in a - * given process. + * When a user sets SENTRY_AUTH_TOKEN (or SENTRY_TOKEN) — or a `token` + * in `.sentryclirc`, which the env shim copies into SENTRY_AUTH_TOKEN — + * but also has a stored OAuth login from `sentry auth login`, the CLI + * silently prefers the stored login. Users then wonder why their valid + * provisioned token isn't being used — this was the most painful item + * in the CLI UX feedback issue (getsentry/cli#785 #4). The hint surfaces + * the collision on stderr the first time the CLI reaches for auth in a + * given process, naming the real source (env var vs `.sentryclirc`). * * Design notes: * - Fires at most once per process (module-local latch). CLI invocations @@ -32,6 +33,7 @@ import { import { getUserInfo } from "./db/user.js"; import { getEnv } from "./env.js"; import { logger } from "./logger.js"; +import { getRcInjectedTokenSource } from "./sentryclirc.js"; const log = logger.withTag("auth"); @@ -79,15 +81,30 @@ export function maybeWarnEnvTokenIgnored(): void { hintEmitted = true; - const envVar = getActiveEnvVarName(); const userLabel = resolveStoredUserLabel(); log.info( - `Detected ${envVar} env var but using stored login for ${userLabel}.\n` + - " Set SENTRY_FORCE_ENV_TOKEN=1 to prefer the env var." + `${describeIgnoredTokenSource()} but using stored login for ${userLabel}.\n` + + " Set SENTRY_FORCE_ENV_TOKEN=1 to prefer it." ); } +/** + * Describe where the ignored token came from, for the hint's first line. + * + * The `.sentryclirc` shim copies a `[auth] token` into `SENTRY_AUTH_TOKEN`, + * so a bare "Detected SENTRY_AUTH_TOKEN env var" message would misattribute + * the source — the user set a config file, not an env var (getsentry/cli#1345). + * When the shim was the source, name the `.sentryclirc` file instead. + */ +function describeIgnoredTokenSource(): string { + const rcSource = getRcInjectedTokenSource(); + if (rcSource) { + return `Detected a token in .sentryclirc (${rcSource})`; + } + return `Detected ${getActiveEnvVarName()} env var`; +} + /** * Resolve a user-friendly label for the stored OAuth user. * diff --git a/packages/cli/src/lib/sentryclirc.ts b/packages/cli/src/lib/sentryclirc.ts index 78b46a2cb..23f5d9831 100644 --- a/packages/cli/src/lib/sentryclirc.ts +++ b/packages/cli/src/lib/sentryclirc.ts @@ -323,6 +323,24 @@ export function loadSentryCliRc(cwd: string): Promise { return promise; } +/** + * Path of the `.sentryclirc` file whose `token` the shim copied into + * `SENTRY_AUTH_TOKEN`, pinned by {@link applySentryCliRcEnvShim} at the + * one point it knows the provenance. `undefined` when the active env + * token was set by the user rather than injected from rc. + * + * Mirrors the boot-time snapshot pattern in `env-token-host.ts` + * (`pinnedHost` + getter): lets the synchronous env-token-ignored hint + * name the real source instead of claiming the user set an env var they + * never did, without re-awaiting the async rc loader. + */ +let rcInjectedTokenSource: string | undefined; + +/** Read the pinned rc-injected token source. See {@link rcInjectedTokenSource}. */ +export function getRcInjectedTokenSource(): string | undefined { + return rcInjectedTokenSource; +} + /** * Apply env shim for `.sentryclirc` token and URL fields. * @@ -350,6 +368,7 @@ export async function applySentryCliRcEnvShim(cwd: string): Promise { `Setting SENTRY_AUTH_TOKEN from ${CONFIG_FILENAME} (${config.sources.token})` ); env.SENTRY_AUTH_TOKEN = config.token; + rcInjectedTokenSource = config.sources.token; } const normalizedRcUrl = config.url ? normalizeUrl(config.url) : undefined; @@ -400,4 +419,5 @@ export function clearSentryCliRcCache(): void { cache.clear(); // Reset global paths — tests change SENTRY_CONFIG_DIR between runs globalPaths = null; + rcInjectedTokenSource = undefined; } diff --git a/packages/cli/test/lib/auth-hint.test.ts b/packages/cli/test/lib/auth-hint.test.ts index 547762092..b2a5a7b03 100644 --- a/packages/cli/test/lib/auth-hint.test.ts +++ b/packages/cli/test/lib/auth-hint.test.ts @@ -8,8 +8,11 @@ * or when SENTRY_FORCE_ENV_TOKEN is set. * - Repeat calls in the same process are silent. * - User label preference order (username > email > name > fallback). + * - Names the `.sentryclirc` source when the token came from the rc shim. */ +import { writeFileSync } from "node:fs"; +import { join } from "node:path"; import { afterEach, beforeEach, describe, expect, test, vi } from "vitest"; import { maybeWarnEnvTokenIgnored, @@ -17,9 +20,14 @@ import { } from "../../src/lib/auth-hint.js"; import { setAuthToken } from "../../src/lib/db/auth.js"; import { setUserInfo } from "../../src/lib/db/user.js"; +import { + applySentryCliRcEnvShim, + CONFIG_FILENAME, + clearSentryCliRcCache, +} from "../../src/lib/sentryclirc.js"; import { useTestConfigDir } from "../helpers.js"; -useTestConfigDir("auth-hint-"); +const getConfigDir = useTestConfigDir("auth-hint-"); let savedAuthToken: string | undefined; let savedSentryToken: string | undefined; @@ -32,6 +40,7 @@ beforeEach(() => { delete process.env.SENTRY_AUTH_TOKEN; delete process.env.SENTRY_TOKEN; delete process.env.SENTRY_FORCE_ENV_TOKEN; + clearSentryCliRcCache(); resetAuthHintState(); }); @@ -51,6 +60,7 @@ afterEach(() => { } else { delete process.env.SENTRY_FORCE_ENV_TOKEN; } + clearSentryCliRcCache(); }); /** @@ -137,6 +147,29 @@ describe("maybeWarnEnvTokenIgnored", () => { } }); + test("names the .sentryclirc source when the token came from the rc shim", async () => { + setAuthToken("stored_oauth", 3600, "refresh_a"); + setUserInfo({ userId: "u-1", username: "alice" }); + + // The shim reads $SENTRY_CONFIG_DIR/.sentryclirc as a global fallback + // and copies its token into SENTRY_AUTH_TOKEN, recording the source. + const rcPath = join(getConfigDir(), CONFIG_FILENAME); + writeFileSync(rcPath, "[auth]\ntoken = rc-token\n", "utf-8"); + await applySentryCliRcEnvShim(getConfigDir()); + + const cap = captureStderr(); + try { + maybeWarnEnvTokenIgnored(); + expect(cap.hintCalls()).toBe(1); + const text = cap.text(); + expect(text).toContain(`Detected a token in .sentryclirc (${rcPath})`); + expect(text).not.toContain("env var"); + expect(text).toContain("stored login for alice"); + } finally { + cap.restore(); + } + }); + test("is silent on repeat calls within the same process", () => { setAuthToken("stored_oauth", 3600, "refresh_a"); process.env.SENTRY_AUTH_TOKEN = "env_token"; diff --git a/packages/cli/test/lib/sentryclirc.test.ts b/packages/cli/test/lib/sentryclirc.test.ts index bb3ec1233..220c3ea0d 100644 --- a/packages/cli/test/lib/sentryclirc.test.ts +++ b/packages/cli/test/lib/sentryclirc.test.ts @@ -18,6 +18,7 @@ import { assertRcUrlTrusted, CONFIG_FILENAME, clearSentryCliRcCache, + getRcInjectedTokenSource, loadSentryCliRc, } from "../../src/lib/sentryclirc.js"; import { cleanupTestDir, createTestConfigDir } from "../helpers.js"; @@ -232,6 +233,23 @@ describe("applySentryCliRcEnvShim", () => { expect(readEnv("SENTRY_AUTH_TOKEN")).toBe("rc-token"); }); + test("records the rc file as the injected token source", async () => { + delete process.env.SENTRY_AUTH_TOKEN; + delete process.env.SENTRY_TOKEN; + writeRcFile(testDir, "[auth]\ntoken = rc-token\n"); + + await applySentryCliRcEnvShim(testDir); + expect(getRcInjectedTokenSource()).toBe(join(testDir, CONFIG_FILENAME)); + }); + + test("does not record an injected source when a real env token is set", async () => { + process.env.SENTRY_AUTH_TOKEN = "existing-token"; + writeRcFile(testDir, "[auth]\ntoken = rc-token\n"); + + await applySentryCliRcEnvShim(testDir); + expect(getRcInjectedTokenSource()).toBeUndefined(); + }); + test("does not override existing SENTRY_AUTH_TOKEN", async () => { process.env.SENTRY_AUTH_TOKEN = "existing-token"; writeRcFile(testDir, "[auth]\ntoken = rc-token\n");