Skip to content

Commit c47051a

Browse files
cliffhallclaude
andcommitted
Treat npm outdated's exit 1 without a payload as a failure; compare epochs in PEP 440 prefix matching
Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: cliffhall <[email protected]>
1 parent fbfea0c commit c47051a

4 files changed

Lines changed: 56 additions & 6 deletions

File tree

‎scripts/dependency-refresh.mjs‎

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -470,12 +470,34 @@ function runOutdated(root, spawn) {
470470
// 0 means nothing is outdated and 1 means something is; both are successful
471471
// runs. Anything else is a real failure that also prints nothing to stdout,
472472
// so accepting it would report a clean sweep over an outage.
473+
const stdout = result.stdout ?? "";
473474
if (result.status !== 0 && result.status !== 1) {
474475
throw new Error(
475476
`npm outdated failed (exit ${result.status}): ${(result.stderr ?? "").trim()}`,
476477
);
477478
}
478-
return result.stdout ?? "";
479+
// Exit 1 is overloaded: npm also exits 1 on some command and registry
480+
// failures. "Outdated" always comes with a payload naming what is, so an
481+
// exit 1 without one, or with npm's `{"error": …}`, is a failure too.
482+
if (result.status === 1) {
483+
let payload;
484+
try {
485+
payload = JSON.parse(stdout);
486+
} catch {
487+
payload = null;
488+
}
489+
if (
490+
payload === null ||
491+
typeof payload !== "object" ||
492+
"error" in payload ||
493+
Object.keys(payload).length === 0
494+
) {
495+
throw new Error(
496+
`npm outdated exited 1 with no outdated payload: ${(result.stderr ?? "").trim() || stdout.trim()}`,
497+
);
498+
}
499+
}
500+
return stdout;
479501
}
480502

481503
function runUv(dir, args, spawn) {

‎scripts/dependency-refresh.test.mjs‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -607,6 +607,23 @@ test("main fails, writing nothing, when a lookup fails", (t) => {
607607
{ outdated: { status: 2, stderr: "E404 registry" } },
608608
/npm outdated failed/,
609609
],
610+
[
611+
{ outdated: { status: 1, stdout: "", stderr: "ENOTFOUND" } },
612+
/exited 1 with no outdated payload: ENOTFOUND/,
613+
],
614+
[
615+
{ outdated: { status: 1, stdout: "{}" } },
616+
/exited 1 with no outdated payload/,
617+
],
618+
[
619+
{
620+
outdated: {
621+
status: 1,
622+
stdout: JSON.stringify({ error: { code: "E503" } }),
623+
},
624+
},
625+
/exited 1 with no outdated payload/,
626+
],
610627
[
611628
{ uvLock: { status: 2, stderr: "no network" } },
612629
/uv lock --upgrade --dry-run failed/,

‎scripts/lib/pep440.mjs‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,12 @@ const CLAUSE_RE = /^(~=|===|==|!=|<=|>=|<|>|=)\s*(\S+)$/;
104104

105105
/** `==1.2.*` style prefix match: the release starts with the prefix. */
106106
function prefixMatches(version, prefix) {
107-
const v = parseVersion(version).release;
108-
const p = parseVersion(prefix).release;
109-
return p.every((part, i) => (v[i] ?? 0) === part);
107+
const v = parseVersion(version);
108+
const p = parseVersion(prefix);
109+
return (
110+
v.epoch === p.epoch &&
111+
p.release.every((part, i) => (v.release[i] ?? 0) === part)
112+
);
110113
}
111114

112115
/**
@@ -128,13 +131,13 @@ function clauseMatches(version, clause) {
128131
return op === "!=" ? !equal : equal;
129132
}
130133
if (op === "~=") {
131-
const release = parseVersion(operand).release;
134+
const { epoch, release } = parseVersion(operand);
132135
if (release.length < 2) {
133136
throw new Error(`~= needs at least two release segments: "${clause}"`);
134137
}
135138
return (
136139
compareVersions(version, operand) >= 0 &&
137-
prefixMatches(version, release.slice(0, -1).join("."))
140+
prefixMatches(version, `${epoch}!${release.slice(0, -1).join(".")}`)
138141
);
139142
}
140143
const d = compareVersions(version, operand);

‎scripts/lib/pep440.test.mjs‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,14 @@ test("exclusive bounds follow PEP 440's pre- and post-release rules", () => {
9393
assert.equal(satisfies("1.0.post1", ">=1.0"), true);
9494
});
9595

96+
test("prefix matching compares epochs too", () => {
97+
assert.equal(satisfies("2!1.4.5", "~=1!1.4"), false);
98+
assert.equal(satisfies("1!1.4.5", "~=1!1.4"), true);
99+
assert.equal(satisfies("2!1.4.5", "==1!1.4.*"), false);
100+
assert.equal(satisfies("1!1.4.5", "==1!1.4.*"), true);
101+
assert.equal(satisfies("1!1.4.5", "==1.4.*"), false);
102+
});
103+
96104
test("satisfies throws instead of guessing", () => {
97105
assert.throws(() => satisfies("1.0", "about 1.0"), /specifier clause/);
98106
assert.throws(() => satisfies("1.0", "~=1"), /two release segments/);

0 commit comments

Comments
 (0)