cve-lite-cli 1.40.0 → 1.41.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (68) hide show
  1. package/README.md +17 -11
  2. package/dist/advisory/local-advisory-source.js +9 -5
  3. package/dist/advisory/local-db.js +91 -13
  4. package/dist/advisory/osv-sync.js +187 -56
  5. package/dist/advisory/staging-directory.js +112 -0
  6. package/dist/cli/advisories-sync.js +16 -2
  7. package/dist/cli/args.js +46 -35
  8. package/dist/cli/config.js +4 -1
  9. package/dist/cli/help.js +4 -3
  10. package/dist/cli/validate.js +23 -1
  11. package/dist/constants.js +11 -25
  12. package/dist/index.js +8 -3
  13. package/dist/output/cyclonedx.js +23 -14
  14. package/dist/output/debug.js +68 -18
  15. package/dist/output/formatters.js +33 -5
  16. package/dist/output/html-reporter.js +55 -5
  17. package/dist/output/multi-folder-html-reporter.js +59 -3
  18. package/dist/output/output-dir.js +193 -7
  19. package/dist/output/printers.js +16 -5
  20. package/dist/output/sarif.js +131 -48
  21. package/dist/output/sbom-dependency-edges.js +16 -10
  22. package/dist/output/sbom-file.js +14 -6
  23. package/dist/output/scan-json.js +21 -3
  24. package/dist/output/spdx.js +61 -23
  25. package/dist/output/spinner.js +2 -1
  26. package/dist/output/write-outputs.js +142 -52
  27. package/dist/overrides/context-builder.js +23 -70
  28. package/dist/parsers/bun-lock.js +1 -2
  29. package/dist/parsers/index.js +22 -1
  30. package/dist/parsers/npm-lock-graph.js +39 -1
  31. package/dist/parsers/package-lock.js +25 -5
  32. package/dist/parsers/pnpm-lock.js +3 -2
  33. package/dist/parsers/utils.js +5 -3
  34. package/dist/parsers/yarn-lock-graph.js +5 -2
  35. package/dist/parsers/yarn-lock.js +1 -2
  36. package/dist/remediation/fix-commands.js +105 -44
  37. package/dist/remediation/npm-transitive-graph.js +30 -72
  38. package/dist/remediation/npm-transitive-resolution.js +24 -77
  39. package/dist/remediation/parent-upgrade-blockers.js +51 -0
  40. package/dist/remediation/parent-upgrade.js +5 -79
  41. package/dist/remediation/registry-parent-ranges.js +21 -0
  42. package/dist/remediation/transitive-chain-resolver.js +3 -3
  43. package/dist/scan/completeness.js +10 -4
  44. package/dist/scan/multi-folder-scan.js +133 -58
  45. package/dist/scan/single-scan.js +41 -25
  46. package/dist/scanner.js +257 -58
  47. package/dist/usage/scanner.js +19 -8
  48. package/dist/utils/array.js +11 -3
  49. package/dist/utils/debug-bundle.js +118 -0
  50. package/dist/utils/debug-project.js +102 -0
  51. package/dist/utils/debug-redaction.js +117 -0
  52. package/dist/utils/file.js +2 -1
  53. package/dist/utils/fix-runner.js +95 -13
  54. package/dist/utils/import-type-queries.js +322 -0
  55. package/dist/utils/network.js +13 -9
  56. package/dist/utils/npm-lockfile.js +12 -0
  57. package/dist/utils/osv-fetch.js +70 -0
  58. package/dist/utils/package-identity.js +43 -0
  59. package/dist/utils/package-manager-source.js +24 -0
  60. package/dist/utils/purl.js +37 -0
  61. package/dist/utils/registry-config.js +1 -16
  62. package/dist/utils/string.js +8 -0
  63. package/dist/utils/validate-url.js +1 -1
  64. package/dist/utils/version.js +30 -7
  65. package/dist/utils/versions/osv-range.js +191 -0
  66. package/dist/utils/versions/pep440.js +246 -0
  67. package/dist/utils/versions/scheme.js +10 -0
  68. package/package.json +3 -2
@@ -1,13 +1,14 @@
1
1
  import fs from "node:fs";
2
2
  import path from "node:path";
3
+ import { randomUUID } from "node:crypto";
3
4
  import { chalk } from "../utils/chalk.js";
4
5
  import { writeSarifReport, deriveLockfileUri } from "./sarif.js";
5
6
  import { writeCycloneDxReport } from "./cyclonedx.js";
6
7
  import { writeSpdxReport } from "./spdx.js";
7
- import { resolveDependencyEdges } from "./sbom-dependency-edges.js";
8
+ import { resolveDependencyEdgesResult } from "./sbom-dependency-edges.js";
8
9
  import { resolveSbomFormat } from "../utils/sbom-format.js";
9
10
  import { buildScanJson } from "./scan-json.js";
