cve-lite-cli 1.27.0 → 1.29.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.
- package/README.md +45 -3
- package/dist/advisory/osv-advisory-source.js +5 -3
- package/dist/cli/advisories-init.js +24 -0
- package/dist/cli/args.js +30 -0
- package/dist/cli/help.js +6 -2
- package/dist/cli/validate.js +6 -0
- package/dist/constants.js +1 -0
- package/dist/index.js +67 -7
- package/dist/maintenance/dm001-maintenance-risk.js +122 -0
- package/dist/maintenance/score.js +51 -0
- package/dist/maintenance/types.js +1 -0
- package/dist/osv/cache.js +49 -50
- package/dist/output/formatters.js +10 -4
- package/dist/output/html-reporter.js +163 -14
- package/dist/output/maintenance-findings-html.js +60 -0
- package/dist/output/maintenance-findings-json.js +3 -0
- package/dist/output/maintenance-findings-sarif.js +58 -0
- package/dist/output/maintenance-terminal.js +138 -0
- package/dist/output/multi-folder-html-reporter.js +61 -5
- package/dist/output/multi-folder-printer.js +11 -2
- package/dist/output/override-findings-html.js +22 -32
- package/dist/output/printers.js +95 -22
- package/dist/output/sarif.js +23 -7
- package/dist/output/spinner.js +5 -0
- package/dist/output/write-outputs.js +7 -2
- package/dist/parsers/bun-lock.js +47 -7
- package/dist/parsers/npm-lock-graph.js +90 -12
- package/dist/parsers/pnpm-lock.js +42 -6
- package/dist/parsers/utils.js +0 -15
- package/dist/parsers/yarn-lock.js +140 -58
- package/dist/remediation/cooldown.js +74 -0
- package/dist/remediation/fix-commands.js +42 -2
- package/dist/remediation/npm-registry.js +70 -42
- package/dist/remediation/parent-upgrade.js +114 -18
- package/dist/scan/completeness.js +49 -0
- package/dist/scan/multi-folder-scan.js +193 -13
- package/dist/scanner.js +284 -165
- package/dist/usage/scanner.js +12 -2
- package/dist/utils/network.js +66 -0
- package/dist/utils/vuln.js +16 -0
- package/package.json +2 -2
package/dist/output/sarif.js
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
import fs from "node:fs";
|
|
2
2
|
import path from "node:path";
|
|
3
|
-
import {
|
|
3
|
+
import { findFixTargetForFinding } from "../remediation/fix-commands.js";
|
|
4
|
+
import { getRecommendedAction, formatCooldownWarning } from "./formatters.js";
|
|
4
5
|
import { getCliVersion } from "../utils/version-info.js";
|
|
5
6
|
import { severityToSarifLevel } from "../utils/severity.js";
|
|
6
7
|
import { sarifFingerprintHash } from "../utils/sarif.js";
|
|
7
8
|
import { buildOverrideSarifComponent, buildOverrideSarifResults } from "./override-findings-sarif.js";
|
|
9
|
+
import { buildMaintenanceSarifComponent, buildMaintenanceSarifResults } from "./maintenance-findings-sarif.js";
|
|
8
10
|
const SARIF_SCHEMA = "https://docs.oasis-open.org/sarif/sarif/v2.1.0/errata01/os/schemas/sarif-schema-2.1.0.json";
|
|
9
11
|
const LOCKFILE_NAMES = {
|
|
10
12
|
"package-lock": "package-lock.json",
|
|
@@ -75,13 +77,17 @@ export function deriveLockfileUri(scanInput) {
|
|
|
75
77
|
return path.basename(scanInput.filePath);
|
|
76
78
|
return LOCKFILE_NAMES[scanInput.source] ?? "lockfile";
|
|
77
79
|
}
|
|
78
|
-
export function buildSarifOutput(findings, lockfileUri, version,
|
|
80
|
+
export function buildSarifOutput(findings, lockfileUri, version, plan, overrideFindings, maintenanceFindings) {
|
|
79
81
|
const ruleMap = new Map();
|
|
80
82
|
const ruleIndexMap = new Map();
|
|
81
83
|
const results = [];
|
|
82
84
|
for (const finding of findings) {
|
|
83
85
|
const level = severityToSarifLevel(finding.severity);
|
|
84
86
|
const action = getRecommendedAction(finding);
|
|
87
|
+
// If the recommended fix for this finding lands on a version inside the
|
|
88
|
+
// project's configured release cooldown, surface that on the result so it
|
|
89
|
+
// reaches Code Scanning (message text for humans, property for tooling).
|
|
90
|
+
const cooldown = plan ? findFixTargetForFinding(plan, finding)?.cooldownWarning ?? null : null;
|
|
85
91
|
const location = {
|
|
86
92
|
physicalLocation: {
|
|
87
93
|
artifactLocation: { uri: lockfileUri, uriBaseId: "%SRCROOT%" },
|
|
@@ -116,20 +122,30 @@ export function buildSarifOutput(findings, lockfileUri, version, _plan, override
|
|
|
116
122
|
kind: "open",
|
|
117
123
|
level,
|
|
118
124
|
message: {
|
|
119
|
-
text: `${finding.pkg.name}@${finding.pkg.version} is vulnerable (${finding.severity}). ${action}
|
|
125
|
+
text: `${finding.pkg.name}@${finding.pkg.version} is vulnerable (${finding.severity}). ${action}`
|
|
126
|
+
+ (cooldown ? ` Release cooldown: ${formatCooldownWarning(cooldown)}` : ""),
|
|
120
127
|
},
|
|
121
128
|
locations: [location],
|
|
122
129
|
partialFingerprints: {
|
|
123
130
|
primaryLocationLineHash: sarifFingerprintHash(`${ruleId}:${finding.pkg.name}@${finding.pkg.version}`),
|
|
124
131
|
},
|
|
132
|
+
...(cooldown ? { properties: { cooldownWarning: cooldown } } : {}),
|
|
125
133
|
});
|
|
126
134
|
}
|
|
127
135
|
}
|
|
128
136
|
const hasOverrides = overrideFindings != null && overrideFindings.length > 0;
|
|
129
|
-
const
|
|
137
|
+
const hasMaintenance = maintenanceFindings != null && maintenanceFindings.length > 0;
|
|
138
|
+
const extensions = [
|
|
139
|
+
...(hasOverrides ? [buildOverrideSarifComponent()] : []),
|
|
140
|
+
...(hasMaintenance ? [buildMaintenanceSarifComponent()] : []),
|
|
141
|
+
];
|
|
142
|
+
const maintenanceExtensionIndex = hasOverrides ? 1 : 0;
|
|
130
143
|
const allResults = [
|
|
131
144
|
...results,
|
|
132
145
|
...(hasOverrides ? buildOverrideSarifResults(overrideFindings) : []),
|
|
146
|
+
...(hasMaintenance
|
|
147
|
+
? buildMaintenanceSarifResults(maintenanceFindings, lockfileUri, maintenanceExtensionIndex)
|
|
148
|
+
: []),
|
|
133
149
|
];
|
|
134
150
|
return {
|
|
135
151
|
$schema: SARIF_SCHEMA,
|
|
@@ -143,7 +159,7 @@ export function buildSarifOutput(findings, lockfileUri, version, _plan, override
|
|
|
143
159
|
informationUri: "https://owasp.org/cve-lite-cli/",
|
|
144
160
|
rules: Array.from(ruleMap.values()),
|
|
145
161
|
},
|
|
146
|
-
...(extensions ? { extensions } : {}),
|
|
162
|
+
...(extensions.length > 0 ? { extensions } : {}),
|
|
147
163
|
},
|
|
148
164
|
results: allResults,
|
|
149
165
|
artifacts: [
|
|
@@ -153,11 +169,11 @@ export function buildSarifOutput(findings, lockfileUri, version, _plan, override
|
|
|
153
169
|
],
|
|
154
170
|
};
|
|
155
171
|
}
|
|
156
|
-
export function writeSarifReport(findings, lockfileUri, plan, overrideFindings) {
|
|
172
|
+
export function writeSarifReport(findings, lockfileUri, plan, overrideFindings, maintenanceFindings) {
|
|
157
173
|
const ts = new Date().toISOString().replace(/[:.]/g, "-").slice(0, 19);
|
|
158
174
|
const filename = `cve-lite-scan-${ts}.sarif`;
|
|
159
175
|
const outputPath = path.join(process.cwd(), filename);
|
|
160
|
-
const sarif = buildSarifOutput(findings, lockfileUri, getCliVersion(), plan, overrideFindings);
|
|
176
|
+
const sarif = buildSarifOutput(findings, lockfileUri, getCliVersion(), plan, overrideFindings, maintenanceFindings);
|
|
161
177
|
try {
|
|
162
178
|
fs.writeFileSync(outputPath, JSON.stringify(sarif, null, 2));
|
|
163
179
|
}
|
package/dist/output/spinner.js
CHANGED
|
@@ -7,6 +7,7 @@ export function createSpinner(initialMessage, options) {
|
|
|
7
7
|
update: () => { },
|
|
8
8
|
succeed: () => { },
|
|
9
9
|
fail: () => { },
|
|
10
|
+
warn: () => { },
|
|
10
11
|
stop: () => { }
|
|
11
12
|
};
|
|
12
13
|
}
|
|
@@ -36,6 +37,10 @@ export function createSpinner(initialMessage, options) {
|
|
|
36
37
|
clearLine();
|
|
37
38
|
console.log(chalk.red(`✗ ${message}`));
|
|
38
39
|
},
|
|
40
|
+
warn(message) {
|
|
41
|
+
clearLine();
|
|
42
|
+
console.log(chalk.yellow(`⚠ ${message}`));
|
|
43
|
+
},
|
|
39
44
|
stop() {
|
|
40
45
|
clearLine();
|
|
41
46
|
}
|
|
@@ -5,6 +5,7 @@ import { serializeFinding } from "./formatters.js";
|
|
|
5
5
|
import { writeSarifReport, deriveLockfileUri } from "./sarif.js";
|
|
6
6
|
import { writeCycloneDxReport } from "./cyclonedx.js";
|
|
7
7
|
import { overrideFindingsToJson } from "./override-findings-json.js";
|
|
8
|
+
import { maintenanceFindingsToJson } from "./maintenance-findings-json.js";
|
|
8
9
|
function readProjectMeta(projectPath) {
|
|
9
10
|
try {
|
|
10
11
|
const pkgPath = path.join(projectPath, "package.json");
|
|
@@ -34,6 +35,9 @@ export async function writeOutputs(options, scanState, scanInput, projectPath) {
|
|
|
34
35
|
source: scanInput.source,
|
|
35
36
|
packageCount: scanInput.packages.length,
|
|
36
37
|
findingCount: scanState.sorted.length,
|
|
38
|
+
status: scanState.completeness.complete ? "ok" : "partial",
|
|
39
|
+
complete: scanState.completeness.complete,
|
|
40
|
+
diagnostics: scanState.completeness.diagnostics,
|
|
37
41
|
suggestedFixCommands: scanState.suggestedFixCommands,
|
|
38
42
|
notes: [...scanInput.notes, ...scanState.coverage],
|
|
39
43
|
warnings: scanInput.warnings,
|
|
@@ -41,13 +45,14 @@ export async function writeOutputs(options, scanState, scanInput, projectPath) {
|
|
|
41
45
|
findings: scanState.sorted.map(finding => serializeFinding(finding, scanState.suggestedFixCommands)),
|
|
42
46
|
};
|
|
43
47
|
const overridePayload = overrideFindingsToJson(scanState.overrideFindings ?? []);
|
|
44
|
-
const
|
|
48
|
+
const maintenancePayload = maintenanceFindingsToJson(scanState.maintenanceFindings ?? []);
|
|
49
|
+
const combined = { ...payload, ...overridePayload, ...maintenancePayload };
|
|
45
50
|
fs.writeFileSync(jsonOutputPath, JSON.stringify(combined, null, 2));
|
|
46
51
|
console.log(`${chalk.gray("JSON saved to")} ${chalk.cyan(jsonFilename)}`);
|
|
47
52
|
}
|
|
48
53
|
if (options.sarif) {
|
|
49
54
|
const lockfileUri = deriveLockfileUri(scanInput);
|
|
50
|
-
const sarifFilename = writeSarifReport(scanState.sorted, lockfileUri, scanState.suggestedFixCommands, scanState.overrideFindings);
|
|
55
|
+
const sarifFilename = writeSarifReport(scanState.sorted, lockfileUri, scanState.suggestedFixCommands, scanState.overrideFindings, scanState.maintenanceFindings);
|
|
51
56
|
console.log(`${chalk.gray("SARIF report written to")} ${chalk.cyan(sarifFilename)}`);
|
|
52
57
|
}
|
|
53
58
|
if (options.cdx) {
|
package/dist/parsers/bun-lock.js
CHANGED
|
@@ -54,6 +54,24 @@ function collectBunPaths(rootDepNames, childDepsByPackageName, map, packageNameT
|
|
|
54
54
|
}
|
|
55
55
|
}
|
|
56
56
|
}
|
|
57
|
+
// Walk the package graph from a set of root dependency names and return every reachable name.
|
|
58
|
+
// Used to classify dev vs prod: a package is dev only when it is reachable from a
|
|
59
|
+
// devDependency root but not from any production root.
|
|
60
|
+
function collectReachableNames(rootNames, childDepsByPackageName) {
|
|
61
|
+
const reachable = new Set();
|
|
62
|
+
const stack = [...rootNames];
|
|
63
|
+
while (stack.length > 0) {
|
|
64
|
+
const name = stack.pop();
|
|
65
|
+
if (reachable.has(name))
|
|
66
|
+
continue;
|
|
67
|
+
reachable.add(name);
|
|
68
|
+
for (const child of childDepsByPackageName.get(name) ?? []) {
|
|
69
|
+
if (!reachable.has(child))
|
|
70
|
+
stack.push(child);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
return reachable;
|
|
74
|
+
}
|
|
57
75
|
export function buildBunWorkspaceMap(filePath) {
|
|
58
76
|
const raw = parseJsonc(fs.readFileSync(filePath, "utf8"));
|
|
59
77
|
const workspaces = raw?.workspaces ?? {};
|
|
@@ -80,15 +98,18 @@ export function loadFromBunLock(filePath, prodOnly) {
|
|
|
80
98
|
const map = new Map();
|
|
81
99
|
const childDepsByPackageName = new Map();
|
|
82
100
|
const packageNameToVersion = new Map();
|
|
83
|
-
const
|
|
84
|
-
const
|
|
101
|
+
const prodRootNames = new Set();
|
|
102
|
+
const devRootNames = new Set();
|
|
85
103
|
const rootDepNames = [];
|
|
86
104
|
for (const workspace of Object.values(workspaces)) {
|
|
87
105
|
for (const name of Object.keys(workspace?.dependencies ?? {})) {
|
|
88
|
-
|
|
106
|
+
prodRootNames.add(name);
|
|
107
|
+
}
|
|
108
|
+
for (const name of Object.keys(workspace?.optionalDependencies ?? {})) {
|
|
109
|
+
prodRootNames.add(name);
|
|
89
110
|
}
|
|
90
111
|
for (const name of Object.keys(workspace?.devDependencies ?? {})) {
|
|
91
|
-
|
|
112
|
+
devRootNames.add(name);
|
|
92
113
|
}
|
|
93
114
|
}
|
|
94
115
|
for (const [workspacePath, workspace] of Object.entries(workspaces)) {
|
|
@@ -105,6 +126,7 @@ export function loadFromBunLock(filePath, prodOnly) {
|
|
|
105
126
|
}
|
|
106
127
|
}
|
|
107
128
|
}
|
|
129
|
+
// First pass: build the package graph (package key -> version, package key -> child dep names).
|
|
108
130
|
for (const [pkgName, entry] of Object.entries(packages)) {
|
|
109
131
|
if (!Array.isArray(entry) || entry.length < 1)
|
|
110
132
|
continue;
|
|
@@ -112,13 +134,31 @@ export function loadFromBunLock(filePath, prodOnly) {
|
|
|
112
134
|
const atIndex = nameAtVersion.lastIndexOf("@");
|
|
113
135
|
if (atIndex <= 0)
|
|
114
136
|
continue;
|
|
115
|
-
const name = nameAtVersion.slice(0, atIndex);
|
|
116
137
|
const version = nameAtVersion.slice(atIndex + 1);
|
|
117
|
-
if (!
|
|
138
|
+
if (!version)
|
|
118
139
|
continue;
|
|
119
140
|
packageNameToVersion.set(pkgName, version);
|
|
120
141
|
childDepsByPackageName.set(pkgName, extractChildDependencyNames(entry[2]));
|
|
121
|
-
|
|
142
|
+
}
|
|
143
|
+
// Classify dev vs prod by graph reachability rather than by direct declaration only: a
|
|
144
|
+
// package is dev when it is reachable from a devDependency root but not from any production
|
|
145
|
+
// (dependencies/optionalDependencies) root. This correctly marks a transitive pulled in only
|
|
146
|
+
// through a devDependency as dev, while keeping any package used in production anywhere prod.
|
|
147
|
+
const prodReachable = collectReachableNames(prodRootNames, childDepsByPackageName);
|
|
148
|
+
const devReachable = collectReachableNames(devRootNames, childDepsByPackageName);
|
|
149
|
+
// Second pass: create package refs with the reachability-based dev flag.
|
|
150
|
+
for (const [, entry] of Object.entries(packages)) {
|
|
151
|
+
if (!Array.isArray(entry) || entry.length < 1)
|
|
152
|
+
continue;
|
|
153
|
+
const nameAtVersion = String(entry[0] ?? "");
|
|
154
|
+
const atIndex = nameAtVersion.lastIndexOf("@");
|
|
155
|
+
if (atIndex <= 0)
|
|
156
|
+
continue;
|
|
157
|
+
const name = nameAtVersion.slice(0, atIndex);
|
|
158
|
+
const version = nameAtVersion.slice(atIndex + 1);
|
|
159
|
+
if (!name || !version)
|
|
160
|
+
continue;
|
|
161
|
+
const dev = devReachable.has(name) && !prodReachable.has(name);
|
|
122
162
|
if (prodOnly && dev)
|
|
123
163
|
continue;
|
|
124
164
|
const resolvedUrl = typeof entry[1] === "string" && entry[1] !== "" ? entry[1] : undefined;
|
|
@@ -9,10 +9,12 @@ export function loadNpmLockGraph(filePath, options) {
|
|
|
9
9
|
const nodeIdByPackagePath = new Map();
|
|
10
10
|
const dependencyRangesByNodeId = new Map();
|
|
11
11
|
const resolutionBasePathsByNodeId = new Map();
|
|
12
|
+
const peerRangesByNodeId = new Map();
|
|
12
13
|
const parentNodeIdsByChildNodeId = new Map();
|
|
13
14
|
const childNodeIdsByParentNodeId = new Map();
|
|
14
15
|
const rangeByParentNodeId = new Map();
|
|
15
16
|
const pathSetByNodeId = new Map();
|
|
17
|
+
const workspaceEntryNodeIds = [];
|
|
16
18
|
if (!packages || typeof packages !== "object") {
|
|
17
19
|
return createGraph({
|
|
18
20
|
entryPackages: [],
|
|
@@ -49,18 +51,41 @@ export function loadNpmLockGraph(filePath, options) {
|
|
|
49
51
|
const keySet = nodeIdsByPackageKey.get(packageKey) ?? new Set();
|
|
50
52
|
keySet.add(id);
|
|
51
53
|
nodeIdsByPackageKey.set(packageKey, keySet);
|
|
52
|
-
|
|
54
|
+
// Workspace / linked packages (link:true, resolved -> a workspace dir) are
|
|
55
|
+
// installed with their devDependencies, like the root, and are the true roots
|
|
56
|
+
// of their own dependency subtrees. Collect their full deps (incl. devDeps) and
|
|
57
|
+
// treat them as entry points; otherwise every transitive that lives only under a
|
|
58
|
+
// workspace is orphaned from "project" and reports "no parent identified".
|
|
59
|
+
dependencyRangesByNodeId.set(id, isLinkNode
|
|
60
|
+
? collectRootDependencyRanges(resolveDependencySource(meta, rawPackages))
|
|
61
|
+
: collectDependencyRanges(resolveDependencySource(meta, rawPackages)));
|
|
53
62
|
resolutionBasePathsByNodeId.set(id, resolveBasePaths(packagePath, meta));
|
|
63
|
+
const peerRanges = collectPeerRanges(resolveDependencySource(meta, rawPackages));
|
|
64
|
+
if (Object.keys(peerRanges).length > 0)
|
|
65
|
+
peerRangesByNodeId.set(id, peerRanges);
|
|
66
|
+
if (isLinkNode)
|
|
67
|
+
workspaceEntryNodeIds.push(id);
|
|
54
68
|
}
|
|
55
69
|
const rootMeta = isRecord(rawPackages[""]) ? rawPackages[""] : null;
|
|
56
70
|
const entryPackages = resolveEdgesForParent("", collectRootDependencyRanges(rootMeta), [""], nodeIdByPackagePath, childNodeIdsByParentNodeId, parentNodeIdsByChildNodeId, rangeByParentNodeId);
|
|
57
71
|
for (const node of nodesById.values()) {
|
|
58
72
|
resolveEdgesForParent(node.packagePath, dependencyRangesByNodeId.get(node.id) ?? {}, resolutionBasePathsByNodeId.get(node.id) ?? [node.packagePath], nodeIdByPackagePath, childNodeIdsByParentNodeId, parentNodeIdsByChildNodeId, rangeByParentNodeId);
|
|
59
73
|
}
|
|
74
|
+
const entryNodeIds = unique([...entryPackages, ...workspaceEntryNodeIds]);
|
|
75
|
+
applyPeerDependencyFallback({
|
|
76
|
+
nodesById,
|
|
77
|
+
entryNodeIds,
|
|
78
|
+
peerRangesByNodeId,
|
|
79
|
+
resolutionBasePathsByNodeId,
|
|
80
|
+
nodeIdByPackagePath,
|
|
81
|
+
parentNodeIdsByChildNodeId,
|
|
82
|
+
childNodeIdsByParentNodeId,
|
|
83
|
+
rangeByParentNodeId,
|
|
84
|
+
});
|
|
60
85
|
if (options?.includePaths !== false) {
|
|
61
86
|
const MAX_PATHS_PER_NODE = 5;
|
|
62
87
|
const MAX_PATH_DEPTH = 10;
|
|
63
|
-
const queue =
|
|
88
|
+
const queue = entryNodeIds.map((entryNodeId) => ({
|
|
64
89
|
nodeId: entryNodeId,
|
|
65
90
|
path: ["project", nodesById.get(entryNodeId)?.name ?? entryNodeId],
|
|
66
91
|
}));
|
|
@@ -87,7 +112,7 @@ export function loadNpmLockGraph(filePath, options) {
|
|
|
87
112
|
}
|
|
88
113
|
}
|
|
89
114
|
return createGraph({
|
|
90
|
-
entryPackages,
|
|
115
|
+
entryPackages: entryNodeIds,
|
|
91
116
|
nodesById,
|
|
92
117
|
nodeIdsByPackageKey: setsToArrays(nodeIdsByPackageKey),
|
|
93
118
|
parentNodeIdsByChildNodeId: setsToArrays(parentNodeIdsByChildNodeId),
|
|
@@ -155,18 +180,59 @@ function resolveEdgesForParent(parentPackagePath, dependencyRanges, resolutionBa
|
|
|
155
180
|
const parentNodeId = parentPackagePath ? nodeIdByPackagePath.get(parentPackagePath) : null;
|
|
156
181
|
if (!parentNodeId)
|
|
157
182
|
continue;
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
parentNodeIdsByChildNodeId.set(childNodeId, parentSet);
|
|
164
|
-
const rangesForParent = rangeByParentNodeId.get(parentNodeId) ?? new Map();
|
|
165
|
-
rangesForParent.set(dependencyName, range);
|
|
166
|
-
rangeByParentNodeId.set(parentNodeId, rangesForParent);
|
|
183
|
+
addParentChildEdge(parentNodeId, childNodeId, dependencyName, range, {
|
|
184
|
+
childNodeIdsByParentNodeId,
|
|
185
|
+
parentNodeIdsByChildNodeId,
|
|
186
|
+
rangeByParentNodeId,
|
|
187
|
+
});
|
|
167
188
|
}
|
|
168
189
|
return unique(resolvedChildNodeIds);
|
|
169
190
|
}
|
|
191
|
+
// npm 7+ auto-installs peer dependencies, so a peer is a real reason its version is on
|
|
192
|
+
// disk - but only matters for attribution when nothing else pulls the package. Attribute
|
|
193
|
+
// an otherwise-orphaned node (no regular-dependency parent and not a top-level entry) to
|
|
194
|
+
// the package(s) that peer-depend on it, so a peer-only transitive is not left "no parent
|
|
195
|
+
// identified". Anything already reached through a regular dependency or a root/workspace
|
|
196
|
+
// entry keeps that attribution, so this never overrides a truer source (e.g. a peer that
|
|
197
|
+
// is also a direct dependency).
|
|
198
|
+
function applyPeerDependencyFallback(args) {
|
|
199
|
+
const { nodesById, entryNodeIds, peerRangesByNodeId, resolutionBasePathsByNodeId, nodeIdByPackagePath, parentNodeIdsByChildNodeId, } = args;
|
|
200
|
+
const entryNodeIdSet = new Set(entryNodeIds);
|
|
201
|
+
const orphanNodeIds = new Set();
|
|
202
|
+
for (const nodeId of nodesById.keys()) {
|
|
203
|
+
const hasParent = (parentNodeIdsByChildNodeId.get(nodeId)?.size ?? 0) > 0;
|
|
204
|
+
if (!hasParent && !entryNodeIdSet.has(nodeId))
|
|
205
|
+
orphanNodeIds.add(nodeId);
|
|
206
|
+
}
|
|
207
|
+
if (orphanNodeIds.size === 0)
|
|
208
|
+
return;
|
|
209
|
+
for (const node of nodesById.values()) {
|
|
210
|
+
const peerRanges = peerRangesByNodeId.get(node.id);
|
|
211
|
+
if (!peerRanges)
|
|
212
|
+
continue;
|
|
213
|
+
const basePaths = resolutionBasePathsByNodeId.get(node.id) ?? [node.packagePath];
|
|
214
|
+
for (const [peerName, range] of Object.entries(peerRanges)) {
|
|
215
|
+
const childPackagePath = resolveDependencyPackagePath(basePaths, peerName, nodeIdByPackagePath);
|
|
216
|
+
if (!childPackagePath)
|
|
217
|
+
continue;
|
|
218
|
+
const childNodeId = nodeIdByPackagePath.get(childPackagePath);
|
|
219
|
+
if (!childNodeId || !orphanNodeIds.has(childNodeId))
|
|
220
|
+
continue;
|
|
221
|
+
addParentChildEdge(node.id, childNodeId, peerName, range, args);
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
function addParentChildEdge(parentNodeId, childNodeId, dependencyName, range, edges) {
|
|
226
|
+
const childSet = edges.childNodeIdsByParentNodeId.get(parentNodeId) ?? new Set();
|
|
227
|
+
childSet.add(childNodeId);
|
|
228
|
+
edges.childNodeIdsByParentNodeId.set(parentNodeId, childSet);
|
|
229
|
+
const parentSet = edges.parentNodeIdsByChildNodeId.get(childNodeId) ?? new Set();
|
|
230
|
+
parentSet.add(parentNodeId);
|
|
231
|
+
edges.parentNodeIdsByChildNodeId.set(childNodeId, parentSet);
|
|
232
|
+
const rangesForParent = edges.rangeByParentNodeId.get(parentNodeId) ?? new Map();
|
|
233
|
+
rangesForParent.set(dependencyName, range);
|
|
234
|
+
edges.rangeByParentNodeId.set(parentNodeId, rangesForParent);
|
|
235
|
+
}
|
|
170
236
|
function collectDependencyRanges(meta) {
|
|
171
237
|
if (!meta)
|
|
172
238
|
return {};
|
|
@@ -185,6 +251,18 @@ function collectDependencyRanges(meta) {
|
|
|
185
251
|
}
|
|
186
252
|
return ranges;
|
|
187
253
|
}
|
|
254
|
+
function collectPeerRanges(meta) {
|
|
255
|
+
const ranges = {};
|
|
256
|
+
const source = meta?.peerDependencies;
|
|
257
|
+
if (!source || typeof source !== "object")
|
|
258
|
+
return ranges;
|
|
259
|
+
for (const [name, range] of Object.entries(source)) {
|
|
260
|
+
if (typeof range !== "string" || !range)
|
|
261
|
+
continue;
|
|
262
|
+
ranges[name] = range;
|
|
263
|
+
}
|
|
264
|
+
return ranges;
|
|
265
|
+
}
|
|
188
266
|
function collectRootDependencyRanges(meta) {
|
|
189
267
|
if (!meta)
|
|
190
268
|
return {};
|
|
@@ -1,7 +1,7 @@
|
|
|
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
|
|
4
|
+
import { upsertPackage } from "./utils.js";
|
|
5
5
|
import { uniquePathArrays } from "../utils/array.js";
|
|
6
6
|
const MAX_PATHS_PER_PACKAGE = 5;
|
|
7
7
|
const MAX_PATH_DEPTH = 10;
|
|
@@ -166,7 +166,8 @@ function loadV9(parsed, prodOnly) {
|
|
|
166
166
|
upsertPackage(map, { name: ref.name, version: ref.version, ecosystem: "npm", dev, paths: [], resolvedUrl: resolutionUrls.get(ref.key) });
|
|
167
167
|
}
|
|
168
168
|
const rootDeps = [];
|
|
169
|
-
const
|
|
169
|
+
const prodRootKeys = [];
|
|
170
|
+
const prodPkgKeys = new Set();
|
|
170
171
|
for (const importer of Object.values(importers)) {
|
|
171
172
|
for (const depSectionName of ["dependencies", "optionalDependencies", "devDependencies"]) {
|
|
172
173
|
if (prodOnly && depSectionName === "devDependencies")
|
|
@@ -174,13 +175,13 @@ function loadV9(parsed, prodOnly) {
|
|
|
174
175
|
const depSection = importer?.[depSectionName];
|
|
175
176
|
if (!depSection || typeof depSection !== "object")
|
|
176
177
|
continue;
|
|
178
|
+
const isProdSection = depSectionName !== "devDependencies";
|
|
177
179
|
for (const [depName, depRef] of Object.entries(depSection)) {
|
|
178
|
-
if (depSectionName === "devDependencies") {
|
|
179
|
-
devDepNames.add(String(depName));
|
|
180
|
-
}
|
|
181
180
|
const resolved = normalizePnpmDepRefV9(String(depName), depRef);
|
|
182
181
|
if (resolved) {
|
|
183
182
|
rootDeps.push(resolved);
|
|
183
|
+
if (isProdSection)
|
|
184
|
+
prodRootKeys.push(resolved);
|
|
184
185
|
}
|
|
185
186
|
else {
|
|
186
187
|
const fallbackVersion = normalizeRawVersion(depRef);
|
|
@@ -191,15 +192,50 @@ function loadV9(parsed, prodOnly) {
|
|
|
191
192
|
ecosystem: "npm",
|
|
192
193
|
paths: [["project", String(depName)]]
|
|
193
194
|
});
|
|
195
|
+
// Unresolved fallback deps are not in the snapshot graph, so record their key
|
|
196
|
+
// directly as production-reachable when they come from a production section.
|
|
197
|
+
if (isProdSection)
|
|
198
|
+
prodPkgKeys.add(`${String(depName)}@${fallbackVersion}`);
|
|
194
199
|
}
|
|
195
200
|
}
|
|
196
201
|
}
|
|
197
202
|
}
|
|
198
203
|
}
|
|
199
204
|
collectPnpmPaths(rootDeps, graph, map, parsePnpmPackageKeyV9);
|
|
200
|
-
|
|
205
|
+
markPnpmDevByProdReachability(map, graph, prodRootKeys, prodPkgKeys, parsePnpmPackageKeyV9);
|
|
201
206
|
return [...map.values()];
|
|
202
207
|
}
|
|
208
|
+
// Classify pnpm v9 packages dev vs prod by graph reachability from production roots, matching
|
|
209
|
+
// pnpm's own semantics: a reached package is dev only when it is not reachable from any
|
|
210
|
+
// importer's production (dependencies/optionalDependencies) declarations. This replaces the
|
|
211
|
+
// earlier name-based inference, which wrongly marked a package dev when it was a devDependency
|
|
212
|
+
// in one workspace but a production dependency in another (the aggregated devDependency name
|
|
213
|
+
// set could not tell the two apart). Only packages already reached during path collection are
|
|
214
|
+
// classified, matching prior behavior.
|
|
215
|
+
function markPnpmDevByProdReachability(map, graph, prodRootKeys, prodFallbackPkgKeys, parseKey) {
|
|
216
|
+
const prodReachable = new Set(prodFallbackPkgKeys);
|
|
217
|
+
const visited = new Set();
|
|
218
|
+
const stack = [...prodRootKeys];
|
|
219
|
+
while (stack.length > 0) {
|
|
220
|
+
const key = stack.pop();
|
|
221
|
+
if (visited.has(key))
|
|
222
|
+
continue;
|
|
223
|
+
visited.add(key);
|
|
224
|
+
const ref = parseKey(key);
|
|
225
|
+
if (ref)
|
|
226
|
+
prodReachable.add(`${ref.name}@${ref.version}`);
|
|
227
|
+
for (const child of graph.get(key) ?? []) {
|
|
228
|
+
if (!visited.has(child))
|
|
229
|
+
stack.push(child);
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
for (const [pkgKey, pkg] of map) {
|
|
233
|
+
if ((pkg.paths?.length ?? 0) === 0)
|
|
234
|
+
continue;
|
|
235
|
+
if (!prodReachable.has(pkgKey))
|
|
236
|
+
pkg.dev = true;
|
|
237
|
+
}
|
|
238
|
+
}
|
|
203
239
|
function collectPnpmPaths(rootDeps, graph, map, parsePackageKey) {
|
|
204
240
|
const queue = rootDeps.map(dep => ({ key: dep, path: ["project"] }));
|
|
205
241
|
const visitedStates = new Set();
|
package/dist/parsers/utils.js
CHANGED
|
@@ -1,19 +1,4 @@
|
|
|
1
1
|
import { uniquePathArrays } from "../utils/array.js";
|
|
2
|
-
export function markDevPackages(map, devDepNames) {
|
|
3
|
-
if (devDepNames.size === 0)
|
|
4
|
-
return;
|
|
5
|
-
for (const pkg of map.values()) {
|
|
6
|
-
const paths = pkg.paths ?? [];
|
|
7
|
-
if (paths.length === 0)
|
|
8
|
-
continue;
|
|
9
|
-
// A package is dev only if every known path starts from a devDependency root.
|
|
10
|
-
// path[1] is the root dep name (path[0] is always "project").
|
|
11
|
-
const allPathsFromDev = paths.every(p => p.length >= 2 && devDepNames.has(p[1]));
|
|
12
|
-
if (allPathsFromDev) {
|
|
13
|
-
pkg.dev = true;
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
2
|
export function upsertPackage(map, candidate) {
|
|
18
3
|
const key = `${candidate.name}@${candidate.version}`;
|
|
19
4
|
const existing = map.get(key);
|