Skip to content

Commit 2394297

Browse files
feat(cli): run sentry through npx in the Xcode build phase when missing
When no v4 `sentry` is on the PATH an Xcode build phase sees, the generated upload phase now runs `npx --yes sentry debug-files upload`, so a project set up with `npx sentry wizard` uploads debug symbols without installing the CLI first. It only warns when neither `sentry` nor `npx` is found. The wizard's preflight matches: it notes that builds will use `npx sentry` (and how to install the CLI for faster builds) when only `npx` is in reach, and warns as before when neither is. Existing-phase detection keys on `debug-files upload`, since the command now runs through `"$@"`.
1 parent ed81e1d commit 2394297

5 files changed

Lines changed: 77 additions & 33 deletions

File tree

‎packages/cli/src/lib/wizard/apple/check-installed-cli.ts‎

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
* The user runs the wizard through `sentry`, so the CLI is installed, but an
55
* Xcode build phase doesn't see the user's shell `PATH`: it searches the
66
* system directories plus the ones the generated script adds (see
7-
* {@link xcodeSearchDirs}). When `sentry` is in none of them, the wizard
8-
* prints how to install it where Xcode finds it.
7+
* {@link xcodeSearchDirs}). Without `sentry` there, the build phase runs it
8+
* with `npx`, so the wizard only notes that builds would be faster with the
9+
* CLI installed. With neither, it warns and prints how to install it where
10+
* Xcode finds it.
911
*/
1012

1113
import { homedir } from "node:os";
@@ -32,45 +34,58 @@ export function xcodeSearchDirs(home: string = homedir()): string[] {
3234
];
3335
}
3436

35-
/** How to install the Sentry CLI where an Xcode build phase finds it. */
36-
export const SENTRY_CLI_INSTALL_INSTRUCTIONS = [
37-
"Install the Sentry CLI with one of:",
37+
/** Commands that install the Sentry CLI where an Xcode build phase finds it. */
38+
const SENTRY_CLI_INSTALL_COMMANDS = [
3839
" brew install getsentry/tools/sentry",
3940
" curl https://cli.sentry.dev/install -fsS | bash",
4041
" npm install -g sentry",
41-
].join("\n");
42+
];
4243

4344
/** Default warning when `sentry` is out of the build phase's reach. */
4445
const DEFAULT_WARNING =
4546
"Xcode build phases can't find the `sentry` CLI, so your builds won't upload debug symbols to Sentry.";
4647