10
- import { resolveOutputDir, displayOutputPath } from "./output-dir.js";
11
+ import { resolveOutputTarget, displayOutputPath, checkFormatExtensionMismatch, drainStdout } from "./output-dir.js";
11
12
  import { getCliVersion } from "../utils/version-info.js";
12
13
  function readProjectMeta(projectPath) {
13
14
  try {
@@ -27,61 +28,150 @@ function readProjectMeta(projectPath) {
27
28
  return null;
28
29
  }
29
30
  }
30
- export async function writeOutputs(options, scanState, scanInput, projectPath) {
31
- const outputDir = resolveOutputDir(options.output);
32
- if (options.json) {
33
- const ts = new Date().toISOString().replace(/[:.]/g, "-").slice(0, 19);
34
- const jsonFilename = `cve-lite-scan-${ts}.json`;
35
- const jsonOutputPath = path.join(outputDir, jsonFilename);
36
- const payload = buildScanJson({
37
- projectPath,
38
- scanInput,
39
- registry: scanState.registry,
40
- findings: scanState.sorted,
41
- acceptedFindings: scanState.acceptedFindings,
42
- suggestedFixCommands: scanState.suggestedFixCommands,
43
- coverage: scanState.coverage,
44
- completeness: scanState.completeness,
45
- overrideFindings: scanState.overrideFindings,
46
- // Only passed when override checking ran, so `[]` means "ran, nothing skipped" and
47
- // absent means "never audited". An unconditional `[]` collapses those two.
48
- overrideSkipped: scanState.overrideSkipped,
49
- maintenanceFindings: scanState.maintenanceFindings,
50
- licenseFindings: scanState.licenseFindings,
51
- licenseLimitedToDirectOnly: scanState.licenseLimitedToDirectOnly,
52
- // Saved-artifact provenance for the --json payload (#1296).
53
- provenance: { cliVersion: getCliVersion(), options },
54
- });
55
- fs.writeFileSync(jsonOutputPath, JSON.stringify(payload, null, 2));
56
- console.log(`${chalk.gray("JSON saved to")} ${chalk.cyan(displayOutputPath(jsonOutputPath))}`);
31
+ function stageOrWrite(outputDir, customFilename, logLabel, writeFn, stagedOutputs, note, isStdout) {
32
+ if (isStdout) {
33
+ writeFn();
34
+ return;
57
35
  }
58
- if (options.sarif) {
59
- const lockfileUri = deriveLockfileUri(scanInput);
60
- const sarifFilename = writeSarifReport(scanState.sorted, lockfileUri, scanState.suggestedFixCommands, {
61
- acceptedFindings: scanState.acceptedFindings,
62
- overrideFindings: scanState.overrideFindings,
63
- maintenanceFindings: scanState.maintenanceFindings,
64
- licenseFindings: scanState.licenseFindings,
65
- overrideSkipped: scanState.overrideSkipped,
66
- outputDir,
36
+ if (customFilename) {
37
+ const tempFilename = `.${customFilename}.${randomUUID()}.tmp`;
38
+ const tempPath = path.join(outputDir, tempFilename);
39
+ const destinationPath = path.join(outputDir, customFilename);
40
+ stagedOutputs.push({
41
+ tempPath,
42
+ destinationPath,
43
+ logMessage: `${chalk.gray(logLabel)} ${chalk.cyan(displayOutputPath(destinationPath))}`,
44
+ note,
67
45
  });
68
- console.log(`${chalk.gray("SARIF report written to")} ${chalk.cyan(sarifFilename)}`);
46
+ writeFn(tempFilename);
47
+ }
48
+ else {
49
+ const writtenPath = writeFn();
50
+ if (writtenPath) {
51
+ console.log(`${chalk.gray(logLabel)} ${chalk.cyan(writtenPath)}`);
52
+ }
53
+ if (note) {
54
+ console.log(note);
55
+ }
69
56
  }
57
+ }
58
+ function warnOnFormatMismatch(customFilename, format) {
59
+ const warning = checkFormatExtensionMismatch(customFilename, format);
60
+ if (warning) {
61
+ console.warn(chalk.yellow(`Warning: ${warning}`));
62
+ }
63
+ }
64
+ export const SPDX_NOASSERTION_LOCKFILE_NOTE = "Note: Package licenses are marked NOASSERTION (not determined) because pnpm, Yarn and Bun lockfiles do not record licenses.";
65
+ function stageJsonOutput(options, scanState, scanInput, projectPath, outputDir, customFilename, stagedOutputs, isStdout) {
66
+ if (!options.json)
67
+ return undefined;
68
+ warnOnFormatMismatch(customFilename, "json");
69
+ const ts = new Date().toISOString().replace(/[:.]/g, "-").slice(0, 19);
70
+ const jsonFilename = customFilename ?? `cve-lite-scan-${ts}.json`;
71
+ const jsonOutputPath = isStdout ? undefined : path.join(outputDir, jsonFilename);
72
+ const payload = buildScanJson({
73
+ projectPath,
74
+ scanInput,
75
+ registry: scanState.registry,
76
+ findings: scanState.sorted,
77
+ acceptedFindings: scanState.acceptedFindings,
78
+ suggestedFixCommands: scanState.suggestedFixCommands,
79
+ coverage: scanState.coverage,
80
+ completeness: scanState.completeness,
81
+ overrideFindings: scanState.overrideFindings,
82
+ // Only passed when override checking ran, so `[]` means "ran, nothing skipped" and
83
+ // absent means "never audited". An unconditional `[]` collapses those two.
84
+ overrideSkipped: scanState.overrideSkipped,
85
+ maintenanceFindings: scanState.maintenanceFindings,
86
+ licenseFindings: scanState.licenseFindings,
87
+ licenseLimitedToDirectOnly: scanState.licenseLimitedToDirectOnly,
88
+ // Saved-artifact provenance for the --json payload (#1296).
89
+ provenance: { cliVersion: getCliVersion(), options },
90
+ });
91
+ const jsonContent = JSON.stringify(payload, null, 2);
92
+ stageOrWrite(outputDir, customFilename, "JSON saved to", (tempFilename) => {
93
+ if (isStdout) {
94
+ process.stdout.write(jsonContent + "\n");
95
+ return undefined;
96
+ }
97
+ const targetPath = tempFilename ? path.join(outputDir, tempFilename) : jsonOutputPath;
98
+ fs.writeFileSync(targetPath, jsonContent);
99
+ return displayOutputPath(jsonOutputPath);
100
+ }, stagedOutputs, undefined, isStdout);
101
+ return jsonOutputPath;
102
+ }
103
+ function stageSbomOutput(options, scanState, scanInput, projectPath, outputDir, customFilename, stagedOutputs, isStdout) {
70
104
  const sbomFormat = resolveSbomFormat(options);
71
- if (sbomFormat) {
72
- const projectMeta = readProjectMeta(projectPath);
73
- if (sbomFormat === "spdx") {
74
- const spdxFilename = writeSpdxReport(scanState.allPackages, scanState.sorted, scanState.suggestedFixCommands, projectMeta, {
75
- inventoryOnly: options.sbomInventoryOnly,
76
- // Resolved from the lockfile graph where available. Empty for pnpm,
77
- // Yarn and Bun, which falls back to path-derived edges.
78
- dependencyEdges: resolveDependencyEdges(scanInput, scanState.allPackages),
79
- }, outputDir);
80
- console.log(`${chalk.gray("SPDX SBOM written to")} ${chalk.cyan(spdxFilename)}`);
105
+ if (!sbomFormat)
106
+ return;
107
+ warnOnFormatMismatch(customFilename, sbomFormat);
108
+ const projectMeta = readProjectMeta(projectPath);
109
+ if (sbomFormat === "spdx") {
110
+ const { edges: dependencyEdges, graphLoaded } = resolveDependencyEdgesResult(scanInput, scanState.allPackages);
111
+ const spdxNote = scanInput.source === "pnpm-lock" ||
112
+ scanInput.source === "yarn-lock" ||
113
+ scanInput.source === "bun-lock"
114
+ ? chalk.gray(SPDX_NOASSERTION_LOCKFILE_NOTE)
115
+ : undefined;
116
+ stageOrWrite(outputDir, customFilename, "SPDX SBOM written to", (tempFilename) => writeSpdxReport(scanState.allPackages, scanState.sorted, scanState.suggestedFixCommands, projectMeta, {
117
+ inventoryOnly: options.sbomInventoryOnly,
118
+ dependencyEdges,
119
+ graphFailed: !graphLoaded,
120
+ ...(tempFilename ? { filename: tempFilename } : isStdout ? { filename: "-" } : {}),
121
+ }, outputDir), stagedOutputs, spdxNote, isStdout);
122
+ }
123
+ else {
124
+ stageOrWrite(outputDir, customFilename, "CycloneDX BOM written to", (tempFilename) => writeCycloneDxReport(scanState.allPackages, scanState.sorted, scanState.suggestedFixCommands, projectMeta, outputDir, tempFilename ?? (isStdout ? "-" : undefined)), stagedOutputs, undefined, isStdout);
125
+ }
126
+ }
127
+ function stageSarifOutput(options, scanState, scanInput, outputDir, customFilename, stagedOutputs, isStdout) {
128
+ if (!options.sarif)
129
+ return;
130
+ warnOnFormatMismatch(customFilename, "sarif");
131
+ const lockfileUri = deriveLockfileUri(scanInput);
132
+ stageOrWrite(outputDir, customFilename, "SARIF report written to", (tempFilename) => writeSarifReport(scanState.sorted, lockfileUri, scanState.suggestedFixCommands, {
133
+ acceptedFindings: scanState.acceptedFindings,
134
+ overrideFindings: scanState.overrideFindings,
135
+ maintenanceFindings: scanState.maintenanceFindings,
136
+ licenseFindings: scanState.licenseFindings,
137
+ overrideSkipped: scanState.overrideSkipped,
138
+ completeness: scanState.completeness,
139
+ outputDir,
140
+ ...(tempFilename ? { filename: tempFilename } : isStdout ? { filename: "-" } : {}),
141
+ }), stagedOutputs, undefined, isStdout);
142
+ }
143
+ export async function writeOutputs(options, scanState, scanInput, projectPath) {
144
+ const target = resolveOutputTarget(options.output);
145
+ const isStdout = target.isStdout === true;
146
+ const outputDir = target.dir;
147
+ const customFilename = target.isFile ? target.filename : undefined;
148
+ let jsonOutputPath;
149
+ const stagedOutputs = [];
150
+ try {
151
+ jsonOutputPath = stageJsonOutput(options, scanState, scanInput, projectPath, outputDir, customFilename, stagedOutputs, isStdout);
152
+ stageSarifOutput(options, scanState, scanInput, outputDir, customFilename, stagedOutputs, isStdout);
153
+ stageSbomOutput(options, scanState, scanInput, projectPath, outputDir, customFilename, stagedOutputs, isStdout);
154
+ for (const item of stagedOutputs) {
155
+ fs.renameSync(item.tempPath, item.destinationPath);
156
+ console.log(item.logMessage);
157
+ if (item.note) {
158
+ console.log(item.note);
159
+ }
81
160
  }
82
- else {
83
- const cdxFilename = writeCycloneDxReport(scanState.allPackages, scanState.sorted, scanState.suggestedFixCommands, projectMeta, outputDir);
84
- console.log(`${chalk.gray("CycloneDX BOM written to")} ${chalk.cyan(cdxFilename)}`);
161
+ if (isStdout) {
162
+ await drainStdout();
163
+ }
164
+ }
165
+ catch (err) {
166
+ for (const item of stagedOutputs) {
167
+ try {
168
+ fs.rmSync(item.tempPath, { force: true });
169
+ }
170
+ catch {
171
+ /* best-effort cleanup of temp file */
172
+ }
85
173
  }
174
+ throw err;
86
175
  }
176
+ return jsonOutputPath;
87
177
  }
@@ -1,11 +1,8 @@
1
1
  import { existsSync, readFileSync } from "node:fs";
2
- import { join } from "node:path";
2
+ import { basename, join } from "node:path";
3
3
  import { extractOverrideEntries } from "./parsing/package-json.js";
4
4
  import { walkInstalledTree } from "./parsing/installed-tree.js";
5
- import { loadFromPackageLock } from "../parsers/package-lock.js";
6
- import { loadFromPnpmLock } from "../parsers/pnpm-lock.js";
7
- import { loadFromYarnLock } from "../parsers/yarn-lock.js";
8
- import { loadFromBunLock } from "../parsers/bun-lock.js";
5
+ import { readSelectedLockfile, selectRootLockfile } from "../parsers/index.js";
9
6
  import { scanAllImports, usageScanTruncationWarning } from "../usage/scanner.js";
10
7
  import { readWorkspaceMemberManifests } from "../utils/package-json.js";
11
8
  import { isNodeBuiltinSpecifier } from "../utils/node-builtins.js";
@@ -18,8 +15,11 @@ export function buildOverrideContext(projectPath, opts) {
18
15
  const raw = readFileSync(pkgJsonPath, "utf8");
19
16
  const parsed = JSON.parse(raw);
20
17
  const overrideEntries = extractOverrideEntries(parsed);
21
- const packageManager = detectPackageManager(projectPath);
22
- const readResult = readLockfileNames(projectPath, packageManager);
18
+ // The same root lockfile the scan reads, so the override rules see the tree
19
+ // the findings came from (#1497).
20
+ const lockfile = selectRootLockfile(projectPath);
21
+ const packageManager = lockfile ? PACKAGE_MANAGER_BY_LOCKFILE[lockfile.name] : "unknown";
22
+ const readResult = readLockfileNames(lockfile);
23
23
  const lockfilePackageNames = readResult.kind === "ok" ? readResult.names : new Set();
24
24
  const lockfileVersions = readResult.kind === "ok" ? readResult.versions : new Map();
25
25
  const nodeModulesExists = existsSync(join(projectPath, "node_modules"));
@@ -104,17 +104,13 @@ export function buildOverrideContext(projectPath, opts) {
104
104
  logger: opts.logger,
105
105
  };
106
106
  }
107
- function detectPackageManager(projectPath) {
108
- if (existsSync(join(projectPath, "pnpm-lock.yaml")))
109
- return "pnpm";
110
- if (existsSync(join(projectPath, "yarn.lock")))
111
- return "yarn";
112
- if (existsSync(join(projectPath, "bun.lock")))
113
- return "bun";
114
- if (existsSync(join(projectPath, "package-lock.json")))
115
- return "npm";
116
- return "unknown";
117
- }
107
+ const PACKAGE_MANAGER_BY_LOCKFILE = {
108
+ "package-lock.json": "npm",
109
+ "npm-shrinkwrap.json": "npm",
110
+ "pnpm-lock.yaml": "pnpm",
111
+ "yarn.lock": "yarn",
112
+ "bun.lock": "bun",
113
+ };
118
114
  /**
119
115
  * Groups resolved lockfile versions by package name. A package can appear at
120
116
  * several versions in one tree, so every one is kept: an override selector only
@@ -136,60 +132,17 @@ function versionsByName(refs) {
136
132
  }
137
133
  return byName;
138
134
  }
139
- function readLockfileNames(projectPath, pm) {
140
- if (pm === "npm") {
141
- const lockPath = join(projectPath, "package-lock.json");
142
- if (!existsSync(lockPath))
143
- return { kind: "absent" };
144
- try {
145
- const refs = loadFromPackageLock(lockPath, false);
146
- return { kind: "ok", names: new Set(refs.map((r) => r.name)), versions: versionsByName(refs) };
147
- }
148
- catch (e) {
149
- const message = e instanceof Error ? e.message : String(e);
150
- return { kind: "error", message: `package-lock.json: ${message}` };
151
- }
152
- }
153
- if (pm === "pnpm") {
154
- const lockPath = join(projectPath, "pnpm-lock.yaml");
155
- if (!existsSync(lockPath))
156
- return { kind: "absent" };
157
- try {
158
- const refs = loadFromPnpmLock(lockPath, false);
159
- return { kind: "ok", names: new Set(refs.map((r) => r.name)), versions: versionsByName(refs) };
160
- }
161
- catch (e) {
162
- const message = e instanceof Error ? e.message : String(e);
163
- return { kind: "error", message: `pnpm-lock.yaml: ${message}` };
164
- }
165
- }
166
- if (pm === "yarn") {
167
- const lockPath = join(projectPath, "yarn.lock");
168
- if (!existsSync(lockPath))
169
- return { kind: "absent" };
170
- try {
171
- const refs = loadFromYarnLock(lockPath);
172
- return { kind: "ok", names: new Set(refs.map((r) => r.name)), versions: versionsByName(refs) };
173
- }
174
- catch (e) {
175
- const message = e instanceof Error ? e.message : String(e);
176
- return { kind: "error", message: `yarn.lock: ${message}` };
177
- }
135
+ function readLockfileNames(lockfile) {
136
+ if (lockfile === null)
137
+ return { kind: "absent" };
138
+ try {
139
+ const refs = readSelectedLockfile(lockfile);
140
+ return { kind: "ok", names: new Set(refs.map((r) => r.name)), versions: versionsByName(refs) };
178
141
  }
179
- if (pm === "bun") {
180
- const lockPath = join(projectPath, "bun.lock");
181
- if (!existsSync(lockPath))
182
- return { kind: "absent" };
183
- try {
184
- const refs = loadFromBunLock(lockPath, false);
185
- return { kind: "ok", names: new Set(refs.map((r) => r.name)), versions: versionsByName(refs) };
186
- }
187
- catch (e) {
188
- const message = e instanceof Error ? e.message : String(e);
189
- return { kind: "error", message: `bun.lock: ${message}` };
190
- }
142
+ catch (e) {
143
+ const message = e instanceof Error ? e.message : String(e);
144
+ return { kind: "error", message: `${basename(lockfile.filePath)}: ${message}` };
191
145
  }
192
- return { kind: "absent" };
193
146
  }
194
147
  function seedRootDeclarations(pkg, parentDeclarations) {
195
148
  const rootName = typeof pkg.name === "string" ? pkg.name : "<root>";
@@ -1,7 +1,6 @@
1
1
  import { uniquePathArrays } from "../utils/array.js";
2
- import { upsertPackage } from "./utils.js";
2
+ import { upsertPackage, MAX_PATHS_PER_PACKAGE } from "./utils.js";
3
3
  import { buildBunChildKeyGraph, buildBunKeyIndex, collectReachableKeys, readAndParseBunLock, resolveBunChildKey, } from "./bun-lock-graph.js";
4
- const MAX_PATHS_PER_PACKAGE = 5;
5
4
  const MAX_PATH_DEPTH = 10;
6
5
  const DEPENDENCY_SECTIONS = ["dependencies", "optionalDependencies", "devDependencies"];
7
6
  /**
@@ -90,7 +90,13 @@ function findRootLockfile(projectRoot) {
90
90
  if (present.length === 0)
91
91
  return null;
92
92
  const declared = readDeclaredPackageManager(projectRoot);
93
- const declaredLockfile = declared ? PACKAGE_MANAGER_LOCKFILES[declared] : undefined;
93
+ // npm treats npm-shrinkwrap.json as authoritative over package-lock.json.
94
+ // Keep that rule even when package.json declares npm, while preserving the
95
+ // packageManager override for other ecosystems (for example, pnpm beside a
96
+ // stale package-lock.json).
97
+ const declaredLockfile = declared === "npm" && present.includes("npm-shrinkwrap.json")
98
+ ? "npm-shrinkwrap.json"
99
+ : declared ? PACKAGE_MANAGER_LOCKFILES[declared] : undefined;
94
100
  const chosen = declaredLockfile && present.includes(declaredLockfile) ? declaredLockfile : present[0];
95
101
  return {
96
102
  filePath: path.join(projectRoot, chosen),
@@ -132,6 +138,21 @@ function buildLockfileScan(filePath, name, prodOnly, label, extraNotes) {
132
138
  skippedDependencies: [],
133
139
  };
134
140
  }
141
+ /**
142
+ * The lockfile a scan of `projectRoot` reads at that folder's root, by the same
143
+ * rules `loadPackages` uses. Anything that has to agree with the scan about
144
+ * which tree it is looking at, such as the override rules, takes its lockfile
145
+ * from here rather than choosing one by its own order (#1497). A nested
146
+ * lockfile is deliberately not returned: it belongs to a different manifest.
147
+ */
148
+ export function selectRootLockfile(projectRoot) {
149
+ const root = findRootLockfile(projectRoot);
150
+ return root === null ? null : { filePath: root.filePath, name: root.name };
151
+ }
152
+ /** Reads every resolved package from a selected lockfile, dev dependencies included. */
153
+ export function readSelectedLockfile(lockfile) {
154
+ return LOCKFILE_HANDLERS[lockfile.name].load(lockfile.filePath, false);
155
+ }
135
156
  export function loadPackages(projectRoot, prodOnly, maxDepth) {
136
157
  const rootLockfile = findRootLockfile(projectRoot);
137
158
  if (rootLockfile !== null) {
@@ -1,5 +1,6 @@
1
1
  import fs from "node:fs";
2
2
  import { unique } from "../utils/array.js";
3
+ import { npmAliasRegistryName } from "../utils/package-identity.js";
3
4
  export function loadNpmLockGraph(filePath, options) {
4
5
  const raw = JSON.parse(fs.readFileSync(filePath, "utf8"));
5
6
  const packages = raw?.packages;
@@ -67,7 +68,8 @@ export function loadNpmLockGraph(filePath, options) {
67
68
  workspaceEntryNodeIds.push(id);
68
69
  }
69
70
  const rootMeta = isRecord(rawPackages[""]) ? rawPackages[""] : null;
70
- const entryPackages = resolveEdgesForParent("", collectRootDependencyRanges(rootMeta), [""], nodeIdByPackagePath, childNodeIdsByParentNodeId, parentNodeIdsByChildNodeId, rangeByParentNodeId);
71
+ const rootRanges = collectRootDependencyRanges(rootMeta);
72
+ const entryPackages = resolveEdgesForParent("", rootRanges, [""], nodeIdByPackagePath, childNodeIdsByParentNodeId, parentNodeIdsByChildNodeId, rangeByParentNodeId);
71
73
  for (const node of nodesById.values()) {
72
74
  resolveEdgesForParent(node.packagePath, dependencyRangesByNodeId.get(node.id) ?? {}, resolutionBasePathsByNodeId.get(node.id) ?? [node.packagePath], nodeIdByPackagePath, childNodeIdsByParentNodeId, parentNodeIdsByChildNodeId, rangeByParentNodeId);
73
75
  }
@@ -82,6 +84,21 @@ export function loadNpmLockGraph(filePath, options) {
82
84
  childNodeIdsByParentNodeId,
83
85
  rangeByParentNodeId,
84
86
  });
87
+ // A different manifest name also occurs for git/tarball installs. Only an
88
+ // npm: declaration establishes a registry alias for the resolved graph slot.
89
+ const rootEntryNodeIds = new Set(entryPackages);
90
+ for (const node of nodesById.values()) {
91
+ const declaredRanges = [
92
+ rootEntryNodeIds.has(node.id) ? rootRanges[node.name] : undefined,
93
+ ...[...(parentNodeIdsByChildNodeId.get(node.id) ?? [])]
94
+ .map(parentId => rangeByParentNodeId.get(parentId)?.get(node.name)),
95
+ ];
96
+ const registryName = declaredRanges.map(npmAliasRegistryName)
97
+ .find(name => name && name !== node.name);
98
+ if (registryName) {
99
+ node.registryName = registryName;
100
+ }
101
+ }
85
102
  if (options?.includePaths !== false) {
86
103
  const MAX_PATHS_PER_NODE = 5;
87
104
  const MAX_PATH_DEPTH = 10;
@@ -119,6 +136,7 @@ export function loadNpmLockGraph(filePath, options) {
119
136
  childNodeIdsByParentNodeId: setsToArrays(childNodeIdsByParentNodeId),
120
137
  rangeByParentNodeId,
121
138
  pathSetByNodeId,
139
+ peerRangesByChildNodeId: collectPeerRangesByChild(nodesById, peerRangesByNodeId, resolutionBasePathsByNodeId, nodeIdByPackagePath),
122
140
  });
123
141
  }
124
142
  function createGraph(args) {
@@ -165,8 +183,28 @@ function createGraph(args) {
165
183
  return [...(args.pathSetByNodeId.get(nodeId) ?? new Set())]
166
184
  .map((item) => item.split(">"));
167
185
  },
186
+ peerRangesFor(nodeId) {
187
+ return args.peerRangesByChildNodeId?.get(nodeId) ?? EMPTY_ARRAY;
188
+ },
168
189
  };
169
190
  }
191
+ // npm refuses an install whose version breaks a peer requirement, so every
192
+ // peer range on an installed copy limits what it can be moved to, whether or
193
+ // not the peer fallback below turned that requirement into an edge.
194
+ function collectPeerRangesByChild(nodesById, peerRangesByNodeId, resolutionBasePathsByNodeId, nodeIdByPackagePath) {
195
+ const rangesByChild = new Map();
196
+ for (const [nodeId, peerRanges] of peerRangesByNodeId) {
197
+ const basePaths = resolutionBasePathsByNodeId.get(nodeId) ?? [nodesById.get(nodeId)?.packagePath ?? nodeId];
198
+ for (const [peerName, range] of Object.entries(peerRanges)) {
199
+ const childPackagePath = resolveDependencyPackagePath(basePaths, peerName, nodeIdByPackagePath);
200
+ const childNodeId = childPackagePath ? nodeIdByPackagePath.get(childPackagePath) : undefined;
201
+ if (!childNodeId)
202
+ continue;
203
+ rangesByChild.set(childNodeId, [...(rangesByChild.get(childNodeId) ?? []), range]);
204
+ }
205
+ }
206
+ return rangesByChild;
207
+ }
170
208
  function resolveEdgesForParent(parentPackagePath, dependencyRanges, resolutionBasePaths, nodeIdByPackagePath, childNodeIdsByParentNodeId, parentNodeIdsByChildNodeId, rangeByParentNodeId) {
171
209
  const resolvedChildNodeIds = [];
172
210
  for (const [dependencyName, range] of Object.entries(dependencyRanges)) {
@@ -1,7 +1,8 @@
1
1
  import fs from "node:fs";
2
2
  import { upsertPackage, normalizeNodeModulesPath } from "./utils.js";
3
3
  import { loadNpmLockGraph } from "./npm-lock-graph.js";
4
- export function buildNpmWorkspaceMap(filePath) {
4
+ import { npmAliasRegistryName, packageNameKey, parseNpmAliasVersion, registryPackageName } from "../utils/package-identity.js";
5
+ export function buildNpmWorkspaceMap(filePath, options) {
5
6
  const raw = JSON.parse(fs.readFileSync(filePath, "utf8"));
6
7
  const map = new Map();
7
8
  if (!raw.packages || typeof raw.packages !== "object")
@@ -19,6 +20,14 @@ export function buildNpmWorkspaceMap(filePath) {
19
20
  if (!existing.includes(workspacePath)) {
20
21
  map.set(depName, [...existing, workspacePath]);
21
22
  }
23
+ const registryName = options?.includeAliasIdentities ? npmAliasRegistryName(depSection[depName]) : null;
24
+ if (registryName) {
25
+ const key = packageNameKey({ name: depName, registryName });
26
+ const members = map.get(key) ?? [];
27
+ if (!members.includes(workspacePath)) {
28
+ map.set(key, [...members, workspacePath]);
29
+ }
30
+ }
22
31
  }
23
32
  }
24
33
  }
@@ -50,12 +59,19 @@ export function loadFromPackageLock(filePath, prodOnly) {
50
59
  continue;
51
60
  if (prodOnly && dev)
52
61
  continue;
53
- const nodeIds = graph.nodeIdsFor(name, version);
62
+ const registryName = graph.getNode(pkgPath)?.registryName;
63
+ const nodeIds = graph.nodeIdsFor(name, version).filter(id => {
64
+ const node = graph.getNode(id);
65
+ return node && registryPackageName(node) === registryPackageName({ name, registryName });
66
+ });
54
67
  const graphPaths = nodeIds.flatMap(id => graph.pathsFor(id));
55
68
  const paths = graphPaths.length > 0 ? graphPaths : [normalizeNodeModulesPath(pkgPath)];
56
69
  const resolvedUrl = meta?.resolved;
57
70
  const license = readLockfileLicense(meta?.license);
58
- upsertPackage(map, { name, version, ecosystem: "npm", dev, paths, resolvedUrl, license });
71
+ upsertPackage(map, {
72
+ name, version, ecosystem: "npm", dev, paths, resolvedUrl, license,
73
+ ...(registryName ? { registryName } : {}),
74
+ });
59
75
  }
60
76
  }
61
77
  if (map.size === 0 && raw.dependencies && typeof raw.dependencies === "object") {
@@ -65,11 +81,15 @@ export function loadFromPackageLock(filePath, prodOnly) {
65
81
  }
66
82
  function walkLegacyDeps(deps, map, prodOnly, currentPath) {
67
83
  for (const [name, meta] of Object.entries(deps)) {
68
- const version = meta?.version;
84
+ const alias = parseNpmAliasVersion(meta?.version);
85
+ const version = alias?.version ?? meta?.version;
69
86
  const dev = !!meta?.dev;
70
87
  const nextPath = [...currentPath, name];
71
88
  if (name && version && !(prodOnly && dev)) {
72
- upsertPackage(map, { name, version, ecosystem: "npm", dev, paths: [nextPath] });
89
+ upsertPackage(map, {
90
+ name, version, ecosystem: "npm", dev, paths: [nextPath],
91
+ ...(alias ? { registryName: alias.registryName } : {}),
92
+ });
73
93
  }
74
94
  if (meta?.dependencies) {
75
95
  walkLegacyDeps(meta.dependencies, map, prodOnly, nextPath);
@@ -1,9 +1,8 @@
1
1
  import fs from "node:fs";
2
2
  import YAML, { YAMLParseError } from "yaml";
3
3
  import { looksLikeVersion, normalizeRawVersion } from "../utils/version.js";
4
- import { upsertPackage } from "./utils.js";
4
+ import { upsertPackage, MAX_PATHS_PER_PACKAGE } from "./utils.js";
5
5
  import { uniquePathArrays } from "../utils/array.js";
6
- const MAX_PATHS_PER_PACKAGE = 5;
7
6
  const MAX_PATH_DEPTH = 10;
8
7
  function parsePnpmLockContent(content) {
9
8
  try {
@@ -136,6 +135,7 @@ function loadLegacy(parsed, prodOnly) {
136
135
  name: String(depName),
137
136
  version: fallbackVersion,
138
137
  ecosystem: "npm",
138
+ dev: depSectionName === "devDependencies",
139
139
  paths: [["project", String(depName)]]
140
140
  });
141
141
  }
@@ -206,6 +206,7 @@ function loadV9(parsed, prodOnly) {
206
206
  name: String(depName),
207
207
  version: fallbackVersion,
208
208
  ecosystem: "npm",
209
+ dev: depSectionName === "devDependencies",
209
210
  paths: [["project", String(depName)]]
210
211
  });
211
212
  // Unresolved fallback deps are not in the snapshot graph, so record their key
@@ -1,13 +1,15 @@
1
1
  import { uniquePathArrays } from "../utils/array.js";
2
+ import { packageInstanceKey } from "../utils/package-identity.js";
3
+ export const MAX_PATHS_PER_PACKAGE = 5;
2
4
  export function upsertPackage(map, candidate) {
3
- const key = `${candidate.name}@${candidate.version}`;
5
+ const key = packageInstanceKey(candidate);
4
6
  const existing = map.get(key);
5
7
  if (!existing) {
6
- map.set(key, { ...candidate, paths: uniquePathArrays(candidate.paths ?? []).slice(0, 5) });
8
+ map.set(key, { ...candidate, paths: uniquePathArrays(candidate.paths ?? []).slice(0, MAX_PATHS_PER_PACKAGE) });
7
9
  return;
8
10
  }
9
11
  existing.dev = existing.dev && candidate.dev;
10
- existing.paths = uniquePathArrays([...(existing.paths ?? []), ...(candidate.paths ?? [])]).slice(0, 5);
12
+ existing.paths = uniquePathArrays([...(existing.paths ?? []), ...(candidate.paths ?? [])]).slice(0, MAX_PATHS_PER_PACKAGE);
11
13
  // The same name@version can appear at several lockfile paths; keep the first
12
14
  // license we see rather than losing it if a later entry omits the field.
13
15
  existing.license ??= candidate.license;
@@ -83,8 +83,11 @@ function buildClassicIndex(parsed) {
83
83
  continue;
84
84
  const canonicalKey = `${selectors[0].slice(0, atIndex)}@${version}`;
85
85
  const children = new Set();
86
- const deps = meta?.dependencies;
87
- if (deps && typeof deps === "object") {
86
+ // An optional dependency is installed whenever it can be, so its parent
87
+ // constrains the installed copy as much as a regular one does.
88
+ for (const deps of [meta?.dependencies, meta?.optionalDependencies]) {
89
+ if (!deps || typeof deps !== "object")
90
+ continue;
88
91
  for (const [depName, depRange] of Object.entries(deps)) {
89
92
  const childKey = selectorToKey.get(`${depName}@${depRange}`);
90
93
  if (childKey)
@@ -2,8 +2,7 @@ import fs from "node:fs";
2
2
  import path from "node:path";
3
3
  import { parse as parseYarnLock } from "yarn-lockfile";
4
4
  import { normalizeRawVersion } from "../utils/version.js";
5
- import { upsertPackage } from "./utils.js";
6
- const MAX_PATHS_PER_PACKAGE = 5;
5
+ import { upsertPackage, MAX_PATHS_PER_PACKAGE } from "./utils.js";
7
6
  const MAX_PATH_DEPTH = 10;
8
7
  // Record a display path for a package, matching the existing de-dup + cap behavior (keep the
9
8
  // first MAX_PATHS_PER_PACKAGE unique paths, in insertion order).