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

feat: force node 15 for lerna when npm lockfileVersion=2 #8961

Merged
merged 6 commits into from
Mar 3, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/manager/npm/post-update/lerna.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ export async function generateLockFiles(
lernaCommand = lernaCommand.replace('--ignore-scripts ', '');
}
lernaCommand += cmdOptions;
const tagConstraint = await getNodeConstraint(config);
const allowUnstable = true; // lerna will pick the default installed npm@6 unless we use node@>=15
const tagConstraint = await getNodeConstraint(config, allowUnstable);
const execOptions: ExecOptions = {
cwd,
extraEnv: {
Expand Down
27 changes: 27 additions & 0 deletions lib/manager/npm/post-update/node-version.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,33 @@ describe('getNodeConstraint', () => {
const node16IsStable = isStable('16.100.0');
expect(isAugmentedRange || node16IsStable).toBe(true);
});
it('forces node 15 if v2 lockfile detected and constraint allows', async () => {
fs.readLocalFile.mockResolvedValueOnce(null);
fs.readLocalFile.mockResolvedValueOnce(null);
fs.readLocalFile.mockResolvedValueOnce('{"lockfileVersion":2}');
const res = await getNodeConstraint({
...config,
constraints: { node: '>= 12.16.0' },
});
const isAugmentedRange = res === '>=15';
const node16IsStable = isStable('16.100.0');
expect(isAugmentedRange || node16IsStable).toBe(true);
});
it('forces node 15 if v2 lockfile detected and no constraint', async () => {
fs.readLocalFile.mockResolvedValueOnce(null);
fs.readLocalFile.mockResolvedValueOnce(null);
fs.readLocalFile.mockResolvedValueOnce('{"lockfileVersion":2}');
const res = await getNodeConstraint(
{
...config,
constraints: {},
},
true
);
const isAugmentedRange = res === '>=15';
const node16IsStable = isStable('16.100.0');
expect(isAugmentedRange || node16IsStable).toBe(true);
});
it('returns .node-version value', async () => {
fs.readLocalFile = jest.fn();
fs.readLocalFile.mockResolvedValueOnce(null);
Expand Down
26 changes: 21 additions & 5 deletions lib/manager/npm/post-update/node-version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,26 +29,42 @@ function getPackageJsonConstraint(config: PostUpdateConfig): string | null {
}

export async function getNodeConstraint(
config: PostUpdateConfig
config: PostUpdateConfig,
allowUnstable = false
): Promise<string> | null {
const { packageFile } = config;
let constraint =
(await getNodeFile(getSiblingFileName(packageFile, '.nvmrc'))) ||
(await getNodeFile(getSiblingFileName(packageFile, '.node-version'))) ||
getPackageJsonConstraint(config);
let lockfileVersion = 1;
try {
const lockFileName = getSiblingFileName(packageFile, 'package-lock.json');
lockfileVersion = JSON.parse(await readLocalFile(lockFileName, 'utf8'))
rarkins marked this conversation as resolved.
Show resolved Hide resolved
.lockfileVersion;
} catch (err) {
// do nothing
}
// Avoid using node 15 if node 14 also satisfies the same constraint
// Remove this once node 16 is LTS
if (constraint) {
if (
validRange(constraint) &&
satisfies('14.100.0', constraint) &&
satisfies('15.100.0', constraint) &&
!isStable('16.100.0') && // this should return false as soon as Node 16 is LTS
validRange(`${constraint} <15`)
!isStable('16.100.0')
) {
logger.debug('Augmenting constraint to avoid node 15');
constraint = `${constraint} <15`;
if (lockfileVersion === 2) {
logger.debug('Forcing node 15 to ensure lockfileVersion=2 is used');
constraint = '>=15';
} else if (validRange(`${constraint} <15`)) {
logger.debug('Augmenting constraint to avoid node 15');
constraint = `${constraint} <15`;
}
}
} else if (allowUnstable && lockfileVersion === 2) {
logger.debug('Using node >=15 for lockfileVersion=2');
constraint = '>=15';
} else {
logger.debug('No node constraint found - using latest');
}
Expand Down