Skip to content

The agent that grows with you

fix(update): resolve npm's manifest through symlinks and fall back to the probe

liuhao1024646c3c8ad58c
diff --git a/scripts/build/node-deps.mjs b/scripts/build/node-deps.mjs
index fca2fee394f..edab59ec067 100644
--- a/scripts/build/node-deps.mjs
+++ b/scripts/build/node-deps.mjs
@@ -34,6 +34,20 @@ export function npmCommand({ env = process.env } = {}) {
return [process.execPath, cli]
}
+// npm's own manifest states its version — no child spawn. A node-under-node spawn
+// hits Windows Job-Object EBUSY (#123933), and the probe runs before the reuse
+// short-circuit: a read-only version read must not abort an otherwise complete run.
+// npm_execpath may point through a symlink; the manifest sits beside the resolved
+// CLI, never beside the link. Layouts without a readable manifest return undefined
+// so the caller falls back to the child probe, matching the pre-manifest behavior.
+function npmManifestVersion(cli) {
+ try {
+ return JSON.parse(readFileSync(join(dirname(realpathSync(cli)), '..', 'package.json'), 'utf8')).version
+ } catch {
+ return undefined
+ }
+}
+
function completedInstallMatches({ source, receipt, hiddenLock, key, nativeKey }) {
if (!existsSync(receipt) || !existsSync(hiddenLock)) return false
const installed = readFileSync(hiddenLock)
@@ -88,11 +102,8 @@ export function prepareNodeDependencies({ source, workspaces, env = process.env,
return path
}))].sort()
const [node, npm] = npmCommand({ env })
- // npm's own manifest states its version — no child spawn. A node-under-node spawn
- // hits Windows Job-Object EBUSY (#123933), and this probe runs before the reuse
- // short-circuit: a read-only version read must not abort an otherwise complete run.
- const npmVersion = JSON.parse(
- readFileSync(join(dirname(npm), '..', 'package.json'), 'utf8')).version
+ const npmVersion = npmManifestVersion(npm) ?? execFileSync(
+ node, [npm, '--version'], { cwd: source, env, encoding: 'utf8' }).trim()
const { satisfies } = createRequire(npm)('semver')
for (const [name, version] of [['node', process.versions.node], ['npm', npmVersion]]) {
const range = manifest.engines?.[name]
diff --git a/tests-js/node-deps.test.mjs b/tests-js/node-deps.test.mjs
index 83c7fec8970..01f5514cca5 100644
--- a/tests-js/node-deps.test.mjs
+++ b/tests-js/node-deps.test.mjs
@@ -231,3 +231,56 @@ test('any other npm ci failure keeps node_modules and does not retry', async ()
expect(readFileSync(join(source, 'ci-runs'), 'utf8')).toBe('ci\n')
expect(existsSync(join(source, 'node_modules/.bin/stuck'))).toBe(true)
}, 30000)
+
+// A fake npm whose version probes die but whose ci succeeds: the Job-Object
+// failure lane from #123933. The version must come from the manifest without
+// a single child spawn, and npm_execpath resolving through a symlink (whose
+// parent holds no manifest) must still find it beside the resolved CLI.
+function fakeNpmTree(source, { manifest }) {
+ const fake = mkdtempSync(join(tmpdir(), 'npm layout with spaces-fake-'))
+ roots.push(fake)
+ const npmRoot = join(fake, 'real/node_modules/npm')
+ mkdirSync(join(npmRoot, 'bin'), { recursive: true })
+ if (manifest) json(join(npmRoot, 'package.json'), { name: 'npm', version: manifest })
+ json(join(fake, 'real/node_modules/semver/package.json'), { name: 'semver', version: '1.0.0', main: 'index.js' })
+ writeFileSync(join(fake, 'real/node_modules/semver/index.js'), 'module.exports = { satisfies: () => true }\n')
+ const spawned = join(source, 'spawned.jsonl')
+ writeFileSync(join(npmRoot, 'bin/npm-cli.js'), [
+ `const { appendFileSync } = require('node:fs')`,
+ `appendFileSync(${JSON.stringify(spawned)}, process.argv[2] + '\\n')`,
+ `if (process.argv[2] === '--version') { console.log('10.8.2'); process.exit(${manifest ? 3 : 0}) }`,
+ ].join('\n'))
+ return { fake, npmRoot, spawned }
+}
+
+test.skipIf(process.platform === 'win32')('the npm version reads from the manifest without a child spawn, through a symlinked npm_execpath', async () => {
+ const { prepareNodeDependencies } = await import('../scripts/build/node-deps.mjs')
+ const source = fixture()
+ const { fake, npmRoot, spawned } = fakeNpmTree(source, { manifest: '10.8.2' })
+ // The symlink sits outside npm's package directory: its parent holds no
+ // manifest, while the resolved CLI's parent does. semver stays resolvable
+ // from the link, as with a real prefix bin symlink.
+ mkdirSync(join(fake, 'real/bin'), { recursive: true })
+ symlinkSync(join(npmRoot, 'bin/npm-cli.js'), join(fake, 'real/bin/npm-cli.js'))
+ mkdirSync(join(source, 'node_modules'))
+ writeFileSync(join(source, 'node_modules/.package-lock.json'), '{"packages": {}}')
+ const options = { source, workspaces: ['web'], reuse: true,
+ env: { ...process.env, npm_config_cache: join(source, '.npm-cache'), npm_execpath: join(fake, 'real/bin/npm-cli.js') } }
+ prepareNodeDependencies(options)
+ prepareNodeDependencies(options)
+ // Both runs reached ci or the reuse short-circuit; no version probe spawned.
+ expect(readFileSync(spawned, 'utf8')).toBe('ci\n')
+}, 30000)
+
+test('an npm layout without a readable manifest falls back to the child version probe', async () => {
+ const { prepareNodeDependencies } = await import('../scripts/build/node-deps.mjs')
+ const source = fixture()
+ const { fake, spawned } = fakeNpmTree(source, { manifest: null })
+ mkdirSync(join(source, 'node_modules'))
+ writeFileSync(join(source, 'node_modules/.package-lock.json'), '{"packages": {}}')
+ const options = { source, workspaces: ['web'], reuse: true,
+ env: { ...process.env, npm_config_cache: join(source, '.npm-cache'), npm_execpath: join(fake, 'real/node_modules/npm/bin/npm-cli.js') } }
+ prepareNodeDependencies(options)
+ // The unresolvable manifest kept the pre-fix behavior: probe first, then ci.
+ expect(readFileSync(spawned, 'utf8')).toBe('--version\nci\n')
+}, 30000)
diff --git a/tests/hermes_cli/test_source_build.py b/tests/hermes_cli/test_source_build.py
index c6c2405013f..a623f1ad561 100644
--- a/tests/hermes_cli/test_source_build.py
+++ b/tests/hermes_cli/test_source_build.py
@@ -243,49 +243,6 @@ def test_preparation_reuses_only_the_exact_completed_workspace_union(source_chec
assert len(_events(root)) == 2
-def test_version_probe_cannot_abort_completed_dependency_preparation(source_checkout, tmp_path):
- """A read-only npm version probe must not abort node-deps (#123933): spawning
- node-under-node hits Windows Job-Object EBUSY even when the dependencies are
- already complete, so the version is read from npm's own manifest and the reuse
- short-circuit survives in a spawn lane that fails every version probe."""
- root, _ = source_checkout
- fake = tmp_path / "job-object-npm"
- (fake / "node_modules/npm/bin").mkdir(parents=True)
- (fake / "node_modules/semver").mkdir(parents=True)
- (fake / "npm").write_text("#!/bin/sh\nexit 0\n", encoding="utf-8")
- (fake / "npm").chmod(0o755)
- # Stand-in for the Job-Object failure lane: version probes die, ci succeeds.
- (fake / "node_modules/npm/bin/npm-cli.js").write_text(
- "import { appendFileSync } from 'node:fs'\n"
- "appendFileSync('spawned.jsonl', process.argv[2] + '\\n')\n"
- "process.exit(process.argv[2] === '--version' ? 3 : 0)\n", encoding="utf-8")
- (fake / "node_modules/npm/package.json").write_text(
- json.dumps({"name": "npm", "version": "9.9.9"}), encoding="utf-8")
- (fake / "node_modules/semver/package.json").write_text(
- json.dumps({"name": "semver", "version": "1.0.0", "main": "index.js"}), encoding="utf-8")
- (fake / "node_modules/semver/index.js").write_text(
- "module.exports = { satisfies: () => true }\n", encoding="utf-8")
- # This checkout's install already completed; the fake ci lane leaves it in place.
- (root / "node_modules").mkdir(exist_ok=True)
- (root / "node_modules/.package-lock.json").write_text('{"packages": {}}', encoding="utf-8")
- env = {**os.environ, "PATH": os.pathsep.join([str(fake), os.environ["PATH"]]),
- "npm_execpath": ""}
- script = root / "scripts/build/node-deps.mjs"
- install = subprocess.run([shutil.which("node"), str(script), "--source", str(root),
- "--reuse", "--workspace", "web"], cwd=root, env=env,
- capture_output=True, text=True)
- assert install.returncode == 0, install.stderr
- assert "installing workspace dependencies" in install.stdout
- reuse = subprocess.run([shutil.which("node"), str(script), "--source", str(root),
- "--reuse", "--workspace", "web"], cwd=root, env=env,
- capture_output=True, text=True)
- assert reuse.returncode == 0, reuse.stderr
- assert "reusing completed install" in reuse.stdout
- # The reused run spawned nothing at all — not even a version probe.
- assert (root / "spawned.jsonl").read_text(encoding="utf-8") == "ci\n"
-
-
@pytest.mark.platforms("linux")
@pytest.mark.parametrize("desktop", [False, True])
def test_update_builds_selected_products_after_one_union_preparation(source_products, desktop):