Skip to content

Commit 4af75ec

Browse files
committed
fix: derive LINUX_PKG_PATTERN from LIBC_BY_SUFFIX keys (#1172)
Greptile flagged that the unknown-suffix error message told developers to extend LIBC_BY_SUFFIX, but the regex (gnu|musl) capture group is the actual gatekeeper — adding only a map entry would leave the package permanently unmatched. Derive LINUX_PKG_PATTERN from Object.keys(LIBC_BY_SUFFIX) so the regex and map can never drift apart, and update the error to reflect that adding a map entry is now genuinely the only required change. Also switch the script path interpolation to use fileURLToPath(import.meta.url) instead of string-replacing the file:// scheme, per Claude's review note.
1 parent e3701bb commit 4af75ec

1 file changed

Lines changed: 14 additions & 4 deletions

File tree

scripts/verify-lockfile-libc.mjs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,24 @@ import { readFileSync } from 'node:fs';
1818
import { dirname, resolve } from 'node:path';
1919
import { fileURLToPath } from 'node:url';
2020

21+
// Map of napi-rs name suffix → expected `libc` value in package-lock.json.
22+
// Adding a new entry here is the ONLY edit required to teach the verifier
23+
// about a new libc variant — `LINUX_PKG_PATTERN` is derived from these keys
24+
// below so the regex and the map can never drift apart.
2125
const LIBC_BY_SUFFIX = {
2226
gnu: 'glibc',
2327
musl: 'musl',
2428
};
25-
const LINUX_PKG_PATTERN = /^@optave\/codegraph-linux-[^-]+-(gnu|musl)$/;
29+
const LIBC_SUFFIXES = Object.keys(LIBC_BY_SUFFIX);
30+
const LINUX_PKG_PATTERN = new RegExp(
31+
`^@optave/codegraph-linux-[^-]+-(${LIBC_SUFFIXES.join('|')})$`,
32+
);
2633

2734
// Resolve relative to this script's location so it works regardless of CWD
2835
// (e.g. running `node scripts/verify-lockfile-libc.mjs` from the `scripts/`
2936
// subdirectory still finds the repo-root manifests).
30-
const __dirname = dirname(fileURLToPath(import.meta.url));
37+
const __filename = fileURLToPath(import.meta.url);
38+
const __dirname = dirname(__filename);
3139
const repoRoot = resolve(__dirname, '..');
3240
const pkg = JSON.parse(readFileSync(resolve(repoRoot, 'package.json'), 'utf8'));
3341
const lock = JSON.parse(readFileSync(resolve(repoRoot, 'package-lock.json'), 'utf8'));
@@ -66,13 +74,15 @@ for (const pkgName of linuxPackages) {
6674
}
6775

6876
if (unknownSuffixes.length > 0) {
77+
const knownRule = LIBC_SUFFIXES.map((s) => `-${s}${LIBC_BY_SUFFIX[s]}`).join(', ');
6978
console.error(
7079
'package-lock.json libc discriminator check cannot infer expected libc for:\n',
7180
);
7281
for (const name of unknownSuffixes) console.error(` - ${name}`);
7382
console.error(
74-
`\nExtend LIBC_BY_SUFFIX in ${import.meta.url.replace('file://', '')}\n` +
75-
'to cover the new suffix (current rule: -gnu → glibc, -musl → musl).',
83+
`\nAdd the new suffix to LIBC_BY_SUFFIX in ${__filename}\n` +
84+
'(LINUX_PKG_PATTERN is derived from its keys, so no separate regex update\n' +
85+
`is needed). Current rule: ${knownRule}.`,
7686
);
7787
process.exit(1);
7888
}

0 commit comments

Comments
 (0)