Public: anyone can read this; the crew can change it.

A lockfile reader lists every dependency a repository pins, from bun.lock and package-lock.json #17064

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

Adds src/maintain/lockfiles.ts. lockfileDependencies(path, text) reads a lockfile's text and returns { dependencies: [{ecosystem: "npm", name, version}], reason? }, with each package listed once, sorted by name then version.

Notes

opened this

Adds src/maintain/lockfiles.ts. lockfileDependencies(path, text) reads a lockfile's text and returns { dependencies: [{ecosystem: "npm", name, version}], reason? }, with each package listed once, sorted by name then version. It never throws: a malformed file gives an empty list and a reason that names the problem, such as bun.lock does not parse or package-lock.json node_modules/a has no version. A file name it doesn't know gets a reason too, rather than a guess.

  • bun.lock is parsed with Bun.JSONC.parse, the same way src/checks/fillable.ts does, so trailing commas are fine. Each name and version comes from the id at the start of an entry, so an alias is listed under its real package name.
  • package-lock.json handles versions 2 and 3 through packages, taking the name from the install path or from name for an alias. Version 1 is read through nested dependencies, including npm:real@x aliases.
  • The repository's own code is left out: the root package, workspace:, link: and file: entries, and link: true entries.

What to look at:

  • Two allow-list entries. Nothing in src/ calls the module yet, so it is added to scripts/unused-exports-allow.json and scripts/unreached-allow.json. The caller will be #17047, which matches pinned dependencies against OSV. The second list had only two entries before this, so please confirm allowing the module until #17047 is acceptable.
  • No existing helper to reuse. bun scripts/similar.ts found nothing like this under src/. The only reuse is the JSONC parsing from fillable.ts.
  • Tests: test/lockfiles.test.ts covers small fixtures of bun.lock (with trailing commas, a workspace member, an alias and a duplicate), package-lock v3 and v1. It also covers malformed files, an unknown lockfile name and an empty lockfile. No network.

Not done: yarn.lock, pnpm-lock.yaml, uv.lock and Cargo.lock; each comes back with the "not a lockfile this reader knows" reason. There is no size cap here, since reading files and limiting their size is the caller's job.

Checks: the gate passes all 32 rules, bun x tsc --noEmit is clean, and the lockfiles, unreached and unused-exports tests pass. The first push went out before the full named test run finished, because my command didn't stop when tests failed. That run had two failures: the unreached check, fixed in the second commit, and test/scripts-fast.test.ts "a clean tree and a docs-only diff exit 3…". That second test also fails when run alone here, by hitting its 5 s timeout. It runs scripts/fast.ts in a temporary repository and this change doesn't touch it, so it looks like sandbox slowness, but I didn't confirm that on main. The forge's check will settle it.

🤖 Generated with Claude Code

Closes #17046

commented

Adds src/maintain/lockfiles.ts. lockfileDependencies(path, text) reads a lockfile's text and returns { dependencies: [{ecosystem: "npm", name, version}], reason? }, with each package listed once, sorted by name then version. It never throws: a malformed file gives an empty list and a reason that names the problem, such as bun.lock does not parse or package-lock.json node_modules/a has no version. A file name it doesn't know gets a reason too, rather than a guess.

  • bun.lock is parsed with Bun.JSONC.parse, the same way src/checks/fillable.ts does, so trailing commas are fine. Each name and version comes from the id at the start of an entry, so an alias is listed under its real package name.
  • package-lock.json handles versions 2 and 3 through packages, taking the name from the install path or from name for an alias. Version 1 is read through nested dependencies, including npm:real@x aliases.
  • The repository's own code is left out: the root package, workspace:, link: and file: entries, and link: true entries.

What to look at:

  • Two allow-list entries. Nothing in src/ calls the module yet, so it is added to scripts/unused-exports-allow.json and scripts/unreached-allow.json. The caller will be #17047, which matches pinned dependencies against OSV. The second list had only two entries before this, so please confirm allowing the module until #17047 is acceptable.
  • No existing helper to reuse. bun scripts/similar.ts found nothing like this under src/. The only reuse is the JSONC parsing from fillable.ts.
  • Tests: test/lockfiles.test.ts covers small fixtures of bun.lock (with trailing commas, a workspace member, an alias and a duplicate), package-lock v3 and v1. It also covers malformed files, an unknown lockfile name and an empty lockfile. No network.

Not done: yarn.lock, pnpm-lock.yaml, uv.lock and Cargo.lock; each comes back with the "not a lockfile this reader knows" reason. There is no size cap here, since reading files and limiting their size is the caller's job.

Checks: the gate passes all 32 rules, bun x tsc --noEmit is clean, and the lockfiles, unreached and unused-exports tests pass. The first push went out before the full named test run finished, because my command didn't stop when tests failed. That run had two failures: the unreached check, fixed in the second commit, and test/scripts-fast.test.ts "a clean tree and a docs-only diff exit 3…". That second test also fails when run alone here, by hitting its 5 s timeout. It runs scripts/fast.ts in a temporary repository and this change doesn't touch it, so it looks like sandbox slowness, but I didn't confirm that on main. The forge's check will settle it.

🤖 Generated with Claude Code

commented

This adds src/maintain/lockfiles.ts. lockfileDependencies(path, text) returns every package a bun.lock or package-lock.json pins, as {ecosystem: "npm", name, version}, with duplicates removed and sorted, ready for the OSV lookup in #17047. It reads package-lock.json versions 1, 2 and 3. A file it cannot read gives an empty list and a reason that names the problem; it never throws.

Changes since review:

  • Deep nesting no longer throws. Version 1's nested dependencies used to be read by a function calling itself once per level, so a deeply nested file raised RangeError. It is now read in a plain loop. The error message keeps only the last six levels of the path (dependencies.….a.dependencies (100000 levels deep) is not an object), so a hostile file can't make it huge. lockfileDependencies also catches any error as a backstop.
  • Only versions from the npm registry are listed. A version must look like 1.2.3 (pre-release and build tags allowed). That drops github:, git+ssh:, https://…tgz, tags, workspace:, link: and file:. This applies to bun.lock ids, version 1 versions, and the real package behind an npm: alias. Versions 2 and 3 write a normal version number for a git install, so an entry is also dropped when its resolved field points at git or a local file.

What to look at: walkV1 and where in src/maintain/lockfiles.ts, and the three new tests in test/lockfiles.test.ts:

  • a version 1 file nested 100,000 levels deep, both well-formed and broken (the broken one threw before this change);
  • an error inside a nested package, to check the reported path;
  • git, GitHub and URL installs in bun.lock and in versions 1 and 3.

Nothing calls the reader yet, so it is on both allow-lists until #17047 uses it.

To try it: bun test test/lockfiles.test.ts. bun x tsc --noEmit and the gate pass, as do the dead-code, unreached and test-honesty suites (101 tests in 12 files). I didn't run the full suite; the forge's check runs it on the push.

🤖 Generated with Claude Code

Sign in to join this conversation.

Search
↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs