cve-lite-cli 1.28.0 → 1.30.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 +30 -4
- package/dist/advisory/osv-advisory-source.js +3 -0
- package/dist/cli/help.js +3 -2
- package/dist/cli/validate.js +6 -0
- package/dist/constants.js +1 -0
- package/dist/index.js +50 -7
- package/dist/maintenance/dm001-maintenance-risk.js +74 -15
- package/dist/maintenance/score.js +51 -0
- package/dist/output/formatters.js +21 -8
- package/dist/output/html-reporter.js +133 -12
- package/dist/output/maintenance-findings-html.js +4 -0
- package/dist/output/maintenance-findings-sarif.js +58 -0
- package/dist/output/maintenance-terminal.js +63 -5
- package/dist/output/multi-folder-html-reporter.js +64 -7
- package/dist/output/multi-folder-printer.js +11 -2
- package/dist/output/override-findings-html.js +6 -5
- package/dist/output/override-findings-terminal.js +2 -1
- package/dist/output/printers.js +92 -30
- package/dist/output/sarif.js +23 -7
- package/dist/output/write-outputs.js +4 -1
- package/dist/overrides/detectors/oa001-orphaned-target.js +2 -1
- package/dist/overrides/detectors/oa002-floating-tag.js +2 -1
- package/dist/overrides/detectors/oa003-wrong-section.js +2 -1
- package/dist/overrides/detectors/oa004-surpassed-pin.js +2 -1
- package/dist/overrides/detectors/oa005-nested-ineffective.js +2 -1
- package/dist/overrides/detectors/oa006-coupled-platform-binary.js +2 -1
- package/dist/overrides/detectors/oa007-frozen-latest.js +2 -1
- package/dist/overrides/detectors/oa009-stale-floor.js +2 -1
- package/dist/parsers/bun-lock.js +47 -7
- package/dist/parsers/npm-lock-graph.js +90 -12
- package/dist/parsers/pnpm-lock.js +60 -10
- package/dist/parsers/utils.js +0 -15
- package/dist/parsers/yarn-lock.js +140 -58
- package/dist/remediation/fix-commands.js +43 -12
- package/dist/remediation/npm-registry.js +69 -52
- package/dist/remediation/npm-transitive-resolution.js +5 -6
- package/dist/remediation/package-path-lookup.js +29 -0
- package/dist/remediation/parent-upgrade.js +2 -22
- package/dist/scan/completeness.js +49 -0
- package/dist/scan/multi-folder-scan.js +183 -15
- package/dist/scanner.js +210 -155
- package/dist/usage/scanner.js +12 -2
- package/dist/utils/finding.js +28 -0
- package/dist/utils/fix-runner.js +87 -50
- package/dist/utils/version.js +13 -4
- package/dist/utils/vuln.js +16 -0
- package/package.json +3 -3
|
@@ -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;
|
|
@@ -48,9 +48,24 @@ function hasProjectImporterSections(document) {
|
|
|
48
48
|
}
|
|
49
49
|
return false;
|
|
50
50
|
}
|
|
51
|
+
// pnpm-lock.yaml is by far the most expensive lockfile to parse (a full YAML document
|
|
52
|
+
// model, ~380ms on a large monorepo), yet a single scan reads it up to four times: the
|
|
53
|
+
// package load, the scanner's workspace map, the suggested-fix workspace map, and override
|
|
54
|
+
// context. Cache the parsed document per (path, mtime, size) so each unchanged lockfile is
|
|
55
|
+
// parsed exactly once. The mtime/size key invalidates automatically after `--fix` rewrites
|
|
56
|
+
// the lockfile, so a re-scan in the same process always sees the fresh contents.
|
|
57
|
+
const parsedLockCache = new Map();
|
|
58
|
+
function readAndParsePnpmLock(filePath) {
|
|
59
|
+
const stat = fs.statSync(filePath);
|
|
60
|
+
const cacheKey = `${filePath}::${stat.mtimeMs}::${stat.size}`;
|
|
61
|
+
if (parsedLockCache.has(cacheKey))
|
|
62
|
+
return parsedLockCache.get(cacheKey);
|
|
63
|
+
const parsed = parsePnpmLockContent(fs.readFileSync(filePath, "utf8"));
|
|
64
|
+
parsedLockCache.set(cacheKey, parsed);
|
|
65
|
+
return parsed;
|
|
66
|
+
}
|
|
51
67
|
export function buildPnpmWorkspaceMap(filePath) {
|
|
52
|
-
const
|
|
53
|
-
const parsed = parsePnpmLockContent(content);
|
|
68
|
+
const parsed = readAndParsePnpmLock(filePath);
|
|
54
69
|
const importers = parsed?.importers ?? {};
|
|
55
70
|
const map = new Map();
|
|
56
71
|
for (const [importerPath, importer] of Object.entries(importers)) {
|
|
@@ -69,8 +84,7 @@ export function buildPnpmWorkspaceMap(filePath) {
|
|
|
69
84
|
return map;
|
|
70
85
|
}
|
|
71
86
|
export function loadFromPnpmLock(filePath, prodOnly) {
|
|
72
|
-
const
|
|
73
|
-
const parsed = parsePnpmLockContent(content);
|
|
87
|
+
const parsed = readAndParsePnpmLock(filePath);
|
|
74
88
|
const majorVersion = parseInt(String(parsed?.lockfileVersion ?? "0"), 10);
|
|
75
89
|
return majorVersion >= 9 ? loadV9(parsed, prodOnly) : loadLegacy(parsed, prodOnly);
|
|
76
90
|
}
|
|
@@ -166,7 +180,8 @@ function loadV9(parsed, prodOnly) {
|
|
|
166
180
|
upsertPackage(map, { name: ref.name, version: ref.version, ecosystem: "npm", dev, paths: [], resolvedUrl: resolutionUrls.get(ref.key) });
|
|
167
181
|
}
|
|
168
182
|
const rootDeps = [];
|
|
169
|
-
const
|
|
183
|
+
const prodRootKeys = [];
|
|
184
|
+
const prodPkgKeys = new Set();
|
|
170
185
|
for (const importer of Object.values(importers)) {
|
|
171
186
|
for (const depSectionName of ["dependencies", "optionalDependencies", "devDependencies"]) {
|
|
172
187
|
if (prodOnly && depSectionName === "devDependencies")
|
|
@@ -174,13 +189,13 @@ function loadV9(parsed, prodOnly) {
|
|
|
174
189
|
const depSection = importer?.[depSectionName];
|
|
175
190
|
if (!depSection || typeof depSection !== "object")
|
|
176
191
|
continue;
|
|
192
|
+
const isProdSection = depSectionName !== "devDependencies";
|
|
177
193
|
for (const [depName, depRef] of Object.entries(depSection)) {
|
|
178
|
-
if (depSectionName === "devDependencies") {
|
|
179
|
-
devDepNames.add(String(depName));
|
|
180
|
-
}
|
|
181
194
|
const resolved = normalizePnpmDepRefV9(String(depName), depRef);
|
|
182
195
|
if (resolved) {
|
|
183
196
|
rootDeps.push(resolved);
|
|
197
|
+
if (isProdSection)
|
|
198
|
+
prodRootKeys.push(resolved);
|
|
184
199
|
}
|
|
185
200
|
else {
|
|
186
201
|
const fallbackVersion = normalizeRawVersion(depRef);
|
|
@@ -191,15 +206,50 @@ function loadV9(parsed, prodOnly) {
|
|
|
191
206
|
ecosystem: "npm",
|
|
192
207
|
paths: [["project", String(depName)]]
|
|
193
208
|
});
|
|
209
|
+
// Unresolved fallback deps are not in the snapshot graph, so record their key
|
|
210
|
+
// directly as production-reachable when they come from a production section.
|
|
211
|
+
if (isProdSection)
|
|
212
|
+
prodPkgKeys.add(`${String(depName)}@${fallbackVersion}`);
|
|
194
213
|
}
|
|
195
214
|
}
|
|
196
215
|
}
|
|
197
216
|
}
|
|
198
217
|
}
|
|
199
218
|
collectPnpmPaths(rootDeps, graph, map, parsePnpmPackageKeyV9);
|
|
200
|
-
|
|
219
|
+
markPnpmDevByProdReachability(map, graph, prodRootKeys, prodPkgKeys, parsePnpmPackageKeyV9);
|
|
201
220
|
return [...map.values()];
|
|
202
221
|
}
|
|
222
|
+
// Classify pnpm v9 packages dev vs prod by graph reachability from production roots, matching
|
|
223
|
+
// pnpm's own semantics: a reached package is dev only when it is not reachable from any
|
|
224
|
+
// importer's production (dependencies/optionalDependencies) declarations. This replaces the
|
|
225
|
+
// earlier name-based inference, which wrongly marked a package dev when it was a devDependency
|
|
226
|
+
// in one workspace but a production dependency in another (the aggregated devDependency name
|
|
227
|
+
// set could not tell the two apart). Only packages already reached during path collection are
|
|
228
|
+
// classified, matching prior behavior.
|
|
229
|
+
function markPnpmDevByProdReachability(map, graph, prodRootKeys, prodFallbackPkgKeys, parseKey) {
|
|
230
|
+
const prodReachable = new Set(prodFallbackPkgKeys);
|
|
231
|
+
const visited = new Set();
|
|
232
|
+
const stack = [...prodRootKeys];
|
|
233
|
+
while (stack.length > 0) {
|
|
234
|
+
const key = stack.pop();
|
|
235
|
+
if (visited.has(key))
|
|
236
|
+
continue;
|
|
237
|
+
visited.add(key);
|
|
238
|
+
const ref = parseKey(key);
|
|
239
|
+
if (ref)
|
|
240
|
+
prodReachable.add(`${ref.name}@${ref.version}`);
|
|
241
|
+
for (const child of graph.get(key) ?? []) {
|
|
242
|
+
if (!visited.has(child))
|
|
243
|
+
stack.push(child);
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
for (const [pkgKey, pkg] of map) {
|
|
247
|
+
if ((pkg.paths?.length ?? 0) === 0)
|
|
248
|
+
continue;
|
|
249
|
+
if (!prodReachable.has(pkgKey))
|
|
250
|
+
pkg.dev = true;
|
|
251
|
+
}
|
|
252
|
+
}
|
|
203
253
|
function collectPnpmPaths(rootDeps, graph, map, parsePackageKey) {
|
|
204
254
|
const queue = rootDeps.map(dep => ({ key: dep, path: ["project"] }));
|
|
205
255
|
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);
|
|
@@ -1,26 +1,98 @@
|
|
|
1
1
|
import fs from "node:fs";
|
|
2
2
|
import path from "node:path";
|
|
3
3
|
import { parse as parseYarnLock } from "yarn-lockfile";
|
|
4
|
-
import { uniquePathArrays } from "../utils/array.js";
|
|
5
4
|
import { normalizeRawVersion } from "../utils/version.js";
|
|
6
|
-
import { upsertPackage
|
|
5
|
+
import { upsertPackage } from "./utils.js";
|
|
7
6
|
const MAX_PATHS_PER_PACKAGE = 5;
|
|
8
7
|
const MAX_PATH_DEPTH = 10;
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
8
|
+
// Record a display path for a package, matching the existing de-dup + cap behavior (keep the
|
|
9
|
+
// first MAX_PATHS_PER_PACKAGE unique paths, in insertion order).
|
|
10
|
+
function addTaggedPath(taggedPathsByKey, key, path) {
|
|
11
|
+
const list = taggedPathsByKey.get(key) ?? [];
|
|
12
|
+
taggedPathsByKey.set(key, list);
|
|
13
|
+
const serialized = path.join(">");
|
|
14
|
+
if (list.some(entry => entry.path.join(">") === serialized))
|
|
15
|
+
return;
|
|
16
|
+
if (list.length >= MAX_PATHS_PER_PACKAGE)
|
|
17
|
+
return;
|
|
18
|
+
list.push({ path });
|
|
19
|
+
}
|
|
20
|
+
// Write the collected display paths onto each package. Dev/prod is set separately.
|
|
21
|
+
function applyTaggedPaths(map, taggedPathsByKey) {
|
|
22
|
+
for (const [key, tagged] of taggedPathsByKey) {
|
|
23
|
+
const pkg = map.get(key);
|
|
24
|
+
if (!pkg)
|
|
25
|
+
continue;
|
|
26
|
+
pkg.paths = tagged.map(entry => entry.path);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
// Classify dev vs prod by production reachability, mirroring the pnpm and Bun loaders: a
|
|
30
|
+
// package that has recorded paths is dev only when it is not reachable from any production
|
|
31
|
+
// root (a root or workspace dependency/optionalDependency). Reachability is computed over the
|
|
32
|
+
// full graph, so it is independent of the per-package display-path cap - a production path
|
|
33
|
+
// beyond the cap still keeps the package production. Unresolved fallback production deps are
|
|
34
|
+
// seeded directly, since they are not graph nodes.
|
|
35
|
+
function markYarnDevByProdReachability(map, graph, prodRootKeys, prodFallbackKeys) {
|
|
36
|
+
const prodReachable = new Set(prodFallbackKeys);
|
|
37
|
+
const visited = new Set();
|
|
38
|
+
const stack = [...prodRootKeys];
|
|
39
|
+
while (stack.length > 0) {
|
|
40
|
+
const key = stack.pop();
|
|
41
|
+
if (visited.has(key))
|
|
42
|
+
continue;
|
|
43
|
+
visited.add(key);
|
|
44
|
+
prodReachable.add(key);
|
|
45
|
+
for (const child of graph.get(key) ?? []) {
|
|
46
|
+
if (!visited.has(child))
|
|
47
|
+
stack.push(child);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
for (const [key, pkg] of map) {
|
|
51
|
+
if ((pkg.paths?.length ?? 0) === 0)
|
|
52
|
+
continue;
|
|
53
|
+
if (!prodReachable.has(key))
|
|
54
|
+
pkg.dev = true;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
function readRootPackageJson(filePath) {
|
|
13
58
|
try {
|
|
14
|
-
|
|
59
|
+
return JSON.parse(fs.readFileSync(path.join(path.dirname(filePath), "package.json"), "utf8"));
|
|
15
60
|
}
|
|
16
61
|
catch {
|
|
17
|
-
return
|
|
62
|
+
return null;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
export function buildYarnWorkspaceMap(filePath) {
|
|
66
|
+
const map = new Map();
|
|
67
|
+
for (const { name: wsName, pkg: wsPkg } of readWorkspacePackages(filePath)) {
|
|
68
|
+
for (const depSection of ["dependencies", "optionalDependencies", "devDependencies"]) {
|
|
69
|
+
const deps = wsPkg[depSection];
|
|
70
|
+
if (!deps || typeof deps !== "object")
|
|
71
|
+
continue;
|
|
72
|
+
for (const depName of Object.keys(deps)) {
|
|
73
|
+
const existing = map.get(depName) ?? [];
|
|
74
|
+
if (!existing.includes(wsName))
|
|
75
|
+
map.set(depName, [...existing, wsName]);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
18
78
|
}
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
79
|
+
return map;
|
|
80
|
+
}
|
|
81
|
+
// Enumerate a project's Yarn workspace packages (name + parsed package.json) by
|
|
82
|
+
// resolving the root package.json's `workspaces` globs. Shared by the workspace-map
|
|
83
|
+
// builder and by dependency-path seeding, so a transitive pulled in only by a
|
|
84
|
+
// workspace package can be attributed back to that workspace instead of orphaning.
|
|
85
|
+
function readWorkspacePackages(filePath, rootPkg) {
|
|
86
|
+
const dir = path.dirname(filePath);
|
|
87
|
+
const root = rootPkg ?? readRootPackageJson(filePath);
|
|
88
|
+
if (!root)
|
|
89
|
+
return [];
|
|
90
|
+
const patterns = Array.isArray(root.workspaces)
|
|
91
|
+
? root.workspaces
|
|
92
|
+
: root.workspaces?.packages ?? [];
|
|
22
93
|
if (!patterns.length)
|
|
23
|
-
return
|
|
94
|
+
return [];
|
|
95
|
+
const result = [];
|
|
24
96
|
for (const pattern of patterns) {
|
|
25
97
|
const parts = pattern.split("/");
|
|
26
98
|
const lastPart = parts[parts.length - 1];
|
|
@@ -48,22 +120,12 @@ export function buildYarnWorkspaceMap(filePath) {
|
|
|
48
120
|
catch {
|
|
49
121
|
continue;
|
|
50
122
|
}
|
|
51
|
-
|
|
52
|
-
if (!wsName)
|
|
123
|
+
if (!wsPkg.name)
|
|
53
124
|
continue;
|
|
54
|
-
|
|
55
|
-
const deps = wsPkg[depSection];
|
|
56
|
-
if (!deps || typeof deps !== "object")
|
|
57
|
-
continue;
|
|
58
|
-
for (const depName of Object.keys(deps)) {
|
|
59
|
-
const existing = map.get(depName) ?? [];
|
|
60
|
-
if (!existing.includes(wsName))
|
|
61
|
-
map.set(depName, [...existing, wsName]);
|
|
62
|
-
}
|
|
63
|
-
}
|
|
125
|
+
result.push({ name: wsPkg.name, pkg: wsPkg });
|
|
64
126
|
}
|
|
65
127
|
}
|
|
66
|
-
return
|
|
128
|
+
return result;
|
|
67
129
|
}
|
|
68
130
|
function isYarnBerry(content) {
|
|
69
131
|
return content.startsWith('__metadata:') || content.includes('\n__metadata:');
|
|
@@ -83,8 +145,8 @@ function parseYarnPackageKey(key) {
|
|
|
83
145
|
return null;
|
|
84
146
|
return { key, name, version };
|
|
85
147
|
}
|
|
86
|
-
function collectYarnPaths(
|
|
87
|
-
const queue =
|
|
148
|
+
function collectYarnPaths(seeds, graph, map, taggedPathsByKey) {
|
|
149
|
+
const queue = seeds.map(seed => ({ key: seed.key, path: seed.path }));
|
|
88
150
|
const visitedStates = new Set();
|
|
89
151
|
let head = 0;
|
|
90
152
|
while (head < queue.length) {
|
|
@@ -97,13 +159,13 @@ function collectYarnPaths(rootDepKeys, graph, map) {
|
|
|
97
159
|
if (visitedStates.has(stateKey))
|
|
98
160
|
continue;
|
|
99
161
|
visitedStates.add(stateKey);
|
|
100
|
-
|
|
101
|
-
if (
|
|
102
|
-
|
|
103
|
-
}
|
|
162
|
+
// Only record paths for packages that exist in the lockfile map, matching prior behavior.
|
|
163
|
+
if (map.has(ref.key))
|
|
164
|
+
addTaggedPath(taggedPathsByKey, ref.key, nextPath);
|
|
104
165
|
if (nextPath.length >= MAX_PATH_DEPTH)
|
|
105
166
|
continue;
|
|
106
|
-
|
|
167
|
+
const recorded = taggedPathsByKey.get(ref.key) ?? [];
|
|
168
|
+
if (recorded.length >= MAX_PATHS_PER_PACKAGE && !recorded.some(entry => pathsEqual(entry.path, nextPath))) {
|
|
107
169
|
continue;
|
|
108
170
|
}
|
|
109
171
|
for (const child of graph.get(ref.key) ?? []) {
|
|
@@ -111,40 +173,54 @@ function collectYarnPaths(rootDepKeys, graph, map) {
|
|
|
111
173
|
}
|
|
112
174
|
}
|
|
113
175
|
}
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
catch {
|
|
121
|
-
return { keys: [], devDepNames: new Set() };
|
|
122
|
-
}
|
|
123
|
-
const devDepNames = new Set(Object.keys(rootPkg.devDependencies ?? {}));
|
|
124
|
-
const rootDepKeys = [];
|
|
176
|
+
// Resolve one package.json's dependency declarations into BFS seeds. The pathPrefix is
|
|
177
|
+
// the parent chain the seed hangs off: ["project"] for the root, ["project", <workspace>]
|
|
178
|
+
// for a workspace package. Unresolved direct deps (present in package.json but not the
|
|
179
|
+
// lockfile) fall back to a shallow path so they are still attributed.
|
|
180
|
+
function resolveDepSeeds(pkgJson, pathPrefix, selectorToKey, map, taggedPathsByKey, prodFallbackKeys) {
|
|
181
|
+
const seeds = [];
|
|
125
182
|
for (const depSection of ["dependencies", "optionalDependencies", "devDependencies"]) {
|
|
126
|
-
const deps =
|
|
183
|
+
const deps = pkgJson?.[depSection];
|
|
127
184
|
if (!deps || typeof deps !== "object")
|
|
128
185
|
continue;
|
|
186
|
+
const dev = depSection === "devDependencies";
|
|
129
187
|
for (const [depName, depRange] of Object.entries(deps)) {
|
|
130
188
|
const resolved = selectorToKey.get(`${depName}@${depRange}`) ??
|
|
131
189
|
selectorToKey.get(`${depName}@npm:${depRange}`);
|
|
132
190
|
if (resolved) {
|
|
133
|
-
|
|
191
|
+
seeds.push({ key: resolved, path: pathPrefix, dev });
|
|
134
192
|
continue;
|
|
135
193
|
}
|
|
136
194
|
const fallbackVersion = normalizeRawVersion(depRange);
|
|
137
195
|
if (fallbackVersion) {
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
196
|
+
const name = String(depName);
|
|
197
|
+
const key = `${name}@${fallbackVersion}`;
|
|
198
|
+
upsertPackage(map, { name, version: fallbackVersion, ecosystem: "npm", paths: [] });
|
|
199
|
+
addTaggedPath(taggedPathsByKey, key, [...pathPrefix, name]);
|
|
200
|
+
// Unresolved fallback prod deps are not graph nodes, so record their key as
|
|
201
|
+
// production-reachable directly (mirrors the resolved production roots).
|
|
202
|
+
if (!dev)
|
|
203
|
+
prodFallbackKeys.add(key);
|
|
144
204
|
}
|
|
145
205
|
}
|
|
146
206
|
}
|
|
147
|
-
return
|
|
207
|
+
return seeds;
|
|
208
|
+
}
|
|
209
|
+
// Build the BFS seeds for a project. Seeds come from the root package.json (parent chain
|
|
210
|
+
// ["project"]) and from every workspace package (parent chain ["project", <workspace>]),
|
|
211
|
+
// so transitives reachable only through a workspace resolve back to that workspace instead
|
|
212
|
+
// of reporting "No parent identified". Each seed carries whether its declaring edge is a
|
|
213
|
+
// devDependency (used to pick the production roots for reachability-based classification);
|
|
214
|
+
// prodFallbackKeys collects unresolved production direct deps that are not graph nodes.
|
|
215
|
+
function loadSeeds(filePath, selectorToKey, map, taggedPathsByKey, prodFallbackKeys) {
|
|
216
|
+
const rootPkg = readRootPackageJson(filePath);
|
|
217
|
+
if (!rootPkg)
|
|
218
|
+
return [];
|
|
219
|
+
const seeds = resolveDepSeeds(rootPkg, ["project"], selectorToKey, map, taggedPathsByKey, prodFallbackKeys);
|
|
220
|
+
for (const { name: wsName, pkg: wsPkg } of readWorkspacePackages(filePath, rootPkg)) {
|
|
221
|
+
seeds.push(...resolveDepSeeds(wsPkg, ["project", wsName], selectorToKey, map, taggedPathsByKey, prodFallbackKeys));
|
|
222
|
+
}
|
|
223
|
+
return seeds;
|
|
148
224
|
}
|
|
149
225
|
function parseBerryDependencies(block) {
|
|
150
226
|
const dependencies = {};
|
|
@@ -214,9 +290,12 @@ function loadFromYarnBerryLock(content, filePath) {
|
|
|
214
290
|
}
|
|
215
291
|
graph.set(canonicalKey, [...depKeys]);
|
|
216
292
|
}
|
|
217
|
-
const
|
|
218
|
-
|
|
219
|
-
|
|
293
|
+
const taggedPathsByKey = new Map();
|
|
294
|
+
const prodFallbackKeys = new Set();
|
|
295
|
+
const seeds = loadSeeds(filePath, selectorToKey, map, taggedPathsByKey, prodFallbackKeys);
|
|
296
|
+
collectYarnPaths(seeds, graph, map, taggedPathsByKey);
|
|
297
|
+
applyTaggedPaths(map, taggedPathsByKey);
|
|
298
|
+
markYarnDevByProdReachability(map, graph, seeds.filter(seed => !seed.dev).map(seed => seed.key), prodFallbackKeys);
|
|
220
299
|
return [...map.values()];
|
|
221
300
|
}
|
|
222
301
|
function loadFromYarnClassicLock(parsed, filePath) {
|
|
@@ -261,9 +340,12 @@ function loadFromYarnClassicLock(parsed, filePath) {
|
|
|
261
340
|
}
|
|
262
341
|
graph.set(canonicalKey, [...depKeys]);
|
|
263
342
|
}
|
|
264
|
-
const
|
|
265
|
-
|
|
266
|
-
|
|
343
|
+
const taggedPathsByKey = new Map();
|
|
344
|
+
const prodFallbackKeys = new Set();
|
|
345
|
+
const seeds = loadSeeds(filePath, selectorToKey, map, taggedPathsByKey, prodFallbackKeys);
|
|
346
|
+
collectYarnPaths(seeds, graph, map, taggedPathsByKey);
|
|
347
|
+
applyTaggedPaths(map, taggedPathsByKey);
|
|
348
|
+
markYarnDevByProdReachability(map, graph, seeds.filter(seed => !seed.dev).map(seed => seed.key), prodFallbackKeys);
|
|
267
349
|
return [...map.values()];
|
|
268
350
|
}
|
|
269
351
|
export function loadFromYarnLock(filePath) {
|