Skip to content

Commit 6daf50a

Browse files
Merge pull request #291 from snyk/fix/npm-lock-v2-workspace-dependency-resolution
fix: resolve workspace package dependencies in npm lock v2 parser
2 parents 208dcf5 + 81f09a7 commit 6daf50a

File tree

5 files changed

+146
-0
lines changed

5 files changed

+146
-0
lines changed

lib/dep-graph-builders/npm-lock-v2/index.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,14 @@ export const getChildNodeKey = (
490490
if (pkgs[fullPath]) {
491491
return pkgs[fullPath].name || segment;
492492
}
493+
494+
// For workspace packages, the path might not have node_modules/ prefix
495+
// Check if this segment (or joined path) exists as a workspace package
496+
const workspacePath = pathSegments.join('/node_modules/');
497+
if (pkgs[workspacePath]) {
498+
return pkgs[workspacePath].name || segment;
499+
}
500+
493501
return segment;
494502
});
495503

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
{
2+
"schemaVersion": "1.3.0",
3+
"pkgManager": {
4+
"name": "npm"
5+
},
6+
"pkgs": [
7+
{
8+
9+
"info": {
10+
"name": "root-workspace",
11+
"version": "1.0.0"
12+
}
13+
},
14+
{
15+
"id": "@test/[email protected]",
16+
"info": {
17+
"name": "@test/workspace-pkg",
18+
"version": "1.0.0"
19+
}
20+
},
21+
{
22+
23+
"info": {
24+
"name": "lodash",
25+
"version": "4.17.21"
26+
}
27+
}
28+
],
29+
"graph": {
30+
"rootNodeId": "root-node",
31+
"nodes": [
32+
{
33+
"nodeId": "root-node",
34+
"pkgId": "[email protected]",
35+
"deps": [
36+
{
37+
"nodeId": "@test/[email protected]"
38+
}
39+
]
40+
},
41+
{
42+
"nodeId": "@test/[email protected]",
43+
"pkgId": "@test/[email protected]",
44+
"deps": [
45+
{
46+
"nodeId": "[email protected]"
47+
}
48+
],
49+
"info": {
50+
"labels": {
51+
"scope": "prod"
52+
}
53+
}
54+
},
55+
{
56+
"nodeId": "[email protected]",
57+
"pkgId": "[email protected]",
58+
"deps": [],
59+
"info": {
60+
"labels": {
61+
"scope": "prod"
62+
}
63+
}
64+
}
65+
]
66+
}
67+
}

test/jest/dep-graph-builders/fixtures/npm-lock-v2/workspace-nested-deps/package-lock.json

Lines changed: 59 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"name": "root-workspace",
3+
"version": "1.0.0",
4+
"workspaces": [
5+
"packages/*"
6+
],
7+
"dependencies": {
8+
"@test/workspace-pkg": "file:packages/workspace-pkg"
9+
},
10+
"devDependencies": {}
11+
}

test/jest/dep-graph-builders/npm-lock-v2.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ describe('dep-graph-builder npm-lock-v2', () => {
2424
'dist-tag-sub-dependency',
2525
'bundled-top-level-dep',
2626
'missing-optional-dep-minimal',
27+
'workspace-nested-deps',
2728
])('[simple tests] project: %s ', (fixtureName) => {
2829
it('matches expected', async () => {
2930
const pkgJsonContent = readFileSync(

0 commit comments

Comments
 (0)