Skip to content

Commit

Permalink
feat: add error to fail early on workspace scanned as single proj (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
gemaxim authored Jul 16, 2024
1 parent 07138fc commit 307cede
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
14 changes: 14 additions & 0 deletions lib/lock-parser/build-dep-graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,24 @@ export async function buildDepGraph(
const manifestFileContents = fs.readFileSync(manifestFileFullPath, 'utf-8');
const lockFileContents = fs.readFileSync(lockFileFullPath, 'utf-8');

const workspaceRootPath = path.parse(lockFileFullPath).dir;
const workspaceFileFullPath = path.resolve(
workspaceRootPath,
'pnpm-workspace.yaml',
);

switch (lockfileVersion) {
case NodeLockfileVersion.PnpmLockV5:
case NodeLockfileVersion.PnpmLockV6:
case NodeLockfileVersion.PnpmLockV9:
if (fs.existsSync(workspaceFileFullPath)) {
throw new InvalidUserInputError(
'Both `pnpm-lock.yaml` and `pnpm-workspace.yaml` were found in ' +
workspaceRootPath +
'.\n' +
'Please run your command again specifying `--all-projects` flag.',
);
}
return await lockFileParser.parsePnpmProject(
manifestFileContents,
lockFileContents,
Expand Down
25 changes: 25 additions & 0 deletions test/inspect.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { InvalidUserInputError } from 'snyk-nodejs-lockfile-parser';
import { inspect } from '../lib/index';
import * as path from 'path';

Expand Down Expand Up @@ -64,5 +65,29 @@ describe('inspect', () => {
expect(result.scannedProjects[0].depGraph?.toJSON()).not.toEqual({});
},
);

it('should throw error trying to scan a pnpm workspace as a simple file', async () => {
const packageManager = 'pnpm',
lockFileVersion = '9',
fixture = 'workspace-with-cross-ref',
targetFile = 'pnpm-lock.yaml';
const fixturePath = path.resolve(
__dirname,
'fixtures',
packageManager,
`lock-v${lockFileVersion}`,
fixture,
);
process.chdir(fixturePath);

await expect(() => inspect('.', targetFile, {})).rejects.toThrow(
new InvalidUserInputError(
'Both `pnpm-lock.yaml` and `pnpm-workspace.yaml` were found in ' +
fixturePath +
'.\n' +
'Please run your command again specifying `--all-projects` flag.',
),
);
});
});
});

0 comments on commit 307cede

Please sign in to comment.