Skip to content

Commit 307cede

Browse files
authored
feat: add error to fail early on workspace scanned as single proj (#15)
1 parent 07138fc commit 307cede

2 files changed

Lines changed: 39 additions & 0 deletions

File tree

lib/lock-parser/build-dep-graph.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,24 @@ export async function buildDepGraph(
3333
const manifestFileContents = fs.readFileSync(manifestFileFullPath, 'utf-8');
3434
const lockFileContents = fs.readFileSync(lockFileFullPath, 'utf-8');
3535

36+
const workspaceRootPath = path.parse(lockFileFullPath).dir;
37+
const workspaceFileFullPath = path.resolve(
38+
workspaceRootPath,
39+
'pnpm-workspace.yaml',
40+
);
41+
3642
switch (lockfileVersion) {
3743
case NodeLockfileVersion.PnpmLockV5:
3844
case NodeLockfileVersion.PnpmLockV6:
3945
case NodeLockfileVersion.PnpmLockV9:
46+
if (fs.existsSync(workspaceFileFullPath)) {
47+
throw new InvalidUserInputError(
48+
'Both `pnpm-lock.yaml` and `pnpm-workspace.yaml` were found in ' +
49+
workspaceRootPath +
50+
'.\n' +
51+
'Please run your command again specifying `--all-projects` flag.',
52+
);
53+
}
4054
return await lockFileParser.parsePnpmProject(
4155
manifestFileContents,
4256
lockFileContents,

test/inspect.spec.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { InvalidUserInputError } from 'snyk-nodejs-lockfile-parser';
12
import { inspect } from '../lib/index';
23
import * as path from 'path';
34

@@ -64,5 +65,29 @@ describe('inspect', () => {
6465
expect(result.scannedProjects[0].depGraph?.toJSON()).not.toEqual({});
6566
},
6667
);
68+
69+
it('should throw error trying to scan a pnpm workspace as a simple file', async () => {
70+
const packageManager = 'pnpm',
71+
lockFileVersion = '9',
72+
fixture = 'workspace-with-cross-ref',
73+
targetFile = 'pnpm-lock.yaml';
74+
const fixturePath = path.resolve(
75+
__dirname,
76+
'fixtures',
77+
packageManager,
78+
`lock-v${lockFileVersion}`,
79+
fixture,
80+
);
81+
process.chdir(fixturePath);
82+
83+
await expect(() => inspect('.', targetFile, {})).rejects.toThrow(
84+
new InvalidUserInputError(
85+
'Both `pnpm-lock.yaml` and `pnpm-workspace.yaml` were found in ' +
86+
fixturePath +
87+
'.\n' +
88+
'Please run your command again specifying `--all-projects` flag.',
89+
),
90+
);
91+
});
6792
});
6893
});

0 commit comments

Comments
 (0)