Compare commits

...

4 Commits

Author SHA1 Message Date
Satishchoudhary94
40a7750592
Merge dff445bec7 into 774c1d6296 2026-02-25 19:16:17 +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
Marco Ippolito
efcb663fc6
fix: remove hardcoded bearer (#1467) 2026-02-19 11:58:14 -06:00
Satishchoudhary94
dff445bec7 fix(#1357): Gracefully handle missing pnpm installation during cache
This change prevents the action from failing immediately when pnpm is specified
in packageManager but not yet installed (e.g., when using corepack).

Changes:
- Add isPackageManagerInstalled() function to check if a package manager exists
- Update restoreCache to skip caching with a warning if package manager not found
- Update cachePackages to skip cache save with a warning if package manager not found
- This allows workflows to continue instead of failing
- Users can either install pnpm first or disable caching with package-manager-cache: false

Fixes #1357
Related: https://github.com/actions/setup-node/issues/1357
2026-01-18 14:08:11 +00:00
11 changed files with 205 additions and 27 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

@ -71498,6 +71498,14 @@ const cachePackages = async (packageManager) => {
core.debug(`Caching for '${packageManager}' is not supported`);
return;
}
// Check if the package manager is installed before attempting to save cache
// This prevents cache save failures for package managers that may not be installed
const isInstalled = await (0, cache_utils_1.isPackageManagerInstalled)(packageManager);
if (!isInstalled) {
core.warning(`Package manager '${packageManager}' was not found in the PATH. ` +
`Skipping cache save.`);
return;
}
if (!cachePaths.length) {
// TODO: core.getInput has a bug - it can return undefined despite its definition (tests only?)
// export declare function getInput(name: string, options?: InputOptions): string;
@ -71561,7 +71569,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.repoHasYarnBerryManagedDependencies = exports.getCacheDirectories = exports.resetProjectDirectoriesMemoized = exports.getPackageManagerInfo = exports.getCommandOutputNotEmpty = exports.getCommandOutput = exports.supportedPackageManagers = void 0;
exports.repoHasYarnBerryManagedDependencies = exports.getCacheDirectories = exports.resetProjectDirectoriesMemoized = exports.isPackageManagerInstalled = exports.getPackageManagerInfo = exports.getCommandOutputNotEmpty = exports.getCommandOutput = exports.supportedPackageManagers = void 0;
exports.isGhes = isGhes;
exports.isCacheFeatureAvailable = isCacheFeatureAvailable;
const core = __importStar(__nccwpck_require__(37484));
@ -71632,6 +71640,25 @@ const getPackageManagerInfo = async (packageManager) => {
}
};
exports.getPackageManagerInfo = getPackageManagerInfo;
/**
* Checks if a package manager is installed and available on the PATH
* This helps prevent cache failures when a package manager is specified
* but not yet installed (e.g., pnpm via corepack)
* See: https://github.com/actions/setup-node/issues/1357
*/
const isPackageManagerInstalled = async (packageManager) => {
try {
const { exitCode } = await exec.getExecOutput(`${packageManager} --version`, undefined, {
ignoreReturnCode: true,
silent: true
});
return exitCode === 0;
}
catch {
return false;
}
};
exports.isPackageManagerInstalled = isPackageManagerInstalled;
/**
* getProjectDirectoriesFromCacheDependencyPath is called twice during `restoreCache`
* - first through `getCacheDirectories`
@ -71877,6 +71904,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;
}

44
dist/setup/index.js vendored
View File

@ -81110,6 +81110,16 @@ const restoreCache = async (packageManager, cacheDependencyPath) => {
if (!packageManagerInfo) {
throw new Error(`Caching for '${packageManager}' is not supported`);
}
// Check if the package manager is installed before attempting to cache
// This prevents cache failures for package managers that need to be installed first
// See: https://github.com/actions/setup-node/issues/1357
const isInstalled = await (0, cache_utils_1.isPackageManagerInstalled)(packageManager);
if (!isInstalled) {
core.warning(`Package manager '${packageManager}' was not found in the PATH. ` +
`Skipping cache restore. Please ensure the package manager is installed ` +
`before running this action or set 'package-manager-cache: false' to disable caching.`);
return;
}
const platform = process.env.RUNNER_OS;
const arch = os_1.default.arch();
const cachePaths = await (0, cache_utils_1.getCacheDirectories)(packageManagerInfo, cacheDependencyPath);
@ -81199,7 +81209,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.repoHasYarnBerryManagedDependencies = exports.getCacheDirectories = exports.resetProjectDirectoriesMemoized = exports.getPackageManagerInfo = exports.getCommandOutputNotEmpty = exports.getCommandOutput = exports.supportedPackageManagers = void 0;
exports.repoHasYarnBerryManagedDependencies = exports.getCacheDirectories = exports.resetProjectDirectoriesMemoized = exports.isPackageManagerInstalled = exports.getPackageManagerInfo = exports.getCommandOutputNotEmpty = exports.getCommandOutput = exports.supportedPackageManagers = void 0;
exports.isGhes = isGhes;
exports.isCacheFeatureAvailable = isCacheFeatureAvailable;
const core = __importStar(__nccwpck_require__(37484));
@ -81270,6 +81280,25 @@ const getPackageManagerInfo = async (packageManager) => {
}
};
exports.getPackageManagerInfo = getPackageManagerInfo;
/**
* Checks if a package manager is installed and available on the PATH
* This helps prevent cache failures when a package manager is specified
* but not yet installed (e.g., pnpm via corepack)
* See: https://github.com/actions/setup-node/issues/1357
*/
const isPackageManagerInstalled = async (packageManager) => {
try {
const { exitCode } = await exec.getExecOutput(`${packageManager} --version`, undefined, {
ignoreReturnCode: true,
silent: true
});
return exitCode === 0;
}
catch {
return false;
}
};
exports.isPackageManagerInstalled = isPackageManagerInstalled;
/**
* getProjectDirectoriesFromCacheDependencyPath is called twice during `restoreCache`
* - first through `getCacheDirectories`
@ -81660,7 +81689,7 @@ class BaseDistribution {
const dataUrl = `${initialUrl}/index.json`;
const headers = {};
if (this.nodeInfo.mirrorToken) {
headers['Authorization'] = `Bearer ${this.nodeInfo.mirrorToken}`;
headers['Authorization'] = this.nodeInfo.mirrorToken;
}
const response = await this.httpClient.getJson(dataUrl, headers);
return response.result || [];
@ -82415,6 +82444,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).
@ -470,7 +480,7 @@ Please refer to the [Ensuring workflow access to your package - Configuring a pa
It is possible to use a private mirror hosting Node.js binaries. This mirror must be a full mirror of the official Node.js distribution.
The mirror URL can be set using the `mirror` input.
It is possible to specify a token to authenticate with the mirror using the `mirror-token` input.
The token will be passed as a bearer token in the `Authorization` header.
The token will be passed in the `Authorization` header.
```yaml
- uses: actions/setup-node@v6

View File

@ -9,6 +9,7 @@ import {State} from './constants';
import {
getCacheDirectories,
getPackageManagerInfo,
isPackageManagerInstalled,
repoHasYarnBerryManagedDependencies,
PackageManagerInfo
} from './cache-utils';
@ -21,6 +22,20 @@ export const restoreCache = async (
if (!packageManagerInfo) {
throw new Error(`Caching for '${packageManager}' is not supported`);
}
// Check if the package manager is installed before attempting to cache
// This prevents cache failures for package managers that need to be installed first
// See: https://github.com/actions/setup-node/issues/1357
const isInstalled = await isPackageManagerInstalled(packageManager);
if (!isInstalled) {
core.warning(
`Package manager '${packageManager}' was not found in the PATH. ` +
`Skipping cache restore. Please ensure the package manager is installed ` +
`before running this action or set 'package-manager-cache: false' to disable caching.`
);
return;
}
const platform = process.env.RUNNER_OS;
const arch = os.arch();

View File

@ -2,7 +2,7 @@ import * as core from '@actions/core';
import * as cache from '@actions/cache';
import {State} from './constants';
import {getPackageManagerInfo} from './cache-utils';
import {getPackageManagerInfo, isPackageManagerInstalled} from './cache-utils';
// Catch and log any unhandled exceptions. These exceptions can leak out of the uploadChunk method in
// @actions/toolkit when a failed upload closes the file descriptor causing any in-process reads to
@ -45,6 +45,17 @@ const cachePackages = async (packageManager: string) => {
return;
}
// Check if the package manager is installed before attempting to save cache
// This prevents cache save failures for package managers that may not be installed
const isInstalled = await isPackageManagerInstalled(packageManager);
if (!isInstalled) {
core.warning(
`Package manager '${packageManager}' was not found in the PATH. ` +
`Skipping cache save.`
);
return;
}
if (!cachePaths.length) {
// TODO: core.getInput has a bug - it can return undefined despite its definition (tests only?)
// export declare function getInput(name: string, options?: InputOptions): string;

View File

@ -110,6 +110,30 @@ export const getPackageManagerInfo = async (packageManager: string) => {
}
};
/**
* Checks if a package manager is installed and available on the PATH
* This helps prevent cache failures when a package manager is specified
* but not yet installed (e.g., pnpm via corepack)
* See: https://github.com/actions/setup-node/issues/1357
*/
export const isPackageManagerInstalled = async (
packageManager: string
): Promise<boolean> => {
try {
const {exitCode} = await exec.getExecOutput(
`${packageManager} --version`,
undefined,
{
ignoreReturnCode: true,
silent: true
}
);
return exitCode === 0;
} catch {
return false;
}
};
/**
* getProjectDirectoriesFromCacheDependencyPath is called twice during `restoreCache`
* - first through `getCacheDirectories`

View File

@ -103,7 +103,7 @@ export default abstract class BaseDistribution {
const headers = {};
if (this.nodeInfo.mirrorToken) {
headers['Authorization'] = `Bearer ${this.nodeInfo.mirrorToken}`;
headers['Authorization'] = this.nodeInfo.mirrorToken;
}
const response = await this.httpClient.getJson<INodeVersion[]>(

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;
}