Files
get-shit-done/tests/bug-2649-sdk-fail-fast.test.cjs
Tom Boucher 8caa7d4c3a fix(#2649): installer fail-fast when sdk/dist missing in npx cache (#2667)
Root cause shared with #2647: a broken 1.38.3 tarball shipped without
sdk/dist/. The pre-#2441-decouple installer reacted by running
spawnSync('npm.cmd', ['install'], { cwd: sdkDir }) inside the npx cache
on Windows, where the cache is read-only, producing the misleading
"Failed to npm install in sdk/" error.

Defensive changes here (user-facing behavior only; packaging fix lives
in the sibling PR for #2647):

- Classify the install context (classifySdkInstall): detect npx cache
  paths, node_modules-based installs, and dev clones via path heuristics
  plus a side-effect-free write probe. Exported for test.
- Rewrite the dist-missing error to branch on context:
    tarball + npxCache -> "don't touch npx cache; npm i -g ...@latest"
    tarball (other)    -> upgrade path + clone-build escape hatch
    dev-clone          -> keep existing cd sdk && npm install && npm run build
- Preserve the invariant that the installer never shells out to
  npm install itself — users always drive that.
- Add tests/bug-2649-sdk-fail-fast.test.cjs covering the classifier and
  both failure messages, with spawnSync/execSync interceptors that
  assert no nested npm install is attempted.

Cross-ref: #2647 (packaging).

Fixes #2649

Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-24 18:05:04 -04:00

180 lines
6.2 KiB
JavaScript

/**
* Regression test for #2649 — installer must fail fast with a clear,
* actionable error when `sdk/dist/cli.js` is missing, and must NOT attempt
* a nested `npm install` inside the sdk directory (which, on Windows, lives
* in the read-only npx cache `%LOCALAPPDATA%\\npm-cache\\_npx\\<hash>\\...`).
*
* Shares a root cause with #2647 (packaging drops sdk/dist/). This test
* covers the installer's defensive behavior when that packaging bug — or
* any future regression that loses the prebuilt dist — reaches users.
*/
'use strict';
const { test, describe, before } = require('node:test');
const assert = require('node:assert/strict');
const fs = require('fs');
const os = require('os');
const path = require('path');
const INSTALL_PATH = path.join(__dirname, '..', 'bin', 'install.js');
function loadInstaller() {
process.env.GSD_TEST_MODE = '1';
delete require.cache[require.resolve(INSTALL_PATH)];
return require(INSTALL_PATH);
}
function makeTempSdk({ npxCache = false } = {}) {
let root;
if (npxCache) {
root = path.join(os.tmpdir(), `gsd-npx-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`, 'npm-cache', '_npx', 'deadbeefcafe0001', 'node_modules', 'get-shit-done-cc');
fs.mkdirSync(root, { recursive: true });
} else {
root = fs.mkdtempSync(path.join(os.tmpdir(), 'gsd-clone-'));
}
const sdkDir = path.join(root, 'sdk');
fs.mkdirSync(sdkDir, { recursive: true });
// Note: intentionally no sdk/dist/ directory.
return { root, sdkDir };
}
function cleanup(dir) {
try { fs.rmSync(dir, { recursive: true, force: true }); } catch {}
}
function runWithIntercepts(fn) {
const stderr = [];
const stdout = [];
const origErr = console.error;
const origLog = console.log;
console.error = (...a) => stderr.push(a.join(' '));
console.log = (...a) => stdout.push(a.join(' '));
const origExit = process.exit;
let exitCode = null;
process.exit = (code) => { exitCode = code; throw new Error('__EXIT__'); };
const cp = require('child_process');
const origSpawnSync = cp.spawnSync;
const origExecSync = cp.execSync;
const spawnCalls = [];
cp.spawnSync = (cmd, argv, opts) => {
spawnCalls.push({ cmd, argv, opts });
return { status: 0, stdout: Buffer.from(''), stderr: Buffer.from('') };
};
cp.execSync = (cmd, opts) => {
spawnCalls.push({ cmd, opts, via: 'execSync' });
return Buffer.from('');
};
try {
try { fn(); } catch (e) { if (e.message !== '__EXIT__') throw e; }
} finally {
console.error = origErr;
console.log = origLog;
process.exit = origExit;
cp.spawnSync = origSpawnSync;
cp.execSync = origExecSync;
}
return {
stderr: stderr.join('\n'),
stdout: stdout.join('\n'),
exitCode,
spawnCalls,
};
}
describe('installer SDK dist-missing fail-fast (#2649)', () => {
let installer;
before(() => { installer = loadInstaller(); });
test('exposes test hooks for SDK check', () => {
assert.ok(typeof installer.installSdkIfNeeded === 'function',
'installSdkIfNeeded must be exported in test mode');
assert.ok(typeof installer.classifySdkInstall === 'function',
'classifySdkInstall must be exported in test mode');
});
test('classifySdkInstall tags npx cache paths as tarball + npxCache', () => {
const { root, sdkDir } = makeTempSdk({ npxCache: true });
try {
const c = installer.classifySdkInstall(sdkDir);
assert.strictEqual(c.mode, 'tarball');
assert.strictEqual(c.npxCache, true);
assert.ok('readOnly' in c);
} finally {
cleanup(root);
}
});
test('classifySdkInstall tags plain git-clone dirs as dev-clone', () => {
const { root, sdkDir } = makeTempSdk({ npxCache: false });
try {
fs.mkdirSync(path.join(root, '.git'), { recursive: true });
const c = installer.classifySdkInstall(sdkDir);
assert.strictEqual(c.mode, 'dev-clone');
assert.strictEqual(c.npxCache, false);
} finally {
cleanup(root);
}
});
test('missing dist in npx cache: fail fast, no nested npm install', () => {
const { root, sdkDir } = makeTempSdk({ npxCache: true });
try {
const result = runWithIntercepts(() => {
installer.installSdkIfNeeded({ sdkDir });
});
assert.strictEqual(result.exitCode, 1, 'must exit non-zero');
// (a) actionable upgrade path in error output
assert.match(result.stderr, /npm i(nstall)? -g get-shit-done-cc@latest/,
'error must mention the global-install upgrade path');
assert.match(result.stderr, /sdk\/dist/,
'error must name the missing artifact');
// (b) no nested `npm install` / `npm.cmd install` inside sdkDir
const nestedInstall = result.spawnCalls.find((c) => {
const argv = Array.isArray(c.argv) ? c.argv : [];
const cwd = c.opts && c.opts.cwd;
const isNpm = /\bnpm(\.cmd)?$/i.test(String(c.cmd || ''));
const isInstall = argv.includes('install') || argv.includes('i');
const isInSdk = typeof cwd === 'string' && cwd.includes(sdkDir);
return isNpm && isInstall && isInSdk;
});
assert.strictEqual(nestedInstall, undefined,
'must NOT spawn `npm install` inside the npx-cache sdk dir');
} finally {
cleanup(root);
}
});
test('missing dist in a dev clone: fail fast with clone build hint', () => {
const { root, sdkDir } = makeTempSdk({ npxCache: false });
try {
fs.mkdirSync(path.join(root, '.git'), { recursive: true });
const result = runWithIntercepts(() => {
installer.installSdkIfNeeded({ sdkDir });
});
assert.strictEqual(result.exitCode, 1);
// Dev clone path: suggest the local build, not the global upgrade.
assert.match(result.stderr, /cd sdk && npm install && npm run build/,
'dev-clone error must keep the build-from-clone instructions');
const nestedInstall = result.spawnCalls.find((c) => {
const argv = Array.isArray(c.argv) ? c.argv : [];
const isNpm = /\bnpm(\.cmd)?$/i.test(String(c.cmd || ''));
const isInstall = argv.includes('install') || argv.includes('i');
return isNpm && isInstall;
});
assert.strictEqual(nestedInstall, undefined,
'installer itself must never shell out to `npm install`; the user does that');
} finally {
cleanup(root);
}
});
});