Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions changes/fix-family-install-retry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
### Fixed

- **`cli-dev audit --family` installs again when the registry drops the connection.** npm's `--fetch-retries` does
not cover a download cut off midway (`read ECONNRESET`), and about half the family installs failed that way. A
network error now tries the install up to three times; any other error, such as a package npm does not have,
fails at once as before.
52 changes: 51 additions & 1 deletion src/dev/audit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { AUDIT_RULES, PARITY_GROUPS, type ParityGroup, REQUIRED_ROOTS } from "..
import { listEnvelopeSchema } from "../standards/output.js"
import { VOCABULARY } from "../standards/vocabulary.js"
import { type AuditInput, type AuditOptions, auditClis, scorecardTable, snakeKeys } from "./audit.js"
import { familyPackages } from "./family.js"
import { familyPackages, installFamily } from "./family.js"
import { type DevIo, main } from "./main.js"

const READS = { remote: "read", local: "none" } as const
Expand Down Expand Up @@ -786,6 +786,56 @@ describe("audit --family", () => {
])
})

it("installs again when the registry drops the connection, and not when a package is missing", () => {
const answer = (status: number, stderr: string) => ({
pid: 1,
output: [],
stdout: "",
stderr,
status,
signal: null,
})
const packages = new Map([["tg", "@wirecat/tg-cli"]])
const dropped = [answer(1, "npm error code ECONNRESET\nnpm error network read ECONNRESET"), answer(0, "")]
const tries: string[][] = []
const family = installFamily(
packages,
(args) => {
tries.push(args)
return dropped.shift() ?? answer(0, "")
},
0,
)
family.close()
expect(tries).toHaveLength(2)

let missing = 0
expect(() =>
installFamily(
packages,
() => {
missing++
return answer(1, "npm error code E404")
},
0,
),
).toThrow(/E404/)
expect(missing).toBe(1)

let down = 0
expect(() =>
installFamily(
packages,
() => {
down++
return answer(1, "npm error network read ECONNRESET")
},
0,
),
).toThrow(/ECONNRESET/)
expect(down).toBe(3)
})

it("takes exactly one --cli, this CLI's own", async () => {
const err: string[] = []
const io: DevIo = { cwd: tmpdir(), out: () => {}, err: (text) => err.push(text) }
Expand Down
51 changes: 32 additions & 19 deletions src/dev/family.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { SpawnSyncReturns } from "node:child_process"
import { mkdtempSync, rmSync } from "node:fs"
import { tmpdir } from "node:os"
import { join } from "node:path"
Expand Down Expand Up @@ -31,28 +32,40 @@ export interface Family {
close: () => void
}

// `--fetch-retries` covers a refused request, not a connection the registry drops mid-download: about half the
// installs of eight CLIs failed that way on 2026-11-19 and 2026-11-21 (`read ECONNRESET`).
const NETWORK = /^npm error network |\b(ECONNRESET|ETIMEDOUT|EAI_AGAIN|ECONNREFUSED|EPIPE|ENETUNREACH)\b/m
const ATTEMPTS = 3

/** Installs the latest published `packages` into a folder of their own, without their install scripts. */
export const installFamily = (packages: ReadonlyMap<string, string>): Family => {
export const installFamily = (
packages: ReadonlyMap<string, string>,
install: (args: string[]) => SpawnSyncReturns<string> = (args) =>
spawn.sync("npm", args, { encoding: "utf8", maxBuffer: 16 * 1024 * 1024 }),
wait = 3000,
): Family => {
const prefix = mkdtempSync(join(tmpdir(), "cli-dev-family-"))
const close = () => rmSync(prefix, { recursive: true, force: true })
const run = spawn.sync(
"npm",
[
"install",
"--global",
"--prefix",
prefix,
"--ignore-scripts",
"--no-audit",
"--no-fund",
"--loglevel",
"error",
"--fetch-retries",
"6",
...packages.values(),
],
{ encoding: "utf8", maxBuffer: 16 * 1024 * 1024 },
)
const args = [
"install",
"--global",
"--prefix",
prefix,
"--ignore-scripts",
"--no-audit",
"--no-fund",
"--loglevel",
"error",
"--fetch-retries",
"6",
...packages.values(),
]
let run = install(args)
for (let attempt = 1; attempt < ATTEMPTS && run.status !== 0 && NETWORK.test(String(run.stderr)); attempt++) {
// A connection the registry dropped under load is often dropped again at once.
Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, wait)
run = install(args)
}
if (run.error || run.status !== 0) {
close()
throw new Error(`npm install of the family failed: ${run.error?.message ?? String(run.stderr).trim()}`)
Expand Down