4748
/**
48-
* Whether an Xcode build phase finds `sentry`, warning with install
49-
* instructions when it doesn't.
49+
* Whether an Xcode build phase finds `sentry`, or `npx` to run it with,
50+
* warning with install instructions when it finds neither.
5051
*
5152
* @param warning - Explains what doesn't work without `sentry`.
52-
* @returns The path of the `sentry` the build phase would run, if any.
53+
* @returns The path of the `sentry` or `npx` the build phase would run, if any.
5354
*/
5455
export function checkInstalledCLI(
5556
warning: string = DEFAULT_WARNING,
5657
): string | undefined {
5758
debug("Checking whether Xcode build phases can find sentry");
5859
const searchDirs = xcodeSearchDirs();
59-
const cliPath =
60-
whichSync("sentry", { PATH: searchDirs.join(path.delimiter) }) ?? undefined;
60+
const searchPath = { PATH: searchDirs.join(path.delimiter) };
61+
const cliPath = whichSync("sentry", searchPath) ?? undefined;
6162
Sentry.setTag("has-cli", cliPath !== undefined);
6263
if (cliPath) {
6364
debug(`Xcode build phases will run ${cliPath}`);
6465
return cliPath;
6566
}
6667

68+
const npxPath = whichSync("npx", searchPath) ?? undefined;
69+
if (npxPath) {
70+
debug(`Xcode build phases will run the Sentry CLI with ${npxPath}`);
71+
clack.log.info(
72+
[
73+
"Xcode build phases will run the Sentry CLI with `npx sentry`, which downloads it on the first build.",
74+
"For faster builds, install it with one of:",
75+
...SENTRY_CLI_INSTALL_COMMANDS,
76+
].join("\n"),
77+
);
78+
return npxPath;
79+
}
80+
6781
clack.log.warn(
6882
[
6983
warning,
7084
"Xcode doesn't use your shell's PATH: it looks in",
7185
`${searchDirs.join(", ")}.`,
7286
"",
73-
SENTRY_CLI_INSTALL_INSTRUCTIONS,
87+
"Install the Sentry CLI with one of:",
88+
...SENTRY_CLI_INSTALL_COMMANDS,
7489
].join("\n"),
7590
);
7691
return undefined;

‎packages/cli/src/lib/wizard/apple/templates.ts‎

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
* The Xcode build phase that uploads debug symbols with `sentry debug-files
33
* upload`. Xcode doesn't use the user's shell `PATH`, so the script adds the
44
* standalone installer's and Homebrew's directories before looking for
5-
* `sentry`.
5+
* `sentry`. Without an installed `sentry`, it runs the CLI from npm with
6+
* `npx`, which downloads it on the first build.
67
*
78
* The upload passes no `--org` or `--project`: `sentry` reads them from
89
* `SENTRY_ORG` and `SENTRY_PROJECT`, which the script exports because CI has
@@ -23,26 +24,31 @@ export SENTRY_ORG=${orgSlug}
2324
export SENTRY_PROJECT=${projectSlug}
2425
# The check skips unrelated sentry binaries, such as the self-hosted Sentry server's.
2526
if sentry debug-files --help >/dev/null 2>&1; then
26-
if ! ERROR=$(sentry debug-files upload ${
27-
uploadSource ? "--include-sources " : ""
28-
}"$DWARF_DSYM_FOLDER_PATH" 2>&1 >/dev/null); then
29-
echo "warning: sentry - $ERROR"
30-
fi
27+
set -- sentry
28+
elif command -v npx >/dev/null 2>&1; then
29+
# No installed Sentry CLI: run it from npm, downloaded on the first build.
30+
set -- npx --yes sentry
3131
else
3232
echo "warning: Sentry CLI not installed. Install it with: curl https://cli.sentry.dev/install -fsS | bash (see https://cli.sentry.dev/getting-started/)"
33+
exit 0
34+
fi
35+
if ! ERROR=$("$@" debug-files upload ${
36+
uploadSource ? "--include-sources " : ""
37+
}"$DWARF_DSYM_FOLDER_PATH" 2>&1 >/dev/null); then
38+
echo "warning: sentry - $ERROR"
3339
fi
3440
`;
3541
}
3642

3743
/**
38-
* Whether `shellScript` is a Sentry debug-file upload build phase: one the
39-
* wizard generated with `sentry debug-files upload`, or an older one that
40-
* runs `sentry-cli`.
44+
* Whether `shellScript` is a Sentry debug-file upload build phase: one that
45+
* runs `debug-files upload` (the wizard's, which picks `sentry` or `npx
46+
* sentry` first), or an older one that runs `sentry-cli`.
4147
*/
4248
export function isSentryUploadScript(shellScript: string): boolean {
4349
return (
4450
shellScript.includes("sentry-cli") ||
45-
/\bsentry\s+debug-files\s+upload\b/.test(shellScript)
51+
/\bdebug-files\s+upload\b/.test(shellScript)
4652
);
4753
}
4854

‎packages/cli/test/lib/wizard/apple/check-installed-cli.test.ts‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import { beforeEach, describe, expect, it, vi } from "vitest";
22

33
const mocks = vi.hoisted(() => ({
4+
info: vi.fn(),
45
warn: vi.fn(),
56
whichSync: vi.fn(),
67
}));
78

89
vi.mock("@clack/prompts", () => ({
9-
log: { warn: mocks.warn },
10+
log: { info: mocks.info, warn: mocks.warn },
1011
}));
1112

1213
vi.mock("../../../../src/lib/which.js", () => ({
@@ -33,7 +34,19 @@ describe("checkInstalledCLI", () => {
3334
expect(mocks.warn).not.toHaveBeenCalled();
3435
});
3536

36-
it("prints v4 install instructions when sentry is out of reach", () => {
37+
it("falls back to npx when only npx is in reach", () => {
38+
mocks.whichSync.mockImplementation((command: string) =>
39+
command === "npx" ? "/opt/homebrew/bin/npx" : null,
40+
);
41+
42+
expect(checkInstalledCLI()).toBe("/opt/homebrew/bin/npx");
43+
const message = String(mocks.info.mock.calls[0]?.[0]);
44+
expect(message).toContain("`npx sentry`");
45+
expect(message).toContain("brew install getsentry/tools/sentry");
46+
expect(mocks.warn).not.toHaveBeenCalled();
47+
});
48+
49+
it("prints v4 install instructions when neither sentry nor npx is in reach", () => {
3750
mocks.whichSync.mockReturnValue(null);
3851

3952
expect(checkInstalledCLI("Custom warning.")).toBeUndefined();

‎packages/cli/test/lib/wizard/apple/templates.test.ts‎

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,16 @@ export SENTRY_ORG=test-org
2828
export SENTRY_PROJECT=test-project
2929
# The check skips unrelated sentry binaries, such as the self-hosted Sentry server's.
3030
if sentry debug-files --help >/dev/null 2>&1; then
31-
if ! ERROR=$(sentry debug-files upload --include-sources "$DWARF_DSYM_FOLDER_PATH" 2>&1 >/dev/null); then
32-
echo "warning: sentry - $ERROR"
33-
fi
31+
set -- sentry
32+
elif command -v npx >/dev/null 2>&1; then
33+
# No installed Sentry CLI: run it from npm, downloaded on the first build.
34+
set -- npx --yes sentry
3435
else
3536
echo "warning: Sentry CLI not installed. Install it with: curl https://cli.sentry.dev/install -fsS | bash (see https://cli.sentry.dev/getting-started/)"
37+
exit 0
38+
fi
39+
if ! ERROR=$("$@" debug-files upload --include-sources "$DWARF_DSYM_FOLDER_PATH" 2>&1 >/dev/null); then
40+
echo "warning: sentry - $ERROR"
3641
fi
3742
`,
3843
},
@@ -46,11 +51,16 @@ export SENTRY_ORG=test-org
4651
export SENTRY_PROJECT=test-project
4752
# The check skips unrelated sentry binaries, such as the self-hosted Sentry server's.
4853
if sentry debug-files --help >/dev/null 2>&1; then
49-
if ! ERROR=$(sentry debug-files upload "$DWARF_DSYM_FOLDER_PATH" 2>&1 >/dev/null); then
50-
echo "warning: sentry - $ERROR"
51-
fi
54+
set -- sentry
55+
elif command -v npx >/dev/null 2>&1; then
56+
# No installed Sentry CLI: run it from npm, downloaded on the first build.
57+
set -- npx --yes sentry
5258
else
5359
echo "warning: Sentry CLI not installed. Install it with: curl https://cli.sentry.dev/install -fsS | bash (see https://cli.sentry.dev/getting-started/)"
60+
exit 0
61+
fi
62+
if ! ERROR=$("$@" debug-files upload "$DWARF_DSYM_FOLDER_PATH" 2>&1 >/dev/null); then
63+
echo "warning: sentry - $ERROR"
5464
fi
5565
`,
5666
},

‎packages/cli/test/lib/wizard/apple/xcode-manager.test.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2363,7 +2363,7 @@ describe("XcodeManager", () => {
23632363
expect(updatedPhase).toBeDefined();
23642364
expect(
23652365
(updatedPhase as PBXShellScriptBuildPhase | undefined)?.shellScript,
2366-
).toContain("sentry debug-files upload");
2366+
).toContain("debug-files upload");
23672367
});
23682368
});
23692369

@@ -2772,7 +2772,7 @@ describe("XcodeManager", () => {
27722772
return (
27732773
phase &&
27742774
typeof phase !== "string" &&
2775-
phase.shellScript?.includes("sentry debug-files upload")
2775+
phase.shellScript?.includes("debug-files upload")
27762776
);
27772777
});
27782778
expect(sentryPhase).toBeDefined();
@@ -2813,7 +2813,7 @@ describe("XcodeManager", () => {
28132813
return (
28142814
phase &&
28152815
typeof phase !== "string" &&
2816-
phase.shellScript?.includes("sentry debug-files upload")
2816+
phase.shellScript?.includes("debug-files upload")
28172817
);
28182818
});
28192819
expect(sentryPhase).toBeDefined();

0 commit comments

Comments
 (0)