Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/cli/packaging/pypi-README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ pip install sentry-cli
sentry --help
```

Version 4 installs a `sentry` command, not `sentry-cli`. To keep version 3,
pin `sentry-cli<4`. The `sentry` package (self-hosted Sentry) also installs a
Version 4 installs a `sentry` command. Its `sentry-cli` command only says so
and exits with an error. To keep version 3, pin `sentry-cli<4`. The `sentry` package (self-hosted Sentry) also installs a
`sentry` command, so do not install both into the same environment.

See [Migrating from v3](https://cli.sentry.dev/migrating-from-v3/) and the
Expand Down
24 changes: 24 additions & 0 deletions packages/cli/script/lib/python-wheel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,34 @@
* holds one platform's binary as a data script, so pip puts a `sentry` (or
* `sentry.exe`) command on PATH and needs no Python code to run it.
* See https://packaging.python.org/en/latest/specifications/binary-distribution-format/
*
* The `sentry-cli` command only says the command is now `sentry` (see
* src/lib/sentry-cli-rename.ts). It is a console script, so pip writes a launcher for
* it on every platform, `sentry-cli.exe` on Windows.
*/

import { createHash } from "node:crypto";
import { type Zippable, zipSync } from "fflate";
import {
SENTRY_CLI_RENAMED_EXIT_CODE,
SENTRY_CLI_RENAMED_MESSAGE,
} from "../../src/lib/sentry-cli-rename.js";

/** Distribution name, normalized for wheel file names. */
const DISTRIBUTION = "sentry_cli";

/** Python module behind the `sentry-cli` console script. */
const SHIM_MODULE = "sentry_cli_renamed";

// A JSON string of ASCII text is also a valid Python string literal.
const SHIM_SOURCE = `import sys


