Compare commits

...

3 Commits

Author SHA1 Message Date
Satishchoudhary94
273d25e939
Merge 844d397646 into 774c1d6296 2026-02-25 19:16:18 +05:00
Ferdinand Thiessen
774c1d6296
feat(node-version-file): support parsing devEngines field (#1283)
* feat(node-version-file): support parsing `devEngines` field

Signed-off-by: Ferdinand Thiessen <opensource@fthiessen.de>

* test: adjust for array like `devEngines`

Co-authored-by: Grigory <grigory.orlov.set@gmail.com>
Signed-off-by: Ferdinand Thiessen <opensource@fthiessen.de>

* ci(versions.yml): update actions and reduce duplicated tests

Signed-off-by: Ferdinand Thiessen <opensource@fthiessen.de>

* docs: consolidate advanced usage

Signed-off-by: Ferdinand Thiessen <opensource@fthiessen.de>

* chore: compile assets

Signed-off-by: Ferdinand Thiessen <opensource@fthiessen.de>

---------

Signed-off-by: Ferdinand Thiessen <opensource@fthiessen.de>
Co-authored-by: Grigory <grigory.orlov.set@gmail.com>
2026-02-23 12:10:57 -06:00
Satishchoudhary94
844d397646 fix(#1027): Improve yarn v4+ corepack support with better error handling
This change improves the action's handling of yarn v4+ which requires corepack.

Changes:
- Add enableCorepackIfSupported() helper function to automatically enable corepack
- Update yarn getCacheFolderPath to call corepack enable before checking yarn version
- Detect corepack-related errors and provide clear, actionable error messages
- Users can either enable corepack before the action or disable caching

The error message now clearly explains:
- The requirement for corepack with yarn v4+
- How to enable corepack: 'corepack enable'
- Alternative: disable caching with 'package-manager-cache: false'
- Link to GitHub issue for more context

Fixes #1027
Related: https://github.com/actions/setup-node/issues/1027
2026-01-18 14:11:36 +00:00
8 changed files with 209 additions and 28 deletions

View File

@ -168,6 +168,21 @@ jobs:
- name: Verify node
run: __tests__/verify-node.sh 24
version-file-dev-engines:
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
steps:
- uses: actions/checkout@v6
- name: Setup node from node version file
uses: ./
with:
node-version-file: '__tests__/data/package-dev-engines.json'
- name: Verify node
run: __tests__/verify-node.sh 20
version-file-volta:
runs-on: ${{ matrix.os }}
strategy:

View File

@ -0,0 +1,11 @@
{
"engines": {
"node": "^19"
},
"devEngines": {
"runtime": {
"name": "node",
"version": "^20"
}
}
}

View File

@ -94,22 +94,24 @@ describe('main tests', () => {
describe('getNodeVersionFromFile', () => {
each`
contents | expected
${'12'} | ${'12'}
${'12.3'} | ${'12.3'}
${'12.3.4'} | ${'12.3.4'}
${'v12.3.4'} | ${'12.3.4'}
${'lts/erbium'} | ${'lts/erbium'}
${'lts/*'} | ${'lts/*'}
${'nodejs 12.3.4'} | ${'12.3.4'}
${'ruby 2.3.4\nnodejs 12.3.4\npython 3.4.5'} | ${'12.3.4'}
${''} | ${''}
${'unknown format'} | ${'unknown format'}
${' 14.1.0 '} | ${'14.1.0'}
${'{"volta": {"node": ">=14.0.0 <=17.0.0"}}'}| ${'>=14.0.0 <=17.0.0'}
${'{"volta": {"extends": "./package.json"}}'}| ${'18.0.0'}
${'{"engines": {"node": "17.0.0"}}'} | ${'17.0.0'}
${'{}'} | ${null}
contents | expected
${'12'} | ${'12'}
${'12.3'} | ${'12.3'}
${'12.3.4'} | ${'12.3.4'}
${'v12.3.4'} | ${'12.3.4'}
${'lts/erbium'} | ${'lts/erbium'}
${'lts/*'} | ${'lts/*'}
${'nodejs 12.3.4'} | ${'12.3.4'}
${'ruby 2.3.4\nnodejs 12.3.4\npython 3.4.5'} | ${'12.3.4'}
${''} | ${''}
${'unknown format'} | ${'unknown format'}
${' 14.1.0 '} | ${'14.1.0'}
${'{}'} | ${null}
${'{"volta": {"node": ">=14.0.0 <=17.0.0"}}'} | ${'>=14.0.0 <=17.0.0'}
${'{"volta": {"extends": "./package.json"}}'} | ${'18.0.0'}
${'{"engines": {"node": "17.0.0"}}'} | ${'17.0.0'}
${'{"devEngines": {"runtime": {"name": "node", "version": "22.0.0"}}}'} | ${'22.0.0'}
${'{"devEngines": {"runtime": [{"name": "bun"}, {"name": "node", "version": "22.0.0"}]}}'} | ${'22.0.0'}
`.it('parses "$contents"', ({contents, expected}) => {
const existsSpy = jest.spyOn(fs, 'existsSync');
existsSpy.mockImplementation(() => true);

View File

@ -71586,7 +71586,24 @@ exports.supportedPackageManagers = {
name: 'yarn',
lockFilePatterns: ['yarn.lock'],
getCacheFolderPath: async (projectDir) => {
const yarnVersion = await (0, exports.getCommandOutputNotEmpty)(`yarn --version`, 'Could not retrieve version of yarn', projectDir);
// Try to enable corepack first if available
// This helps with yarn v2+ which requires corepack
await enableCorepackIfSupported();
let yarnVersion;
try {
yarnVersion = await (0, exports.getCommandOutputNotEmpty)(`yarn --version`, 'Could not retrieve version of yarn', projectDir);
}
catch (err) {
// Check if this is a corepack error message
const errorMsg = err.message;
if (errorMsg.includes('packageManager') &&
errorMsg.includes('Corepack')) {
throw new Error(`Yarn v4+ requires corepack to be enabled. Please run 'corepack enable' before using ` +
`actions/setup-node with yarn, or disable caching with 'package-manager-cache: false'. ` +
`See: https://github.com/actions/setup-node/issues/1027 for more information.`);
}
throw err;
}
core.debug(`Consumed yarn version is ${yarnVersion} (working dir: "${projectDir || ''}")`);
const stdOut = yarnVersion.startsWith('1.')
? await (0, exports.getCommandOutput)('yarn cache dir', projectDir)
@ -71598,6 +71615,24 @@ exports.supportedPackageManagers = {
}
}
};
/**
* Tries to enable corepack for Node.js versions that support it (16.9+)
* This helps with yarn v2+ which requires corepack
* See: https://github.com/actions/setup-node/issues/1027
*/
const enableCorepackIfSupported = async () => {
try {
await exec.exec('corepack', ['enable'], {
ignoreReturnCode: true,
silent: true
});
core.debug('Corepack enabled successfully');
}
catch {
// Corepack not available or failed silently
core.debug('Corepack not available on this system');
}
};
const getCommandOutput = async (toolCommand, cwd) => {
let { stdout, stderr, exitCode } = await exec.getExecOutput(toolCommand, undefined, { ignoreReturnCode: true, ...(cwd && { cwd }) });
if (exitCode) {
@ -71877,6 +71912,17 @@ function getNodeVersionFromFile(versionFilePath) {
if (manifest.volta?.node) {
return manifest.volta.node;
}
// support devEngines from npm 11
if (manifest.devEngines?.runtime) {
// find an entry with name set to node and having set a version.
// the devEngines.runtime can either be an object or an array of objects
const nodeEntry = [manifest.devEngines.runtime]
.flat()
.find(({ name, version }) => name?.toLowerCase() === 'node' && version);
if (nodeEntry) {
return nodeEntry.version;
}
}
if (manifest.engines?.node) {
return manifest.engines.node;
}

48
dist/setup/index.js vendored
View File

@ -81224,7 +81224,24 @@ exports.supportedPackageManagers = {
name: 'yarn',
lockFilePatterns: ['yarn.lock'],
getCacheFolderPath: async (projectDir) => {
const yarnVersion = await (0, exports.getCommandOutputNotEmpty)(`yarn --version`, 'Could not retrieve version of yarn', projectDir);
// Try to enable corepack first if available
// This helps with yarn v2+ which requires corepack
await enableCorepackIfSupported();
let yarnVersion;
try {
yarnVersion = await (0, exports.getCommandOutputNotEmpty)(`yarn --version`, 'Could not retrieve version of yarn', projectDir);
}
catch (err) {
// Check if this is a corepack error message
const errorMsg = err.message;
if (errorMsg.includes('packageManager') &&
errorMsg.includes('Corepack')) {
throw new Error(`Yarn v4+ requires corepack to be enabled. Please run 'corepack enable' before using ` +
`actions/setup-node with yarn, or disable caching with 'package-manager-cache: false'. ` +
`See: https://github.com/actions/setup-node/issues/1027 for more information.`);
}
throw err;
}
core.debug(`Consumed yarn version is ${yarnVersion} (working dir: "${projectDir || ''}")`);
const stdOut = yarnVersion.startsWith('1.')
? await (0, exports.getCommandOutput)('yarn cache dir', projectDir)
@ -81236,6 +81253,24 @@ exports.supportedPackageManagers = {
}
}
};
/**
* Tries to enable corepack for Node.js versions that support it (16.9+)
* This helps with yarn v2+ which requires corepack
* See: https://github.com/actions/setup-node/issues/1027
*/
const enableCorepackIfSupported = async () => {
try {
await exec.exec('corepack', ['enable'], {
ignoreReturnCode: true,
silent: true
});
core.debug('Corepack enabled successfully');
}
catch {
// Corepack not available or failed silently
core.debug('Corepack not available on this system');
}
};
const getCommandOutput = async (toolCommand, cwd) => {
let { stdout, stderr, exitCode } = await exec.getExecOutput(toolCommand, undefined, { ignoreReturnCode: true, ...(cwd && { cwd }) });
if (exitCode) {
@ -82415,6 +82450,17 @@ function getNodeVersionFromFile(versionFilePath) {
if (manifest.volta?.node) {
return manifest.volta.node;
}
// support devEngines from npm 11
if (manifest.devEngines?.runtime) {
// find an entry with name set to node and having set a version.
// the devEngines.runtime can either be an object or an array of objects
const nodeEntry = [manifest.devEngines.runtime]
.flat()
.find(({ name, version }) => name?.toLowerCase() === 'node' && version);
if (nodeEntry) {
return nodeEntry.version;
}
}
if (manifest.engines?.node) {
return manifest.engines.node;
}

View File

@ -90,21 +90,31 @@ steps:
- run: npm test
```
When using the `package.json` input, the action will look for `volta.node` first. If `volta.node` isn't defined, then it will look for `engines.node`.
When using the `package.json` input, the action will look in the following fields for a specified Node version:
1. It checks `volta.node` first.
2. Then it checks `devEngines.runtime` for an entry with `"name": "node"`.
3. Then it will look for `engines.node`.
4. Otherwise it tries to resolve the file defined by [`volta.extends`](https://docs.volta.sh/advanced/workspaces)
and look for `volta.node`, `devEngines.runtime`, or `engines.node` recursively.
```json
{
"engines": {
"node": ">=16.0.0"
"node": "^22 || ^24"
},
"devEngines": {
"runtime": {
"name": "node",
"version": "^24.3"
}
},
"volta": {
"node": "16.0.0"
"node": "24.11.1"
}
}
```
Otherwise, when [`volta.extends`](https://docs.volta.sh/advanced/workspaces) is defined, then it will resolve the corresponding file and look for `volta.node` or `engines.node` recursively.
## Architecture
You can use any of the [supported operating systems](https://docs.github.com/en/actions/using-github-hosted-runners/about-github-hosted-runners), and the compatible `architecture` can be selected using `architecture`. Values are `x86`, `x64`, `arm64`, `armv6l`, `armv7l`, `ppc64le`, `s390x` (not all of the architectures are available on all platforms).

View File

@ -40,11 +40,32 @@ export const supportedPackageManagers: SupportedPackageManagers = {
name: 'yarn',
lockFilePatterns: ['yarn.lock'],
getCacheFolderPath: async projectDir => {
const yarnVersion = await getCommandOutputNotEmpty(
`yarn --version`,
'Could not retrieve version of yarn',
projectDir
);
// Try to enable corepack first if available
// This helps with yarn v2+ which requires corepack
await enableCorepackIfSupported();
let yarnVersion: string;
try {
yarnVersion = await getCommandOutputNotEmpty(
`yarn --version`,
'Could not retrieve version of yarn',
projectDir
);
} catch (err) {
// Check if this is a corepack error message
const errorMsg = (err as Error).message;
if (
errorMsg.includes('packageManager') &&
errorMsg.includes('Corepack')
) {
throw new Error(
`Yarn v4+ requires corepack to be enabled. Please run 'corepack enable' before using ` +
`actions/setup-node with yarn, or disable caching with 'package-manager-cache: false'. ` +
`See: https://github.com/actions/setup-node/issues/1027 for more information.`
);
}
throw err;
}
core.debug(
`Consumed yarn version is ${yarnVersion} (working dir: "${
@ -66,6 +87,24 @@ export const supportedPackageManagers: SupportedPackageManagers = {
}
};
/**
* Tries to enable corepack for Node.js versions that support it (16.9+)
* This helps with yarn v2+ which requires corepack
* See: https://github.com/actions/setup-node/issues/1027
*/
const enableCorepackIfSupported = async (): Promise<void> => {
try {
await exec.exec('corepack', ['enable'], {
ignoreReturnCode: true,
silent: true
});
core.debug('Corepack enabled successfully');
} catch {
// Corepack not available or failed silently
core.debug('Corepack not available on this system');
}
};
export const getCommandOutput = async (
toolCommand: string,
cwd?: string

View File

@ -26,6 +26,18 @@ export function getNodeVersionFromFile(versionFilePath: string): string | null {
return manifest.volta.node;
}
// support devEngines from npm 11
if (manifest.devEngines?.runtime) {
// find an entry with name set to node and having set a version.
// the devEngines.runtime can either be an object or an array of objects
const nodeEntry = [manifest.devEngines.runtime]
.flat()
.find(({name, version}) => name?.toLowerCase() === 'node' && version);
if (nodeEntry) {
return nodeEntry.version;
}
}
if (manifest.engines?.node) {
return manifest.engines.node;
}