From e712ef1f137569d7a3989059b197ff820d2bafeb Mon Sep 17 00:00:00 2001 From: Alexey Karimov Date: Wed, 21 Oct 2026 22:59:03 +0500 Subject: [PATCH 1/2] chore(npm): retire the legacy @bugsee/bugsee-cli package MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The cargo-dist npm launcher exits 0 when the binary is killed by a signal (`process.exit(result.status)` with `status: null`), so a killed CLI looks like a success to any caller (#87). The generated file is not in this repo, so it cannot be patched at source. 0.8.2 was published by hand with the launcher fixed (same 0.8.1 binary), and every earlier version is deprecated. Stop publishing the package: - npm-publish.yml: drop the `publish` job. `publish-optional-deps`, which publishes @bugsee/cli + the six platform packages from the release's binary archives, is unchanged apart from comments that referred to the removed job. - Cargo.toml: drop "npm" from dist's `installers` AND remove `npm-scope`, which makes dist emit the npm installer on its own. `dist plan` no longer lists bugsee-cli-npm-package.tar.gz; the binary archives are unaffected. - Docs: mark @bugsee/bugsee-cli deprecated in README, CLAUDE.md, npm/README.md and npm/cli/README.md. Closes #87 πŸ€– Generated with [Claude Code](https://claude.com/claude-code) --- .github/workflows/npm-publish.yml | 169 +++++------------------------- CLAUDE.md | 17 ++- Cargo.toml | 9 +- README.md | 24 ++--- npm/README.md | 5 +- npm/cli/README.md | 6 +- 6 files changed, 55 insertions(+), 175 deletions(-) diff --git a/.github/workflows/npm-publish.yml b/.github/workflows/npm-publish.yml index f5c10ee..c514afe 100644 --- a/.github/workflows/npm-publish.yml +++ b/.github/workflows/npm-publish.yml @@ -4,17 +4,17 @@ name: Publish to npm # trust target on every published package's npmjs.com settings β€” # `workflow_filename = npm-publish.yml` in npm's Trusted Publisher UI. # -# It publishes EIGHT packages in two independent jobs: +# It publishes SEVEN packages in the one `publish-optional-deps` job: +# @bugsee/cli + the six @bugsee/cli- packages β€” the binary split per +# os/cpu behind optionalDependencies, assembled by npm/build.mjs from the +# release's binary archives. See npm/README.md. # -# publish @bugsee/bugsee-cli β€” the cargo-dist npm installer, -# one package whose postinstall downloads the binary. -# Taken verbatim from the release asset. -# publish-optional-deps @bugsee/cli + the six @bugsee/cli- -# packages β€” the binary split per os/cpu behind -# optionalDependencies, assembled by npm/build.mjs -# from the release's binary archives. See npm/README.md. +# The legacy single-package `@bugsee/bugsee-cli` (the cargo-dist npm installer) +# is NOT published from here any more: its launcher exited 0 when the binary was +# killed by a signal (#87). It was frozen at 0.8.2 (the one patched release) and +# deprecated in favour of @bugsee/cli. Do not re-add a job for it. # -# EVERY ONE of those eight names needs its OWN Trusted Publisher entry on +# EVERY ONE of those seven names needs its OWN Trusted Publisher entry on # npmjs.com (repository bugsee/bugsee-cli, workflow npm-publish.yml) before its # FIRST publish. That entry lives on a PACKAGE's settings page, so it cannot # even be created for a name that has never been published: each new name needs @@ -25,16 +25,14 @@ name: Publish to npm # # >>> RUNBOOK: `bash npm/bootstrap-names.sh ` does both steps and is # >>> idempotent; npm/README.md, "First-publish bootstrap" explains it. Read it before the -# >>> first release that includes the @bugsee/cli family. @bugsee/bugsee-cli is -# >>> already bootstrapped and needs nothing. +# >>> first release that includes a new package name. # # Why a separate workflow (and not folded into release.yml): # `dist init` regenerates release.yml on every dist upgrade and would # silently clobber any OIDC wiring there. Keeping the npm publish in # our own workflow file insulates it from dist's lifecycle. dist still -# generates the `bugsee-cli-npm-package.tar.gz` tarball as a GitHub -# Release asset (via `installers = [..., "npm"]`); we just download -# the asset and publish ourselves. +# builds the per-platform binary archives and attaches them to the GitHub +# Release; we just download those and publish ourselves. # # Triggers: # - `workflow_run` on the dist `Release` workflow completing β€” the @@ -47,7 +45,7 @@ name: Publish to npm # it fires reliably. Guarded to a SUCCESSFUL, tag-`push`-triggered # Release whose tag looks like `vX.Y.Z` (PR-mode Release runs and # failed releases are skipped). By the time Release has completed, -# the npm tarball asset is already attached to the release. +# the binary archives are already attached to the release. # - `workflow_dispatch` with a `tag` input β€” the MANUAL path, for # backfilling missing versions or re-publishing a specific release. # @@ -84,134 +82,21 @@ concurrency: cancel-in-progress: false jobs: - publish: - name: publish to npm via OIDC + publish-optional-deps: + name: publish @bugsee/cli family via OIDC runs-on: ubuntu-latest # Manual dispatch always runs. The automated path runs only for a # SUCCESSFUL Release that was triggered by a tag PUSH whose ref looks # like a version tag β€” this filters out PR-mode Release runs (which # complete with event == 'pull_request') and failed releases. The - # exact `vX.Y.Z` shape is re-checked in `resolve` as a belt-and-braces - # guard (`startsWith(..., 'v')` alone would also match e.g. `vnext`). - if: >- - github.event_name == 'workflow_dispatch' || - (github.event.workflow_run.conclusion == 'success' && - github.event.workflow_run.event == 'push' && - startsWith(github.event.workflow_run.head_branch, 'v')) - steps: - - name: Resolve release tag - id: resolve - env: - # Via env, never interpolated into the script: head_branch and the - # dispatch input are untrusted strings (defence in depth), and `${{ }}` is - # substituted BEFORE the shell parses the script (code injection). - EVENT_NAME: ${{ github.event_name }} - INPUT_TAG: ${{ github.event.inputs.tag }} - HEAD_BRANCH: ${{ github.event.workflow_run.head_branch }} - run: | - if [ "$EVENT_NAME" = "workflow_dispatch" ]; then - tag="$INPUT_TAG" - else - tag="$HEAD_BRANCH" - fi - # A newline would let the value smuggle extra `key=value` lines into - # $GITHUB_OUTPUT (e.g. should_publish=true) β€” refuse before writing it. - case "$tag" in *$'\n'*|*$'\r'*) echo "::error::tag contains a newline"; exit 1 ;; esac - echo "tag=$tag" >> "$GITHUB_OUTPUT" - if echo "$tag" | grep -Eq '^v[0-9]+\.[0-9]+\.[0-9]+'; then - echo "should_publish=true" >> "$GITHUB_OUTPUT" - echo "Resolved release tag: $tag" - else - echo "should_publish=false" >> "$GITHUB_OUTPUT" - echo "::notice::'$tag' is not a version tag (vX.Y.Z); skipping npm publish." - fi - - - name: Setup Node - # DELIBERATELY no `registry-url:` here β€” setup-node@v6 with that - # option writes an .npmrc containing - # `//registry.npmjs.org/:_authToken=${NODE_AUTH_TOKEN}`, which - # tells npm to use token auth instead of OIDC. We want the - # opposite: no .npmrc, so npm 11.5+ auto-uses the OIDC token - # minted from `permissions: id-token: write` above. - if: steps.resolve.outputs.should_publish == 'true' - uses: actions/setup-node@v7 - with: - node-version: '22' - - - name: Upgrade npm to satisfy Trusted Publishing floor (β‰₯ 11.5.1) - # Node 22's bundled npm is below the OIDC floor as of 2026. - if: steps.resolve.outputs.should_publish == 'true' - run: | - npm install -g npm@latest - node --version - npm --version - - - name: Download npm package tarball from release - if: steps.resolve.outputs.should_publish == 'true' - env: - GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} - TAG: ${{ steps.resolve.outputs.tag }} - run: | - mkdir -p release-assets - gh release download "$TAG" \ - --repo "$GITHUB_REPOSITORY" \ - --dir release-assets \ - --pattern 'bugsee-cli-npm-package.tar.gz' - ls -la release-assets/ - - - name: Publish via OIDC - # `npm publish ` accepts an npm-pack-format tarball - # directly β€” no extraction needed. npm 11.5+ detects the - # ACTIONS_ID_TOKEN_REQUEST_* env vars GitHub injects when - # `id-token: write` is granted, mints the OIDC token, and - # presents it to the registry. Trust on npmjs.com side maps - # (repo, workflow filename) β†’ @bugsee/bugsee-cli publish. - # - # Absolute path is critical: a bare relative path like - # `release-assets/bugsee-cli-npm-package.tar.gz` is parsed by - # npm as a git spec (`owner/repo`) and routed through git - # ls-remote, failing with a confusing SSH/permission error. - # - # `--tag` is intentionally omitted so each publish moves - # `dist-tags.latest`; the automated path always runs for the - # newest release, and manual backfills must be run oldestβ†’newest - # so `latest` ends on the highest version. - # - # `--provenance` emits a sigstore build-provenance attestation - # (supply-chain integrity). It requires a PUBLIC source repo, and - # bugsee/bugsee-cli is public β€” so it's enabled. If the repo ever - # goes private again, drop this flag: npm rejects provenance from - # private repos with `E422 … repository visibility: "private"` - # (OIDC auth itself is unaffected by visibility). - # - # Do NOT add `env: NODE_AUTH_TOKEN: ...` β€” even an empty value - # makes npm prefer broken token auth over OIDC. - if: steps.resolve.outputs.should_publish == 'true' - run: | - tarball="$GITHUB_WORKSPACE/release-assets/bugsee-cli-npm-package.tar.gz" - ls -la "$tarball" - npm publish "$tarball" --access public --provenance - - publish-optional-deps: - name: publish @bugsee/cli family via OIDC - runs-on: ubuntu-latest - # DELIBERATELY NOT `needs: publish`. The two jobs publish different - # packages from the same release and neither depends on the other's - # output; chaining them would make a @bugsee/bugsee-cli failure (or a - # re-publish of an already-published version) suppress the family, and - # vice versa. They share only the trigger. - # - # Same guard as `publish` above, for the same reasons β€” see its comment. + # exact `vX.Y.Z` shape is re-checked in `resolve` + # (`startsWith(..., 'v')` alone would also match e.g. `vnext`). if: >- github.event_name == 'workflow_dispatch' || (github.event.workflow_run.conclusion == 'success' && github.event.workflow_run.event == 'push' && startsWith(github.event.workflow_run.head_branch, 'v')) steps: - # Repeated rather than factored into a shared `resolve` job on purpose: - # hoisting it would mean editing the `publish` job above, which is the - # live, working OIDC path for @bugsee/bugsee-cli. Ten duplicated lines - # are cheaper than any risk to that. - name: Resolve release tag id: resolve env: @@ -231,15 +116,11 @@ jobs: # $GITHUB_OUTPUT (e.g. should_publish=true) β€” refuse before writing it. case "$tag" in *$'\n'*|*$'\r'*) echo "::error::tag contains a newline"; exit 1 ;; esac echo "tag=$tag" >> "$GITHUB_OUTPUT" - # ANCHORED, unlike the `publish` job's copy above. Unanchored, - # `^v[0-9]+\.[0-9]+\.[0-9]+` also matches `v1.2.3-rc1`, which would - # then publish as 1.2.3-rc1 with no `--tag` and MOVE - # `dist-tags.latest` onto a prerelease. Here that would hit all seven - # packages at once, so prereleases are matched explicitly and routed - # to `next` rather than silently treated as stable. The same latent - # bug in the `publish` job above is deliberately left alone: that is - # the live @bugsee/bugsee-cli path and is out of scope for this - # change. Worth a follow-up. + # ANCHORED. Unanchored, `^v[0-9]+\.[0-9]+\.[0-9]+` also matches + # `v1.2.3-rc1`, which would then publish as 1.2.3-rc1 with no `--tag` + # and MOVE `dist-tags.latest` onto a prerelease. That would hit all + # seven packages at once, so prereleases are matched explicitly and + # routed to `next` rather than silently treated as stable. if echo "$tag" | grep -Eq '^v[0-9]+\.[0-9]+\.[0-9]+$'; then { echo "should_publish=true" @@ -278,9 +159,9 @@ jobs: persist-credentials: false - name: Setup Node - # No `registry-url:` β€” see the note on the same step in `publish`. - # An .npmrc with `_authToken` would make npm prefer token auth over - # the OIDC token and every publish below would fail. + # DELIBERATELY no `registry-url:` β€” setup-node with that option writes + # an .npmrc with `_authToken=${NODE_AUTH_TOKEN}`, which makes npm prefer + # token auth over the OIDC token, and every publish below would fail. if: steps.resolve.outputs.should_publish == 'true' uses: actions/setup-node@v7 with: diff --git a/CLAUDE.md b/CLAUDE.md index 49a4fb9..42f24e1 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -169,21 +169,20 @@ size-check FAIL can fail the build (exit 40). ## Distribution & activation Releases are tag-driven: pushing a `vX.Y.Z` tag runs `release.yml` (cargo-dist -builds the target triples + a GitHub Release with shell/powershell/npm/homebrew +builds the target triples + a GitHub Release with shell/powershell/homebrew installers). Two workflows then chain off it automatically via `workflow_run` β€” `npm-publish.yml` (npm, via OIDC Trusted Publishing) and `mirror-to-s3.yml` (`download.bugsee.com/cli/v/` + the `latest` alias and the `cli/v.x/version.txt` pointer that `bugsee-cli update` reads). Both also keep a `workflow_dispatch` path for backfilling a missed version. -`npm-publish.yml` has TWO independent jobs, because npm ships two packages -for the same binary: `publish` takes the cargo-dist-generated -`bugsee-cli-npm-package.tar.gz` release asset straight to `@bugsee/bugsee-cli` -(a single package whose postinstall downloads the binary), while -`publish-optional-deps` runs `npm/build.mjs` over the release's binary archives -to assemble and publish `@bugsee/cli` plus its six `@bugsee/cli-` -packages (per-`os`/`cpu` `optionalDependencies`, so nothing downloads at install -time). Seven packages, platform packages FIRST β€” npm skips an unresolvable +`npm-publish.yml`'s one job, `publish-optional-deps`, runs `npm/build.mjs` over +the release's binary archives to assemble and publish `@bugsee/cli` plus its six +`@bugsee/cli-` packages (per-`os`/`cpu` `optionalDependencies`, so +nothing downloads at install time). The legacy cargo-dist package +`@bugsee/bugsee-cli` is retired (frozen at 0.8.2, deprecated; its launcher +exited 0 on a signal kill, #87) β€” there is no `"npm"` in dist's `installers` and +no job for it; do not re-add either. Seven packages, platform packages FIRST β€” npm skips an unresolvable optional dependency silently, so the front package must never be live ahead of them. See `npm/README.md`. Each package needs its own Trusted Publisher entry pointing at `npm-publish.yml`, written with `npm trust github --file diff --git a/Cargo.toml b/Cargo.toml index 281303f..bb7efb7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -66,12 +66,13 @@ cargo-dist-version = "0.32.0" allow-dirty = ["ci"] # CI backends to support ci = "github" -# The installers to generate for each app -installers = ["shell", "powershell", "npm", "homebrew"] +# The installers to generate for each app. +# No "npm", and NO `npm-scope`: setting a scope makes dist emit the npm installer +# (`@bugsee/bugsee-cli`) even when "npm" is absent here. That package is retired +# (#87); npm packages are built by `npm/build.mjs` and published by `npm-publish.yml`. +installers = ["shell", "powershell", "homebrew"] # A GitHub repo to push Homebrew formulas to tap = "bugsee/homebrew-tap" -# A namespace to use when publishing this package to the npm registry -npm-scope = "@bugsee" # Publish jobs to run in CI publish-jobs = ["homebrew"] # Target platforms to build apps for (Rust target-triple syntax) diff --git a/README.md b/README.md index 4b5b1dd..53e6001 100644 --- a/README.md +++ b/README.md @@ -528,7 +528,7 @@ Every metadata `POST` sets `X-Bugsee-Uploader: cli`. The in-language fallback up |---|---| | GitHub Releases + `download.bugsee.com/cli` (archives, `.sha256`, `latest/` and `v.x/version.txt`), with the shell / PowerShell installers and `bugsee-cli update` | everyone; generic CI | | npm `@bugsee/cli` β€” per-platform `optionalDependencies` | JS toolchains (React Native, web bundler plugins) | -| npm `@bugsee/bugsee-cli` β€” single package, `postinstall` downloader | legacy alias for the above | +| npm `@bugsee/bugsee-cli` β€” single package, `postinstall` downloader | **deprecated**, frozen at 0.8.2; use `@bugsee/cli` | | CDN download + SHA-256 checksum on first use | fastlane plugin (`resolveCli`), iOS SDK BugseeAgent | | Homebrew tap `bugsee/tap` | macOS / Linux developers | @@ -536,13 +536,9 @@ Planned, not published yet: Maven Central `com.bugsee:bugsee-cli`, NuGet `Bugsee Target platforms: macOS arm64 + x86_64, Linux x86_64 + aarch64 (glibc only; no musl build), Windows x86_64 + arm64. (Windows arm64 builds natively on a `windows-11-arm` runner β€” see [#20](https://github.com/bugsee/bugsee-cli/issues/20).) -### The two npm packages +### npm packages -Both ship the same binary at the same version, and both are published from -`.github/workflows/npm-publish.yml` off the same release. They differ in how -the binary reaches `node_modules`: - -**`@bugsee/cli` β€” prefer this one.** The binary lives in six per-platform +**`@bugsee/cli`.** The binary lives in six per-platform packages (`@bugsee/cli-darwin-arm64`, `-darwin-x64`, `-linux-arm64`, `-linux-x64`, `-win32-x64`, `-win32-arm64`), each declaring `os`/`cpu` and pinned to the exact version by the front package's `optionalDependencies`. npm @@ -556,12 +552,14 @@ it warns and exits 0, leaving the error to surface only if `bugsee-cli` is actually invoked. It also exports `binaryPath()` for spawning the binary directly. Sources: [`npm/`](npm/). -**`@bugsee/bugsee-cli` β€” the cargo-dist package, kept as a working alias.** -One package, no binary inside; its `postinstall` runs `install.js`, which -downloads the archive for the host on every fresh install. That means it needs -network at install time and does nothing at all under `--ignore-scripts`. -Generated by cargo-dist from `installers = [..., "npm"]`; unchanged, still -published, still supported. New integrations should use `@bugsee/cli`. +**`@bugsee/bugsee-cli` β€” deprecated.** The former cargo-dist package: one +package, no binary inside, whose `postinstall` downloaded the archive on every +fresh install (so it needed network and did nothing under `--ignore-scripts`). +It is no longer published. 0.8.2 is the last version and still downloads the +0.8.1 binary; it fixes a launcher that exited `0` when the binary was killed by +a signal ([#87](https://github.com/bugsee/bugsee-cli/issues/87)). Every earlier +version is deprecated. Migrate to `@bugsee/cli`, which reports a signal death as +`128 + signum`. ## Design notes diff --git a/npm/README.md b/npm/README.md index 9930d3f..1d7415a 100644 --- a/npm/README.md +++ b/npm/README.md @@ -22,7 +22,8 @@ six `optionalDependencies` to that exact version β€” never a range. npm resolves one of them by the `os`/`cpu` fields, so a consumer downloads one binary, not six, and nothing has to run at install time. That is the difference from `@bugsee/bugsee-cli` (generated by cargo-dist, one package whose `postinstall` -downloads the binary), which still publishes unchanged as an alias. +downloads the binary), which is deprecated and no longer published (last +version 0.8.2, see #87). ## Layout @@ -166,7 +167,7 @@ blocks publishing across the whole account for a while. A granular token is not a way around it either: npm removes direct publish for 2FA-bypass tokens in January 2027. -All eight published names β€” `@bugsee/bugsee-cli`, `@bugsee/cli` and the six +All seven published names β€” `@bugsee/cli` and the six platform packages β€” are already bootstrapped and need none of this. (Their `0.0.0` placeholders were kept; the 72-hour unpublish window has passed, and they are not under any dist-tag a user would install.) diff --git a/npm/cli/README.md b/npm/cli/README.md index a81ebd0..c2b5f66 100644 --- a/npm/cli/README.md +++ b/npm/cli/README.md @@ -79,9 +79,9 @@ gate). A launcher that cannot find a binary at all exits `1`, which is the ## Other install channels -`@bugsee/bugsee-cli` is the same CLI published by cargo-dist as a single -package that downloads its binary in a `postinstall`. It remains supported as -an alias; **prefer `@bugsee/cli`** β€” it is the one that works under +`@bugsee/bugsee-cli` was the same CLI published by cargo-dist as a single +package that downloads its binary in a `postinstall`. It is deprecated and no +longer published (last version 0.8.2); use `@bugsee/cli`, which also works under `--ignore-scripts`. See the [Distribution section](https://github.com/bugsee/bugsee-cli#distribution) for the shell installer, Homebrew tap, and the per-build-system bundles. From 9cda22d78e8338077db9199aa67d8a65e0abbd2c Mon Sep 17 00:00:00 2001 From: Alexey Karimov Date: Wed, 21 Oct 2026 23:18:03 +0500 Subject: [PATCH 2/2] test(npm): pin the @bugsee/cli launcher's exit status on a signal kill MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit #87 asked for a test that spawns a child which kills itself with SIGKILL and asserts a non-zero launcher exit. The fixed launcher was already correct, but nothing pinned it, and the retired package shows how easily it regresses. npm/test-launcher.js copies the real launcher to a temp dir, points it at a fake binary through its vendor/ fallback, and checks SIGKILL -> 137, SIGTERM -> 143, ordinary exit codes forwarded verbatim, and argv/stdout passed through. Verified to fail against `process.exit(result.status)`. Wired into the `npm scripts` CI job. πŸ€– Generated with [Claude Code](https://claude.com/claude-code) --- .github/workflows/ci.yml | 3 ++ npm/README.md | 1 + npm/test-launcher.js | 75 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 79 insertions(+) create mode 100644 npm/test-launcher.js diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 75ace0e..736ca6c 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -125,6 +125,9 @@ jobs: shellcheck npm/bootstrap-names.sh npm/test-bootstrap-names.sh - name: trust_state fixtures run: bash npm/test-bootstrap-names.sh + - name: launcher exit status + # A binary killed by a signal must not look like a success (#87). + run: node --test npm/test-launcher.js test: name: test (${{ matrix.os }}) diff --git a/npm/README.md b/npm/README.md index 1d7415a..11286dc 100644 --- a/npm/README.md +++ b/npm/README.md @@ -37,6 +37,7 @@ version 0.8.2, see #87). | `build.mjs` | Assembles all seven packages from a directory of release assets. | | `bootstrap-names.sh` | One-time registry + trusted-publisher bootstrap for a NEW package name (see below). | | `test-bootstrap-names.sh` | Fixture tests for that script's trust-entry check. No network or npm needed: `bash npm/test-bootstrap-names.sh`. | +| `test-launcher.js` | Pins the launcher's exit-status contract, incl. a binary killed by a signal (#87). No network or built binary: `node --test npm/test-launcher.js`. | The six platform packages have no sources here: `build.mjs` mints each `package.json` and `README.md` from the platform table, so adding a platform is diff --git a/npm/test-launcher.js b/npm/test-launcher.js new file mode 100644 index 0000000..16309a5 --- /dev/null +++ b/npm/test-launcher.js @@ -0,0 +1,75 @@ +// Tests the exit-status contract of the @bugsee/cli launcher (npm/cli/bin/bugsee-cli.js). +// No network, npm or built binary needed. +// +// Integrators branch on the launcher's exit code (<= 2 falls back, >= 10 is a real +// failure), so a binary killed by a signal must never be reported as success: +// `spawnSync` returns `status: null` for it, and `process.exit(null)` exits 0. That +// is the bug that made the retired @bugsee/bugsee-cli launcher report a killed CLI +// as a success (bugsee/bugsee-cli#87). +// +// The launcher is copied into a temp dir and pointed at a fake binary through its +// `vendor/` fallback, so the test runs the real file without touching the package. +// +// Run: node --test npm/test-launcher.js + +"use strict"; + +const { test } = require("node:test"); +const assert = require("node:assert"); +const { spawnSync } = require("node:child_process"); +const fs = require("node:fs"); +const os = require("node:os"); +const path = require("node:path"); + +const { PLATFORMS, currentTriple } = require("./cli/lib/platforms.js"); + +const triple = currentTriple(); +// The fake binary is a shell script, and Windows has no signals to die from. +const skip = + process.platform === "win32" + ? "needs POSIX signals" + : !triple && "no published binary for this platform"; + +/** Run the launcher against a fake binary whose body is `script`. */ +function runLauncher(script, args = []) { + const root = fs.mkdtempSync(path.join(os.tmpdir(), "bugsee-launcher-")); + try { + fs.cpSync(path.join(__dirname, "cli"), path.join(root, "cli"), { + recursive: true, + filter: (src) => !src.includes("node_modules"), + }); + const vendor = path.join(root, "cli", "vendor"); + fs.mkdirSync(vendor, { recursive: true }); + const bin = path.join(vendor, PLATFORMS[triple].bin); + fs.writeFileSync(bin, `#!/bin/sh\n${script}\n`, { mode: 0o755 }); + return spawnSync( + process.execPath, + [path.join(root, "cli", "bin", "bugsee-cli.js"), ...args], + { encoding: "utf8" }, + ); + } finally { + fs.rmSync(root, { recursive: true, force: true }); + } +} + +test("a binary killed by SIGKILL is not reported as success", { skip }, () => { + const r = runLauncher("kill -9 $$"); + assert.strictEqual(r.status, 128 + os.constants.signals.SIGKILL, r.stderr); +}); + +test("a binary killed by SIGTERM exits 128 + signum", { skip }, () => { + const r = runLauncher("kill -15 $$"); + assert.strictEqual(r.status, 128 + os.constants.signals.SIGTERM, r.stderr); +}); + +test("every ordinary exit code is forwarded verbatim", { skip }, () => { + for (const code of [0, 1, 2, 10, 23, 40]) { + assert.strictEqual(runLauncher(`exit ${code}`).status, code, `exit ${code}`); + } +}); + +test("argv and stdout pass through untouched", { skip }, () => { + const r = runLauncher('printf "%s|" "$@"', ["a b", "--flag=1"]); + assert.strictEqual(r.status, 0); + assert.strictEqual(r.stdout, "a b|--flag=1|"); +});