Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(npm): use lockfileVersion from npm-shrinkwrap #26726

Merged
merged 1 commit into from Jan 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
41 changes: 40 additions & 1 deletion lib/modules/manager/npm/post-update/npm.spec.ts
Expand Up @@ -199,7 +199,7 @@ describe('modules/manager/npm/post-update/npm', () => {
]);
});

it('deduplicates dependencies after installation with npm <= 6', async () => {
it('deduplicates package-lock.json dependencies after installation with npm <= 6', async () => {
const execSnapshots = mockExecAll();
// package.json
fs.readLocalFile.mockResolvedValueOnce('{}');
Expand Down Expand Up @@ -234,6 +234,45 @@ describe('modules/manager/npm/post-update/npm', () => {
]);
});

it('deduplicates npm-shrinkwrap.json dependencies after installation with npm <= 6', async () => {
const execSnapshots = mockExecAll();
// package.json
fs.readLocalFile.mockResolvedValueOnce('{}');
const packageLockContents = JSON.stringify({
dependencies: {},
lockfileVersion: 1,
});
fs.readLocalFile.mockResolvedValueOnce(packageLockContents);
fs.readLocalFile.mockResolvedValueOnce(packageLockContents);
const postUpdateOptions = ['npmDedupe'];
const updates = [
{ packageName: 'some-dep', newVersion: '1.0.1', isLockfileUpdate: false },
];
const res = await npmHelper.generateLockFile(
'some-dir',
{},
'npm-shrinkwrap.json',
{ postUpdateOptions },
updates,
);
expect(fs.readLocalFile).toHaveBeenCalledTimes(3);
expect(fs.readLocalFile).toHaveBeenCalledWith(
'some-dir/npm-shrinkwrap.json',
'utf8',
);
expect(res.error).toBeFalse();
expect(res.lockFile).toBe(packageLockContents);
expect(execSnapshots).toHaveLength(2);
expect(execSnapshots).toMatchObject([
{
cmd: 'npm install --no-audit --ignore-scripts',
},
{
cmd: 'npm dedupe',
},
]);
});

it('runs twice if remediating', async () => {
const execSnapshots = mockExecAll();
fs.readLocalFile.mockResolvedValueOnce('package-lock-contents');
Expand Down
5 changes: 3 additions & 2 deletions lib/modules/manager/npm/post-update/npm.ts
Expand Up @@ -32,8 +32,9 @@ import { getPackageManagerVersion, lazyLoadPackageJson } from './utils';

async function getNpmConstraintFromPackageLock(
lockFileDir: string,
filename: string,
): Promise<string | null> {
const packageLockFileName = upath.join(lockFileDir, 'package-lock.json');
const packageLockFileName = upath.join(lockFileDir, filename);
const packageLockContents = await readLocalFile(packageLockFileName, 'utf8');
const packageLockJson = Result.parse(
packageLockContents,
Expand Down Expand Up @@ -79,7 +80,7 @@ export async function generateLockFile(
constraint:
config.constraints?.npm ??
getPackageManagerVersion('npm', await lazyPkgJson.getValue()) ??
(await getNpmConstraintFromPackageLock(lockFileDir)) ??
(await getNpmConstraintFromPackageLock(lockFileDir, filename)) ??
null,
};
const supportsPreferDedupeFlag =
Expand Down