fix(install): require persistent SDK reachability before reporting ready (#3231) (#3249)

* test: reproduce false GSD SDK ready signals on Linux (#3231)

* fix(install): require persistent SDK reachability before reporting ready (#3231)

* changeset: pr=3249 for #3231

* fix(install): filter _npx from login-shell PATH probe (CR finding 1)

Apply filterNpxFromPath() to the getUserShellPath() result before passing
it to isGsdSdkOnPath(), mirroring the same filtering already applied to
process.env.PATH. Without this, a transient _npx entry in the login-shell
PATH can falsely satisfy the cross-shell reachability check and reintroduce
the false-ready condition this PR fixes.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* fix(test): unconditional legacy-shim replacement assertion (CR finding 2)

Replace readFileSync+includes source-grep check with isLegacyGsdSdkShim()
and add an else branch asserting that when sdkReady is false, a warning/error
was emitted. Previously the sdkReady===false path had no assertion at all,
allowing the test to pass without verifying any postcondition.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* test: replace text-grep assertions with structured ones (CR finding 2 + nitpick)

Finding 2: restructure the legacy-shim replacement assertion to branch on
isLegacyGsdSdkShim() state (a behavioral fact) rather than console output,
and add an unconditional postcondition for both branches.

Nitpick 3 (4 locations):
- lines 149-153: replace /GSD SDK ready/.test(combined) with
  isGsdSdkOnPath(filterNpxFromPath(PATH)) === false
- lines 167-169, 185-189: split filterNpxFromPath result into segments array
  and use array.includes() instead of string.includes() on the raw PATH string
- lines 375-377: replace /GSD SDK ready/.test(combined) with
  fs.existsSync(shimPath) + isGsdSdkOnPath(filterNpxFromPath(localBin))

All 8 tests pass. lint-no-source-grep: 0 violations.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* fix(build-hooks): per-PID staging dir eliminates concurrent-cleanup TOCTOU race

When multiple test before() hooks spawned build-hooks.js concurrently
(--test-concurrency=4), a race existed: Process A would finish all copies,
call rmdirSync('.dist-staging/') in cleanup, then Process B — still in its
copy loop — would call copyFileSync(src, '.dist-staging/hook.pid.ts') and
get ENOENT because the staging directory was gone.

On macOS/Linux, copyFileSync reports the SOURCE path in ENOENT errors when
the destination directory is missing, making the failure appear to be a
missing source file (hooks/gsd-statusline.js) rather than a missing
destination directory. This misled the diagnosis.

Fix: make STAGE_DIR per-PID ('.dist-staging-<pid>/') so each builder owns
its own staging directory. No other process touches it, eliminating all
contention on staging-dir creation and cleanup. Update .gitignore to match
the new 'hooks/.dist-staging-*/' glob.

Reproduces as: CI test matrix (macos-24, ubuntu-22, ubuntu-24) all failing
with ENOENT on hooks/gsd-statusline.js in bug-2136 before() hook. The new
test file added in this PR (bug-3231) shifts the concurrency schedule just
enough to expose the race on every CI run.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* test: assert on captured console output, not tautological PATH state (CR finding)

The two discarded `captureConsole()` return values in the bug-3231 test
were flagged by CodeRabbit as tautological assertions. Fix:

- Test 1 (transient _npx PATH): capture stdout/stderr and assert the
  installer does NOT emit "GSD SDK ready" (the false-positive the PR
  fixes), and that it does emit some diagnostic output instead.

- Test 3 (clean install): capture stdout/stderr and assert the installer
  DOES emit "GSD SDK ready" after successfully self-linking into a
  persistent PATH dir — confirming the positive path works correctly.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

---------

Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Tom Boucher
2026-05-08 09:39:33 -04:00
committed by GitHub
parent 75cc4fe660
commit c4d3fe62a5
5 changed files with 547 additions and 30 deletions

View File

@@ -9437,8 +9437,14 @@ function installSdkIfNeeded(opts) {
// strictly weaker invariant than the one workflows depend on
// (`command -v gsd-sdk` resolving), and led to a false ✓ in npx-cache
// installs (issue #2775).
//
// #3231: strip transient npx-injected PATH segments before checking. The
// installer subprocess PATH includes `~/.npm/_npx/<hash>/node_modules/.bin`
// which is ephemeral — it is NOT reachable from the user's interactive
// shell. A gsd-sdk found there must NOT count as "on PATH".
const shimSrc = path.resolve(__dirname, 'gsd-sdk.js');
let onPath = isGsdSdkOnPath();
const persistentPath = filterNpxFromPath(process.env.PATH || '');
let onPath = isGsdSdkOnPath(persistentPath);
// Track WHERE we wrote the shim so the diagnostic can be specific even
// when isGsdSdkOnPath() returns false because the write target isn't on
@@ -9455,7 +9461,7 @@ function installSdkIfNeeded(opts) {
const linked = trySelfLinkGsdSdk(shimSrc);
if (linked) {
shimDir = path.dirname(linked);
onPath = isGsdSdkOnPath();
onPath = isGsdSdkOnPath(persistentPath);
if (onPath) {
console.log(` ${dim}↪ linked gsd-sdk → ${linked}${reset}`);
}
@@ -9470,13 +9476,23 @@ function installSdkIfNeeded(opts) {
// require the shim to be reachable there too before claiming ✓.
// POSIX-only probe; on Windows getUserShellPath() returns null and
// we trust the existing check (Windows-specific fix is separate).
//
// #3231: when getUserShellPath() returns null (e.g. $SHELL unset on
// Linux, rc-file timeout), we cannot confirm persistent reachability.
// In that case, do NOT preserve a true onPath — require the initial
// check (on persistentPath) to have found the shim in a persistent
// location. Since we already filtered npx dirs above, onPath=true here
// means a non-transient dir has the shim, which is sufficient.
const userShellPath = getUserShellPath();
if (onPath && userShellPath !== null) {
const userSees = isGsdSdkOnPath(userShellPath);
const persistentUserShellPath = filterNpxFromPath(userShellPath);
const userSees = isGsdSdkOnPath(persistentUserShellPath);
if (!userSees) {
onPath = false;
}
}
// If userShellPath is null (POSIX probe failed), onPath reflects
// the persistent-PATH check — that is the best available invariant.
if (onPath) {
console.log(` ${green}✓${reset} GSD SDK ready (sdk/dist/cli.js)`);
@@ -9517,6 +9533,74 @@ function installSdkIfNeeded(opts) {
}
}
/**
* #3231 helper: detect whether a `gsd-sdk` binary is the legacy deprecated
* shim pointing at `gsd-tools.cjs`.
*
* Reads the first 512 bytes of the file and looks for the `@deprecated`
* marker alongside a `gsd-tools.cjs` reference — the fingerprint that
* distinguishes the old binary from the modern SDK. Treats any I/O error
* (missing file, EACCES) as "not legacy" so callers do not need to guard.
*
* This is intentionally a plain-text sniff of the file header, not a
* semantic parse — the marker is a stable, human-authored string that we
* own. Returns false conservatively (prefer false positives to false
* negatives: a non-legacy binary reported as legacy triggers a harmless
* replacement; a legacy binary reported as non-legacy would keep the broken
* shim in place).
*/
function isLegacyGsdSdkShim(filePath) {
const fs = require('fs');
try {
const fd = fs.openSync(filePath, 'r');
let header;
try {
const buf = Buffer.alloc(512);
const bytesRead = fs.readSync(fd, buf, 0, 512, 0);
header = buf.slice(0, bytesRead).toString('utf8');
} finally {
try { fs.closeSync(fd); } catch {}
}
// The legacy binary contains "@deprecated" AND "gsd-tools.cjs" within
// its first 512 bytes.
return header.includes('@deprecated') && header.includes('gsd-tools.cjs');
} catch {
return false;
}
}
/**
* #3231 helper: strip transient npx-injected PATH segments.
*
* npm/npx injects `~/.npm/_npx/<hash>/node_modules/.bin` (and equivalents)
* into the installer subprocess PATH. Those directories are ephemeral — they
* exist only for the duration of the `npx` run — and MUST NOT be treated as
* evidence that `gsd-sdk` is durably reachable.
*
* Strips any segment whose absolute form contains `/_npx/` or `\\_npx\\`
* as a proper path-component boundary. A user-named directory that merely
* contains the substring "npx" (e.g. `/home/user/my-npx-scripts/bin`) is
* preserved: we require the boundary characters (`/` or `\`) on both sides.
*
* Returns the filtered PATH string (may be empty if all segments were npx).
*/
function filterNpxFromPath(pathString) {
const path = require('path');
const input = typeof pathString === 'string' ? pathString : (process.env.PATH || '');
return input
.split(path.delimiter)
.filter((seg) => {
if (!seg) return false;
// Normalize to forward-slash form for the pattern check so both
// POSIX and Windows paths match a single expression. The sep-anchored
// pattern avoids matching "my-npx-scripts" etc.
const norm = seg.replace(/\\/g, '/');
// Must have /_npx/ as a real path component, not just a substring.
return !norm.includes('/_npx/');
})
.join(path.delimiter);
}
/**
* #2775 helper: check whether a callable `gsd-sdk` exists on a PATH.
*
@@ -9531,6 +9615,10 @@ function installSdkIfNeeded(opts) {
* shims). Callers can pass the user-shell PATH from getUserShellPath() to
* verify the shim is reachable from the runtime shell, not just the
* install context. Zero-arg form preserves existing behavior.
*
* #3231: a candidate that passes the file/exec check is further tested via
* isLegacyGsdSdkShim — a symlink pointing at the deprecated gsd-tools.cjs
* binary must NOT be treated as "on PATH" even if it is executable.
*/
function isGsdSdkOnPath(pathString) {
const path = require('path');
@@ -9547,8 +9635,15 @@ function isGsdSdkOnPath(pathString) {
try {
const st = fs.statSync(candidate);
if (st.isFile()) {
if (process.platform === 'win32') return true;
if ((st.mode & 0o111) !== 0) return true;
if (process.platform === 'win32') {
if (!isLegacyGsdSdkShim(candidate)) return true;
} else if ((st.mode & 0o111) !== 0) {
// #3231: resolve symlink before sniffing, so we detect legacy
// through any level of indirection.
let target = candidate;
try { target = fs.realpathSync(candidate); } catch {}
if (!isLegacyGsdSdkShim(target)) return true;
}
}
} catch {
// missing / EACCES on dir — keep scanning.
@@ -10040,6 +10135,8 @@ if (process.env.GSD_TEST_MODE) {
trySelfLinkGsdSdkWindows,
buildWindowsShimTriple,
formatSdkPathDiagnostic,
filterNpxFromPath,
isLegacyGsdSdkShim,
isGsdSdkOnPath,
getUserShellPath,
homePathCoveredByRc,