From 91b7e4cb947835ba687f7d1d19bd6aaacf93db3f Mon Sep 17 00:00:00 2001 From: Kevin Codex Date: Wed, 18 Nov 2026 13:23:16 +0800 Subject: [PATCH 1/8] feat(npm): publish the CLI as @twigpine/cli, one package with a build per platform The npm package moves from @gitlawb/gl to @twigpine/cli, built the way Zero is: a wrapper with launchers and a payload per platform, published as versions of the same package (X.Y.Z--) and pinned through npm: aliases. No install scripts and no downloads at install time. - npm/cli: the wrapper's manifest and launchers. twigpine and twig run the CLI, gl runs it under its old name, and both helper names run the git helper with stdio, exit status and signals passed through. A missing payload says how to reinstall. Windows x64 is published for the first time; Windows on ARM uses it. - scripts/npm/build-platform-packages.mjs builds the wrapper and the five payloads from the release archives, checking each against its .sha256. - scripts/npm/publish-packages.sh publishes the payloads under the platform dist-tag, waits until npm shows them, then publishes the wrapper; latest never moves onto a payload or backward, and re-runs skip what already went out. - release.yml: a new npm-publish-cli job in its own npm-publish environment fetches the archives by asset id, builds and publishes. The @gitlawb/gl job stays for one release as the bridge, then goes. - The CLI tells users of an @gitlawb/gl install, on a terminal, how to move to the new package. - scripts/npm/test-npm-packages.sh covers the build, the launchers (with the real CLI when one is passed) and the publish order against a fake registry; pr-checks runs it. - docs/NPM_PACKAGING.md describes the package, the publishing rules, the one-time npm and GitHub setup, and the move from @gitlawb/gl. --- .github/workflows/pr-checks.yml | 15 ++ .github/workflows/release.yml | 98 +++++++++ crates/twigpine-cli/src/main.rs | 52 +++++ docs/LEGACY-NAMES.md | 5 +- docs/NPM_PACKAGING.md | 112 +++++++++++ npm/cli/README.md | 38 ++++ npm/cli/bin/git-remote-twigpine.js | 7 + npm/cli/bin/gl.js | 6 + npm/cli/bin/launch.js | 101 ++++++++++ npm/cli/bin/twigpine.js | 5 + npm/cli/package.json | 47 +++++ npm/packages/gl/README.md | 3 + scripts/npm/build-platform-packages.mjs | 253 +++++++++++++++++++++++ scripts/npm/publish-packages.sh | 195 ++++++++++++++++++ scripts/npm/test-npm-packages.sh | 255 ++++++++++++++++++++++++ scripts/test-resolve-release-tag.sh | 18 ++ 16 files changed, 1209 insertions(+), 1 deletion(-) create mode 100644 docs/NPM_PACKAGING.md create mode 100644 npm/cli/README.md create mode 100755 npm/cli/bin/git-remote-twigpine.js create mode 100755 npm/cli/bin/gl.js create mode 100755 npm/cli/bin/launch.js create mode 100755 npm/cli/bin/twigpine.js create mode 100644 npm/cli/package.json create mode 100755 scripts/npm/build-platform-packages.mjs create mode 100755 scripts/npm/publish-packages.sh create mode 100755 scripts/npm/test-npm-packages.sh diff --git a/.github/workflows/pr-checks.yml b/.github/workflows/pr-checks.yml index acd3105b..4000275d 100644 --- a/.github/workflows/pr-checks.yml +++ b/.github/workflows/pr-checks.yml @@ -124,6 +124,21 @@ jobs: - name: Test release tag resolver run: scripts/test-resolve-release-tag.sh + npm-packaging: + name: npm packaging + runs-on: ubuntu-latest + timeout-minutes: 5 + steps: + - name: Check out repository + uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 + with: + persist-credentials: false + + # The @twigpine/cli build, launchers and publish order, against fake + # release archives and a fake registry. Nothing is published. + - name: Test the npm package build, launchers and publish script + run: bash scripts/npm/test-npm-packages.sh + installer: name: installer checksum enforcement runs-on: ubuntu-latest diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 1dd2e577..09bffce4 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -801,6 +801,104 @@ jobs: npm publish "npm/packages/$pkg" --provenance --access public --tag "$dist_tag" done + npm-publish-cli: + # @twigpine/cli: one package, a wrapper plus a platform version per target, + # no install script (docs/NPM_PACKAGING.md). Its own environment so its + # trusted publisher and approval are separate from the rest of the release. + # The @gitlawb/gl job above stays for one more release, as the bridge that + # points existing installs at this package, and is then removed. + name: Publish @twigpine/cli to npm + needs: [release-please, release-binaries] + if: >- + ${{ !cancelled() && ( + (needs.release-please.outputs.release_created == 'true' && needs.release-binaries.result == 'success') || + (github.event_name == 'workflow_dispatch' && inputs.npm_backfill_tag != '') + ) }} + runs-on: ubuntu-latest + permissions: + contents: read + id-token: write # npm trusted publishing (OIDC) + provenance + environment: npm-publish + steps: + # The packaging scripts and launchers come from this workflow's commit; + # only the programs come from the release. A backfill of a release cut + # before this job existed therefore still gets today's launchers. + - name: Check out workflow scripts + uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 + with: + persist-credentials: false + + - name: Resolve release tag + id: rel + env: + DISPATCH_TAG: ${{ inputs.npm_backfill_tag }} + RELEASE_TAG: ${{ needs.release-please.outputs.tag_name }} + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} + run: | + set -euo pipefail + scripts/resolve-release-tag.sh "${DISPATCH_TAG:-$RELEASE_TAG}" + + - name: Set up Node 24 + OIDC-capable npm + uses: actions/setup-node@a0853c24544627f65ddf259abe73b1d18a591444 # v5.0.0 + with: + node-version: "24" + - name: Ensure npm supports trusted publishing + run: | + set -euo pipefail + npm install -g npm@11.19.1 + installed="$(npm --version)" + echo "npm installed: $installed" + if [ "$installed" != "11.19.1" ]; then + echo "::error::npm is not the pinned 11.19.1 (got: $installed)" + exit 1 + fi + + - name: Fetch release archives + env: + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} + VERSION: ${{ steps.rel.outputs.version }} + TAG: ${{ steps.rel.outputs.tag }} + ASSETS: ${{ steps.rel.outputs.assets }} + run: | + set -euo pipefail + mkdir -p _art + for archive in \ + "twigpine-node-${VERSION}-aarch64-apple-darwin.tar.gz" \ + "twigpine-node-${VERSION}-x86_64-apple-darwin.tar.gz" \ + "twigpine-node-${VERSION}-aarch64-unknown-linux-musl.tar.gz" \ + "twigpine-node-${VERSION}-x86_64-unknown-linux-musl.tar.gz" \ + "twigpine-node-${VERSION}-x86_64-pc-windows-msvc.zip"; do + for file in "$archive" "$archive.sha256"; do + # By the asset id the resolver captured and uploader-checked, not + # by name: release assets are mutable, an id is not. + want="$(printf '%s' "$file" | base64 -w0)" + asset_id="$(printf '%s\n' "$ASSETS" | awk -v n="$want" '$1 == n {print $2; exit}')" + if [ -z "$asset_id" ]; then + echo "::error::release $TAG has no asset $file captured at resolve time" + exit 1 + fi + gh api "repos/$GITHUB_REPOSITORY/releases/assets/$asset_id" \ + -H 'Accept: application/octet-stream' > "_art/$file" + done + done + + - name: Build packages + env: + VERSION: ${{ steps.rel.outputs.version }} + run: | + set -euo pipefail + node scripts/npm/build-platform-packages.mjs \ + --version "$VERSION" --artifacts-dir _art --out-dir _npm + + - name: Publish @twigpine/cli + env: + # No token: npm exchanges this job's GitHub OIDC identity with the + # registry (trusted publishing). + TWIGPINE_NPM_PUBLISH_FLAGS: --provenance + run: | + set -euo pipefail + scripts/npm/publish-packages.sh _npm + homebrew-bump: name: Bump Homebrew tap needs: [release-please, release-binaries] diff --git a/crates/twigpine-cli/src/main.rs b/crates/twigpine-cli/src/main.rs index ebc3948d..13696231 100644 --- a/crates/twigpine-cli/src/main.rs +++ b/crates/twigpine-cli/src/main.rs @@ -158,6 +158,33 @@ fn old_command_name_notice() { } } +/// The npm package was `@gitlawb/gl` and is `@twigpine/cli` now. A program +/// installed by the old package says so once per run, on a terminal only: +/// npm hides install-script output, and `npm update -g @gitlawb/gl` cannot +/// move anyone to a package with another name. +fn old_npm_package_notice() { + use std::io::IsTerminal; + let from_old_package = std::env::current_exe() + .and_then(std::fs::canonicalize) + .is_ok_and(|exe| installed_by_old_npm_package(&exe)); + if from_old_package && std::io::stderr().is_terminal() { + eprintln!( + "note: on npm, @gitlawb/gl is now @twigpine/cli. To keep getting updates:\n\ + \x20 npm uninstall -g @gitlawb/gl && npm install -g @twigpine/cli" + ); + } +} + +/// Whether `exe` lives inside an `@gitlawb/gl` npm install. +fn installed_by_old_npm_package(exe: &std::path::Path) -> bool { + let parts: Vec<_> = exe.components().map(|c| c.as_os_str()).collect(); + parts.windows(3).any(|w| { + w[0] == std::ffi::OsStr::new("node_modules") + && w[1] == std::ffi::OsStr::new("@gitlawb") + && w[2] == std::ffi::OsStr::new("gl") + }) +} + fn main() -> Result<()> { // Before the runtime starts its threads, and before anything reads a // setting: accept the names settings had before the rename. @@ -176,6 +203,7 @@ async fn run() -> Result<()> { .init(); old_command_name_notice(); + old_npm_package_notice(); let cli = Cli::parse(); match cli.command { @@ -210,3 +238,27 @@ async fn run() -> Result<()> { Commands::Whoami(args) => whoami::run(args).await, } } + +#[cfg(test)] +mod npm_package_notice_tests { + use super::installed_by_old_npm_package; + use std::path::Path; + + #[test] + fn only_an_install_under_the_old_package_name() { + assert!(installed_by_old_npm_package(Path::new( + "/usr/local/lib/node_modules/@gitlawb/gl/bin/gl" + ))); + assert!(installed_by_old_npm_package(Path::new( + "/home/a/.npm-global/lib/node_modules/@gitlawb/gl/bin/twigpine" + ))); + for other in [ + "/usr/local/lib/node_modules/@twigpine/cli/node_modules/@twigpine/cli-linux-x64/twigpine", + "/home/a/.local/bin/twigpine", + "/opt/gitlawb/gl/bin/gl", + "/usr/local/lib/node_modules/@gitlawb/glx/bin/gl", + ] { + assert!(!installed_by_old_npm_package(Path::new(other)), "{other}"); + } + } +} diff --git a/docs/LEGACY-NAMES.md b/docs/LEGACY-NAMES.md index a2c072cf..c43d35f4 100644 --- a/docs/LEGACY-NAMES.md +++ b/docs/LEGACY-NAMES.md @@ -52,13 +52,16 @@ Readers accept both spellings today, through `twigpine_core::url_scheme`. | `X-Gitlawb-*` headers | `twigpine-core/src/legacy_headers.rs` | | `gl`, `git-remote-gitlawb`, `gitlawb-node` command names | Installers, Homebrew formula, Docker image, npm package. `git-remote-gitlawb` stays as long as `gitlawb://` remotes exist. | +## Being retired + +- The npm package `@gitlawb/gl`, replaced by `@twigpine/cli`: published once more as a bridge, then deprecated (`docs/NPM_PACKAGING.md`). The CLI points users of the old package at the new one. + ## Not decided yet - `gitlawb_*` metric names, with the infra change and whatever dashboards read them. - The default database URL `postgresql://localhost/gitlawb` in `twigpine-node/src/config.rs`, and the database and user names in `docker-compose.yml`: existing databases carry these names. - `infra/`, the macOS app and `bootstrap-peers.json`, which still set the old setting names and work through the fallback. -- The npm package name `@gitlawb/gl`, replaced by `@twigpine/cli` in its own change. ## Not ours diff --git a/docs/NPM_PACKAGING.md b/docs/NPM_PACKAGING.md new file mode 100644 index 00000000..c1cb3bce --- /dev/null +++ b/docs/NPM_PACKAGING.md @@ -0,0 +1,112 @@ +# npm packaging + +How `@twigpine/cli` is put together on npm and the rules the release pipeline +follows. Read this before touching `npm/cli/`, `scripts/npm/`, or the npm jobs +in `.github/workflows/release.yml`. The design is the one Zero uses +(`Twigpine/zero`, `docs/NPM_PACKAGING.md`), without Zero's vendored helpers +and without a fallback downloader. + +## Goals + +`npm install -g @twigpine/cli` must be silent and self-contained: + +- **No install scripts anywhere in the tree.** npm's `allow-scripts` gating, + Bun's blocked-by-default scripts and pnpm's strict mode all install it + without prompts. The old package, `@gitlawb/gl`, copied its programs into + place from a `postinstall` script, and npm links commands before that runs, + so for a while it installed with no commands on PATH at all. +- **Nothing fetched outside the npm registry at install time.** +- **Every command name works:** `twigpine`, `twig`, `gl`, and the git helper + under both names git looks for, `git-remote-twigpine` and `git-remote-gitlawb`. + +## Architecture + +``` +@twigpine/cli <- wrapper: launchers + optionalDependencies +├─ @twigpine/cli-darwin-arm64 -> npm:@twigpine/cli@{version}-darwin-arm64 +├─ @twigpine/cli-darwin-x64 -> npm:@twigpine/cli@{version}-darwin-x64 +├─ @twigpine/cli-linux-arm64 -> npm:@twigpine/cli@{version}-linux-arm64 +├─ @twigpine/cli-linux-x64 -> npm:@twigpine/cli@{version}-linux-x64 +└─ @twigpine/cli-win32-x64 -> npm:@twigpine/cli@{version}-win32-x64 +``` + +- The platform payloads are **versions of the same package**, published at + suffixed versions (`0.9.0-linux-x64`) and referenced through `npm:` aliases. + One package name means one trusted-publisher setting. Each payload sets `os` + and `cpu`, so npm installs exactly one. +- A payload holds `twigpine` and `git-remote-twigpine` (`.exe` on Windows), + copied from that platform's release archive after its checksum is checked, + plus the licence files. It has no `bin`, no scripts and no dependencies. + The node daemon is not on npm. +- The wrapper holds the launchers in `npm/cli/bin/` and a manifest with no + scripts and no dependencies, only the exact-version `optionalDependencies`. + `scripts/npm/build-platform-packages.mjs` writes both from the release + archives. +- A launcher finds its payload from `process.platform`/`process.arch` and runs + the program with stdio, the exit status and a terminating signal passed + straight through, so git can drive the helper through it. `gl` starts the + program under the name `gl`, which prints a note about the new name on a + terminal. Windows on ARM runs the x64 payload. +- There is no downloader. If the payload is missing (`--omit=optional`, an + unsupported platform), the launcher says how to reinstall or install + without npm. + +## Publishing rules + +`scripts/npm/publish-packages.sh` implements these and +`scripts/npm/test-npm-packages.sh` checks them against a fake registry. + +1. **Platform versions never become `latest`.** They are semver prereleases + of the release version, so each goes out under the `platform` dist-tag, + and the script checks `latest` was not moved before it publishes the wrapper. +2. **Payloads are on the registry before the wrapper.** npm applies a publish + asynchronously, so the script waits until npm shows every payload before + publishing the wrapper (up to 30 minutes in all). +3. **Exact pinning.** The wrapper at `X.Y.Z` references `X.Y.Z--` + exactly; the script refuses to start when the wrapper's aliases do not + match the payloads beside it. +4. **`latest` never moves backward.** A backfill of an older release publishes + the wrapper under `backfill` instead. +5. **Re-runs are safe.** A version already on the registry is skipped. Only + npm's E404 counts as "not published"; any other lookup failure stops the job. +6. **Trusted publishing only.** The `npm-publish-cli` job runs in the + `npm-publish` environment and authenticates with GitHub OIDC; no tokens. + npm checks provenance against `repository` in the manifest, so it must + name `Twigpine/node`. + +## Before the first publish + +Done once, by a maintainer: + +1. Publish a placeholder `@twigpine/cli@0.0.0` by hand; npm only lets a + trusted publisher be configured on a package that exists. +2. On npmjs.com, add a trusted publisher to `@twigpine/cli`: repository + `Twigpine/node`, workflow `release.yml`, environment `npm-publish`. +3. In the GitHub repository settings, create the `npm-publish` environment, + limited to the `main` branch, with a required reviewer. + +Until then the `npm-publish-cli` job fails on its own; the rest of the release +is unaffected. + +## The move from `@gitlawb/gl` + +Both packages provide the same commands, so npm refuses to install one over +the other (`EEXIST`), and forcing it lets a later uninstall of the old package +delete the shared commands. Users uninstall the old package first: + +```sh +npm uninstall -g @gitlawb/gl +npm install -g @twigpine/cli +``` + +To reach existing users: + +- The first release that publishes `@twigpine/cli` also publishes + `@gitlawb/gl` once more (the `npm-publish` job), so `npm update -g + @gitlawb/gl` installs something and shows its deprecation message. +- A CLI installed from `@gitlawb/gl` prints the two commands above on a + terminal. +- After that release, a maintainer deprecates every `@gitlawb/gl*` version + with a pointer to `@twigpine/cli`, and the `npm-publish` job and + `npm/packages/` are removed. The old versions stay on the registry: existing + installs resolve their platform packages through them. diff --git a/npm/cli/README.md b/npm/cli/README.md new file mode 100644 index 00000000..ac6da850 --- /dev/null +++ b/npm/cli/README.md @@ -0,0 +1,38 @@ +# @twigpine/cli + +The Twigpine command line: `twigpine` (short form `twig`) and the git remote +helper that lets plain `git clone`, `git fetch` and `git push` talk to +Twigpine nodes. + +```sh +npm install -g @twigpine/cli +twigpine doctor +twigpine quickstart +``` + +npm installs one prebuilt program for your platform: macOS (arm64, x64), +Linux (arm64, x64) or Windows (x64). There is no install script. + +The package also provides `gl`, the command's name before the rename, and the +helper under both names git looks for: `git-remote-twigpine` and +`git-remote-gitlawb`. + +## Moving from `@gitlawb/gl` + +Both packages provide the same commands, so npm will not install one over +the other. Uninstall the old one first: + +```sh +npm uninstall -g @gitlawb/gl +npm install -g @twigpine/cli +``` + +## Without npm + +```sh +curl -fsSL https://twigpine.com/install.sh | sh # macOS, Linux +irm https://twigpine.com/install.ps1 | iex # Windows +brew install twigpine/tap/gl # Homebrew +``` + +Source, issues and documentation: https://github.com/Twigpine/node diff --git a/npm/cli/bin/git-remote-twigpine.js b/npm/cli/bin/git-remote-twigpine.js new file mode 100755 index 00000000..49561fdc --- /dev/null +++ b/npm/cli/bin/git-remote-twigpine.js @@ -0,0 +1,7 @@ +#!/usr/bin/env node +// The git remote helper, as `git-remote-twigpine` and `git-remote-gitlawb`. +// Git picks the helper by URL scheme and talks to it over stdin and stdout, +// which pass straight through. +import { run } from './launch.js'; + +run('git-remote-twigpine', 'git-remote-twigpine'); diff --git a/npm/cli/bin/gl.js b/npm/cli/bin/gl.js new file mode 100755 index 00000000..e4a394e2 --- /dev/null +++ b/npm/cli/bin/gl.js @@ -0,0 +1,6 @@ +#!/usr/bin/env node +// `gl`, the command's name before the rename. The program sees itself started +// as `gl` and prints a one-line note about the new name on a terminal. +import { run } from './launch.js'; + +run('twigpine', 'gl'); diff --git a/npm/cli/bin/launch.js b/npm/cli/bin/launch.js new file mode 100755 index 00000000..a336c61e --- /dev/null +++ b/npm/cli/bin/launch.js @@ -0,0 +1,101 @@ +// Shared by the command launchers in this directory. Finds the native program +// in this platform's payload and runs it, passing arguments, stdio, the exit +// status and any terminating signal straight through. +// +// The payload is a version of this same package, installed under an npm: +// alias named after process.platform and process.arch (see +// docs/NPM_PACKAGING.md). There is no install script and no downloader: if +// npm did not install the payload, the launcher says how to fix it. + +import { spawnSync } from 'node:child_process'; +import { existsSync, readFileSync } from 'node:fs'; +import { createRequire } from 'node:module'; +import { dirname, join } from 'node:path'; +import { fileURLToPath } from 'node:url'; + +const packageRoot = dirname(dirname(fileURLToPath(import.meta.url))); +const DEFAULT_PACKAGE_NAME = '@twigpine/cli'; + +// The platforms a payload is published for. Anything else has no prebuilt +// program; Windows on ARM runs the x64 one under emulation. +export const SUPPORTED = ['darwin-arm64', 'darwin-x64', 'linux-arm64', 'linux-x64', 'win32-x64']; + +// The npm name this wrapper was published under, read from its own manifest +// so the name lives in one place. +export function readPackageName(root = packageRoot) { + try { + const { name } = JSON.parse(readFileSync(join(root, 'package.json'), 'utf8')); + if (typeof name === 'string' && name !== '') return name; + } catch { + // An unreadable manifest must not stop the command from running. + } + return DEFAULT_PACKAGE_NAME; +} + +// The payload alias for a platform. Windows on ARM falls back to x64. +export function payloadName(packageName, platform = process.platform, arch = process.arch) { + const effectiveArch = platform === 'win32' && arch === 'arm64' ? 'x64' : arch; + return `${packageName}-${platform}-${effectiveArch}`; +} + +// Path of `program` inside the installed payload, or null when the payload or +// the program is missing. +export function nativeProgram(program, { + packageName = readPackageName(), + platform = process.platform, + arch = process.arch, + resolve = (specifier) => createRequire(import.meta.url).resolve(specifier), +} = {}) { + let manifest; + try { + manifest = resolve(`${payloadName(packageName, platform, arch)}/package.json`); + } catch { + return null; + } + const file = platform === 'win32' ? `${program}.exe` : program; + const candidate = join(dirname(manifest), file); + return existsSync(candidate) ? candidate : null; +} + +export function missingProgramMessage(command, packageName, platform = process.platform, arch = process.arch) { + const here = `${platform}-${arch}`; + const supported = SUPPORTED.includes(here) || (platform === 'win32' && arch === 'arm64'); + const lines = [`${command}: the Twigpine program for ${here} is not installed.`]; + if (supported) { + lines.push( + `npm installs it as an optional dependency (${payloadName(packageName, platform, arch)}).`, + 'Reinstall without omitting optional dependencies:', + ` npm install -g ${packageName}`, + ); + } else { + lines.push(`No prebuilt program is published for ${here} (supported: ${SUPPORTED.join(', ')}).`); + } + lines.push( + 'Or install without npm:', + platform === 'win32' + ? ' irm https://twigpine.com/install.ps1 | iex' + : ' curl -fsSL https://twigpine.com/install.sh | sh', + ); + return lines.join('\n'); +} + +// Run `program` with this process's arguments. `argv0` is what the program +// sees as its own name; the CLI uses it to tell when it was started as `gl`. +export function run(program, argv0) { + const packageName = readPackageName(); + const binary = nativeProgram(program, { packageName }); + if (!binary) { + process.stderr.write(missingProgramMessage(argv0, packageName) + '\n'); + process.exit(1); + } + const child = spawnSync(binary, process.argv.slice(2), { stdio: 'inherit', argv0 }); + if (child.error) { + process.stderr.write(`${argv0}: could not start ${binary}: ${child.error.message}\n`); + process.exit(1); + } + if (child.signal) { + // End the same way the program did, so a shell or git sees the signal. + process.kill(process.pid, child.signal); + } + process.exit(child.status ?? 1); +} diff --git a/npm/cli/bin/twigpine.js b/npm/cli/bin/twigpine.js new file mode 100755 index 00000000..52aec791 --- /dev/null +++ b/npm/cli/bin/twigpine.js @@ -0,0 +1,5 @@ +#!/usr/bin/env node +// `twigpine` and its short form `twig`. +import { run } from './launch.js'; + +run('twigpine', 'twigpine'); diff --git a/npm/cli/package.json b/npm/cli/package.json new file mode 100644 index 00000000..5e95c7c4 --- /dev/null +++ b/npm/cli/package.json @@ -0,0 +1,47 @@ +{ + "name": "@twigpine/cli", + "version": "0.0.0", + "description": "Twigpine CLI and git remote helper: decentralized git for AI agents and developers.", + "type": "module", + "bin": { + "twigpine": "bin/twigpine.js", + "twig": "bin/twigpine.js", + "gl": "bin/gl.js", + "git-remote-twigpine": "bin/git-remote-twigpine.js", + "git-remote-gitlawb": "bin/git-remote-twigpine.js" + }, + "files": [ + "bin/*.js" + ], + "engines": { + "node": ">=18" + }, + "os": [ + "darwin", + "linux", + "win32" + ], + "cpu": [ + "x64", + "arm64" + ], + "keywords": [ + "twigpine", + "git", + "decentralized", + "cli", + "ai-agents", + "did", + "ucan" + ], + "repository": { + "type": "git", + "url": "git+https://github.com/Twigpine/node.git", + "directory": "npm/cli" + }, + "homepage": "https://twigpine.com", + "bugs": { + "url": "https://github.com/Twigpine/node/issues" + }, + "license": "MIT OR Apache-2.0" +} diff --git a/npm/packages/gl/README.md b/npm/packages/gl/README.md index 618712a9..89bf2f3d 100644 --- a/npm/packages/gl/README.md +++ b/npm/packages/gl/README.md @@ -1,5 +1,8 @@ # @gitlawb/gl +> **This package is replaced by [`@twigpine/cli`](https://www.npmjs.com/package/@twigpine/cli).** Both provide the same commands, so uninstall this one first: +> `npm uninstall -g @gitlawb/gl && npm install -g @twigpine/cli` + The [Twigpine](https://twigpine.com) CLI — decentralized git for AI agents and developers. ## Install diff --git a/scripts/npm/build-platform-packages.mjs b/scripts/npm/build-platform-packages.mjs new file mode 100755 index 00000000..d785ecb9 --- /dev/null +++ b/scripts/npm/build-platform-packages.mjs @@ -0,0 +1,253 @@ +#!/usr/bin/env node +// build-platform-packages.mjs: assemble the npm payloads for a release from +// the release archives (twigpine-node--.tar.gz / .zip). +// +// Layout written under --out-dir (default dist/npm): +// wrapper/ @twigpine/cli@X.Y.Z: the command launchers +// (npm/cli/bin) and a manifest with NO scripts +// and NO dependencies, only optionalDependencies +// aliasing the platform versions below. +// platforms/cli--/ +// @twigpine/cli@X.Y.Z--: the +// twigpine and git-remote-twigpine programs +// from that platform's archive. +// +// The platform payloads are versions of the SAME package at suffixed +// (semver prerelease) versions, referenced from the wrapper through npm: +// aliases, so one package name needs one trusted-publisher setting. See +// docs/NPM_PACKAGING.md. +// +// Each archive is checked against its .sha256 file before extraction. All five +// platforms are required unless --only narrows the set (tests only); the +// wrapper always references all five. +// +// Usage: +// node scripts/npm/build-platform-packages.mjs --version X.Y.Z \ +// --artifacts-dir [--out-dir dist/npm] [--only linux-x64,...] + +import { + chmodSync, + cpSync, + existsSync, + mkdirSync, + mkdtempSync, + readdirSync, + readFileSync, + rmSync, + writeFileSync, +} from 'node:fs'; +import { createHash } from 'node:crypto'; +import { tmpdir } from 'node:os'; +import { dirname, join } from 'node:path'; +import { fileURLToPath } from 'node:url'; +import { spawnSync } from 'node:child_process'; + +const repoRoot = dirname(dirname(dirname(fileURLToPath(import.meta.url)))); +const sourceDir = join(repoRoot, 'npm', 'cli'); + +// Node's platform/arch names (the launcher derives the alias from them) and +// the release target each one is built from. +export const MATRIX = [ + { platform: 'darwin', arch: 'arm64', target: 'aarch64-apple-darwin', ext: 'tar.gz' }, + { platform: 'darwin', arch: 'x64', target: 'x86_64-apple-darwin', ext: 'tar.gz' }, + { platform: 'linux', arch: 'arm64', target: 'aarch64-unknown-linux-musl', ext: 'tar.gz' }, + { platform: 'linux', arch: 'x64', target: 'x86_64-unknown-linux-musl', ext: 'tar.gz' }, + { platform: 'win32', arch: 'x64', target: 'x86_64-pc-windows-msvc', ext: 'zip' }, +]; + +// The programs a payload carries. The daemon is not part of the npm package. +const PROGRAMS = ['twigpine', 'git-remote-twigpine']; + +function fail(message) { + console.error(`[build-platform-packages] ${message}`); + process.exit(1); +} + +function parseArgs(argv) { + const args = { version: '', artifactsDir: '', outDir: join(repoRoot, 'dist', 'npm'), only: null }; + for (let i = 0; i < argv.length; i += 1) { + const arg = argv[i]; + const next = () => { + i += 1; + if (i >= argv.length) fail(`missing value for ${arg}`); + return argv[i]; + }; + switch (arg) { + case '--version': + args.version = next(); + break; + case '--artifacts-dir': + args.artifactsDir = next(); + break; + case '--out-dir': + args.outDir = next(); + break; + case '--only': + args.only = new Set(next().split(',').map((entry) => entry.trim()).filter(Boolean)); + break; + default: + fail(`unknown argument ${arg}`); + } + } + if (!args.artifactsDir) fail('--artifacts-dir is required'); + // A plain release version: it becomes part of every platform version, and a + // prerelease suffix here would be read as part of the platform suffix. + if (!/^(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)$/.test(args.version)) { + fail(`--version must be X.Y.Z, got ${JSON.stringify(args.version)}`); + } + return args; +} + +function readSourceManifest() { + const pkg = JSON.parse(readFileSync(join(sourceDir, 'package.json'), 'utf8')); + if (!pkg.name) fail('npm/cli/package.json has no name'); + return pkg; +} + +export function platformVersion(version, entry) { + return `${version}-${entry.platform}-${entry.arch}`; +} + +export function optionalDependencies(packageName, version) { + const aliases = {}; + for (const entry of MATRIX) { + aliases[`${packageName}-${entry.platform}-${entry.arch}`] = + `npm:${packageName}@${platformVersion(version, entry)}`; + } + return aliases; +} + +// " " or " *", as sha256sum writes it. A line that names +// a file must name this one. +function expectedDigest(text, assetName) { + for (const raw of text.split(/\r?\n/)) { + const match = raw.trim().match(/^([a-fA-F0-9]{64})(?:\s+\*?(.+))?$/); + if (!match) continue; + if (!match[2] || match[2].trim() === assetName) return match[1].toLowerCase(); + } + fail(`no SHA-256 for ${assetName} in its .sha256 file`); +} + +function verifyChecksum(archivePath, assetName) { + const sumPath = `${archivePath}.sha256`; + if (!existsSync(sumPath)) fail(`missing checksum file ${sumPath}`); + const expected = expectedDigest(readFileSync(sumPath, 'utf8'), assetName); + const actual = createHash('sha256').update(readFileSync(archivePath)).digest('hex'); + if (actual !== expected) fail(`checksum mismatch for ${assetName}: expected ${expected}, got ${actual}`); +} + +function extract(archivePath, ext, destDir) { + mkdirSync(destDir, { recursive: true }); + const attempts = + ext === 'zip' + ? [['unzip', ['-q', archivePath, '-d', destDir]], ['tar', ['-xf', archivePath, '-C', destDir]]] + : [['tar', ['-xzf', archivePath, '-C', destDir]]]; + let lastError = ''; + for (const [command, commandArgs] of attempts) { + const result = spawnSync(command, commandArgs, { stdio: ['ignore', 'ignore', 'pipe'] }); + if (result.status === 0) return; + lastError = result.error ? result.error.message : String(result.stderr || '').trim(); + } + fail(`could not extract ${archivePath}: ${lastError}`); +} + +// Release archives hold one directory named after the archive; accept the +// programs at the root too. +function programsDir(extracted, packageDirName, probe) { + for (const dir of [join(extracted, packageDirName), extracted]) { + if (existsSync(join(dir, probe))) return dir; + } + const dirs = readdirSync(extracted, { withFileTypes: true }).filter((e) => e.isDirectory()); + if (dirs.length === 1 && existsSync(join(extracted, dirs[0].name, probe))) { + return join(extracted, dirs[0].name); + } + fail(`${packageDirName} does not contain ${probe}`); +} + +function buildPlatformPackage(entry, source, version, artifactsDir, outDir) { + const base = `twigpine-node-${version}-${entry.target}`; + const assetName = `${base}.${entry.ext}`; + const archivePath = join(artifactsDir, assetName); + if (!existsSync(archivePath)) fail(`missing release archive ${archivePath}`); + verifyChecksum(archivePath, assetName); + + const suffix = entry.platform === 'win32' ? '.exe' : ''; + const temp = mkdtempSync(join(tmpdir(), 'twigpine-npm-')); + try { + extract(archivePath, entry.ext, temp); + const from = programsDir(temp, base, `twigpine${suffix}`); + const packageDir = join(outDir, 'platforms', `cli-${entry.platform}-${entry.arch}`); + rmSync(packageDir, { recursive: true, force: true }); + mkdirSync(packageDir, { recursive: true }); + for (const program of PROGRAMS) { + const file = `${program}${suffix}`; + if (!existsSync(join(from, file))) fail(`${assetName} does not contain ${file}`); + cpSync(join(from, file), join(packageDir, file)); + if (!suffix) chmodSync(join(packageDir, file), 0o755); + } + for (const license of ['LICENSE-MIT', 'LICENSE-APACHE']) { + cpSync(join(repoRoot, license), join(packageDir, license)); + } + const manifest = { + name: source.name, + version: platformVersion(version, entry), + description: `Twigpine programs for ${entry.platform}-${entry.arch} (installed by ${source.name}@${version})`, + os: [entry.platform], + cpu: [entry.arch], + license: source.license, + repository: source.repository, + // No bin, no scripts, no dependencies: the wrapper runs the programs. + }; + writeFileSync(join(packageDir, 'package.json'), JSON.stringify(manifest, null, 2) + '\n'); + return packageDir; + } finally { + rmSync(temp, { recursive: true, force: true }); + } +} + +function buildWrapper(source, version, outDir) { + const wrapperDir = join(outDir, 'wrapper'); + rmSync(wrapperDir, { recursive: true, force: true }); + mkdirSync(join(wrapperDir, 'bin'), { recursive: true }); + for (const file of readdirSync(join(sourceDir, 'bin'))) { + if (!file.endsWith('.js')) continue; + cpSync(join(sourceDir, 'bin', file), join(wrapperDir, 'bin', file)); + chmodSync(join(wrapperDir, 'bin', file), 0o755); + } + for (const file of ['README.md']) { + cpSync(join(sourceDir, file), join(wrapperDir, file)); + } + for (const license of ['LICENSE-MIT', 'LICENSE-APACHE']) { + cpSync(join(repoRoot, license), join(wrapperDir, license)); + } + const pkg = { ...source }; + // No lifecycle scripts and no regular dependencies, ever: nothing for a + // package manager to run, block or warn about. + delete pkg.scripts; + delete pkg.dependencies; + pkg.version = version; + pkg.optionalDependencies = optionalDependencies(source.name, version); + writeFileSync(join(wrapperDir, 'package.json'), JSON.stringify(pkg, null, 2) + '\n'); + return wrapperDir; +} + +function main() { + const args = parseArgs(process.argv.slice(2)); + const source = readSourceManifest(); + const selected = MATRIX.filter((e) => !args.only || args.only.has(`${e.platform}-${e.arch}`)); + if (args.only) { + for (const key of args.only) { + if (!MATRIX.some((e) => `${e.platform}-${e.arch}` === key)) fail(`--only lists unknown platform ${key}`); + } + } + if (selected.length === 0) fail('--only matched no platforms'); + const platformDirs = selected.map((e) => buildPlatformPackage(e, source, args.version, args.artifactsDir, args.outDir)); + const wrapperDir = buildWrapper(source, args.version, args.outDir); + process.stdout.write( + JSON.stringify({ name: source.name, version: args.version, wrapperDir, platformDirs }, null, 2) + '\n', + ); +} + +if (process.argv[1] && fileURLToPath(import.meta.url) === process.argv[1]) { + main(); +} diff --git a/scripts/npm/publish-packages.sh b/scripts/npm/publish-packages.sh new file mode 100755 index 00000000..76e6ec32 --- /dev/null +++ b/scripts/npm/publish-packages.sh @@ -0,0 +1,195 @@ +#!/usr/bin/env bash +# publish-packages.sh: publish the npm payloads build-platform-packages.mjs +# assembled under (wrapper/ plus platforms/*/), in the order and +# with the dist-tags docs/NPM_PACKAGING.md requires: +# +# 1. every platform payload under the `platform` dist-tag, never `latest`; +# 2. wait until npm shows every platform version, then check `latest` was +# not moved onto one; +# 3. the wrapper: under `latest` when it is the newest release, otherwise +# (a backfill of an older version) under `backfill`, so `latest` never +# moves backward; then wait until npm shows it. +# +# The waits exist because npm applies a publish asynchronously: `npm publish` +# returns once the upload is accepted, and the version can appear minutes +# later. Ordering the uploads alone would still let the wrapper appear first, +# with aliases that 404. +# +# A version already on the registry is skipped, so re-running a job that failed +# halfway finishes the release instead of failing on what went out. +# +# Usage: scripts/npm/publish-packages.sh +# +# Auth comes from the environment (npm trusted publishing in CI). +# TWIGPINE_NPM_PUBLISH_RETRY_DELAY sets the seconds between registry lookups +# (default 5), TWIGPINE_NPM_PUBLISH_WAIT_TIMEOUT the seconds to wait for npm to +# apply the publishes (default 1800), and TWIGPINE_NPM_PUBLISH_FLAGS extra +# flags for every `npm publish` (CI passes --provenance). +set -euo pipefail + +fail() { + echo "::error::$*" >&2 + exit 1 +} + +npm_dir="${1:-}" +[ -n "$npm_dir" ] || fail "usage: publish-packages.sh " +npm_dir="${npm_dir%/}" +wrapper_dir="${npm_dir}/wrapper" +[ -f "${wrapper_dir}/package.json" ] || fail "${wrapper_dir}/package.json not found; run build-platform-packages.mjs first" + +manifest_field() { + node -e 'const v = JSON.parse(require("fs").readFileSync(process.argv[1], "utf8"))[process.argv[2]]; process.stdout.write(v === undefined ? "" : String(v));' "$1/package.json" "$2" +} + +name="$(manifest_field "$wrapper_dir" name)" +version="$(manifest_field "$wrapper_dir" version)" +[ -n "$name" ] && [ -n "$version" ] || fail "${wrapper_dir}/package.json has no name or version" + +# whole_seconds : a decimal number of seconds, checked +# before anything is published (Bash reads a leading zero as octal). +whole_seconds() { + case "$2" in + '' | *[!0-9]*) fail "$1 must be a whole number of seconds, got '$2'" ;; + esac + [ "${#2}" -le 18 ] || fail "$1 must be at most $3 seconds, got '$2'" + local seconds=$((10#$2)) + [ "$seconds" -le "$3" ] || fail "$1 must be at most $3 seconds, got '$2'" + echo "$seconds" +} + +delay="$(whole_seconds TWIGPINE_NPM_PUBLISH_RETRY_DELAY "${TWIGPINE_NPM_PUBLISH_RETRY_DELAY:-5}" 600)" +wait_timeout="$(whole_seconds TWIGPINE_NPM_PUBLISH_WAIT_TIMEOUT "${TWIGPINE_NPM_PUBLISH_WAIT_TIMEOUT:-1800}" 86400)" +read -r -a extra_flags <<< "${TWIGPINE_NPM_PUBLISH_FLAGS:-}" + +# The wrapper pins every platform version exactly; a wrapper whose aliases do +# not match the payloads beside it would install aliases that 404. +shopt -s nullglob +platform_dirs=("${npm_dir}"/platforms/*/) +shopt -u nullglob +[ "${#platform_dirs[@]}" -gt 0 ] || fail "no platform payloads under ${npm_dir}/platforms" +expected_platforms="$(node -e 'const p = JSON.parse(require("fs").readFileSync(process.argv[1], "utf8")); process.stdout.write(Object.values(p.optionalDependencies || {}).sort().join("\n"));' "${wrapper_dir}/package.json")" +actual_platforms="" +for dir in "${platform_dirs[@]}"; do + dir="${dir%/}" + platform_name="$(manifest_field "$dir" name)" + platform_version="$(manifest_field "$dir" version)" + [ "$platform_name" = "$name" ] || fail "${dir} is ${platform_name}, not ${name}" + case "$platform_version" in + "${version}-"*) ;; + *) fail "${dir} version ${platform_version} is not a platform version of ${name}@${version}" ;; + esac + actual_platforms="${actual_platforms}npm:${name}@${platform_version}"$'\n' +done +actual_platforms="$(printf '%s' "$actual_platforms" | sort)" +if [ "$actual_platforms" != "$expected_platforms" ]; then + fail "the wrapper's optionalDependencies do not match the payloads under ${npm_dir}/platforms (wrapper: $(echo "$expected_platforms" | tr '\n' ' '); payloads: $(echo "$actual_platforms" | tr '\n' ' '))" +fi + +# Only npm's E404 means "not published". Any other lookup failure is retried +# and then stops the job: guessing would republish a version already out. +already_published() { + local output status errors + errors="$(mktemp)" + for _ in 1 2 3; do + status=0 + output="$(npm view "${name}@$1" version 2>"$errors")" || status=$? + if [ "$status" -eq 0 ]; then + rm -f "$errors" + [ "$output" = "$1" ] + return + fi + if grep -q 'E404' "$errors"; then + rm -f "$errors" + return 1 + fi + sleep "$delay" + done + cat "$errors" >&2 + rm -f "$errors" + fail "could not look up ${name}@$1 on the registry, so cannot tell whether it is already published" +} + +# publish [npm publish flags...] +publish() { + local dir="$1" package_version="$2" + shift 2 + if already_published "$package_version"; then + echo "${name}@${package_version} is already on the registry; not publishing it again." + return 0 + fi + echo "publishing ${name}@${package_version} from ${dir}" + (cd "$dir" && npm publish --access public --ignore-scripts ${extra_flags[@]+"${extra_flags[@]}"} "$@") +} + +# While npm has not applied a publish, an error and an E404 both mean "not +# yet"; the deadline bounds the wait. +version_visible() { + [ "$(npm view "${name}@$1" version 2>/dev/null || true)" = "$1" ] +} + +tag_is() { + [ "$(npm view "$name" "dist-tags.$1" 2>/dev/null || true)" = "$2" ] +} + +deadline=$((SECONDS + wait_timeout)) + +# wait_until +wait_until() { + local what="$1" + shift + until "$@"; do + if [ "$SECONDS" -ge "$deadline" ]; then + fail "npm accepted the upload but still does not show ${what} after ${wait_timeout}s. Re-run once it does: versions already on the registry are skipped." + fi + sleep "$delay" + done +} + +# The wrapper's dist-tag, decided before anything is published. `latest` +# unless the registry already has a newer release. A failed lookup other +# than E404 (no package yet) stops the job rather than guess. +latest_errors="$(mktemp)" +trap 'rm -f -- "$latest_errors"' EXIT +latest_before="" +if ! latest_before="$(npm view "$name" dist-tags.latest 2>"$latest_errors")"; then + grep -q 'E404' "$latest_errors" || { cat "$latest_errors" >&2; fail "could not read ${name}'s latest dist-tag; not guessing the wrapper's dist-tag"; } + latest_before="" +fi +wrapper_tag="latest" +case "$latest_before" in + '' | "${version}" | "${version}-"*) ;; + *) + newest="$(printf '%s\n%s\n' "$latest_before" "$version" | sort -V | tail -n 1)" + if [ "$newest" != "$version" ]; then + wrapper_tag="backfill" + echo "::notice::${version} is older than ${name}@${latest_before}; publishing the wrapper under dist-tag backfill" + fi + ;; +esac + +for dir in "${platform_dirs[@]}"; do + dir="${dir%/}" + publish "$dir" "$(manifest_field "$dir" version)" --tag platform +done + +for dir in "${platform_dirs[@]}"; do + platform_version="$(manifest_field "${dir%/}" version)" + wait_until "${name}@${platform_version}" version_visible "$platform_version" + echo "${name}@${platform_version} is on the registry." +done + +# Platform versions are semver prereleases of the wrapper version; one that +# took `latest` would make a plain install deliver a payload with no commands. +latest="$(npm view "$name" dist-tags.latest 2>/dev/null || true)" +case "$latest" in + "${version}-"*) + fail "dist-tag latest points at platform version ${latest}; a platform publish moved it. Fix the dist-tag before publishing the wrapper." + ;; +esac +echo "dist-tag latest is ${latest:-unset} (unchanged by the platform publishes)." + +publish "$wrapper_dir" "$version" --tag "$wrapper_tag" + +wait_until "${name}@${version} as dist-tag ${wrapper_tag}" tag_is "$wrapper_tag" "$version" +echo "dist-tag ${wrapper_tag} is ${version}." diff --git a/scripts/npm/test-npm-packages.sh b/scripts/npm/test-npm-packages.sh new file mode 100755 index 00000000..e44a4435 --- /dev/null +++ b/scripts/npm/test-npm-packages.sh @@ -0,0 +1,255 @@ +#!/usr/bin/env bash +# Tests the @twigpine/cli npm packaging with no network and no registry: +# +# - build-platform-packages.mjs against fake release archives: manifests, +# aliases, programs, checksum refusal; +# - the command launchers against an installed layout: arguments, exit +# status, stdin/stdout, signals, a missing payload, and (when a built CLI +# is passed as $1) the real program; +# - publish-packages.sh against a fake `npm` that keeps a registry in a +# directory: order, dist-tags, re-runs, backfills, refusals. +# +# Usage: scripts/npm/test-npm-packages.sh [path/to/built/twigpine] +set -euo pipefail + +ROOT="$(cd "$(dirname "$0")/../.." && pwd)" +BUILD="$ROOT/scripts/npm/build-platform-packages.mjs" +PUBLISH="$ROOT/scripts/npm/publish-packages.sh" +REAL_CLI="${1:-}" +VERSION=9.9.0 +TARGETS="aarch64-apple-darwin x86_64-apple-darwin aarch64-unknown-linux-musl x86_64-unknown-linux-musl" + +W="$(mktemp -d)" +trap 'rm -rf "${W:?}"' EXIT + +PASS=0 +FAIL=0 +ok() { PASS=$((PASS + 1)); echo " ok $1"; } +bad() { FAIL=$((FAIL + 1)); echo " FAIL $1"; if [ -f "$W/out" ]; then sed 's/^/ | /' "$W/out" | tail -n 12; fi; } +check() { if "${@:2}"; then ok "$1"; else bad "$1"; fi; } + +sha() { + if command -v sha256sum >/dev/null 2>&1; then sha256sum "$1" | awk '{print $1}' + else shasum -a 256 "$1" | awk '{print $1}'; fi +} +field() { node -e 'const p=JSON.parse(require("fs").readFileSync(process.argv[1],"utf8")); const v=process.argv[2].split(".").reduce((o,k)=>o==null?o:o[k],p); process.stdout.write(v===undefined?"":typeof v==="object"?JSON.stringify(v):String(v));' "$1" "$2"; } + +# ── fake release archives ────────────────────────────────────────────────── +# A fake program prints its name and arguments, echoes stdin, and exits with +# FAKE_EXIT; FAKE_SIGNAL makes it kill itself with that signal. +fake_program() { + cat > "$1" < "$pkg.tar.gz.sha256") +done +pkg="twigpine-node-$VERSION-x86_64-pc-windows-msvc" +python3 - "$ART/$pkg.zip" "$pkg" <<'PY' +import sys, zipfile +path, pkg = sys.argv[1:] +with zipfile.ZipFile(path, "w") as z: + for b in ("twigpine", "git-remote-twigpine", "twigpine-node"): + z.writestr(f"{pkg}/{b}.exe", f"{b} exe\n") +PY +(cd "$ART" && printf '%s *%s\n' "$(sha "$pkg.zip")" "$pkg.zip" > "$pkg.zip.sha256") + +# ── build ────────────────────────────────────────────────────────────────── +echo "build-platform-packages.mjs" +OUT="$W/npm" +if node "$BUILD" --version "$VERSION" --artifacts-dir "$ART" --out-dir "$OUT" > "$W/out" 2>&1; then + ok "builds the wrapper and five payloads" +else + bad "builds the wrapper and five payloads" +fi +WP="$OUT/wrapper/package.json" +check "the wrapper is @twigpine/cli at the release version" \ + test "$(field "$WP" name)@$(field "$WP" version)" = "@twigpine/cli@$VERSION" +check "the wrapper has no scripts and no dependencies" \ + test "$(field "$WP" scripts)$(field "$WP" dependencies)" = "" +check "the wrapper pins all five payloads exactly" test "$(field "$WP" optionalDependencies)" = \ + '{"@twigpine/cli-darwin-arm64":"npm:@twigpine/cli@9.9.0-darwin-arm64","@twigpine/cli-darwin-x64":"npm:@twigpine/cli@9.9.0-darwin-x64","@twigpine/cli-linux-arm64":"npm:@twigpine/cli@9.9.0-linux-arm64","@twigpine/cli-linux-x64":"npm:@twigpine/cli@9.9.0-linux-x64","@twigpine/cli-win32-x64":"npm:@twigpine/cli@9.9.0-win32-x64"}' +check "the wrapper provides every command name" test "$(field "$WP" bin)" = \ + '{"twigpine":"bin/twigpine.js","twig":"bin/twigpine.js","gl":"bin/gl.js","git-remote-twigpine":"bin/git-remote-twigpine.js","git-remote-gitlawb":"bin/git-remote-twigpine.js"}' +check "the wrapper ships its launchers, executable" \ + test -x "$OUT/wrapper/bin/twigpine.js" -a -x "$OUT/wrapper/bin/gl.js" -a -x "$OUT/wrapper/bin/git-remote-twigpine.js" -a -f "$OUT/wrapper/bin/launch.js" +LP="$OUT/platforms/cli-linux-x64/package.json" +check "a payload is a platform version, limited to its platform" \ + test "$(field "$LP" name)@$(field "$LP" version) $(field "$LP" os) $(field "$LP" cpu)" = '@twigpine/cli@9.9.0-linux-x64 ["linux"] ["x64"]' +check "a payload has no bin, scripts or dependencies" \ + test "$(field "$LP" bin)$(field "$LP" scripts)$(field "$LP" dependencies)" = "" +check "a payload carries the CLI and the helper, executable" \ + test -x "$OUT/platforms/cli-linux-x64/twigpine" -a -x "$OUT/platforms/cli-linux-x64/git-remote-twigpine" +check "a payload leaves the daemon out" test ! -e "$OUT/platforms/cli-linux-x64/twigpine-node" +check "the Windows payload carries .exe programs" \ + test -f "$OUT/platforms/cli-win32-x64/twigpine.exe" -a -f "$OUT/platforms/cli-win32-x64/git-remote-twigpine.exe" + +cp -R "$ART" "$W/art-bad" +printf 'tampered' >> "$W/art-bad/twigpine-node-$VERSION-x86_64-unknown-linux-musl.tar.gz" +if node "$BUILD" --version "$VERSION" --artifacts-dir "$W/art-bad" --out-dir "$W/npm-bad" > "$W/out" 2>&1; then + bad "refuses an archive that does not match its checksum" +elif grep -q "checksum mismatch" "$W/out"; then ok "refuses an archive that does not match its checksum" +else bad "refuses an archive that does not match its checksum"; fi +rm "$W/art-bad/twigpine-node-$VERSION-x86_64-apple-darwin.tar.gz" +if node "$BUILD" --version "$VERSION" --artifacts-dir "$W/art-bad" --out-dir "$W/npm-bad" > "$W/out" 2>&1; then + bad "refuses a missing archive" +else ok "refuses a missing archive"; fi +for v in 9.9 v9.9.0 9.9.0-rc.1 09.9.0; do + if node "$BUILD" --version "$v" --artifacts-dir "$ART" --out-dir "$W/npm-bad" > "$W/out" 2>&1; then + bad "refuses version $v" + else ok "refuses version $v"; fi +done + +# ── launchers ────────────────────────────────────────────────────────────── +echo "launchers" +HOST_PLATFORM="$(node -p 'process.platform')" +HOST_ARCH="$(node -p 'process.arch')" +INSTALL="$W/install/@twigpine/cli" +mkdir -p "$INSTALL" +cp -R "$OUT/wrapper/." "$INSTALL/" +PAYLOAD="$INSTALL/node_modules/@twigpine/cli-$HOST_PLATFORM-$HOST_ARCH" +mkdir -p "$PAYLOAD" +fake_program "$PAYLOAD/twigpine" twigpine +fake_program "$PAYLOAD/git-remote-twigpine" git-remote-twigpine +echo '{"name":"@twigpine/cli","version":"9.9.0-host"}' > "$PAYLOAD/package.json" + +out="$(node "$INSTALL/bin/twigpine.js" repo list --json 2>&1)" +check "twigpine runs the CLI with its arguments" test "$out" = "twigpine repo list --json" +out="$(node "$INSTALL/bin/gl.js" doctor 2>&1)" +check "gl runs the same CLI" test "$out" = "twigpine doctor" +status=0 +FAKE_EXIT=7 node "$INSTALL/bin/twigpine.js" x > /dev/null 2>&1 || status=$? +check "the program's exit status comes through" test "$status" = 7 +out="$(printf 'capabilities\n\n' | FAKE_STDIN=1 node "$INSTALL/bin/git-remote-twigpine.js" origin twigpine://did:key:z/r 2>&1)" +check "the helper gets git's stdin and answers on stdout" \ + test "$out" = "$(printf 'git-remote-twigpine origin twigpine://did:key:z/r\ncapabilities')" +# Through `sh -c ...; echo $?`, so the signal is observed as a status and the +# test's own shell does not report a killed job. +status="$(sh -c 'FAKE_SIGNAL=TERM node "$1" > /dev/null 2>&1; echo $?' _ "$INSTALL/bin/twigpine.js" 2>/dev/null)" +check "a program killed by a signal ends the launcher the same way" test "$status" = 143 + +rm -rf "$PAYLOAD" +status=0 +node "$INSTALL/bin/twigpine.js" > "$W/out" 2>&1 || status=$? +if [ "$status" = 1 ] && grep -q "npm install -g @twigpine/cli" "$W/out" && grep -q "install.sh" "$W/out"; then + ok "a missing payload says how to fix it" +else bad "a missing payload says how to fix it"; fi + +if [ -n "$REAL_CLI" ]; then + mkdir -p "$PAYLOAD" + cp "$REAL_CLI" "$PAYLOAD/twigpine" + echo '{"name":"@twigpine/cli","version":"9.9.0-host"}' > "$PAYLOAD/package.json" + out="$(node "$INSTALL/bin/twigpine.js" --version 2>&1)" + case "$out" in + twigpine\ *) ok "runs the real CLI ($out)" ;; + *) echo "$out" > "$W/out"; bad "runs the real CLI" ;; + esac +fi + +# ── publish ──────────────────────────────────────────────────────────────── +echo "publish-packages.sh" +FAKEBIN="$W/fakebin" +mkdir -p "$FAKEBIN" +# A registry in $REG: versions/ per published version, tags/. +# `npm view` of anything unknown answers E404; FAKE_VIEW_BROKEN makes every +# lookup fail some other way. Every call is logged. +cat > "$FAKEBIN/npm" <<'EOF' +#!/usr/bin/env bash +echo "npm $*" >> "$REG/log" +case "$1" in + view) + [ -z "${FAKE_VIEW_BROKEN:-}" ] || { echo "npm error code ECONNRESET" >&2; exit 1; } + spec="$2"; what="$3" + if [ "$what" = version ]; then + v="${spec##*@}" + [ -f "$REG/versions/$v" ] && { echo "$v"; exit 0; } + echo "npm error code E404" >&2; exit 1 + fi + tag="${what#dist-tags.}" + [ -d "$REG/versions" ] && [ -n "$(ls -A "$REG/versions" 2>/dev/null)" ] || { echo "npm error code E404" >&2; exit 1; } + [ -f "$REG/tags/$tag" ] && cat "$REG/tags/$tag" + exit 0 + ;; + publish) + v="$(node -p 'require("./package.json").version')" + tag=latest + while [ $# -gt 0 ]; do [ "$1" = --tag ] && tag="$2"; shift; done + mkdir -p "$REG/versions" "$REG/tags" + touch "$REG/versions/$v" + echo "$v" > "$REG/tags/$tag" + echo "published $v $tag" >> "$REG/log" + ;; + *) echo "unexpected npm $*" >&2; exit 2 ;; +esac +EOF +chmod 755 "$FAKEBIN/npm" + +run_publish() { + rm -f "$REG/log" + PATH="$FAKEBIN:$PATH" REG="$REG" TWIGPINE_NPM_PUBLISH_RETRY_DELAY=0 TWIGPINE_NPM_PUBLISH_WAIT_TIMEOUT=5 \ + bash "$PUBLISH" "$1" > "$W/out" 2>&1 +} +published_order() { grep '^published' "$REG/log" | awk '{print $2, $3}'; } + +REG="$W/reg1"; mkdir -p "$REG" +if run_publish "$OUT"; then ok "publishes a first release"; else bad "publishes a first release"; fi +check "platform payloads go first under the platform tag, the wrapper last under latest" \ + test "$(published_order | tr '\n' ';')" = "9.9.0-darwin-arm64 platform;9.9.0-darwin-x64 platform;9.9.0-linux-arm64 platform;9.9.0-linux-x64 platform;9.9.0-win32-x64 platform;9.9.0 latest;" +check "latest ends on the release" test "$(cat "$REG/tags/latest")" = "$VERSION" +check "every publish skips install scripts" \ + test "$(grep -c '^npm publish' "$REG/log")" = "$(grep '^npm publish' "$REG/log" | grep -c -- '--ignore-scripts')" + +if run_publish "$OUT" && [ -z "$(published_order)" ] && grep -q "already on the registry" "$W/out"; then + ok "a re-run publishes nothing twice" +else bad "a re-run publishes nothing twice"; fi + +REG="$W/reg2"; mkdir -p "$REG/versions" "$REG/tags" +touch "$REG/versions/9.9.0-darwin-arm64" "$REG/versions/9.9.0-darwin-x64" +echo "9.8.0" > "$REG/tags/latest"; touch "$REG/versions/9.8.0" +if run_publish "$OUT" && test "$(published_order | tr '\n' ';')" = "9.9.0-linux-arm64 platform;9.9.0-linux-x64 platform;9.9.0-win32-x64 platform;9.9.0 latest;"; then + ok "a re-run after a partial publish finishes the rest" +else bad "a re-run after a partial publish finishes the rest"; fi + +REG="$W/reg3"; mkdir -p "$REG/versions" "$REG/tags" +touch "$REG/versions/9.10.0"; echo "9.10.0" > "$REG/tags/latest" +if run_publish "$OUT" && grep -q '^published 9.9.0 backfill$' "$REG/log" && [ "$(cat "$REG/tags/latest")" = 9.10.0 ]; then + ok "a backfill of an older release leaves latest where it is" +else bad "a backfill of an older release leaves latest where it is"; fi + +REG="$W/reg4"; mkdir -p "$REG" +cp -R "$OUT" "$W/npm-short"; rm -rf "$W/npm-short/platforms/cli-win32-x64" +if run_publish "$W/npm-short"; then bad "refuses payloads that do not match the wrapper" +elif [ ! -f "$REG/log" ] || ! grep -q '^published' "$REG/log"; then ok "refuses payloads that do not match the wrapper, before publishing" +else bad "refuses payloads that do not match the wrapper"; fi + +REG="$W/reg5"; mkdir -p "$REG" +rm -f "$REG/log" +if PATH="$FAKEBIN:$PATH" REG="$REG" FAKE_VIEW_BROKEN=1 TWIGPINE_NPM_PUBLISH_RETRY_DELAY=0 \ + bash "$PUBLISH" "$OUT" > "$W/out" 2>&1; then + bad "stops when the registry cannot be read" +elif ! grep -q '^published' "$REG/log" 2>/dev/null; then ok "stops when the registry cannot be read, before publishing" +else bad "stops when the registry cannot be read"; fi + +for bad_value in 601 -1 abc; do + REG="$W/reg6"; rm -rf "$REG"; mkdir -p "$REG" + if PATH="$FAKEBIN:$PATH" REG="$REG" TWIGPINE_NPM_PUBLISH_RETRY_DELAY="$bad_value" \ + bash "$PUBLISH" "$OUT" > "$W/out" 2>&1; then + bad "refuses retry delay $bad_value" + else ok "refuses retry delay $bad_value"; fi +done + +echo "$PASS passed, $FAIL failed" +[ "$FAIL" -eq 0 ] diff --git a/scripts/test-resolve-release-tag.sh b/scripts/test-resolve-release-tag.sh index d2b95096..3671008e 100755 --- a/scripts/test-resolve-release-tag.sh +++ b/scripts/test-resolve-release-tag.sh @@ -605,6 +605,23 @@ job=npm-publish npm publish "npm/packages/$pkg" --provenance --access public --tag "$dist_tag" done +job=npm-publish-cli + - name: Check out workflow scripts + uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 + with: + persist-credentials: false + +job=npm-publish-cli + - name: Resolve release tag + id: rel + env: + DISPATCH_TAG: ${{ inputs.npm_backfill_tag }} + RELEASE_TAG: ${{ needs.release-please.outputs.tag_name }} + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} + run: | + set -euo pipefail + scripts/resolve-release-tag.sh "${DISPATCH_TAG:-$RELEASE_TAG}" + job=homebrew-bump - name: Check out workflow scripts if: ${{ steps.guard.outputs.enabled == 'true' }} @@ -892,6 +909,7 @@ docker=release docker-manifest=release release-binaries=release npm-publish=release +npm-publish-cli=npm-publish homebrew-bump=release web-sync=release EOF From 39b528b79a5aef26499422d9c6f0448a60de6bfe Mon Sep 17 00:00:00 2001 From: Kevin Codex Date: Wed, 18 Nov 2026 13:46:17 +0800 Subject: [PATCH 2/8] fix(npm): review follow-ups for @twigpine/cli - Windows on ARM: the win32 payload lists arm64 as well as x64; npm installs a payload only when its cpu matches, so the launcher's x64 fallback found nothing. - The release builds Windows best-effort. A release without the Windows archive is now packaged without it (--allow-missing win32-x64): the wrapper leaves win32 out of its aliases and os, instead of the whole npm publish failing. - Launchers forward SIGINT, SIGTERM and SIGHUP sent to them alone, so a supervisor or MCP client stopping one by pid stops the program too, and end with 128+signal where Node cannot re-raise the program's signal (SIGPIPE). - publish-packages.sh: a wrapper already on the registry is not waited on under a tag it never had, so re-running a backfill finishes at once instead of timing out. - The wrapper tarball carries the licence files; payloads set preferUnplugged for Yarn PnP; unzip warnings are not extraction failures; the build script runs when called through a symlinked path. - Docs: setup order (environment first), placeholder contents, the npm trust command, npx usage, Windows being optional, and that git needs node on PATH for the npm helper. - Tests for each, checked against the previous launcher. --- .github/workflows/release.yml | 13 +++- docs/NPM_PACKAGING.md | 54 +++++++++++------ npm/README.md | 51 ++++++++-------- npm/cli/README.md | 8 ++- npm/cli/bin/launch.js | 81 ++++++++++++++++--------- npm/cli/package.json | 3 +- scripts/npm/build-platform-packages.mjs | 77 +++++++++++++++++------ scripts/npm/publish-packages.sh | 13 ++-- scripts/npm/test-npm-packages.sh | 61 ++++++++++++++++++- 9 files changed, 257 insertions(+), 104 deletions(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 09bffce4..3a3a1c72 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -862,18 +862,26 @@ jobs: run: | set -euo pipefail mkdir -p _art + windows="twigpine-node-${VERSION}-x86_64-pc-windows-msvc.zip" for archive in \ "twigpine-node-${VERSION}-aarch64-apple-darwin.tar.gz" \ "twigpine-node-${VERSION}-x86_64-apple-darwin.tar.gz" \ "twigpine-node-${VERSION}-aarch64-unknown-linux-musl.tar.gz" \ "twigpine-node-${VERSION}-x86_64-unknown-linux-musl.tar.gz" \ - "twigpine-node-${VERSION}-x86_64-pc-windows-msvc.zip"; do + "$windows"; do for file in "$archive" "$archive.sha256"; do # By the asset id the resolver captured and uploader-checked, not # by name: release assets are mutable, an id is not. want="$(printf '%s' "$file" | base64 -w0)" asset_id="$(printf '%s\n' "$ASSETS" | awk -v n="$want" '$1 == n {print $2; exit}')" if [ -z "$asset_id" ]; then + # Windows is built best-effort (release-binaries); without it + # the package is published for the other platforms. + if [ "$archive" = "$windows" ]; then + echo "::warning::release $TAG has no $file; publishing without Windows" + rm -f "_art/$windows" "_art/$windows.sha256" + continue 2 + fi echo "::error::release $TAG has no asset $file captured at resolve time" exit 1 fi @@ -888,7 +896,8 @@ jobs: run: | set -euo pipefail node scripts/npm/build-platform-packages.mjs \ - --version "$VERSION" --artifacts-dir _art --out-dir _npm + --version "$VERSION" --artifacts-dir _art --out-dir _npm \ + --allow-missing win32-x64 - name: Publish @twigpine/cli env: diff --git a/docs/NPM_PACKAGING.md b/docs/NPM_PACKAGING.md index c1cb3bce..da83ce4f 100644 --- a/docs/NPM_PACKAGING.md +++ b/docs/NPM_PACKAGING.md @@ -36,20 +36,34 @@ and without a fallback downloader. and `cpu`, so npm installs exactly one. - A payload holds `twigpine` and `git-remote-twigpine` (`.exe` on Windows), copied from that platform's release archive after its checksum is checked, - plus the licence files. It has no `bin`, no scripts and no dependencies. + plus the licence files. It has no `bin`, no scripts and no dependencies, + and sets `preferUnplugged` so Yarn's Plug'n'Play does not keep it zipped. The node daemon is not on npm. +- The release builds Windows best-effort. A release without the Windows + archive is published without Windows: the wrapper then leaves `win32` out + of its aliases and its `os`, so npm on Windows refuses the install instead + of installing commands with no program. - The wrapper holds the launchers in `npm/cli/bin/` and a manifest with no scripts and no dependencies, only the exact-version `optionalDependencies`. `scripts/npm/build-platform-packages.mjs` writes both from the release archives. - A launcher finds its payload from `process.platform`/`process.arch` and runs - the program with stdio, the exit status and a terminating signal passed - straight through, so git can drive the helper through it. `gl` starts the - program under the name `gl`, which prints a note about the new name on a - terminal. Windows on ARM runs the x64 payload. -- There is no downloader. If the payload is missing (`--omit=optional`, an - unsupported platform), the launcher says how to reinstall or install - without npm. + the program with stdio and the exit status passed straight through, so git + can drive the helper through it (checked with an 80 MB clone). SIGINT, + SIGTERM and SIGHUP sent to the launcher alone are forwarded to the program, + and a program ended by a signal ends the launcher the same way (SIGPIPE, + which Node ignores, as exit status 141). `gl` starts the program under the + name `gl`, which prints a note about the new name on a terminal. +- Windows on ARM installs the x64 payload, whose `cpu` lists both, and runs it + under emulation. +- The git helper is a Node script on PATH, so git needs `node` on its PATH as + well. A GUI git client started with a minimal PATH may not find it; the + installers (`install.sh`, `install.ps1`, Homebrew) put the native helper on + PATH instead. +- There is no downloader. If the payload is missing (`--omit=optional`, a + release without that platform's build), the launcher says how to reinstall + or install without npm. +- `npx` needs the command named: `npx -p @twigpine/cli twigpine doctor`. ## Publishing rules @@ -76,17 +90,22 @@ and without a fallback downloader. ## Before the first publish -Done once, by a maintainer: +Done once, by a maintainer, in this order: -1. Publish a placeholder `@twigpine/cli@0.0.0` by hand; npm only lets a - trusted publisher be configured on a package that exists. -2. On npmjs.com, add a trusted publisher to `@twigpine/cli`: repository - `Twigpine/node`, workflow `release.yml`, environment `npm-publish`. -3. In the GitHub repository settings, create the `npm-publish` environment, - limited to the `main` branch, with a required reviewer. +1. In the GitHub repository settings, create the `npm-publish` environment, + limited to the `main` branch, with a required reviewer. First: GitHub + creates an environment a job names on first use, with no protection. +2. Publish a placeholder `@twigpine/cli@0.0.0` by hand with + `npm publish --access public`; npm only lets a trusted publisher be set on + a package that exists. The placeholder is a manifest and a README, with no + `bin`, so it cannot install commands that do nothing. +3. Add the trusted publisher with publish permission: on npmjs.com under the + package's settings, or + `npm trust github @twigpine/cli --file release.yml --repo Twigpine/node --env npm-publish --allow-publish`. Until then the `npm-publish-cli` job fails on its own; the rest of the release -is unaffected. +is unaffected. The `npm-publish` job publishing `@gitlawb/gl` needs that +package's trusted publishers to name `Twigpine/node` the same way. ## The move from `@gitlawb/gl` @@ -107,6 +126,7 @@ To reach existing users: - A CLI installed from `@gitlawb/gl` prints the two commands above on a terminal. - After that release, a maintainer deprecates every `@gitlawb/gl*` version - with a pointer to `@twigpine/cli`, and the `npm-publish` job and + with a pointer to `@twigpine/cli`, the install line in the README and the + website moves to `@twigpine/cli`, and the `npm-publish` job and `npm/packages/` are removed. The old versions stay on the registry: existing installs resolve their platform packages through them. diff --git a/npm/README.md b/npm/README.md index 32217aac..4ac5150b 100644 --- a/npm/README.md +++ b/npm/README.md @@ -1,37 +1,34 @@ # npm distribution for the Twigpine CLI -This directory packages the `gl` and `git-remote-gitlawb` binaries as the -[`@gitlawb/gl`](https://www.npmjs.com/package/@gitlawb/gl) npm package. - -It uses the **optionalDependencies platform-package** pattern (the same one -esbuild/swc/turbo use): the wrapper `@gitlawb/gl` declares four per-platform -packages as `optionalDependencies`, each gated by `os`/`cpu`. npm installs only -the one matching the host, and the wrapper's `postinstall` (`install.js`) copies -the binary out of it into `bin/`. No binaries are downloaded at install time. +Two packages are built from this directory. The full description, publishing +rules and one-time setup are in [`docs/NPM_PACKAGING.md`](../docs/NPM_PACKAGING.md). ```text -packages/ - gl/ @gitlawb/gl wrapper (postinstall, bin shims) - gl-darwin-arm64/ @gitlawb/gl-darwin-arm64 aarch64-apple-darwin - gl-darwin-x64/ @gitlawb/gl-darwin-x64 x86_64-apple-darwin - gl-linux-arm64/ @gitlawb/gl-linux-arm64 aarch64-unknown-linux-musl - gl-linux-x64/ @gitlawb/gl-linux-x64 x86_64-unknown-linux-musl +cli/ @twigpine/cli the package: launchers + manifest; the + release job adds one payload per platform +packages/ @gitlawb/gl the old package, published once more as a + bridge to @twigpine/cli, then removed ``` -## Publishing +## `@twigpine/cli` -Publishing is **automated** by `.github/workflows/release.yml` (the `npm-publish` -job) on every release-please release. That job: +One package, the layout Zero and esbuild use: a wrapper whose launchers run the +program from a per-platform payload, published as versions of the same +package (`X.Y.Z--`) and pinned through `npm:` aliases. No +install script. macOS and Linux (arm64, x64) and Windows (x64, also used on +Windows on ARM). -1. Downloads the four unix release tarballs from the `Twigpine/node` GitHub release. -2. Lays `gl` + `git-remote-gitlawb` into each platform package. -3. Rewrites every `package.json` `version` (and the wrapper's - `optionalDependencies` pins) to the release version. -4. Publishes the four platform packages first, then the wrapper, with - `npm publish --provenance --access public`. +`scripts/npm/build-platform-packages.mjs` builds the payloads and wrapper from +the release archives, `scripts/npm/publish-packages.sh` publishes them, and +`scripts/npm/test-npm-packages.sh` tests both and the launchers. The +`npm-publish-cli` job in `.github/workflows/release.yml` runs them on every +release. The `version` in `cli/package.json` is a placeholder; the release +version comes from the tag. -The committed `version` fields here are placeholders; the workflow is the single -source of truth and always uses the release tag. There is no manual publish step. +## `@gitlawb/gl` (being retired) -Windows is intentionally not published to npm — the wrapper points Windows users -at the curl/PowerShell installer instead. +A wrapper whose `postinstall` (`packages/gl/install.js`) copied the programs +out of four `@gitlawb/gl-` packages. npm links commands before +`postinstall` runs, so `packages/gl/bin/` holds placeholders for npm to link, +which `install.js` overwrites. The `npm-publish` job publishes it for one more +release, after which it is deprecated and this directory and that job go. diff --git a/npm/cli/README.md b/npm/cli/README.md index ac6da850..faf191ab 100644 --- a/npm/cli/README.md +++ b/npm/cli/README.md @@ -11,7 +11,13 @@ twigpine quickstart ``` npm installs one prebuilt program for your platform: macOS (arm64, x64), -Linux (arm64, x64) or Windows (x64). There is no install script. +Linux (arm64, x64) or Windows (x64, which also runs on Windows on ARM). +There is no install script. Without a global install: +`npx -p @twigpine/cli twigpine doctor`. + +The git helper runs through Node, so git needs `node` on its PATH. A GUI git +client started with a minimal PATH may not find it; the installers below put +the native helper on PATH instead. The package also provides `gl`, the command's name before the rename, and the helper under both names git looks for: `git-remote-twigpine` and diff --git a/npm/cli/bin/launch.js b/npm/cli/bin/launch.js index a336c61e..b701e24e 100755 --- a/npm/cli/bin/launch.js +++ b/npm/cli/bin/launch.js @@ -1,25 +1,37 @@ // Shared by the command launchers in this directory. Finds the native program -// in this platform's payload and runs it, passing arguments, stdio, the exit -// status and any terminating signal straight through. +// in this platform's payload and runs it, passing arguments, stdio and the exit +// status straight through, forwarding termination signals to it, and ending +// the way it ended. // // The payload is a version of this same package, installed under an npm: // alias named after process.platform and process.arch (see // docs/NPM_PACKAGING.md). There is no install script and no downloader: if // npm did not install the payload, the launcher says how to fix it. -import { spawnSync } from 'node:child_process'; +import { spawn } from 'node:child_process'; import { existsSync, readFileSync } from 'node:fs'; import { createRequire } from 'node:module'; +import { constants } from 'node:os'; import { dirname, join } from 'node:path'; import { fileURLToPath } from 'node:url'; const packageRoot = dirname(dirname(fileURLToPath(import.meta.url))); const DEFAULT_PACKAGE_NAME = '@twigpine/cli'; -// The platforms a payload is published for. Anything else has no prebuilt -// program; Windows on ARM runs the x64 one under emulation. +// The platforms a payload is published for. Windows on ARM installs the x64 +// payload (its manifest accepts arm64) and runs it under emulation. export const SUPPORTED = ['darwin-arm64', 'darwin-x64', 'linux-arm64', 'linux-x64', 'win32-x64']; +// Signals that, sent to the launcher alone (a supervisor, an MCP client or +// git stopping it by pid), must reach the program too. A terminal's Ctrl-C +// already reaches both, as they share a process group. +const FORWARDED = ['SIGINT', 'SIGTERM', 'SIGHUP']; + +// Signals the launcher can re-raise on itself to end the same way. Node +// ignores SIGPIPE and turns SIGUSR1 into its debugger, so for any other +// signal the launcher exits with the shell's 128 + number instead. +const RERAISED = new Set(['SIGINT', 'SIGTERM', 'SIGHUP', 'SIGQUIT', 'SIGABRT', 'SIGKILL']); + // The npm name this wrapper was published under, read from its own manifest // so the name lives in one place. export function readPackageName(root = packageRoot) { @@ -32,7 +44,7 @@ export function readPackageName(root = packageRoot) { return DEFAULT_PACKAGE_NAME; } -// The payload alias for a platform. Windows on ARM falls back to x64. +// The payload alias for a platform. Windows on ARM uses the x64 payload. export function payloadName(packageName, platform = process.platform, arch = process.arch) { const effectiveArch = platform === 'win32' && arch === 'arm64' ? 'x64' : arch; return `${packageName}-${platform}-${effectiveArch}`; @@ -59,24 +71,32 @@ export function nativeProgram(program, { export function missingProgramMessage(command, packageName, platform = process.platform, arch = process.arch) { const here = `${platform}-${arch}`; - const supported = SUPPORTED.includes(here) || (platform === 'win32' && arch === 'arm64'); - const lines = [`${command}: the Twigpine program for ${here} is not installed.`]; - if (supported) { - lines.push( - `npm installs it as an optional dependency (${payloadName(packageName, platform, arch)}).`, - 'Reinstall without omitting optional dependencies:', - ` npm install -g ${packageName}`, - ); - } else { - lines.push(`No prebuilt program is published for ${here} (supported: ${SUPPORTED.join(', ')}).`); - } - lines.push( - 'Or install without npm:', + return [ + `${command}: the Twigpine program for ${here} is not installed.`, + `npm installs it as an optional dependency (${payloadName(packageName, platform, arch)}),`, + 'so reinstall without omitting optional dependencies, for example:', + ` npm install -g ${packageName}`, + 'If this release has no build for your platform, install without npm:', platform === 'win32' ? ' irm https://twigpine.com/install.ps1 | iex' : ' curl -fsSL https://twigpine.com/install.sh | sh', - ); - return lines.join('\n'); + ].join('\n'); +} + +// End this process the way the program ended. +function endLike(code, signal) { + if (signal) { + if (RERAISED.has(signal)) { + for (const name of FORWARDED) process.removeAllListeners(name); + try { + process.kill(process.pid, signal); + } catch { + // Fall through to the exit status below. + } + } + process.exit(128 + (constants.signals[signal] ?? 0)); + } + process.exit(code ?? 1); } // Run `program` with this process's arguments. `argv0` is what the program @@ -88,14 +108,15 @@ export function run(program, argv0) { process.stderr.write(missingProgramMessage(argv0, packageName) + '\n'); process.exit(1); } - const child = spawnSync(binary, process.argv.slice(2), { stdio: 'inherit', argv0 }); - if (child.error) { - process.stderr.write(`${argv0}: could not start ${binary}: ${child.error.message}\n`); - process.exit(1); - } - if (child.signal) { - // End the same way the program did, so a shell or git sees the signal. - process.kill(process.pid, child.signal); + const child = spawn(binary, process.argv.slice(2), { stdio: 'inherit', argv0 }); + for (const name of FORWARDED) { + process.on(name, () => { + if (child.exitCode === null && child.signalCode === null) child.kill(name); + }); } - process.exit(child.status ?? 1); + child.on('error', (error) => { + process.stderr.write(`${argv0}: could not start ${binary}: ${error.message}\n`); + process.exit(1); + }); + child.on('exit', endLike); } diff --git a/npm/cli/package.json b/npm/cli/package.json index 5e95c7c4..6d7945eb 100644 --- a/npm/cli/package.json +++ b/npm/cli/package.json @@ -11,7 +11,8 @@ "git-remote-gitlawb": "bin/git-remote-twigpine.js" }, "files": [ - "bin/*.js" + "bin/*.js", + "LICENSE-*" ], "engines": { "node": ">=18" diff --git a/scripts/npm/build-platform-packages.mjs b/scripts/npm/build-platform-packages.mjs index d785ecb9..aee3f3d1 100755 --- a/scripts/npm/build-platform-packages.mjs +++ b/scripts/npm/build-platform-packages.mjs @@ -17,15 +17,21 @@ // aliases, so one package name needs one trusted-publisher setting. See // docs/NPM_PACKAGING.md. // -// Each archive is checked against its .sha256 file before extraction. All five -// platforms are required unless --only narrows the set (tests only); the -// wrapper always references all five. +// Each archive is checked against its .sha256 file before extraction. Every +// platform is required except those named in --allow-missing: the release +// builds Windows best-effort, so CI passes --allow-missing win32-x64. A +// platform left out is left out of the wrapper too, its aliases and its `os` +// list, so npm on that platform refuses the install with a clear error +// instead of installing commands with no program. --only narrows the set for +// tests the same way. // // Usage: // node scripts/npm/build-platform-packages.mjs --version X.Y.Z \ -// --artifacts-dir [--out-dir dist/npm] [--only linux-x64,...] +// --artifacts-dir [--out-dir dist/npm] [--allow-missing win32-x64] \ +// [--only linux-x64,...] import { + realpathSync, chmodSync, cpSync, existsSync, @@ -64,7 +70,13 @@ function fail(message) { } function parseArgs(argv) { - const args = { version: '', artifactsDir: '', outDir: join(repoRoot, 'dist', 'npm'), only: null }; + const args = { + version: '', + artifactsDir: '', + outDir: join(repoRoot, 'dist', 'npm'), + only: null, + allowMissing: new Set(), + }; for (let i = 0; i < argv.length; i += 1) { const arg = argv[i]; const next = () => { @@ -85,6 +97,9 @@ function parseArgs(argv) { case '--only': args.only = new Set(next().split(',').map((entry) => entry.trim()).filter(Boolean)); break; + case '--allow-missing': + args.allowMissing = new Set(next().split(',').map((entry) => entry.trim()).filter(Boolean)); + break; default: fail(`unknown argument ${arg}`); } @@ -108,9 +123,9 @@ export function platformVersion(version, entry) { return `${version}-${entry.platform}-${entry.arch}`; } -export function optionalDependencies(packageName, version) { +export function optionalDependencies(packageName, version, entries = MATRIX) { const aliases = {}; - for (const entry of MATRIX) { + for (const entry of entries) { aliases[`${packageName}-${entry.platform}-${entry.arch}`] = `npm:${packageName}@${platformVersion(version, entry)}`; } @@ -145,7 +160,9 @@ function extract(archivePath, ext, destDir) { let lastError = ''; for (const [command, commandArgs] of attempts) { const result = spawnSync(command, commandArgs, { stdio: ['ignore', 'ignore', 'pipe'] }); - if (result.status === 0) return; + // unzip exits 1 for warnings (a backslash in a stored path, say) after + // extracting everything; whether the programs are there is checked next. + if (result.status === 0 || (command === 'unzip' && result.status === 1)) return; lastError = result.error ? result.error.message : String(result.stderr || '').trim(); } fail(`could not extract ${archivePath}: ${lastError}`); @@ -164,10 +181,14 @@ function programsDir(extracted, packageDirName, probe) { fail(`${packageDirName} does not contain ${probe}`); } +function archivePathFor(entry, version, artifactsDir) { + return join(artifactsDir, `twigpine-node-${version}-${entry.target}.${entry.ext}`); +} + function buildPlatformPackage(entry, source, version, artifactsDir, outDir) { const base = `twigpine-node-${version}-${entry.target}`; const assetName = `${base}.${entry.ext}`; - const archivePath = join(artifactsDir, assetName); + const archivePath = archivePathFor(entry, version, artifactsDir); if (!existsSync(archivePath)) fail(`missing release archive ${archivePath}`); verifyChecksum(archivePath, assetName); @@ -193,9 +214,14 @@ function buildPlatformPackage(entry, source, version, artifactsDir, outDir) { version: platformVersion(version, entry), description: `Twigpine programs for ${entry.platform}-${entry.arch} (installed by ${source.name}@${version})`, os: [entry.platform], - cpu: [entry.arch], + // Windows on ARM has no build of its own and runs the x64 one under + // emulation; npm installs a payload only when its cpu list matches. + cpu: entry.platform === 'win32' && entry.arch === 'x64' ? ['x64', 'arm64'] : [entry.arch], license: source.license, repository: source.repository, + // Yarn's Plug'n'Play keeps packages in zip files unless told otherwise, + // and a program inside a zip cannot be started. + preferUnplugged: true, // No bin, no scripts, no dependencies: the wrapper runs the programs. }; writeFileSync(join(packageDir, 'package.json'), JSON.stringify(manifest, null, 2) + '\n'); @@ -205,7 +231,7 @@ function buildPlatformPackage(entry, source, version, artifactsDir, outDir) { } } -function buildWrapper(source, version, outDir) { +function buildWrapper(source, version, outDir, entries) { const wrapperDir = join(outDir, 'wrapper'); rmSync(wrapperDir, { recursive: true, force: true }); mkdirSync(join(wrapperDir, 'bin'), { recursive: true }); @@ -226,7 +252,10 @@ function buildWrapper(source, version, outDir) { delete pkg.scripts; delete pkg.dependencies; pkg.version = version; - pkg.optionalDependencies = optionalDependencies(source.name, version); + pkg.optionalDependencies = optionalDependencies(source.name, version, entries); + // Install only where a payload was published. + const platforms = new Set(entries.map((entry) => entry.platform)); + pkg.os = (pkg.os || []).filter((platform) => platforms.has(platform)); writeFileSync(join(wrapperDir, 'package.json'), JSON.stringify(pkg, null, 2) + '\n'); return wrapperDir; } @@ -234,20 +263,30 @@ function buildWrapper(source, version, outDir) { function main() { const args = parseArgs(process.argv.slice(2)); const source = readSourceManifest(); - const selected = MATRIX.filter((e) => !args.only || args.only.has(`${e.platform}-${e.arch}`)); - if (args.only) { - for (const key of args.only) { - if (!MATRIX.some((e) => `${e.platform}-${e.arch}` === key)) fail(`--only lists unknown platform ${key}`); + for (const [flag, keys] of [['--only', args.only], ['--allow-missing', args.allowMissing]]) { + for (const key of keys || []) { + if (!MATRIX.some((e) => `${e.platform}-${e.arch}` === key)) fail(`${flag} lists unknown platform ${key}`); } } - if (selected.length === 0) fail('--only matched no platforms'); + const selected = MATRIX.filter((e) => { + const key = `${e.platform}-${e.arch}`; + if (args.only && !args.only.has(key)) return false; + if (args.allowMissing.has(key) && !existsSync(archivePathFor(e, args.version, args.artifactsDir))) { + console.error(`[build-platform-packages] no ${key} archive in this release; leaving that platform out`); + return false; + } + return true; + }); + if (selected.length === 0) fail('no platforms to build'); const platformDirs = selected.map((e) => buildPlatformPackage(e, source, args.version, args.artifactsDir, args.outDir)); - const wrapperDir = buildWrapper(source, args.version, args.outDir); + const wrapperDir = buildWrapper(source, args.version, args.outDir, selected); process.stdout.write( JSON.stringify({ name: source.name, version: args.version, wrapperDir, platformDirs }, null, 2) + '\n', ); } -if (process.argv[1] && fileURLToPath(import.meta.url) === process.argv[1]) { +// Run when executed, not when imported. Both sides through realpath: macOS +// reaches temp and checkout paths through symlinks. +if (process.argv[1] && realpathSync(fileURLToPath(import.meta.url)) === realpathSync(process.argv[1])) { main(); } diff --git a/scripts/npm/publish-packages.sh b/scripts/npm/publish-packages.sh index 76e6ec32..ef172d06 100755 --- a/scripts/npm/publish-packages.sh +++ b/scripts/npm/publish-packages.sh @@ -189,7 +189,12 @@ case "$latest" in esac echo "dist-tag latest is ${latest:-unset} (unchanged by the platform publishes)." -publish "$wrapper_dir" "$version" --tag "$wrapper_tag" - -wait_until "${name}@${version} as dist-tag ${wrapper_tag}" tag_is "$wrapper_tag" "$version" -echo "dist-tag ${wrapper_tag} is ${version}." +if already_published "$version"; then + # Out already, under whatever tag it went out with then (a re-run, or a + # backfill of a release published at the time): nothing to wait for. + echo "${name}@${version} is already on the registry; not publishing it again." +else + publish "$wrapper_dir" "$version" --tag "$wrapper_tag" + wait_until "${name}@${version} as dist-tag ${wrapper_tag}" tag_is "$wrapper_tag" "$version" + echo "dist-tag ${wrapper_tag} is ${version}." +fi diff --git a/scripts/npm/test-npm-packages.sh b/scripts/npm/test-npm-packages.sh index e44a4435..00cdfa43 100755 --- a/scripts/npm/test-npm-packages.sh +++ b/scripts/npm/test-npm-packages.sh @@ -40,6 +40,7 @@ field() { node -e 'const p=JSON.parse(require("fs").readFileSync(process.argv[1] fake_program() { cat > "$1" < "\$FAKE_PIDFILE"; sleep 30; exit 0; fi if [ -n "\${FAKE_SIGNAL:-}" ]; then kill -"\$FAKE_SIGNAL" \$\$; fi echo "$2 \$*" if [ -n "\${FAKE_STDIN:-}" ]; then cat; fi @@ -96,6 +97,13 @@ check "a payload carries the CLI and the helper, executable" \ check "a payload leaves the daemon out" test ! -e "$OUT/platforms/cli-linux-x64/twigpine-node" check "the Windows payload carries .exe programs" \ test -f "$OUT/platforms/cli-win32-x64/twigpine.exe" -a -f "$OUT/platforms/cli-win32-x64/git-remote-twigpine.exe" +check "the Windows payload also installs on Windows on ARM" \ + test "$(field "$OUT/platforms/cli-win32-x64/package.json" cpu)" = '["x64","arm64"]' +check "payloads ask Yarn not to keep them zipped" test "$(field "$LP" preferUnplugged)" = true +check "the wrapper installs on every published platform" test "$(field "$WP" os)" = '["darwin","linux","win32"]' +packed="$(cd "$OUT/wrapper" && npm pack --dry-run --json 2>/dev/null | node -e 'let d="";process.stdin.on("data",c=>d+=c).on("end",()=>console.log(JSON.parse(d)[0].files.map(f=>f.path).sort().join(" ")))')" +check "the wrapper tarball carries the launchers and both licence files" test "$packed" = \ + "LICENSE-APACHE LICENSE-MIT README.md bin/git-remote-twigpine.js bin/gl.js bin/launch.js bin/twigpine.js package.json" cp -R "$ART" "$W/art-bad" printf 'tampered' >> "$W/art-bad/twigpine-node-$VERSION-x86_64-unknown-linux-musl.tar.gz" @@ -103,10 +111,34 @@ if node "$BUILD" --version "$VERSION" --artifacts-dir "$W/art-bad" --out-dir "$W bad "refuses an archive that does not match its checksum" elif grep -q "checksum mismatch" "$W/out"; then ok "refuses an archive that does not match its checksum" else bad "refuses an archive that does not match its checksum"; fi -rm "$W/art-bad/twigpine-node-$VERSION-x86_64-apple-darwin.tar.gz" -if node "$BUILD" --version "$VERSION" --artifacts-dir "$W/art-bad" --out-dir "$W/npm-bad" > "$W/out" 2>&1; then +cp -R "$ART" "$W/art-missing" +rm "$W/art-missing/twigpine-node-$VERSION-x86_64-apple-darwin.tar.gz" +if node "$BUILD" --version "$VERSION" --artifacts-dir "$W/art-missing" --out-dir "$W/npm-bad" > "$W/out" 2>&1; then bad "refuses a missing archive" -else ok "refuses a missing archive"; fi +elif grep -q "missing release archive" "$W/out"; then ok "refuses a missing archive" +else bad "refuses a missing archive"; fi + +# The release builds Windows best-effort. +cp -R "$ART" "$W/art-nowin" +rm "$W/art-nowin/twigpine-node-$VERSION-x86_64-pc-windows-msvc.zip"* +if node "$BUILD" --version "$VERSION" --artifacts-dir "$W/art-nowin" --out-dir "$W/npm-nowin" > "$W/out" 2>&1; then + bad "without --allow-missing, a missing Windows archive is an error" +else ok "without --allow-missing, a missing Windows archive is an error"; fi +if node "$BUILD" --version "$VERSION" --artifacts-dir "$W/art-nowin" --out-dir "$W/npm-nowin" \ + --allow-missing win32-x64 > "$W/out" 2>&1 \ + && [ ! -d "$W/npm-nowin/platforms/cli-win32-x64" ] \ + && [ "$(field "$W/npm-nowin/wrapper/package.json" os)" = '["darwin","linux"]' ] \ + && ! grep -q win32 <<< "$(field "$W/npm-nowin/wrapper/package.json" optionalDependencies)"; then + ok "a release without Windows is packaged without it, and the wrapper says so" +else bad "a release without Windows is packaged without it, and the wrapper says so"; fi + +# Called through a symlinked path (macOS temp and checkout paths often are). +ln -s "$ROOT" "$W/linked-root" +if node "$W/linked-root/scripts/npm/build-platform-packages.mjs" --version "$VERSION" \ + --artifacts-dir "$ART" --out-dir "$W/npm-linked" > "$W/out" 2>&1 \ + && [ -f "$W/npm-linked/wrapper/package.json" ]; then + ok "builds when called through a symlinked path" +else bad "builds when called through a symlinked path"; fi for v in 9.9 v9.9.0 9.9.0-rc.1 09.9.0; do if node "$BUILD" --version "$v" --artifacts-dir "$ART" --out-dir "$W/npm-bad" > "$W/out" 2>&1; then bad "refuses version $v" @@ -140,6 +172,19 @@ check "the helper gets git's stdin and answers on stdout" \ # test's own shell does not report a killed job. status="$(sh -c 'FAKE_SIGNAL=TERM node "$1" > /dev/null 2>&1; echo $?' _ "$INSTALL/bin/twigpine.js" 2>/dev/null)" check "a program killed by a signal ends the launcher the same way" test "$status" = 143 +status="$(sh -c 'FAKE_SIGNAL=PIPE node "$1" > /dev/null 2>&1; echo $?' _ "$INSTALL/bin/twigpine.js" 2>/dev/null)" +check "a program killed by SIGPIPE ends the launcher with 141, as a shell expects" test "$status" = 141 + +FAKE_PIDFILE="$W/child.pid" node "$INSTALL/bin/twigpine.js" > /dev/null 2>&1 & +launcher=$! +for _ in $(seq 1 50); do [ -s "$W/child.pid" ] && break; sleep 0.1; done +child="$(cat "$W/child.pid" 2>/dev/null || true)" +kill -TERM "$launcher" 2>/dev/null || true +wait "$launcher" 2>/dev/null || true +gone=no +for _ in $(seq 1 30); do kill -0 "$child" 2>/dev/null || { gone=yes; break; }; sleep 0.1; done +[ "$gone" = yes ] || kill -KILL "$child" 2>/dev/null || true +check "a signal sent to the launcher alone reaches the program" test -n "$child" -a "$gone" = yes rm -rf "$PAYLOAD" status=0 @@ -229,6 +274,16 @@ if run_publish "$OUT" && grep -q '^published 9.9.0 backfill$' "$REG/log" && [ "$ ok "a backfill of an older release leaves latest where it is" else bad "a backfill of an older release leaves latest where it is"; fi +REG="$W/reg3b"; mkdir -p "$REG/versions" "$REG/tags" +for v in 9.9.0 9.9.0-darwin-arm64 9.9.0-darwin-x64 9.9.0-linux-arm64 9.9.0-linux-x64 9.9.0-win32-x64 9.10.0; do + touch "$REG/versions/$v" +done +echo "9.10.0" > "$REG/tags/latest" +start=$SECONDS +if run_publish "$OUT" && [ -z "$(published_order)" ] && [ $((SECONDS - start)) -lt 4 ]; then + ok "re-running a backfill that already went out finishes at once" +else bad "re-running a backfill that already went out finishes at once"; fi + REG="$W/reg4"; mkdir -p "$REG" cp -R "$OUT" "$W/npm-short"; rm -rf "$W/npm-short/platforms/cli-win32-x64" if run_publish "$W/npm-short"; then bad "refuses payloads that do not match the wrapper" From 9a9ae9b1dc4f1308e3ab393271164a044bdc686c Mon Sep 17 00:00:00 2001 From: Kevin Codex Date: Wed, 18 Nov 2026 14:02:37 +0800 Subject: [PATCH 3/8] fix(npm): a rebuild removes payloads the new build leaves out --- scripts/npm/build-platform-packages.mjs | 7 +++++++ scripts/npm/test-npm-packages.sh | 8 ++++++++ 2 files changed, 15 insertions(+) diff --git a/scripts/npm/build-platform-packages.mjs b/scripts/npm/build-platform-packages.mjs index aee3f3d1..0650434e 100755 --- a/scripts/npm/build-platform-packages.mjs +++ b/scripts/npm/build-platform-packages.mjs @@ -278,6 +278,13 @@ function main() { return true; }); if (selected.length === 0) fail('no platforms to build'); + // A payload left from an earlier build into the same directory must not be + // published beside a wrapper that no longer names it. + for (const entry of MATRIX) { + if (!selected.includes(entry)) { + rmSync(join(args.outDir, 'platforms', `cli-${entry.platform}-${entry.arch}`), { recursive: true, force: true }); + } + } const platformDirs = selected.map((e) => buildPlatformPackage(e, source, args.version, args.artifactsDir, args.outDir)); const wrapperDir = buildWrapper(source, args.version, args.outDir, selected); process.stdout.write( diff --git a/scripts/npm/test-npm-packages.sh b/scripts/npm/test-npm-packages.sh index 00cdfa43..8c25f335 100755 --- a/scripts/npm/test-npm-packages.sh +++ b/scripts/npm/test-npm-packages.sh @@ -132,6 +132,14 @@ if node "$BUILD" --version "$VERSION" --artifacts-dir "$W/art-nowin" --out-dir " ok "a release without Windows is packaged without it, and the wrapper says so" else bad "a release without Windows is packaged without it, and the wrapper says so"; fi +# Rebuilt into a directory that held a full build: the old Windows payload goes. +cp -R "$OUT" "$W/npm-rebuild" +if node "$BUILD" --version "$VERSION" --artifacts-dir "$W/art-nowin" --out-dir "$W/npm-rebuild" \ + --allow-missing win32-x64 > "$W/out" 2>&1 && [ ! -e "$W/npm-rebuild/platforms/cli-win32-x64" ] \ + && [ -d "$W/npm-rebuild/platforms/cli-linux-x64" ]; then + ok "a rebuild without Windows removes the earlier Windows payload" +else bad "a rebuild without Windows removes the earlier Windows payload"; fi + # Called through a symlinked path (macOS temp and checkout paths often are). ln -s "$ROOT" "$W/linked-root" if node "$W/linked-root/scripts/npm/build-platform-packages.mjs" --version "$VERSION" \ From ee2d1e22420e82573ce31d2d19c50642f8bdb637 Mon Sep 17 00:00:00 2001 From: Kevin Codex Date: Thu, 19 Nov 2026 08:27:58 +0800 Subject: [PATCH 4/8] fix(npm): refuse a build that leaves out one architecture of a system npm checks a package's os and cpu lists separately, so a wrapper built with --allow-missing darwin-arm64 still listed darwin and arm64 and would install on Apple Silicon with no program behind its commands. The build now checks every pairing of the systems and architectures it would list before writing anything, and narrows the wrapper's cpu list as well as its os list. Full builds, builds without Windows and one-platform builds pass. --- docs/NPM_PACKAGING.md | 4 ++- scripts/npm/build-platform-packages.mjs | 45 +++++++++++++++++++++---- scripts/npm/test-npm-packages.sh | 18 +++++++++- 3 files changed, 58 insertions(+), 9 deletions(-) diff --git a/docs/NPM_PACKAGING.md b/docs/NPM_PACKAGING.md index da83ce4f..eda4bfa0 100644 --- a/docs/NPM_PACKAGING.md +++ b/docs/NPM_PACKAGING.md @@ -42,7 +42,9 @@ and without a fallback downloader. - The release builds Windows best-effort. A release without the Windows archive is published without Windows: the wrapper then leaves `win32` out of its aliases and its `os`, so npm on Windows refuses the install instead - of installing commands with no program. + of installing commands with no program. Only a whole system can be left out: + npm checks `os` and `cpu` separately, so the build refuses a selection that + drops one architecture of a system while keeping another. - The wrapper holds the launchers in `npm/cli/bin/` and a manifest with no scripts and no dependencies, only the exact-version `optionalDependencies`. `scripts/npm/build-platform-packages.mjs` writes both from the release diff --git a/scripts/npm/build-platform-packages.mjs b/scripts/npm/build-platform-packages.mjs index 0650434e..9fdee89e 100755 --- a/scripts/npm/build-platform-packages.mjs +++ b/scripts/npm/build-platform-packages.mjs @@ -21,9 +21,11 @@ // platform is required except those named in --allow-missing: the release // builds Windows best-effort, so CI passes --allow-missing win32-x64. A // platform left out is left out of the wrapper too, its aliases and its `os` -// list, so npm on that platform refuses the install with a clear error -// instead of installing commands with no program. --only narrows the set for -// tests the same way. +// and `cpu` lists, so npm on that platform refuses the install with a clear +// error instead of installing commands with no program. Only a whole system, +// or a selection that is still every pairing of its systems and +// architectures, can be described that way; anything else is refused. +// --only narrows the set for tests the same way. // // Usage: // node scripts/npm/build-platform-packages.mjs --version X.Y.Z \ @@ -214,9 +216,7 @@ function buildPlatformPackage(entry, source, version, artifactsDir, outDir) { version: platformVersion(version, entry), description: `Twigpine programs for ${entry.platform}-${entry.arch} (installed by ${source.name}@${version})`, os: [entry.platform], - // Windows on ARM has no build of its own and runs the x64 one under - // emulation; npm installs a payload only when its cpu list matches. - cpu: entry.platform === 'win32' && entry.arch === 'x64' ? ['x64', 'arm64'] : [entry.arch], + cpu: cpusFor(entry), license: source.license, repository: source.repository, // Yarn's Plug'n'Play keeps packages in zip files unless told otherwise, @@ -231,6 +231,34 @@ function buildPlatformPackage(entry, source, version, artifactsDir, outDir) { } } +// Windows on ARM has no build of its own and runs the x64 one under +// emulation; npm installs a payload only when its cpu list matches. +function cpusFor(entry) { + return entry.platform === 'win32' && entry.arch === 'x64' ? ['x64', 'arm64'] : [entry.arch]; +} + +// The systems and architectures the wrapper may install on. npm checks `os` +// and `cpu` separately, so the wrapper would also install on every pairing of +// the two lists; each pairing needs a payload, or the commands would install +// with no program behind them. Full builds and builds that leave a whole +// system out (Windows, best-effort) pass; leaving out one architecture of a +// system that keeps another does not. +function installTargets(entries) { + const platforms = new Set(entries.map((entry) => entry.platform)); + const cpus = new Set(entries.flatMap(cpusFor)); + for (const platform of platforms) { + for (const cpu of cpus) { + if (!entries.some((entry) => entry.platform === platform && cpusFor(entry).includes(cpu))) { + fail( + `no ${platform}-${cpu} payload, but the wrapper would install there: ` + + `build every architecture of ${platform}, or leave ${platform} out entirely`, + ); + } + } + } + return { platforms, cpus }; +} + function buildWrapper(source, version, outDir, entries) { const wrapperDir = join(outDir, 'wrapper'); rmSync(wrapperDir, { recursive: true, force: true }); @@ -254,8 +282,9 @@ function buildWrapper(source, version, outDir, entries) { pkg.version = version; pkg.optionalDependencies = optionalDependencies(source.name, version, entries); // Install only where a payload was published. - const platforms = new Set(entries.map((entry) => entry.platform)); + const { platforms, cpus } = installTargets(entries); pkg.os = (pkg.os || []).filter((platform) => platforms.has(platform)); + pkg.cpu = (pkg.cpu || []).filter((cpu) => cpus.has(cpu)); writeFileSync(join(wrapperDir, 'package.json'), JSON.stringify(pkg, null, 2) + '\n'); return wrapperDir; } @@ -278,6 +307,8 @@ function main() { return true; }); if (selected.length === 0) fail('no platforms to build'); + // Before anything is written: a selection the wrapper cannot describe fails here. + installTargets(selected); // A payload left from an earlier build into the same directory must not be // published beside a wrapper that no longer names it. for (const entry of MATRIX) { diff --git a/scripts/npm/test-npm-packages.sh b/scripts/npm/test-npm-packages.sh index 8c25f335..7cdd350a 100755 --- a/scripts/npm/test-npm-packages.sh +++ b/scripts/npm/test-npm-packages.sh @@ -100,7 +100,7 @@ check "the Windows payload carries .exe programs" \ check "the Windows payload also installs on Windows on ARM" \ test "$(field "$OUT/platforms/cli-win32-x64/package.json" cpu)" = '["x64","arm64"]' check "payloads ask Yarn not to keep them zipped" test "$(field "$LP" preferUnplugged)" = true -check "the wrapper installs on every published platform" test "$(field "$WP" os)" = '["darwin","linux","win32"]' +check "the wrapper installs on every published platform" test "$(field "$WP" os) $(field "$WP" cpu)" = '["darwin","linux","win32"] ["x64","arm64"]' packed="$(cd "$OUT/wrapper" && npm pack --dry-run --json 2>/dev/null | node -e 'let d="";process.stdin.on("data",c=>d+=c).on("end",()=>console.log(JSON.parse(d)[0].files.map(f=>f.path).sort().join(" ")))')" check "the wrapper tarball carries the launchers and both licence files" test "$packed" = \ "LICENSE-APACHE LICENSE-MIT README.md bin/git-remote-twigpine.js bin/gl.js bin/launch.js bin/twigpine.js package.json" @@ -132,6 +132,22 @@ if node "$BUILD" --version "$VERSION" --artifacts-dir "$W/art-nowin" --out-dir " ok "a release without Windows is packaged without it, and the wrapper says so" else bad "a release without Windows is packaged without it, and the wrapper says so"; fi +# One architecture of a system left out while another stays: npm checks os and +# cpu separately, so such a wrapper would install where it has no program. +cp -R "$ART" "$W/art-noarm" +rm "$W/art-noarm/twigpine-node-$VERSION-aarch64-apple-darwin.tar.gz"* +if node "$BUILD" --version "$VERSION" --artifacts-dir "$W/art-noarm" --out-dir "$W/npm-noarm" \ + --allow-missing darwin-arm64 > "$W/out" 2>&1; then + bad "refuses to leave out one architecture of a system that keeps another" +elif grep -q "no darwin-arm64 payload" "$W/out" && [ ! -e "$W/npm-noarm/wrapper" ]; then + ok "refuses to leave out one architecture of a system that keeps another" +else bad "refuses to leave out one architecture of a system that keeps another"; fi +if node "$BUILD" --version "$VERSION" --artifacts-dir "$ART" --out-dir "$W/npm-only" \ + --only linux-x64 > "$W/out" 2>&1 \ + && [ "$(field "$W/npm-only/wrapper/package.json" os) $(field "$W/npm-only/wrapper/package.json" cpu)" = '["linux"] ["x64"]' ]; then + ok "a one-platform build limits the wrapper to that system and architecture" +else bad "a one-platform build limits the wrapper to that system and architecture"; fi + # Rebuilt into a directory that held a full build: the old Windows payload goes. cp -R "$OUT" "$W/npm-rebuild" if node "$BUILD" --version "$VERSION" --artifacts-dir "$W/art-nowin" --out-dir "$W/npm-rebuild" \ From d3568b3bd7ea74f8c28223c0e265f27eedd3e60b Mon Sep 17 00:00:00 2001 From: Kevin Codex Date: Thu, 19 Nov 2026 08:33:03 +0800 Subject: [PATCH 5/8] fix(npm): review follow-ups for the launcher and the setup docs - On Windows the launcher no longer forwards signals: the console already delivers Ctrl-C and window close to the program, and kill() there can only terminate it (and throws for SIGHUP, which Node raises when the console window closes). The launcher still listens, so it outlives the program and passes its exit status through. kill() is also guarded. - The missing-payload hint now says --include=optional, so a user whose npmrc omits optional dependencies does not repeat the omission. - Setup docs: the npm-publish environment gets no required reviewer, as release has none (a run waiting for approval holds the workflow's concurrency group); the placeholder is published from an empty directory with the manifest given inline, never from npm/cli/; removing the old npm-publish job and deprecating @gitlawb/gl happen in one change, since both jobs read npm_backfill_tag; and a release without a platform's build is refused by npm, not reported by the launcher. --- docs/NPM_PACKAGING.md | 52 +++++++++++++++++++++++--------- npm/cli/bin/launch.js | 15 +++++++-- scripts/npm/test-npm-packages.sh | 2 +- 3 files changed, 52 insertions(+), 17 deletions(-) diff --git a/docs/NPM_PACKAGING.md b/docs/NPM_PACKAGING.md index eda4bfa0..ac49f505 100644 --- a/docs/NPM_PACKAGING.md +++ b/docs/NPM_PACKAGING.md @@ -62,9 +62,11 @@ and without a fallback downloader. well. A GUI git client started with a minimal PATH may not find it; the installers (`install.sh`, `install.ps1`, Homebrew) put the native helper on PATH instead. -- There is no downloader. If the payload is missing (`--omit=optional`, a - release without that platform's build), the launcher says how to reinstall - or install without npm. +- There is no downloader. If the payload is missing (installed with + `--omit=optional`, or a `node_modules` copied from another platform), the + launcher says how to reinstall or install without npm. On a platform the + release has no build for, npm refuses the install itself (the wrapper's + `os` and `cpu`). - `npx` needs the command named: `npx -p @twigpine/cli twigpine doctor`. ## Publishing rules @@ -95,12 +97,30 @@ and without a fallback downloader. Done once, by a maintainer, in this order: 1. In the GitHub repository settings, create the `npm-publish` environment, - limited to the `main` branch, with a required reviewer. First: GitHub - creates an environment a job names on first use, with no protection. -2. Publish a placeholder `@twigpine/cli@0.0.0` by hand with - `npm publish --access public`; npm only lets a trusted publisher be set on - a package that exists. The placeholder is a manifest and a README, with no - `bin`, so it cannot install commands that do nothing. + with deployment branches limited to `main`. Do this first: GitHub creates + an environment a job names on first use, with no protection. Leave out a + required reviewer, as `release` does: merging the release PR is already + the human step, and a run waiting for approval holds the workflow's + `release` concurrency group, so release-please and the Docker images wait + behind it until someone approves or rejects. +2. Publish a placeholder `@twigpine/cli@0.0.0` by hand: npm only lets a + trusted publisher be set on a package that exists. Publish it from an + empty directory holding only this `package.json` (and a README if you + like), **not** from `npm/cli/`, whose manifest has the launchers' `bin` + entries and would publish commands that do nothing, at a version that can + never be published again: + + ```json + { + "name": "@twigpine/cli", + "version": "0.0.0", + "description": "Placeholder so the release workflow can be set up as this package's trusted publisher. Real releases are published from github.com/Twigpine/node.", + "repository": { "type": "git", "url": "git+https://github.com/Twigpine/node.git" }, + "license": "MIT OR Apache-2.0" + } + ``` + + then `npm publish --access public`. 3. Add the trusted publisher with publish permission: on npmjs.com under the package's settings, or `npm trust github @twigpine/cli --file release.yml --repo Twigpine/node --env npm-publish --allow-publish`. @@ -127,8 +147,12 @@ To reach existing users: @gitlawb/gl` installs something and shows its deprecation message. - A CLI installed from `@gitlawb/gl` prints the two commands above on a terminal. -- After that release, a maintainer deprecates every `@gitlawb/gl*` version - with a pointer to `@twigpine/cli`, the install line in the README and the - website moves to `@twigpine/cli`, and the `npm-publish` job and - `npm/packages/` are removed. The old versions stay on the registry: existing - installs resolve their platform packages through them. +- After that release, in one change: the `npm-publish` job and + `npm/packages/` are removed, a maintainer deprecates every `@gitlawb/gl*` + version with a pointer to `@twigpine/cli`, and the install line in the + README and the website moves to `@twigpine/cli`. Keep removing the job and + deprecating together: `npm deprecate` marks only versions that exist, and + while the job is in `release.yml`, every release and every + `npm_backfill_tag` dispatch (which both jobs read) would publish a new + `@gitlawb/gl` version without the deprecation. The old versions stay on the + registry: existing installs resolve their platform packages through them. diff --git a/npm/cli/bin/launch.js b/npm/cli/bin/launch.js index b701e24e..3563ef0a 100755 --- a/npm/cli/bin/launch.js +++ b/npm/cli/bin/launch.js @@ -27,6 +27,12 @@ export const SUPPORTED = ['darwin-arm64', 'darwin-x64', 'linux-arm64', 'linux-x6 // already reaches both, as they share a process group. const FORWARDED = ['SIGINT', 'SIGTERM', 'SIGHUP']; +// On Windows the launcher only listens to these, so that it outlives the +// program and passes its exit status through: the console already delivers +// Ctrl-C and window close to the program, and there kill() can only end it +// outright (or throws, for SIGHUP). +const FORWARD_TO_PROGRAM = process.platform !== 'win32'; + // Signals the launcher can re-raise on itself to end the same way. Node // ignores SIGPIPE and turns SIGUSR1 into its debugger, so for any other // signal the launcher exits with the shell's 128 + number instead. @@ -75,7 +81,7 @@ export function missingProgramMessage(command, packageName, platform = process.p `${command}: the Twigpine program for ${here} is not installed.`, `npm installs it as an optional dependency (${payloadName(packageName, platform, arch)}),`, 'so reinstall without omitting optional dependencies, for example:', - ` npm install -g ${packageName}`, + ` npm install -g --include=optional ${packageName}`, 'If this release has no build for your platform, install without npm:', platform === 'win32' ? ' irm https://twigpine.com/install.ps1 | iex' @@ -111,7 +117,12 @@ export function run(program, argv0) { const child = spawn(binary, process.argv.slice(2), { stdio: 'inherit', argv0 }); for (const name of FORWARDED) { process.on(name, () => { - if (child.exitCode === null && child.signalCode === null) child.kill(name); + if (!FORWARD_TO_PROGRAM || child.exitCode !== null || child.signalCode !== null) return; + try { + child.kill(name); + } catch { + // The program is already gone, or this signal cannot be sent here. + } }); } child.on('error', (error) => { diff --git a/scripts/npm/test-npm-packages.sh b/scripts/npm/test-npm-packages.sh index 7cdd350a..c1bec84e 100755 --- a/scripts/npm/test-npm-packages.sh +++ b/scripts/npm/test-npm-packages.sh @@ -213,7 +213,7 @@ check "a signal sent to the launcher alone reaches the program" test -n "$child" rm -rf "$PAYLOAD" status=0 node "$INSTALL/bin/twigpine.js" > "$W/out" 2>&1 || status=$? -if [ "$status" = 1 ] && grep -q "npm install -g @twigpine/cli" "$W/out" && grep -q "install.sh" "$W/out"; then +if [ "$status" = 1 ] && grep -q "npm install -g --include=optional @twigpine/cli" "$W/out" && grep -q "install.sh" "$W/out"; then ok "a missing payload says how to fix it" else bad "a missing payload says how to fix it"; fi From a090dd83df57c7034a10d99242cda7d9c7f25cbf Mon Sep 17 00:00:00 2001 From: Kevin Codex Date: Thu, 19 Nov 2026 08:40:10 +0800 Subject: [PATCH 6/8] test(npm): check the program is gone before waiting for the launcher The forwarding test waited for the launcher first. The launcher outlives the program, so with forwarding broken the program ran out its 30-second sleep, both exited, and the check still passed. Poll the program first, then wait; with forwarding switched off the check now fails. --- scripts/npm/test-npm-packages.sh | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/scripts/npm/test-npm-packages.sh b/scripts/npm/test-npm-packages.sh index c1bec84e..c766757d 100755 --- a/scripts/npm/test-npm-packages.sh +++ b/scripts/npm/test-npm-packages.sh @@ -204,10 +204,13 @@ launcher=$! for _ in $(seq 1 50); do [ -s "$W/child.pid" ] && break; sleep 0.1; done child="$(cat "$W/child.pid" 2>/dev/null || true)" kill -TERM "$launcher" 2>/dev/null || true -wait "$launcher" 2>/dev/null || true +# Poll before waiting: the launcher outlives the program, so waiting first +# would also pass when nothing was forwarded and the program simply ran out +# its sleep. gone=no for _ in $(seq 1 30); do kill -0 "$child" 2>/dev/null || { gone=yes; break; }; sleep 0.1; done [ "$gone" = yes ] || kill -KILL "$child" 2>/dev/null || true +wait "$launcher" 2>/dev/null || true check "a signal sent to the launcher alone reaches the program" test -n "$child" -a "$gone" = yes rm -rf "$PAYLOAD" From b649c2a2803f11051d3f3d5f6caea2f372356377 Mon Sep 17 00:00:00 2001 From: Kevin Codex Date: Thu, 19 Nov 2026 08:44:25 +0800 Subject: [PATCH 7/8] docs(npm): set up the trusted publisher within two days of the first publish npm expires a new trusted publisher configuration that has not completed a publish within two days; an expired one can only be deleted and created again. Say so in the one-time setup, and make it the last step. --- docs/NPM_PACKAGING.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/docs/NPM_PACKAGING.md b/docs/NPM_PACKAGING.md index ac49f505..da380c33 100644 --- a/docs/NPM_PACKAGING.md +++ b/docs/NPM_PACKAGING.md @@ -124,6 +124,10 @@ Done once, by a maintainer, in this order: 3. Add the trusted publisher with publish permission: on npmjs.com under the package's settings, or `npm trust github @twigpine/cli --file release.yml --repo Twigpine/node --env npm-publish --allow-publish`. + Do this step last, at most two days before the release that first + publishes `@twigpine/cli`: npm expires a new trusted publisher that has + not completed a publish within two days, and an expired one can only be + deleted and created again (which starts a new two days). Until then the `npm-publish-cli` job fails on its own; the rest of the release is unaffected. The `npm-publish` job publishing `@gitlawb/gl` needs that From bf0f67bb988a02b522841c7a413c582bd5b934e1 Mon Sep 17 00:00:00 2001 From: Kevin Codex Date: Thu, 19 Nov 2026 09:10:33 +0800 Subject: [PATCH 8/8] fix(npm): end like a crashing program, and clean up on a failed build - The launcher re-raises SIGSEGV, SIGBUS, SIGILL and SIGFPE as well, so a parent sees a crashed program as a signal, not as exit status 128 + n. - build-platform-packages.mjs throws on a failure instead of calling process.exit(), so the finally blocks that remove extracted archives from the temporary directory still run; the entry point prints the message and sets exit code 1. - The signal tests read the launcher's end through spawnSync, which tells a signal from an exit status a shell would show the same way: SIGTERM and SIGSEGV end it by signal, an exit of 143 stays an exit. A new test builds from an archive missing a program and checks the temporary directory is left empty. Both new checks fail on the previous code. --- npm/cli/bin/launch.js | 18 +++++++++++--- scripts/npm/build-platform-packages.mjs | 15 +++++++++--- scripts/npm/test-npm-packages.sh | 32 +++++++++++++++++++++---- 3 files changed, 55 insertions(+), 10 deletions(-) diff --git a/npm/cli/bin/launch.js b/npm/cli/bin/launch.js index 3563ef0a..8e717474 100755 --- a/npm/cli/bin/launch.js +++ b/npm/cli/bin/launch.js @@ -33,10 +33,22 @@ const FORWARDED = ['SIGINT', 'SIGTERM', 'SIGHUP']; // outright (or throws, for SIGHUP). const FORWARD_TO_PROGRAM = process.platform !== 'win32'; -// Signals the launcher can re-raise on itself to end the same way. Node -// ignores SIGPIPE and turns SIGUSR1 into its debugger, so for any other +// Signals the launcher can re-raise on itself to end the same way, including +// a crashing program's, so a parent sees the crash and not an exit status. +// Node ignores SIGPIPE and turns SIGUSR1 into its debugger, so for any other // signal the launcher exits with the shell's 128 + number instead. -const RERAISED = new Set(['SIGINT', 'SIGTERM', 'SIGHUP', 'SIGQUIT', 'SIGABRT', 'SIGKILL']); +const RERAISED = new Set([ + 'SIGINT', + 'SIGTERM', + 'SIGHUP', + 'SIGQUIT', + 'SIGABRT', + 'SIGKILL', + 'SIGSEGV', + 'SIGBUS', + 'SIGILL', + 'SIGFPE', +]); // The npm name this wrapper was published under, read from its own manifest // so the name lives in one place. diff --git a/scripts/npm/build-platform-packages.mjs b/scripts/npm/build-platform-packages.mjs index 9fdee89e..7b32fc3b 100755 --- a/scripts/npm/build-platform-packages.mjs +++ b/scripts/npm/build-platform-packages.mjs @@ -66,9 +66,12 @@ export const MATRIX = [ // The programs a payload carries. The daemon is not part of the npm package. const PROGRAMS = ['twigpine', 'git-remote-twigpine']; +// Thrown, not process.exit(): the `finally` blocks that remove extracted +// archives from the temporary directory still run on the way out. +class BuildFailure extends Error {} + function fail(message) { - console.error(`[build-platform-packages] ${message}`); - process.exit(1); + throw new BuildFailure(message); } function parseArgs(argv) { @@ -326,5 +329,11 @@ function main() { // Run when executed, not when imported. Both sides through realpath: macOS // reaches temp and checkout paths through symlinks. if (process.argv[1] && realpathSync(fileURLToPath(import.meta.url)) === realpathSync(process.argv[1])) { - main(); + try { + main(); + } catch (error) { + if (!(error instanceof BuildFailure)) throw error; + console.error(`[build-platform-packages] ${error.message}`); + process.exitCode = 1; + } } diff --git a/scripts/npm/test-npm-packages.sh b/scripts/npm/test-npm-packages.sh index c766757d..a3810f6f 100755 --- a/scripts/npm/test-npm-packages.sh +++ b/scripts/npm/test-npm-packages.sh @@ -156,6 +156,22 @@ if node "$BUILD" --version "$VERSION" --artifacts-dir "$W/art-nowin" --out-dir " ok "a rebuild without Windows removes the earlier Windows payload" else bad "a rebuild without Windows removes the earlier Windows payload"; fi +# A checksum-valid archive missing a program: the build fails and leaves no +# extracted archive behind in the temporary directory. +cp -R "$ART" "$W/art-noprog" +pkg="twigpine-node-$VERSION-x86_64-unknown-linux-musl" +rm -rf "$W/stage-noprog" && mkdir -p "$W/stage-noprog/$pkg" +fake_program "$W/stage-noprog/$pkg/twigpine" twigpine +COPYFILE_DISABLE=1 tar -czf "$W/art-noprog/$pkg.tar.gz" -C "$W/stage-noprog" "$pkg" +(cd "$W/art-noprog" && printf '%s %s\n' "$(sha "$pkg.tar.gz")" "$pkg.tar.gz" > "$pkg.tar.gz.sha256") +mkdir -p "$W/tmp-noprog" +if TMPDIR="$W/tmp-noprog" node "$BUILD" --version "$VERSION" --artifacts-dir "$W/art-noprog" \ + --out-dir "$W/npm-noprog" > "$W/out" 2>&1; then + bad "a payload missing a program fails the build and cleans up" +elif grep -q "does not contain git-remote-twigpine" "$W/out" && [ -z "$(ls -A "$W/tmp-noprog")" ]; then + ok "a payload missing a program fails the build and cleans up" +else bad "a payload missing a program fails the build and cleans up"; fi + # Called through a symlinked path (macOS temp and checkout paths often are). ln -s "$ROOT" "$W/linked-root" if node "$W/linked-root/scripts/npm/build-platform-packages.mjs" --version "$VERSION" \ @@ -192,10 +208,18 @@ check "the program's exit status comes through" test "$status" = 7 out="$(printf 'capabilities\n\n' | FAKE_STDIN=1 node "$INSTALL/bin/git-remote-twigpine.js" origin twigpine://did:key:z/r 2>&1)" check "the helper gets git's stdin and answers on stdout" \ test "$out" = "$(printf 'git-remote-twigpine origin twigpine://did:key:z/r\ncapabilities')" -# Through `sh -c ...; echo $?`, so the signal is observed as a status and the -# test's own shell does not report a killed job. -status="$(sh -c 'FAKE_SIGNAL=TERM node "$1" > /dev/null 2>&1; echo $?' _ "$INSTALL/bin/twigpine.js" 2>/dev/null)" -check "a program killed by a signal ends the launcher the same way" test "$status" = 143 +# How the launcher ended, as Node's spawnSync reports it ("status signal"), +# when the program kills itself with $1: a signal and an exit status of +# 128 + n look the same to a shell, not to a parent process. No core files. +launcher_end() { + (ulimit -c 0 2>/dev/null || true + FAKE_SIGNAL="$1" node -e 'const r = require("child_process").spawnSync(process.execPath, [process.argv[1]], { stdio: "ignore" }); console.log(`${r.status} ${r.signal}`);' "$INSTALL/bin/twigpine.js") +} +check "a program killed by a signal ends the launcher by that signal" test "$(launcher_end TERM)" = "null SIGTERM" +check "a program that crashes (SIGSEGV) ends the launcher by SIGSEGV" test "$(launcher_end SEGV)" = "null SIGSEGV" +check "a program that exits with 143 ends the launcher with 143, not a signal" \ + test "$(FAKE_EXIT=143 node -e 'const r = require("child_process").spawnSync(process.execPath, [process.argv[1]], { stdio: "ignore" }); console.log(`${r.status} ${r.signal}`);' "$INSTALL/bin/twigpine.js")" = "143 null" +# Through `sh -c ...; echo $?`, so the status is observed as a shell sees it. status="$(sh -c 'FAKE_SIGNAL=PIPE node "$1" > /dev/null 2>&1; echo $?' _ "$INSTALL/bin/twigpine.js" 2>/dev/null)" check "a program killed by SIGPIPE ends the launcher with 141, as a shell expects" test "$status" = 141