Skip to content

Commit a5cb8b6

Browse files
authored
Merge pull request #252 from snyk/fix/fix-empty-lockfile
fix: handle pnpm empty lockfiles
2 parents 5133a51 + 45d6de9 commit a5cb8b6

File tree

13 files changed

+127
-10
lines changed

13 files changed

+127
-10
lines changed

lib/dep-graph-builders/pnpm/lockfile-parser/index.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,16 @@ import { OpenSourceEcosystems } from '@snyk/error-catalog-nodejs-public';
88
import { NodeLockfileVersion } from '../../../utils';
99

1010
export function getPnpmLockfileParser(
11-
pnpmLockContent: string,
11+
pnpmLockContent: string | undefined,
1212
lockfileVersion?: NodeLockfileVersion,
1313
workspaceArgs?: PnpmWorkspaceArgs,
1414
): PnpmLockfileParser {
15+
// In case of no dependencies, pnpm@7 (lokfile version 5)
16+
// does not create a lockfile at `pnpm install`
17+
// so if there is no lockfile content, default to lockfile version 5
18+
if (!pnpmLockContent) {
19+
return new LockfileV5Parser(pnpmLockContent, workspaceArgs);
20+
}
1521
const rawPnpmLock = load(pnpmLockContent, {
1622
json: true,
1723
schema: FAILSAFE_SCHEMA,

lib/dep-graph-builders/pnpm/lockfile-parser/lockfile-v5.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@ import { PnpmWorkspaceArgs } from '../../types';
55

66
export class LockfileV5Parser extends PnpmLockfileParser {
77
public constructor(rawPnpmLock: any, workspaceArgs?: PnpmWorkspaceArgs) {
8+
// In case of no dependencies, pnpm@7 (lokfile version 5)
9+
// does not create a lockfile at `pnpm install`
10+
if (!rawPnpmLock) {
11+
rawPnpmLock = {
12+
lockfileVersion: '5',
13+
};
14+
}
815
super(rawPnpmLock, workspaceArgs);
916
}
1017

lib/dep-graph-builders/pnpm/lockfile-parser/lockfile-v9.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ export class LockfileV9Parser extends LockfileV6Parser {
1616
super(rawPnpmLock, workspaceArgs);
1717
this.settings = rawPnpmLock.settings;
1818
this.packages = {};
19-
Object.entries(rawPnpmLock.snapshots).forEach(
19+
this.snapshots = rawPnpmLock.snapshots || {};
20+
Object.entries(this.snapshots).forEach(
2021
([depPath, versionData]: [string, any]) => {
2122
const normalizedDepPath = this.excludeTransPeerDepsVersions(depPath);
2223
this.packages[normalizedDepPath] = {

lib/dep-graph-builders/pnpm/parse-project.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { NodeLockfileVersion } from '../../utils';
77

88
export const parsePnpmProject = async (
99
pkgJsonContent: string,
10-
pnpmLockContent: string,
10+
pnpmLockContent: string | undefined,
1111
options: PnpmProjectParseOptions,
1212
lockfileVersion?: NodeLockfileVersion,
1313
): Promise<DepGraph> => {
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"schemaVersion": "1.3.0",
3+
"pkgManager": {
4+
"name": "pnpm"
5+
},
6+
"pkgs": [
7+
{
8+
9+
"info": {
10+
"name": "empty-project",
11+
"version": "1.0.0"
12+
}
13+
}
14+
],
15+
"graph": {
16+
"rootNodeId": "root-node",
17+
"nodes": [
18+
{
19+
"nodeId": "root-node",
20+
"pkgId": "[email protected]",
21+
"deps": []
22+
}
23+
]
24+
}
25+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"name": "empty-project",
3+
"version": "1.0.0"
4+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"schemaVersion": "1.3.0",
3+
"pkgManager": {
4+
"name": "pnpm"
5+
},
6+
"pkgs": [
7+
{
8+
9+
"info": {
10+
"name": "empty-project",
11+
"version": "1.0.0"
12+
}
13+
}
14+
],
15+
"graph": {
16+
"rootNodeId": "root-node",
17+
"nodes": [
18+
{
19+
"nodeId": "root-node",
20+
"pkgId": "[email protected]",
21+
"deps": []
22+
}
23+
]
24+
}
25+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"name": "empty-project",
3+
"version": "1.0.0"
4+
}

test/jest/dep-graph-builders/fixtures/pnpm-lock-v6/empty-project/pnpm-lock.yaml

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"schemaVersion": "1.3.0",
3+
"pkgManager": {
4+
"name": "pnpm"
5+
},
6+
"pkgs": [
7+
{
8+
9+
"info": {
10+
"name": "empty-project",
11+
"version": "1.0.0"
12+
}
13+
}
14+
],
15+
"graph": {
16+
"rootNodeId": "root-node",
17+
"nodes": [
18+
{
19+
"nodeId": "root-node",
20+
"pkgId": "[email protected]",
21+
"deps": []
22+
}
23+
]
24+
}
25+
}

0 commit comments

Comments
 (0)