mirror of
https://github.com/actions/setup-node.git
synced 2026-03-04 06:51:04 +08:00
Compare commits
3 Commits
1b5409412c
...
273d25e939
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
273d25e939 | ||
|
|
774c1d6296 | ||
|
|
844d397646 |
15
.github/workflows/versions.yml
vendored
15
.github/workflows/versions.yml
vendored
@ -168,6 +168,21 @@ jobs:
|
|||||||
- name: Verify node
|
- name: Verify node
|
||||||
run: __tests__/verify-node.sh 24
|
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:
|
version-file-volta:
|
||||||
runs-on: ${{ matrix.os }}
|
runs-on: ${{ matrix.os }}
|
||||||
strategy:
|
strategy:
|
||||||
|
|||||||
11
__tests__/data/package-dev-engines.json
Normal file
11
__tests__/data/package-dev-engines.json
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
{
|
||||||
|
"engines": {
|
||||||
|
"node": "^19"
|
||||||
|
},
|
||||||
|
"devEngines": {
|
||||||
|
"runtime": {
|
||||||
|
"name": "node",
|
||||||
|
"version": "^20"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -94,22 +94,24 @@ describe('main tests', () => {
|
|||||||
|
|
||||||
describe('getNodeVersionFromFile', () => {
|
describe('getNodeVersionFromFile', () => {
|
||||||
each`
|
each`
|
||||||
contents | expected
|
contents | expected
|
||||||
${'12'} | ${'12'}
|
${'12'} | ${'12'}
|
||||||
${'12.3'} | ${'12.3'}
|
${'12.3'} | ${'12.3'}
|
||||||
${'12.3.4'} | ${'12.3.4'}
|
${'12.3.4'} | ${'12.3.4'}
|
||||||
${'v12.3.4'} | ${'12.3.4'}
|
${'v12.3.4'} | ${'12.3.4'}
|
||||||
${'lts/erbium'} | ${'lts/erbium'}
|
${'lts/erbium'} | ${'lts/erbium'}
|
||||||
${'lts/*'} | ${'lts/*'}
|
${'lts/*'} | ${'lts/*'}
|
||||||
${'nodejs 12.3.4'} | ${'12.3.4'}
|
${'nodejs 12.3.4'} | ${'12.3.4'}
|
||||||
${'ruby 2.3.4\nnodejs 12.3.4\npython 3.4.5'} | ${'12.3.4'}
|
${'ruby 2.3.4\nnodejs 12.3.4\npython 3.4.5'} | ${'12.3.4'}
|
||||||
${''} | ${''}
|
${''} | ${''}
|
||||||
${'unknown format'} | ${'unknown format'}
|
${'unknown format'} | ${'unknown format'}
|
||||||
${' 14.1.0 '} | ${'14.1.0'}
|
${' 14.1.0 '} | ${'14.1.0'}
|
||||||
${'{"volta": {"node": ">=14.0.0 <=17.0.0"}}'}| ${'>=14.0.0 <=17.0.0'}
|
${'{}'} | ${null}
|
||||||
${'{"volta": {"extends": "./package.json"}}'}| ${'18.0.0'}
|
${'{"volta": {"node": ">=14.0.0 <=17.0.0"}}'} | ${'>=14.0.0 <=17.0.0'}
|
||||||
${'{"engines": {"node": "17.0.0"}}'} | ${'17.0.0'}
|
${'{"volta": {"extends": "./package.json"}}'} | ${'18.0.0'}
|
||||||
${'{}'} | ${null}
|
${'{"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}) => {
|
`.it('parses "$contents"', ({contents, expected}) => {
|
||||||
const existsSpy = jest.spyOn(fs, 'existsSync');
|
const existsSpy = jest.spyOn(fs, 'existsSync');
|
||||||
existsSpy.mockImplementation(() => true);
|
existsSpy.mockImplementation(() => true);
|
||||||
|
|||||||
48
dist/cache-save/index.js
vendored
48
dist/cache-save/index.js
vendored
@ -71586,7 +71586,24 @@ exports.supportedPackageManagers = {
|
|||||||
name: 'yarn',
|
name: 'yarn',
|
||||||
lockFilePatterns: ['yarn.lock'],
|
lockFilePatterns: ['yarn.lock'],
|
||||||
getCacheFolderPath: async (projectDir) => {
|
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 || ''}")`);
|
core.debug(`Consumed yarn version is ${yarnVersion} (working dir: "${projectDir || ''}")`);
|
||||||
const stdOut = yarnVersion.startsWith('1.')
|
const stdOut = yarnVersion.startsWith('1.')
|
||||||
? await (0, exports.getCommandOutput)('yarn cache dir', projectDir)
|
? 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) => {
|
const getCommandOutput = async (toolCommand, cwd) => {
|
||||||
let { stdout, stderr, exitCode } = await exec.getExecOutput(toolCommand, undefined, { ignoreReturnCode: true, ...(cwd && { cwd }) });
|
let { stdout, stderr, exitCode } = await exec.getExecOutput(toolCommand, undefined, { ignoreReturnCode: true, ...(cwd && { cwd }) });
|
||||||
if (exitCode) {
|
if (exitCode) {
|
||||||
@ -71877,6 +71912,17 @@ function getNodeVersionFromFile(versionFilePath) {
|
|||||||
if (manifest.volta?.node) {
|
if (manifest.volta?.node) {
|
||||||
return 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) {
|
if (manifest.engines?.node) {
|
||||||
return manifest.engines.node;
|
return manifest.engines.node;
|
||||||
}
|
}
|
||||||
|
|||||||
48
dist/setup/index.js
vendored
48
dist/setup/index.js
vendored
@ -81224,7 +81224,24 @@ exports.supportedPackageManagers = {
|
|||||||
name: 'yarn',
|
name: 'yarn',
|
||||||
lockFilePatterns: ['yarn.lock'],
|
lockFilePatterns: ['yarn.lock'],
|
||||||
getCacheFolderPath: async (projectDir) => {
|
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 || ''}")`);
|
core.debug(`Consumed yarn version is ${yarnVersion} (working dir: "${projectDir || ''}")`);
|
||||||
const stdOut = yarnVersion.startsWith('1.')
|
const stdOut = yarnVersion.startsWith('1.')
|
||||||
? await (0, exports.getCommandOutput)('yarn cache dir', projectDir)
|
? 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) => {
|
const getCommandOutput = async (toolCommand, cwd) => {
|
||||||
let { stdout, stderr, exitCode } = await exec.getExecOutput(toolCommand, undefined, { ignoreReturnCode: true, ...(cwd && { cwd }) });
|
let { stdout, stderr, exitCode } = await exec.getExecOutput(toolCommand, undefined, { ignoreReturnCode: true, ...(cwd && { cwd }) });
|
||||||
if (exitCode) {
|
if (exitCode) {
|
||||||
@ -82415,6 +82450,17 @@ function getNodeVersionFromFile(versionFilePath) {
|
|||||||
if (manifest.volta?.node) {
|
if (manifest.volta?.node) {
|
||||||
return 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) {
|
if (manifest.engines?.node) {
|
||||||
return manifest.engines.node;
|
return manifest.engines.node;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -90,21 +90,31 @@ steps:
|
|||||||
- run: npm test
|
- 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
|
```json
|
||||||
{
|
{
|
||||||
"engines": {
|
"engines": {
|
||||||
"node": ">=16.0.0"
|
"node": "^22 || ^24"
|
||||||
|
},
|
||||||
|
"devEngines": {
|
||||||
|
"runtime": {
|
||||||
|
"name": "node",
|
||||||
|
"version": "^24.3"
|
||||||
|
}
|
||||||
},
|
},
|
||||||
"volta": {
|
"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
|
## 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).
|
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).
|
||||||
|
|||||||
@ -40,11 +40,32 @@ export const supportedPackageManagers: SupportedPackageManagers = {
|
|||||||
name: 'yarn',
|
name: 'yarn',
|
||||||
lockFilePatterns: ['yarn.lock'],
|
lockFilePatterns: ['yarn.lock'],
|
||||||
getCacheFolderPath: async projectDir => {
|
getCacheFolderPath: async projectDir => {
|
||||||
const yarnVersion = await getCommandOutputNotEmpty(
|
// Try to enable corepack first if available
|
||||||
`yarn --version`,
|
// This helps with yarn v2+ which requires corepack
|
||||||
'Could not retrieve version of yarn',
|
await enableCorepackIfSupported();
|
||||||
projectDir
|
|
||||||
);
|
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(
|
core.debug(
|
||||||
`Consumed yarn version is ${yarnVersion} (working dir: "${
|
`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 (
|
export const getCommandOutput = async (
|
||||||
toolCommand: string,
|
toolCommand: string,
|
||||||
cwd?: string
|
cwd?: string
|
||||||
|
|||||||
12
src/util.ts
12
src/util.ts
@ -26,6 +26,18 @@ export function getNodeVersionFromFile(versionFilePath: string): string | null {
|
|||||||
return 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) {
|
if (manifest.engines?.node) {
|
||||||
return manifest.engines.node;
|
return manifest.engines.node;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user