From ab0668eac7b6dc91a4e9afe2394b752ad3389d1b Mon Sep 17 00:00:00 2001 From: "sentry-junior[bot]" <264270552+sentry-junior[bot]@users.noreply.github.com> Date: Thu, 3 Dec 2026 08:35:56 +0000 Subject: [PATCH 1/3] fix(cli): Keep the React Native wizard's auth token out of committed sentry.properties The React Native wizard wrote the auth token to ios/ and android/sentry.properties, which stay committed because builds read the org and project from them. With @sentry/react-native 6.3.0 or later, whose iOS build scripts and sentry.gradle read .env.sentry-build-plugin, the token now goes to that gitignored file, and sentry.properties gets only the org, project and, on self-hosted Sentry, URL. Older or unknown SDK versions keep the token in sentry.properties, with a warning. Co-Authored-By: Antonis Lilis --- .../cli-docs/src/fragments/commands/wizard.md | 6 + .../react-native/react-native-wizard.ts | 151 +++++++++++-- .../cli/src/lib/wizard/utils/clack/index.ts | 5 +- .../react-native/patch-native-files.test.ts | 201 ++++++++++++++++++ 4 files changed, 339 insertions(+), 24 deletions(-) create mode 100644 packages/cli/test/lib/wizard/react-native/patch-native-files.test.ts diff --git a/apps/cli-docs/src/fragments/commands/wizard.md b/apps/cli-docs/src/fragments/commands/wizard.md index c14b8df28..6883fd763 100644 --- a/apps/cli-docs/src/fragments/commands/wizard.md +++ b/apps/cli-docs/src/fragments/commands/wizard.md @@ -87,3 +87,9 @@ builds, so those builds fail on machines without the file: set `SENTRY_AUTH_TOKEN` in CI, or create a `sentry.properties` on each machine. On self-hosted Sentry, the file also holds the server URL, so set `SENTRY_URL` in CI too. + +The React Native wizard writes the auth token to `.env.sentry-build-plugin` in +the project root and adds that file to `.gitignore` (`@sentry/react-native` +6.3.0 and later). `ios/sentry.properties` and `android/sentry.properties` hold +only the org, project and, on self-hosted Sentry, URL, so commit them. In CI, +set `SENTRY_AUTH_TOKEN`. diff --git a/packages/cli/src/lib/wizard/react-native/react-native-wizard.ts b/packages/cli/src/lib/wizard/react-native/react-native-wizard.ts index fd4b455e5..8c7fe5458 100644 --- a/packages/cli/src/lib/wizard/react-native/react-native-wizard.ts +++ b/packages/cli/src/lib/wizard/react-native/react-native-wizard.ts @@ -24,6 +24,7 @@ import { type CliSetupConfigContent, abort, abortIfCancelled, + addDotEnvSentryBuildPluginFile, addSentryCliConfig, askFlagQuestion, confirmContinueIfNoOrDirtyGitRepo, @@ -35,11 +36,15 @@ import { printWelcome, propertiesCliSetupConfig, runPrettierIfInstalled, + SENTRY_DOT_ENV_FILE, + SENTRY_PROPERTIES_FILE, } from "../utils/clack/index.js"; import { getPackageVersion, hasPackageInstalled, } from "../utils/package-json.js"; +import { buildAuthTokenSettingsUrl } from "../utils/project-data.js"; +import { fulfillsVersionRange } from "../utils/semver.js"; import { isExpoCNG, patchExpoAppConfig, @@ -113,6 +118,16 @@ export function findAppBuildGradle(): Promise { export const RN_SDK_PACKAGE = "@sentry/react-native"; export const RN_SDK_SUPPORTED_RANGE = ">=6.12.0"; +/** + * First SDK version whose iOS build scripts and `sentry.gradle` point + * sentry-cli at `.env.sentry-build-plugin` in the React Native root + * (getsentry/sentry-react-native#4281). + */ +const RN_SDK_DOTENV_MIN_VERSION = "6.3.0"; + +const IOS_SENTRY_PROPERTIES = `ios/${SENTRY_PROPERTIES_FILE}`; +const ANDROID_SENTRY_PROPERTIES = `android/${SENTRY_PROPERTIES_FILE}`; +const AUTH_TOKEN_ENV_VAR = "SENTRY_AUTH_TOKEN"; export const RN_PACKAGE = "react-native"; export const RN_HUMAN_NAME = "React Native"; @@ -120,10 +135,13 @@ export const RN_HUMAN_NAME = "React Native"; export const SUPPORTED_RN_RANGE = ">=0.69.0"; export const SUPPORTED_EXPO_RANGE = ">=50.0.0"; -export type RNCliSetupConfigContent = Pick< - Required, - "authToken" | "org" | "project" | "url" ->; +/** The auth token, org, project and web URL the wizard writes to config files. */ +export interface RNCliSetupConfigContent { + authToken: string; + org: string; + project: string; + url: string; +} export async function runReactNativeWizard( params: ReactNativeWizardOptions, @@ -230,7 +248,7 @@ Or setup using ${chalk.cyan( return await abortIfSpotlightNotSupported("React Native"); } - const { selectedProject, authToken, sentryUrl } = projectData; + const { selectedProject, authToken, sentryUrl, selfHosted } = projectData; const orgSlug = selectedProject.organization.slug; const projectSlug = selectedProject.slug; const projectId = selectedProject.id; @@ -327,19 +345,7 @@ Or setup using ${chalk.cyan( `Detected Expo Continuous Native Generation (CNG) setup. Skipping native files patching.`, ); } else { - if (existsSync("ios")) { - Sentry.setTag("patch-ios", true); - await traceStep("patch-xcode-files", () => - patchXcodeFiles(cliConfig, rnVersion), - ); - } - - if (existsSync("android")) { - Sentry.setTag("patch-android", true); - await traceStep("patch-android-files", () => - patchAndroidFiles(cliConfig), - ); - } + await patchNativeFiles({ cliConfig, sdkVersion, rnVersion, selfHosted }); } await runPrettierIfInstalled({ cwd: undefined }); @@ -403,14 +409,115 @@ ${chalk.cyan(issuesStreamUrl)}`); ); } +/** + * Writes `ios/` and `android/sentry.properties` and patches the native + * projects that exist. Builds read the org and project from those files, so + * they stay committed. When the SDK reads `.env.sentry-build-plugin`, the + * auth token goes there, gitignored, instead of into them. + */ +export async function patchNativeFiles({ + cliConfig, + sdkVersion, + rnVersion, + selfHosted, +}: { + cliConfig: RNCliSetupConfigContent; + sdkVersion: string | undefined; + rnVersion: string | undefined; + selfHosted: boolean; +}): Promise { + const patchIos = existsSync("ios"); + const patchAndroid = existsSync("android"); + const propertiesFiles = [ + ...(patchIos ? [IOS_SENTRY_PROPERTIES] : []), + ...(patchAndroid ? [ANDROID_SENTRY_PROPERTIES] : []), + ]; + if (propertiesFiles.length === 0) { + return; + } + + const tokenInDotEnv = + !!sdkVersion && + fulfillsVersionRange({ + version: sdkVersion, + acceptableVersions: `>=${RN_SDK_DOTENV_MIN_VERSION}`, + canBeLatest: true, + }); + Sentry.setTag( + "auth-token-location", + tokenInDotEnv ? SENTRY_DOT_ENV_FILE : SENTRY_PROPERTIES_FILE, + ); + + let propertiesConfig: CliSetupConfigContent = cliConfig; + let filesWithEarlierToken: string[] = []; + if (tokenInDotEnv) { + filesWithEarlierToken = propertiesFiles.filter(hasAuthTokenLine); + await addDotEnvSentryBuildPluginFile(cliConfig.authToken); + propertiesConfig = { + org: cliConfig.org, + project: cliConfig.project, + url: selfHosted ? cliConfig.url : undefined, + }; + } + + if (patchIos) { + Sentry.setTag("patch-ios", true); + await traceStep("patch-xcode-files", () => + patchXcodeFiles(propertiesConfig, rnVersion), + ); + } + if (patchAndroid) { + Sentry.setTag("patch-android", true); + await traceStep("patch-android-files", () => + patchAndroidFiles(propertiesConfig), + ); + } + + const settingsUrl = chalk.cyan( + buildAuthTokenSettingsUrl(cliConfig.org, cliConfig.url), + ); + const files = propertiesFiles.map((file) => chalk.cyan(file)).join(" and "); + const several = propertiesFiles.length > 1; + const envVar = chalk.cyan(AUTH_TOKEN_ENV_VAR); + const dotEnvFile = chalk.cyan(SENTRY_DOT_ENV_FILE); + if (!tokenInDotEnv) { + const reason = sdkVersion + ? `${RN_SDK_PACKAGE}@${sdkVersion} doesn't read the token from ${dotEnvFile}` + : `The wizard couldn't detect your ${RN_SDK_PACKAGE} version`; + clack.log.warn( + `${files} ${several ? "hold" : "holds"} your auth token and ${several ? "aren't" : "isn't"} gitignored, since builds read the org and project from ${several ? "them" : "it"}, so committing ${several ? "them" : "it"} commits the token. ${reason}. To keep the token out of git, upgrade ${RN_SDK_PACKAGE} to ${RN_SDK_DOTENV_MIN_VERSION} or later and run the wizard again, or remove the ${chalk.cyan("auth.token")} line and set ${envVar} instead, with a token from:\n${settingsUrl}`, + ); + return; + } + + clack.log.info( + `The wizard put the auth token in ${dotEnvFile}, which is gitignored, and only the ${propertiesConfig.url ? "org, project and server URL" : "org and project"} in ${files}, so commit ${several ? "those" : "that"}. In CI, set ${envVar} to a token from:\n${settingsUrl}`, + ); + for (const file of filesWithEarlierToken) { + clack.log.warn( + `${chalk.cyan(file)} already has an ${chalk.cyan("auth.token")} line from an earlier setup, so the file may already be committed with the token. The wizard didn't change it. Remove the line, since builds now read the token from ${dotEnvFile}, and if the file was pushed, rotate the token at:\n${settingsUrl}`, + ); + } +} + +/** Whether `file`, relative to the cwd, has an `auth.token` line. */ +function hasAuthTokenLine(file: string): boolean { + return ( + existsSync(file) && + propertiesCliSetupConfig.likelyAlreadyHasAuthToken( + readFileSync(file, "utf8"), + ) + ); +} + async function patchXcodeFiles( - config: RNCliSetupConfigContent, + config: CliSetupConfigContent, rnVersion: string | undefined, ) { await addSentryCliConfig(config, { ...propertiesCliSetupConfig, name: "source maps and iOS debug files", - filename: "ios/sentry.properties", + filename: IOS_SENTRY_PROPERTIES, gitignore: false, }); @@ -506,11 +613,11 @@ async function patchXcodeFiles( Sentry.setTag("xcode-project-status", "patched"); } -export async function patchAndroidFiles(config: RNCliSetupConfigContent) { +export async function patchAndroidFiles(config: CliSetupConfigContent) { await addSentryCliConfig(config, { ...propertiesCliSetupConfig, name: "source maps and iOS debug files", - filename: "android/sentry.properties", + filename: ANDROID_SENTRY_PROPERTIES, gitignore: false, }); diff --git a/packages/cli/src/lib/wizard/utils/clack/index.ts b/packages/cli/src/lib/wizard/utils/clack/index.ts index db4f2571d..20276eec3 100644 --- a/packages/cli/src/lib/wizard/utils/clack/index.ts +++ b/packages/cli/src/lib/wizard/utils/clack/index.ts @@ -71,7 +71,8 @@ export interface CliSetupConfig { } export interface CliSetupConfigContent { - authToken: string; + /** Omit to write the config without an auth token line. */ + authToken?: string | undefined; org?: string | undefined; project?: string | undefined; url?: string | undefined; @@ -619,7 +620,7 @@ export async function addSentryCliConfig( if (setupConfig.gitignore) { await addCliConfigFileToGitIgnore(setupConfig.filename); - } else { + } else if (authToken) { clack.log.warn( chalk.yellow("DO NOT commit auth token to your repository!"), ); diff --git a/packages/cli/test/lib/wizard/react-native/patch-native-files.test.ts b/packages/cli/test/lib/wizard/react-native/patch-native-files.test.ts new file mode 100644 index 000000000..204a952c0 --- /dev/null +++ b/packages/cli/test/lib/wizard/react-native/patch-native-files.test.ts @@ -0,0 +1,201 @@ +import { + existsSync, + mkdirSync, + mkdtempSync, + readFileSync, + rmSync, + writeFileSync, +} from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; +import { addExpoEnvLocal } from "../../../../src/lib/wizard/react-native/expo-env-file.js"; +import { patchNativeFiles } from "../../../../src/lib/wizard/react-native/react-native-wizard.js"; +import { Sentry } from "../../../../src/lib/wizard/telemetry.js"; + +const mocks = vi.hoisted(() => ({ info: vi.fn(), warn: vi.fn() })); + +vi.mock("@clack/prompts", () => ({ + log: { + step: vi.fn(), + info: mocks.info, + success: vi.fn(), + warn: mocks.warn, + warning: mocks.warn, + error: vi.fn(), + }, +})); + +const TOKEN = "sntrys_token"; +const DOT_ENV = ".env.sentry-build-plugin"; +const IOS_PROPERTIES = "ios/sentry.properties"; +const ANDROID_PROPERTIES = "android/sentry.properties"; + +const saasConfig = { + authToken: TOKEN, + org: "org-slug", + project: "project-slug", + url: "https://sentry.io/", +}; + +const selfHostedConfig = { ...saasConfig, url: "https://sentry.example.com/" }; + +function read(file: string): string { + return readFileSync(file, "utf-8"); +} + +function messages(mock: typeof mocks.info): string[] { + return mock.mock.calls.map(([message]) => String(message)); +} + +describe("patchNativeFiles", () => { + const originalCwd = process.cwd(); + let dir: string; + + beforeEach(() => { + vi.clearAllMocks(); + vi.spyOn(Sentry, "setTag"); + dir = mkdtempSync(join(tmpdir(), "rn-wizard-native-")); + process.chdir(dir); + // A committed React Native project: native folders, and a .gitignore + // that ignores neither. + mkdirSync("ios"); + mkdirSync("android"); + writeFileSync(".gitignore", "node_modules/\n"); + }); + + afterEach(() => { + process.chdir(originalCwd); + rmSync(dir, { recursive: true, force: true }); + }); + + it("writes the token to a gitignored .env.sentry-build-plugin and keeps it out of sentry.properties", async () => { + await patchNativeFiles({ + cliConfig: saasConfig, + sdkVersion: "8.37.0", + rnVersion: "0.80.0", + selfHosted: false, + }); + + expect(read(DOT_ENV)).toContain(`SENTRY_AUTH_TOKEN=${TOKEN}`); + expect(read(".gitignore").split("\n")).toContain(DOT_ENV); + for (const file of [IOS_PROPERTIES, ANDROID_PROPERTIES]) { + const contents = read(file); + expect(contents).toContain("defaults.org=org-slug"); + expect(contents).toContain("defaults.project=project-slug"); + expect(contents).not.toContain("auth.token"); + expect(contents).not.toContain(TOKEN); + expect(contents).not.toContain("defaults.url"); + expect(read(".gitignore")).not.toContain(file); + } + expect(Sentry.setTag).toHaveBeenCalledWith("auth-token-location", DOT_ENV); + const info = messages(mocks.info).find((m) => m.includes(DOT_ENV)); + expect(info).toContain("gitignored"); + expect(info).toContain("SENTRY_AUTH_TOKEN"); + expect(info).toContain("https://org-slug.sentry.io/settings/auth-tokens/"); + expect(messages(mocks.warn).join("\n")).not.toContain("auth token"); + }); + + it("adds defaults.url on self-hosted Sentry", async () => { + await patchNativeFiles({ + cliConfig: selfHostedConfig, + sdkVersion: "8.37.0", + rnVersion: "0.80.0", + selfHosted: true, + }); + + for (const file of [IOS_PROPERTIES, ANDROID_PROPERTIES]) { + expect(read(file)).toContain("defaults.url=https://sentry.example.com/"); + expect(read(file)).not.toContain("auth.token"); + } + expect(read(DOT_ENV)).toContain(`SENTRY_AUTH_TOKEN=${TOKEN}`); + }); + + it("moves the token for SDK versions before 8.14.0 that read .env.sentry-build-plugin", async () => { + await patchNativeFiles({ + cliConfig: saasConfig, + sdkVersion: "8.13.0", + rnVersion: "0.80.0", + selfHosted: false, + }); + + expect(read(DOT_ENV)).toContain(`SENTRY_AUTH_TOKEN=${TOKEN}`); + expect(read(IOS_PROPERTIES)).not.toContain("auth.token"); + expect(read(ANDROID_PROPERTIES)).not.toContain("auth.token"); + }); + + it.each([ + ["6.2.0", "@sentry/react-native@6.2.0"], + [undefined, "couldn't detect"], + ])( + "keeps the token in sentry.properties and warns with SDK version %s", + async (sdkVersion, reason) => { + await patchNativeFiles({ + cliConfig: saasConfig, + sdkVersion, + rnVersion: "0.80.0", + selfHosted: false, + }); + + expect(existsSync(DOT_ENV)).toBe(false); + for (const file of [IOS_PROPERTIES, ANDROID_PROPERTIES]) { + expect(read(file)).toContain(`auth.token=${TOKEN}`); + expect(read(file)).toContain("defaults.url=https://sentry.io/"); + expect(read(".gitignore")).not.toContain(file); + } + expect(Sentry.setTag).toHaveBeenCalledWith( + "auth-token-location", + "sentry.properties", + ); + const warning = messages(mocks.warn).find((m) => + m.includes("hold your auth token"), + ); + expect(warning).toContain(IOS_PROPERTIES); + expect(warning).toContain(ANDROID_PROPERTIES); + expect(warning).toContain(reason); + expect(warning).toContain("6.3.0"); + expect(warning).toContain("SENTRY_AUTH_TOKEN"); + }, + ); + + it("leaves an earlier auth.token line alone and warns about it", async () => { + const earlier = + "defaults.org=org-slug\ndefaults.project=project-slug\nauth.token=sntrys_old\n"; + writeFileSync(ANDROID_PROPERTIES, earlier); + + await patchNativeFiles({ + cliConfig: saasConfig, + sdkVersion: "8.37.0", + rnVersion: "0.80.0", + selfHosted: false, + }); + + expect(read(ANDROID_PROPERTIES)).toBe(earlier); + expect(read(IOS_PROPERTIES)).not.toContain("auth.token"); + const warnings = messages(mocks.warn).filter((m) => + m.includes("from an earlier setup"), + ); + expect(warnings).toHaveLength(1); + expect(warnings[0]).toContain(ANDROID_PROPERTIES); + expect(warnings[0]).toContain("rotate the token"); + }); + + it("keeps Expo's .env.local token next to .env.sentry-build-plugin", async () => { + // Expo projects with committed native folders run both steps. + await addExpoEnvLocal(saasConfig); + await patchNativeFiles({ + cliConfig: saasConfig, + sdkVersion: "8.37.0", + rnVersion: "0.80.0", + selfHosted: false, + }); + + expect(read(".env.local")).toBe( + `#DO NOT COMMIT THIS\nSENTRY_AUTH_TOKEN=${TOKEN}\n`, + ); + const gitignore = read(".gitignore").split("\n"); + expect(gitignore).toContain(".env.local"); + expect(gitignore).toContain(DOT_ENV); + expect(read(DOT_ENV)).toContain(`SENTRY_AUTH_TOKEN=${TOKEN}`); + }); +}); From 1aa2925a03c9d968a0e1f0ddb8a1f16e4b43f6cb Mon Sep 17 00:00:00 2001 From: "sentry-junior[bot]" <264270552+sentry-junior[bot]@users.noreply.github.com> Date: Thu, 3 Dec 2026 09:04:21 +0000 Subject: [PATCH 2/3] fix(cli): Warn when .env.sentry-build-plugin holds the placeholder token When the wizard could not create an auth token, the React Native wizard wrote the placeholder to .env.sentry-build-plugin and still said it put the auth token there. Like the Android wizard, warn that the file holds a placeholder, that Release builds fail at the Sentry upload until it is replaced, and show the token settings URL. Tag auth-token-placeholder. --- .../react-native/react-native-wizard.ts | 38 ++++++++++++++++-- .../react-native/patch-native-files.test.ts | 40 +++++++++++++++++++ 2 files changed, 74 insertions(+), 4 deletions(-) diff --git a/packages/cli/src/lib/wizard/react-native/react-native-wizard.ts b/packages/cli/src/lib/wizard/react-native/react-native-wizard.ts index 8c7fe5458..92310e459 100644 --- a/packages/cli/src/lib/wizard/react-native/react-native-wizard.ts +++ b/packages/cli/src/lib/wizard/react-native/react-native-wizard.ts @@ -8,7 +8,9 @@ import { buildProjectIssuesUrl } from "../../sentry-web-urls.js"; import { clack } from "../utils/clack/prompts.js"; import chalk from "chalk"; import { existsSync, readFileSync } from "node:fs"; +import { readFile } from "node:fs/promises"; import path from "node:path"; +import { isRegularFile } from "../../fs-utils.js"; import { Sentry } from "../telemetry.js"; import { platform } from "node:os"; @@ -43,7 +45,10 @@ import { getPackageVersion, hasPackageInstalled, } from "../utils/package-json.js"; -import { buildAuthTokenSettingsUrl } from "../utils/project-data.js"; +import { + buildAuthTokenSettingsUrl, + DUMMY_AUTH_TOKEN, +} from "../utils/project-data.js"; import { fulfillsVersionRange } from "../utils/semver.js"; import { isExpoCNG, @@ -450,9 +455,12 @@ export async function patchNativeFiles({ let propertiesConfig: CliSetupConfigContent = cliConfig; let filesWithEarlierToken: string[] = []; + let placeholderToken = false; if (tokenInDotEnv) { filesWithEarlierToken = propertiesFiles.filter(hasAuthTokenLine); await addDotEnvSentryBuildPluginFile(cliConfig.authToken); + placeholderToken = await dotEnvHasPlaceholderToken(cliConfig.authToken); + Sentry.setTag("auth-token-placeholder", placeholderToken); propertiesConfig = { org: cliConfig.org, project: cliConfig.project, @@ -490,9 +498,16 @@ export async function patchNativeFiles({ return; } - clack.log.info( - `The wizard put the auth token in ${dotEnvFile}, which is gitignored, and only the ${propertiesConfig.url ? "org, project and server URL" : "org and project"} in ${files}, so commit ${several ? "those" : "that"}. In CI, set ${envVar} to a token from:\n${settingsUrl}`, - ); + const propertiesNote = `The wizard put only the ${propertiesConfig.url ? "org, project and server URL" : "org and project"} in ${files}, so commit ${several ? "those" : "that"}.`; + if (placeholderToken) { + clack.log.warn( + `${dotEnvFile} holds a placeholder auth token (${chalk.cyan(`"${DUMMY_AUTH_TOKEN}"`)}), so Release builds fail at the Sentry upload until you replace it with a token from:\n${settingsUrl}\n${dotEnvFile} is gitignored. ${propertiesNote} In CI, set ${envVar}.`, + ); + } else { + clack.log.info( + `The wizard put the auth token in ${dotEnvFile}, which is gitignored. ${propertiesNote} In CI, set ${envVar} to a token from:\n${settingsUrl}`, + ); + } for (const file of filesWithEarlierToken) { clack.log.warn( `${chalk.cyan(file)} already has an ${chalk.cyan("auth.token")} line from an earlier setup, so the file may already be committed with the token. The wizard didn't change it. Remove the line, since builds now read the token from ${dotEnvFile}, and if the file was pushed, rotate the token at:\n${settingsUrl}`, @@ -500,6 +515,21 @@ export async function patchNativeFiles({ } } +/** + * Whether `.env.sentry-build-plugin` ends up with the placeholder token. The + * file keeps a token it already had, so that one counts, not this run's + * `authToken`. When the file can't be read, `authToken` decides. + */ +async function dotEnvHasPlaceholderToken(authToken: string): Promise { + const file = path.join(process.cwd(), SENTRY_DOT_ENV_FILE); + if (!(await isRegularFile(file))) { + return authToken === DUMMY_AUTH_TOKEN; + } + const contents = await readFile(file, "utf8"); + const token = /^\s*SENTRY_AUTH_TOKEN\s*=(.*)$/m.exec(contents)?.[1]?.trim(); + return token === DUMMY_AUTH_TOKEN; +} + /** Whether `file`, relative to the cwd, has an `auth.token` line. */ function hasAuthTokenLine(file: string): boolean { return ( diff --git a/packages/cli/test/lib/wizard/react-native/patch-native-files.test.ts b/packages/cli/test/lib/wizard/react-native/patch-native-files.test.ts index 204a952c0..ed370ce2b 100644 --- a/packages/cli/test/lib/wizard/react-native/patch-native-files.test.ts +++ b/packages/cli/test/lib/wizard/react-native/patch-native-files.test.ts @@ -180,6 +180,46 @@ describe("patchNativeFiles", () => { expect(warnings[0]).toContain("rotate the token"); }); + it("warns that .env.sentry-build-plugin holds the placeholder token", async () => { + await patchNativeFiles({ + cliConfig: { ...saasConfig, authToken: "_YOUR_SENTRY_AUTH_TOKEN_" }, + sdkVersion: "8.37.0", + rnVersion: "0.80.0", + selfHosted: false, + }); + + expect(read(DOT_ENV)).toContain( + "SENTRY_AUTH_TOKEN=_YOUR_SENTRY_AUTH_TOKEN_", + ); + expect(Sentry.setTag).toHaveBeenCalledWith("auth-token-placeholder", true); + const warning = messages(mocks.warn).find((m) => + m.includes("placeholder auth token"), + ); + expect(warning).toContain(DOT_ENV); + expect(warning).toContain("Release builds fail at the Sentry upload"); + expect(warning).toContain( + "https://org-slug.sentry.io/settings/auth-tokens/", + ); + expect( + messages(mocks.info).some((m) => m.includes("put the auth token")), + ).toBe(false); + }); + + it("does not warn about the placeholder when .env.sentry-build-plugin keeps a real token", async () => { + writeFileSync(DOT_ENV, "SENTRY_AUTH_TOKEN=sntrys_existing\n"); + + await patchNativeFiles({ + cliConfig: { ...saasConfig, authToken: "_YOUR_SENTRY_AUTH_TOKEN_" }, + sdkVersion: "8.37.0", + rnVersion: "0.80.0", + selfHosted: false, + }); + + expect(read(DOT_ENV)).toBe("SENTRY_AUTH_TOKEN=sntrys_existing\n"); + expect(Sentry.setTag).toHaveBeenCalledWith("auth-token-placeholder", false); + expect(messages(mocks.warn).join("\n")).not.toContain("placeholder"); + }); + it("keeps Expo's .env.local token next to .env.sentry-build-plugin", async () => { // Expo projects with committed native folders run both steps. await addExpoEnvLocal(saasConfig); From 93ab4eb2db893cab01f88901ee7bcef69393cd62 Mon Sep 17 00:00:00 2001 From: "sentry-junior[bot]" <264270552+sentry-junior[bot]@users.noreply.github.com> Date: Thu, 3 Dec 2026 09:48:42 +0000 Subject: [PATCH 3/3] test(cli): Run the React Native patchNativeFiles tests as on macOS on every host patchXcodeFiles asks whether to run `pod install` only on macOS, and the tests' @clack/prompts mock had no `select`, so they failed there. Mock `platform()` to "darwin" and answer the prompt with no, so the tests take the same path on macOS and Linux. --- .../react-native/patch-native-files.test.ts | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/packages/cli/test/lib/wizard/react-native/patch-native-files.test.ts b/packages/cli/test/lib/wizard/react-native/patch-native-files.test.ts index ed370ce2b..8b0b81fa1 100644 --- a/packages/cli/test/lib/wizard/react-native/patch-native-files.test.ts +++ b/packages/cli/test/lib/wizard/react-native/patch-native-files.test.ts @@ -13,9 +13,23 @@ import { addExpoEnvLocal } from "../../../../src/lib/wizard/react-native/expo-en import { patchNativeFiles } from "../../../../src/lib/wizard/react-native/react-native-wizard.js"; import { Sentry } from "../../../../src/lib/wizard/telemetry.js"; -const mocks = vi.hoisted(() => ({ info: vi.fn(), warn: vi.fn() })); +const mocks = vi.hoisted(() => ({ + info: vi.fn(), + warn: vi.fn(), + select: vi.fn(), +})); + +// Run as on macOS on every host, so patchXcodeFiles asks whether to run +// `pod install`; the mocked prompt answers no. +vi.mock("node:os", async (importOriginal) => { + const actual = await importOriginal(); + return { ...actual, platform: () => "darwin" }; +}); vi.mock("@clack/prompts", () => ({ + select: mocks.select, + isCancel: (value: unknown) => typeof value === "symbol", + cancel: vi.fn(), log: { step: vi.fn(), info: mocks.info, @@ -54,6 +68,7 @@ describe("patchNativeFiles", () => { beforeEach(() => { vi.clearAllMocks(); + mocks.select.mockResolvedValue(false); vi.spyOn(Sentry, "setTag"); dir = mkdtempSync(join(tmpdir(), "rn-wizard-native-")); process.chdir(dir); @@ -89,6 +104,10 @@ describe("patchNativeFiles", () => { expect(read(".gitignore")).not.toContain(file); } expect(Sentry.setTag).toHaveBeenCalledWith("auth-token-location", DOT_ENV); + expect(Sentry.setTag).toHaveBeenCalledWith( + "continue-with-pod-install", + false, + ); const info = messages(mocks.info).find((m) => m.includes(DOT_ENV)); expect(info).toContain("gitignored"); expect(info).toContain("SENTRY_AUTH_TOKEN");