def main():
sys.stderr.write(${JSON.stringify(SENTRY_CLI_RENAMED_MESSAGE)})
return ${SENTRY_CLI_RENAMED_EXIT_CODE}
`;

type WheelTarget = {
/** Wheel platform tag. */
platform: string;
Expand Down Expand Up @@ -119,10 +139,14 @@ export function buildWheel(input: WheelInput): {
"",
].join("\n");

const entryPoints = `[console_scripts]\nsentry-cli = ${SHIM_MODULE}:main\n`;

const files: [path: string, data: Uint8Array][] = [
[`${prefix}.data/scripts/${target.exe}`, input.binary],
[`${SHIM_MODULE}.py`, encoder.encode(SHIM_SOURCE)],
[`${distInfo}/METADATA`, encoder.encode(metadata)],
[`${distInfo}/WHEEL`, encoder.encode(wheel)],
[`${distInfo}/entry_points.txt`, encoder.encode(entryPoints)],
[`${distInfo}/LICENSE.md`, encoder.encode(input.license)],
];
const recordPath = `${distInfo}/RECORD`;
Expand Down
16 changes: 13 additions & 3 deletions packages/cli/src/bin.ts
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
// Must stay the first import: later imports can load node:sqlite.
import "./lib/suppress-sqlite-warning.js";
import { wrapCall } from "./lib/react-native/wrap-call.js";
import {
runsAsSentryCli,
SENTRY_CLI_RENAMED_EXIT_CODE,
SENTRY_CLI_RENAMED_MESSAGE,
} from "./lib/sentry-cli-rename.js";
import { installStreamErrorHandlers } from "./lib/stream-errors.js";
import { SENTRY_CLI } from "./sentry-cli.js";

Expand All @@ -21,9 +26,14 @@ if (process.env["__SENTRY_RN_WRAP_XCODE_CALL"] === "1") {
process.exit(wrapCall());
}

// Shell completion runs on every TAB press: load only the completion engine,
// never the full CLI (`@sentry/node`, the command tree).
if (process.argv[2] === "__complete") {
if (runsAsSentryCli(process.argv0, process.execPath)) {
// A v3 script that got this binary as `sentry-cli`: say it is `sentry` now
// instead of running v4 commands it was not written for.
process.stderr.write(SENTRY_CLI_RENAMED_MESSAGE);
process.exitCode = SENTRY_CLI_RENAMED_EXIT_CODE;
} else if (process.argv[2] === "__complete") {
// Shell completion runs on every TAB press: load only the completion
// engine, never the full CLI (`@sentry/node`, the command tree).
import("./complete-bin.js").catch(() => {
// Completions should never crash — silently return no results
process.exitCode = 0;
Expand Down
38 changes: 38 additions & 0 deletions packages/cli/src/lib/sentry-cli-rename.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* v4 renamed the `sentry-cli` command to `sentry`. Scripts written for v3
* still call `sentry-cli`, and some get a v4 binary under that name: the
* release registry's v3 download URL serves it. Instead of running v4 or
* leaving them with "command not found", they get this message. It never
* runs `sentry`: commands and flags changed since v3, so v4 cannot stand in
* for v3.
*
* Two places show it: `bin.ts`, when this binary runs as `sentry-cli`, and
* the `sentry-cli` console script of the PyPI wheels (script/lib/python-wheel.ts).
*/

import { basename } from "node:path";

export const SENTRY_CLI_RENAMED_MESSAGE = `Error: sentry-cli is now sentry. Sentry CLI v4 renamed the command and changed some commands and flags.
Update your scripts: https://cli.sentry.dev/migrating-from-v3/
Or stay on v3:
npm: npm install @sentry/cli@3
pip: pip install "sentry-cli<4"
get-cli: curl -sL https://sentry.io/get-cli/ | SENTRY_CLI_VERSION=3.8.2 sh
`;

/** Not 127, which shells use for "command not found". */
export const SENTRY_CLI_RENAMED_EXIT_CODE = 1;

const SENTRY_CLI_NAME_RE = /^sentry-cli(\.exe)?$/i;

/**
* Whether this process runs as `sentry-cli`. `argv0` is the name it was
* invoked by, which keeps a symlink's name. `execPath` is the file itself,
* which covers Windows, where `argv0` may lack the `.exe`. When the npm
* package runs, both name Node.
*/
export function runsAsSentryCli(argv0: string, execPath: string): boolean {
return [argv0, execPath].some((path) =>
SENTRY_CLI_NAME_RE.test(basename(path)),
);
}
42 changes: 42 additions & 0 deletions packages/cli/test/e2e/sentry-cli-rename.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* The CLI run as `sentry-cli`, the way a v3 script that saved the v4 binary
* under its old name runs it.
*/

import { spawnSync } from "node:child_process";
import { join } from "node:path";
import { describe, expect, test } from "vitest";
import {
SENTRY_CLI_RENAMED_EXIT_CODE,
SENTRY_CLI_RENAMED_MESSAGE,
} from "../../src/lib/sentry-cli-rename.js";
import { getCliCommand } from "../fixture.js";

const cliDir = join(import.meta.dirname, "../..");

function run(args: string[], argv0?: string) {
const [bin, ...binArgs] = getCliCommand();
return spawnSync(bin, [...binArgs, ...args], {
cwd: cliDir,
env: { ...process.env, SENTRY_CLI_NO_TELEMETRY: "1" },
encoding: "utf8",
...(argv0 ? { argv0 } : {}),
});
}

describe("run as sentry-cli", () => {
test("says the command is now sentry and fails, even for --version", () => {
const result = run(["--version"], "sentry-cli");

expect(result.status).toBe(SENTRY_CLI_RENAMED_EXIT_CODE);
expect(result.stderr).toBe(SENTRY_CLI_RENAMED_MESSAGE);
expect(result.stdout).toBe("");
});

test("runs a hidden v3 alias as sentry", () => {
const result = run(["releases", "--help"]);

expect(result.status, result.stderr).toBe(0);
expect(result.stderr).not.toContain("sentry-cli is now sentry");
});
});
23 changes: 23 additions & 0 deletions packages/cli/test/lib/sentry-cli-rename.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { describe, expect, test } from "vitest";
import { runsAsSentryCli } from "../../src/lib/sentry-cli-rename.js";

describe("runsAsSentryCli", () => {
test.each([
["sentry-cli", "/usr/local/bin/sentry"],
["/usr/local/bin/sentry-cli", "/usr/local/bin/sentry"],
["sentry", "/usr/local/bin/sentry-cli"],
["sentry", "/opt/bin/sentry-cli.exe"],
["SENTRY-CLI.EXE", "/opt/bin/sentry.exe"],
])("argv0 %s, execPath %s: yes", (argv0, execPath) => {
expect(runsAsSentryCli(argv0, execPath)).toBe(true);
});

test.each([
["sentry", "/usr/local/bin/sentry"],
["node", "/usr/bin/node"],
["sentry-cli-wrapper", "/usr/local/bin/sentry"],
["/home/me/sentry-cli/bin/sentry", "/home/me/sentry-cli/bin/sentry"],
])("argv0 %s, execPath %s: no", (argv0, execPath) => {
expect(runsAsSentryCli(argv0, execPath)).toBe(false);
});
});
38 changes: 38 additions & 0 deletions packages/cli/test/script/python-wheel.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
import { execFileSync, spawnSync } from "node:child_process";
import { createHash } from "node:crypto";
import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { unzipSync } from "fflate";
import { describe, expect, test } from "vitest";
import {
buildWheel,
toPep440Version,
WHEEL_TARGETS,
} from "../../script/lib/python-wheel.js";
import {
SENTRY_CLI_RENAMED_EXIT_CODE,
SENTRY_CLI_RENAMED_MESSAGE,
} from "../../src/lib/sentry-cli-rename.js";

const decoder = new TextDecoder();
const binary = new TextEncoder().encode("\x7fELF fake binary");
Expand Down Expand Up @@ -84,13 +92,43 @@ describe("buildWheel", () => {
"sentry_cli-4.0.0.dist-info/METADATA",
"sentry_cli-4.0.0.dist-info/RECORD",
"sentry_cli-4.0.0.dist-info/WHEEL",
"sentry_cli-4.0.0.dist-info/entry_points.txt",
"sentry_cli_renamed.py",
]);
expect(files["sentry_cli-4.0.0.data/scripts/sentry"]).toEqual(binary);

const windows = unzipSync(build("windows-x64").contents);
expect(windows["sentry_cli-4.0.0.data/scripts/sentry.exe"]).toEqual(binary);
});

test("installs a sentry-cli command that only says it is now sentry", () => {
const host = `${process.platform === "darwin" ? "darwin" : "linux"}-${process.arch === "arm64" ? "arm64" : "x64"}`;
const wheel = build(host);
const dir = mkdtempSync(join(tmpdir(), "sentry-wheel-test-"));
try {
const wheelPath = join(dir, wheel.filename);
writeFileSync(wheelPath, wheel.contents);
const venv = join(dir, "venv");
execFileSync("python3", ["-m", "venv", venv]);
execFileSync(join(venv, "bin", "pip"), [
"install",
"--quiet",
"--no-index",
"--no-deps",
wheelPath,
]);

const run = spawnSync(join(venv, "bin", "sentry-cli"), ["--version"], {
encoding: "utf8",
});
expect(run.status).toBe(SENTRY_CLI_RENAMED_EXIT_CODE);
expect(run.stderr).toBe(SENTRY_CLI_RENAMED_MESSAGE);
expect(run.stdout).toBe("");
} finally {
rmSync(dir, { recursive: true, force: true });
}
}, 60_000);

test("marks only the command executable", () => {
const attrs = externalAttrs(build("linux-x64").contents);
expect(attrs.get("sentry_cli-4.0.0.data/scripts/sentry")).toBe(
Expand Down
Loading