Skip to content

Commit 1aa2925

Browse files
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.
1 parent ab0668e commit 1aa2925

2 files changed

Lines changed: 74 additions & 4 deletions

File tree

‎packages/cli/src/lib/wizard/react-native/react-native-wizard.ts‎

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ import { buildProjectIssuesUrl } from "../../sentry-web-urls.js";
88
import { clack } from "../utils/clack/prompts.js";
99
import chalk from "chalk";
1010
import { existsSync, readFileSync } from "node:fs";
11+
import { readFile } from "node:fs/promises";
1112
import path from "node:path";
13+
import { isRegularFile } from "../../fs-utils.js";
1214

1315
import { Sentry } from "../telemetry.js";
1416
import { platform } from "node:os";
@@ -43,7 +45,10 @@ import {
4345
getPackageVersion,
4446
hasPackageInstalled,
4547
} from "../utils/package-json.js";
46-
import { buildAuthTokenSettingsUrl } from "../utils/project-data.js";
48+
import {
49+
buildAuthTokenSettingsUrl,
50+
DUMMY_AUTH_TOKEN,
51+
} from "../utils/project-data.js";
4752
import { fulfillsVersionRange } from "../utils/semver.js";
4853
import {
4954
isExpoCNG,
@@ -450,9 +455,12 @@ export async function patchNativeFiles({
450455

451456
let propertiesConfig: CliSetupConfigContent = cliConfig;
452457
let filesWithEarlierToken: string[] = [];
458+
let placeholderToken = false;
453459
if (tokenInDotEnv) {
454460
filesWithEarlierToken = propertiesFiles.filter(hasAuthTokenLine);
455461
await addDotEnvSentryBuildPluginFile(cliConfig.authToken);
462+
placeholderToken = await dotEnvHasPlaceholderToken(cliConfig.authToken);
463+
Sentry.setTag("auth-token-placeholder", placeholderToken);
456464
propertiesConfig = {
457465
org: cliConfig.org,
458466
project: cliConfig.project,
@@ -490,16 +498,38 @@ export async function patchNativeFiles({
490498
return;
491499
}
492500

493-
clack.log.info(
494-
`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}`,
495-
);
501+
const propertiesNote = `The wizard put only the ${propertiesConfig.url ? "org, project and server URL" : "org and project"} in ${files}, so commit ${several ? "those" : "that"}.`;
502+
if (placeholderToken) {
503+
clack.log.warn(
504+
`${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}.`,
505+
);
506+
} else {
507+
clack.log.info(
508+
`The wizard put the auth token in ${dotEnvFile}, which is gitignored. ${propertiesNote} In CI, set ${envVar} to a token from:\n${settingsUrl}`,
509+
);
510+
}
496511
for (const file of filesWithEarlierToken) {
497512
clack.log.warn(
498513
`${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}`,
499514
);
500515
}
501516
}
502517

518+
/**
519+
* Whether `.env.sentry-build-plugin` ends up with the placeholder token. The
520+
* file keeps a token it already had, so that one counts, not this run's
521+
* `authToken`. When the file can't be read, `authToken` decides.
522+
*/
523+
async function dotEnvHasPlaceholderToken(authToken: string): Promise<boolean> {
524+
const file = path.join(process.cwd(), SENTRY_DOT_ENV_FILE);
525+
if (!(await isRegularFile(file))) {
526+
return authToken === DUMMY_AUTH_TOKEN;
527+
}
528+
const contents = await readFile(file, "utf8");
529+
const token = /^\s*SENTRY_AUTH_TOKEN\s*=(.*)$/m.exec(contents)?.[1]?.trim();
530+
return token === DUMMY_AUTH_TOKEN;
531+
}
532+
503533
/** Whether `file`, relative to the cwd, has an `auth.token` line. */
504534
function hasAuthTokenLine(file: string): boolean {
505535
return (

‎packages/cli/test/lib/wizard/react-native/patch-native-files.test.ts‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,46 @@ describe("patchNativeFiles", () => {
180180
expect(warnings[0]).toContain("rotate the token");
181181
});
182182

183+
it("warns that .env.sentry-build-plugin holds the placeholder token", async () => {
184+
await patchNativeFiles({
185+
cliConfig: { ...saasConfig, authToken: "_YOUR_SENTRY_AUTH_TOKEN_" },
186+
sdkVersion: "8.37.0",
187+
rnVersion: "0.80.0",
188+
selfHosted: false,
189+
});
190+
191+
expect(read(DOT_ENV)).toContain(
192+
"SENTRY_AUTH_TOKEN=_YOUR_SENTRY_AUTH_TOKEN_",
193+
);
194+
expect(Sentry.setTag).toHaveBeenCalledWith("auth-token-placeholder", true);
195+
const warning = messages(mocks.warn).find((m) =>
196+
m.includes("placeholder auth token"),
197+
);
198+
expect(warning).toContain(DOT_ENV);
199+
expect(warning).toContain("Release builds fail at the Sentry upload");
200+
expect(warning).toContain(
201+
"https://org-slug.sentry.io/settings/auth-tokens/",
202+
);
203+
expect(
204+
messages(mocks.info).some((m) => m.includes("put the auth token")),
205+
).toBe(false);
206+
});
207+
208+
it("does not warn about the placeholder when .env.sentry-build-plugin keeps a real token", async () => {
209+
writeFileSync(DOT_ENV, "SENTRY_AUTH_TOKEN=sntrys_existing\n");
210+
211+
await patchNativeFiles({
212+
cliConfig: { ...saasConfig, authToken: "_YOUR_SENTRY_AUTH_TOKEN_" },
213+
sdkVersion: "8.37.0",
214+
rnVersion: "0.80.0",
215+
selfHosted: false,
216+
});
217+
218+
expect(read(DOT_ENV)).toBe("SENTRY_AUTH_TOKEN=sntrys_existing\n");
219+
expect(Sentry.setTag).toHaveBeenCalledWith("auth-token-placeholder", false);
220+
expect(messages(mocks.warn).join("\n")).not.toContain("placeholder");
221+
});
222+
183223
it("keeps Expo's .env.local token next to .env.sentry-build-plugin", async () => {
184224
// Expo projects with committed native folders run both steps.
185225
await addExpoEnvLocal(saasConfig);

0 commit comments

Comments
 (0)