From 66f83c8206e0997178c8fd7bf1da9efccf5dd388 Mon Sep 17 00:00:00 2001 From: v-gowridurgad <159780674+v-gowridurgad@users.noreply.github.com> Date: Tue, 17 Nov 2026 21:39:09 +0530 Subject: [PATCH] fix: exclude prereleases when resolving latest/current/node aliases (#1668) * exclude prereleases when resolving latest/current/node aliases * test: verify stable install succeeds when skipping prereleases --------- Co-authored-by: gowridurgad --- .github/workflows/versions.yml | 5 +- README.md | 5 +- __tests__/official-installer.test.ts | 110 ++++++++++++++++++ dist/setup/index.js | 7 +- .../official_builds/official_builds.ts | 14 ++- 5 files changed, 136 insertions(+), 5 deletions(-) diff --git a/.github/workflows/versions.yml b/.github/workflows/versions.yml index e9aabd78..2b7dc25c 100644 --- a/.github/workflows/versions.yml +++ b/.github/workflows/versions.yml @@ -270,7 +270,10 @@ jobs: steps: - name: Get node version run: | - latestNodeVersion=$(curl https://nodejs.org/dist/index.json | jq -r '. [0].version') + latestNodeVersion=$( + curl -fsSL https://nodejs.org/dist/index.json | + jq -r 'first(.[] | select(.version | test("-") | not) | .version)' + ) echo "LATEST_NODE_VERSION=$latestNodeVersion" >> $GITHUB_OUTPUT id: version shell: bash diff --git a/README.md b/README.md index 14d291d1..b1a4dcc5 100644 --- a/README.md +++ b/README.md @@ -151,9 +151,10 @@ Examples: **Note:** Like the other values, `*` will get the latest [locally-cached Node.js version](https://github.com/actions/runner-images/blob/main/images/ubuntu/Ubuntu2204-Readme.md#nodejs), or the latest version from [actions/node-versions](https://github.com/actions/node-versions/blob/main/versions-manifest.json), depending on the [`check-latest`](docs/advanced-usage.md#check-latest-version) input. -`current`/`latest`/`node` always resolve to the latest [dist version](https://nodejs.org/dist/index.json). +`current`/`latest`/`node` always resolve to the latest stable version from [dist index](https://nodejs.org/dist/index.json) (prerelease entries are skipped). That version is then downloaded from actions/node-versions if possible, or directly from Node.js if not. -Since it will not be cached always, there is possibility of hitting rate limit when downloading from dist +If no stable version is available in the dist index, setup fails with an explicit error. +Since it will not always be cached, there is a possibility of hitting rate limits when downloading from dist. ### Checking in lockfiles diff --git a/__tests__/official-installer.test.ts b/__tests__/official-installer.test.ts index 2e7a2960..5e82ce86 100644 --- a/__tests__/official-installer.test.ts +++ b/__tests__/official-installer.test.ts @@ -993,6 +993,111 @@ describe('setup-node', () => { expect(logSpy).toHaveBeenCalledWith('getting latest node version...'); } ); + + it.each(['latest', 'current', 'node'])( + 'skips a newer prerelease and selects the latest stable version for %s', + async inputVersion => { + // Arrange + inputs['node-version'] = inputVersion; + const stableVersion = nodeTestDist[0]; + const prereleaseVersion = { + ...nodeTestDist[0], + version: 'v99.0.0-alpha.1' + }; + + os.platform = 'darwin'; + os.arch = 'x64'; + + const cleanStableVersion = stableVersion.version.replace(/^v/, ''); + const toolPath = path.normalize( + `/cache/node/${cleanStableVersion}/x64` + ); + + findSpy.mockImplementation(() => ''); + getManifestSpy.mockImplementation(() => { + throw new Error('Unable to download manifest'); + }); + getJsonSpy.mockImplementation((url: any) => { + if (url === nodeVersionsManifestUrl) { + throw new Error('Unable to download raw manifest'); + } + + return {result: [prereleaseVersion, ...nodeTestDist]}; + }); + dlSpy.mockImplementation(async () => '/some/temp/path'); + exSpy.mockImplementation(async () => '/some/other/temp/path'); + cacheSpy.mockImplementation(async () => toolPath); + getExecOutputSpy.mockImplementation(async () => ({ + stdout: `v${cleanStableVersion}\n`, + stderr: '', + exitCode: 0 + })); + + // Act + await runWithRetries(); + + // assert + expect(findSpy).toHaveBeenCalledWith( + 'node', + stableVersion.version, + 'x64' + ); + expect(findSpy).not.toHaveBeenCalledWith( + 'node', + prereleaseVersion.version, + 'x64' + ); + expect(dlSpy).toHaveBeenCalledWith( + `https://nodejs.org/dist/v${cleanStableVersion}/node-v${cleanStableVersion}-darwin-x64.tar.gz`, + undefined, + undefined + ); + expect(setFailedSpy).not.toHaveBeenCalled(); + expect(addPathSpy).toHaveBeenCalledWith(path.join(toolPath, 'bin')); + } + ); + + const prereleaseOnlyIndex = [ + {...nodeTestDist[0], version: 'v99.0.0-alpha.1'} + ]; + + it.each([ + ['latest', 'an empty index', []], + ['current', 'an empty index', []], + ['node', 'an empty index', []], + ['latest', 'a prerelease-only index', prereleaseOnlyIndex], + ['current', 'a prerelease-only index', prereleaseOnlyIndex], + ['node', 'a prerelease-only index', prereleaseOnlyIndex] + ])( + 'fails with the stable-version error for %s with %s', + async (inputVersion, _description, index) => { + // Arrange + inputs['node-version'] = inputVersion; + + os.platform = 'darwin'; + os.arch = 'x64'; + + findSpy.mockImplementation(() => ''); + getManifestSpy.mockImplementation(() => { + throw new Error('Unable to download manifest'); + }); + getJsonSpy.mockImplementation((url: any) => { + if (url === nodeVersionsManifestUrl) { + throw new Error('Unable to download raw manifest'); + } + + return {result: index}; + }); + + // Act + await runWithRetries(); + + // assert + expect(setFailedSpy).toHaveBeenCalledWith( + `Unable to find a stable Node version for '${inputVersion}' on platform ${os.platform} and architecture ${os.arch}.` + ); + } + ); }); describe('latest alias syntax from cache', () => { @@ -1017,6 +1122,11 @@ describe('setup-node', () => { // assert expect(logSpy).toHaveBeenCalledWith('getting latest node version...'); + expect(findSpy).toHaveBeenCalledWith( + 'node', + expectedVersion.version, + 'x64' + ); expect(logSpy).toHaveBeenCalledWith(`Found in cache @ ${toolPath}`); } ); diff --git a/dist/setup/index.js b/dist/setup/index.js index 0b1ed034..67d17944 100644 --- a/dist/setup/index.js +++ b/dist/setup/index.js @@ -98991,6 +98991,7 @@ var external_node_path_default = /*#__PURE__*/__nccwpck_require__.n(external_nod + const nodeVersionsManifestUrl = 'https://raw.githubusercontent.com/actions/node-versions/main/versions-manifest.json'; const invalidManifestMessage = 'The manifest fetched is empty, truncated, or does not contain any valid tool release entries.'; function isValidManifest(manifest) { @@ -99114,7 +99115,11 @@ class OfficialBuilds extends BaseDistribution { let version = ''; if (this.isLatestSyntax(this.nodeInfo.versionSpec)) { core_info(`getting latest node version...`); - return versions[0]; + const stableVersion = versions.find(potential => semver_default().prerelease(potential) === null); + if (!stableVersion) { + throw new Error(`Unable to find a stable Node version for '${this.nodeInfo.versionSpec}' on platform ${this.osPlat} and architecture ${this.nodeInfo.arch}.`); + } + return stableVersion; } version = super.evaluateVersions(versions); return version; diff --git a/src/distributions/official_builds/official_builds.ts b/src/distributions/official_builds/official_builds.ts index 28bb1c16..52349346 100644 --- a/src/distributions/official_builds/official_builds.ts +++ b/src/distributions/official_builds/official_builds.ts @@ -3,6 +3,7 @@ import path from 'node:path'; import * as core from '@actions/core'; import * as exec from '@actions/exec'; import * as tc from '@actions/tool-cache'; +import semver from 'semver'; import BaseDistribution from '../base-distribution.js'; import {NodeInputs, INodeVersion, INodeVersionInfo} from '../base-models.js'; @@ -195,7 +196,18 @@ export default class OfficialBuilds extends BaseDistribution { if (this.isLatestSyntax(this.nodeInfo.versionSpec)) { core.info(`getting latest node version...`); - return versions[0]; + + const stableVersion = versions.find( + potential => semver.prerelease(potential) === null + ); + + if (!stableVersion) { + throw new Error( + `Unable to find a stable Node version for '${this.nodeInfo.versionSpec}' on platform ${this.osPlat} and architecture ${this.nodeInfo.arch}.` + ); + } + + return stableVersion; } version = super.evaluateVersions(versions);