[build-utils] Fix warn for ignored project settings node version (#11550)

There were scenarios where the warning "Node.js Version defined in your Project Settings ("18.x") will not apply" would not be triggered. For example:

1. Project Setting is 18.x
2. package.json has "engines.node": ">=18.x"
3. semver.intersects('18.x', '>=18.x') // => true (In this [code path](8ea93839cc/packages/build-utils/src/fs/run-user-scripts.ts (L258))) -> No warning message that Node.js Version was changed
4. After the error message we evaluate the latest node Version to use (20.x) in getSupportedNodeVersion()(this [code path](8ea93839cc/packages/build-utils/src/fs/run-user-scripts.ts (L274)))
5. User doesn't get notified that we changed the node version to something different than their project setting

This fixes that scenario by getting the supported node version first.
This commit is contained in:
Austin Merrick
2024-05-14 16:23:17 -07:00
committed by GitHub
parent b09d7b6130
commit d3c1267e24
3 changed files with 34 additions and 7 deletions

View File

@@ -0,0 +1,5 @@
---
'@vercel/build-utils': patch
---
Fix triggering of ignored project settings node version warning

View File

@@ -251,15 +251,24 @@ export async function getNodeVersion(
return { ...latest, runtime: 'nodejs' };
}
const { packageJson } = await scanParentDirs(destPath, true);
let nodeVersion = config.nodeVersion || nodeVersionFallback;
let isAuto = true;
const nodeVersion = config.nodeVersion || nodeVersionFallback;
const pkgJsonNodeVersion = packageJson?.engines?.node;
const nodeVersionToUse = await getSupportedNodeVersion(
pkgJsonNodeVersion || nodeVersion,
!pkgJsonNodeVersion,
availableVersions
);
if (packageJson?.engines?.node) {
const { node } = packageJson.engines;
if (nodeVersion && validRange(node) && !intersects(nodeVersion, node)) {
if (nodeVersion && !intersects(nodeVersion, nodeVersionToUse.range)) {
console.warn(
`Warning: Due to "engines": { "node": "${node}" } in your \`package.json\` file, the Node.js Version defined in your Project Settings ("${nodeVersion}") will not apply. Learn More: http://vercel.link/node-version`
);
} else if (coerce(node)?.raw === node) {
}
if (coerce(node)?.raw === node) {
console.warn(
`Warning: Detected "engines": { "node": "${node}" } in your \`package.json\` with major.minor.patch, but only major Node.js Version can be selected. Learn More: http://vercel.link/node-version`
);
@@ -268,10 +277,8 @@ export async function getNodeVersion(
`Warning: Detected "engines": { "node": "${node}" } in your \`package.json\` that will automatically upgrade when a new major Node.js Version is released. Learn More: http://vercel.link/node-version`
);
}
nodeVersion = node;
isAuto = false;
}
return getSupportedNodeVersion(nodeVersion, isAuto, availableVersions);
return nodeVersionToUse;
}
export async function scanParentDirs(

View File

@@ -221,6 +221,21 @@ it('should warn when package.json engines is greater than', async () => {
]);
});
it('should warn when project settings gets overrided', async () => {
expect(
await getNodeVersion(
path.join(__dirname, 'pkg-engine-node-greaterthan'),
undefined,
{ nodeVersion: '16.x' },
{}
)
).toHaveProperty('range', '20.x');
expect(warningMessages).toStrictEqual([
'Warning: Due to "engines": { "node": ">=16" } in your `package.json` file, the Node.js Version defined in your Project Settings ("16.x") will not apply. Learn More: http://vercel.link/node-version',
'Warning: Detected "engines": { "node": ">=16" } in your `package.json` that will automatically upgrade when a new major Node.js Version is released. Learn More: http://vercel.link/node-version',
]);
});
it('should not warn when package.json engines matches project setting from config', async () => {
expect(
await getNodeVersion(