Skip to content

Commit 66f83c8

Browse files
v-gowridurgadgowridurgad
andauthored
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 <gowridurgad@gmail.com>
1 parent 436c656 commit 66f83c8

5 files changed

Lines changed: 136 additions & 5 deletions

File tree

‎.github/workflows/versions.yml‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,10 @@ jobs:
270270
steps:
271271
- name: Get node version
272272
run: |
273-
latestNodeVersion=$(curl https://nodejs.org/dist/index.json | jq -r '. [0].version')
273+
latestNodeVersion=$(
274+
curl -fsSL https://nodejs.org/dist/index.json |
275+
jq -r 'first(.[] | select(.version | test("-") | not) | .version)'
276+
)
274277
echo "LATEST_NODE_VERSION=$latestNodeVersion" >> $GITHUB_OUTPUT
275278
id: version
276279
shell: bash

‎README.md‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,9 +151,10 @@ Examples:
151151

152152
**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.
153153

154-
`current`/`latest`/`node` always resolve to the latest [dist version](https://nodejs.org/dist/index.json).
154+
`current`/`latest`/`node` always resolve to the latest stable version from [dist index](https://nodejs.org/dist/index.json) (prerelease entries are skipped).
155155
That version is then downloaded from actions/node-versions if possible, or directly from Node.js if not.
156-
Since it will not be cached always, there is possibility of hitting rate limit when downloading from dist
156+
If no stable version is available in the dist index, setup fails with an explicit error.
157+
Since it will not always be cached, there is a possibility of hitting rate limits when downloading from dist.
157158

158159
### Checking in lockfiles
159160

‎__tests__/official-installer.test.ts‎

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -993,6 +993,111 @@ describe('setup-node', () => {
993993
expect(logSpy).toHaveBeenCalledWith('getting latest node version...');
994994
}
995995
);
996+
997+
it.each(['latest', 'current', 'node'])(
998+
'skips a newer prerelease and selects the latest stable version for %s',
999+
async inputVersion => {
1000+
// Arrange
1001+
inputs['node-version'] = inputVersion;
1002+
const stableVersion = nodeTestDist[0];
1003+
const prereleaseVersion = {
1004+
...nodeTestDist[0],
1005+
version: 'v99.0.0-alpha.1'
1006+
};
1007+
1008+
os.platform = 'darwin';
1009+
os.arch = 'x64';
1010+
1011+
const cleanStableVersion = stableVersion.version.replace(/^v/, '');
1012+
const toolPath = path.normalize(
1013+
`/cache/node/${cleanStableVersion}/x64`
1014+
);
1015+
1016+
findSpy.mockImplementation(() => '');
1017+
getManifestSpy.mockImplementation(() => {
1018+
throw new Error('Unable to download manifest');
1019+
});
1020+
getJsonSpy.mockImplementation((url: any) => {
1021+
if (url === nodeVersionsManifestUrl) {
1022+
throw new Error('Unable to download raw manifest');
1023+
}
1024+
1025+
return {result: [prereleaseVersion, ...nodeTestDist]};
1026+
});
1027+
dlSpy.mockImplementation(async () => '/some/temp/path');
1028+
exSpy.mockImplementation(async () => '/some/other/temp/path');
1029+
cacheSpy.mockImplementation(async () => toolPath);
1030+
getExecOutputSpy.mockImplementation(async () => ({
1031+
stdout: `v${cleanStableVersion}\n`,
1032+
stderr: '',
1033+
exitCode: 0
1034+
}));
1035+
1036+
// Act
1037+
await runWithRetries();
1038+
1039+
// assert
1040+
expect(findSpy).toHaveBeenCalledWith(
1041+
'node',
1042+
stableVersion.version,
1043+
'x64'
1044+
);
1045+
expect(findSpy).not.toHaveBeenCalledWith(
1046+
'node',
1047+
prereleaseVersion.version,
1048+
'x64'
1049+
);
1050+
expect(dlSpy).toHaveBeenCalledWith(
1051+
`https://nodejs.org/dist/v${cleanStableVersion}/node-v${cleanStableVersion}-darwin-x64.tar.gz`,
1052+
undefined,
1053+
undefined
1054+
);
1055+
expect(setFailedSpy).not.toHaveBeenCalled();
1056+
expect(addPathSpy).toHaveBeenCalledWith(path.join(toolPath, 'bin'));
1057+
}
1058+
);
1059+
1060+
const prereleaseOnlyIndex = [
1061+
{...nodeTestDist[0], version: 'v99.0.0-alpha.1'}
1062+
];
1063+
1064+
it.each([
1065+
['latest', 'an empty index', []],
1066+
['current', 'an empty index', []],
1067+
['node', 'an empty index', []],
1068+
['latest', 'a prerelease-only index', prereleaseOnlyIndex],
1069+
['current', 'a prerelease-only index', prereleaseOnlyIndex],
1070+
['node', 'a prerelease-only index', prereleaseOnlyIndex]
1071+
])(
1072+
'fails with the stable-version error for %s with %s',
1073+
async (inputVersion, _description, index) => {
1074+
// Arrange
1075+
inputs['node-version'] = inputVersion;
1076+
1077+
os.platform = 'darwin';
1078+
os.arch = 'x64';
1079+
1080+
findSpy.mockImplementation(() => '');
1081+
getManifestSpy.mockImplementation(() => {
1082+
throw new Error('Unable to download manifest');
1083+
});
1084+
getJsonSpy.mockImplementation((url: any) => {
1085+
if (url === nodeVersionsManifestUrl) {
1086+
throw new Error('Unable to download raw manifest');
1087+
}
1088+
1089+
return {result: index};
1090+
});
1091+
1092+
// Act
1093+
await runWithRetries();
1094+
1095+
// assert
1096+
expect(setFailedSpy).toHaveBeenCalledWith(
1097+
`Unable to find a stable Node version for '${inputVersion}' on platform ${os.platform} and architecture ${os.arch}.`
1098+
);
1099+
}
1100+
);
9961101
});
9971102

9981103
describe('latest alias syntax from cache', () => {
@@ -1017,6 +1122,11 @@ describe('setup-node', () => {
10171122
// assert
10181123

10191124
expect(logSpy).toHaveBeenCalledWith('getting latest node version...');
1125+
expect(findSpy).toHaveBeenCalledWith(
1126+
'node',
1127+
expectedVersion.version,
1128+
'x64'
1129+
);
10201130
expect(logSpy).toHaveBeenCalledWith(`Found in cache @ ${toolPath}`);
10211131
}
10221132
);

‎dist/setup/index.js‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98991,6 +98991,7 @@ var external_node_path_default = /*#__PURE__*/__nccwpck_require__.n(external_nod
9899198991

9899298992

9899398993

98994+
9899498995
const nodeVersionsManifestUrl = 'https://raw.githubusercontent.com/actions/node-versions/main/versions-manifest.json';
9899598996
const invalidManifestMessage = 'The manifest fetched is empty, truncated, or does not contain any valid tool release entries.';
9899698997
function isValidManifest(manifest) {
@@ -99114,7 +99115,11 @@ class OfficialBuilds extends BaseDistribution {
9911499115
let version = '';
9911599116
if (this.isLatestSyntax(this.nodeInfo.versionSpec)) {
9911699117
core_info(`getting latest node version...`);
99117-
return versions[0];
99118+
const stableVersion = versions.find(potential => semver_default().prerelease(potential) === null);
99119+
if (!stableVersion) {
99120+
throw new Error(`Unable to find a stable Node version for '${this.nodeInfo.versionSpec}' on platform ${this.osPlat} and architecture ${this.nodeInfo.arch}.`);
99121+
}
99122+
return stableVersion;
9911899123
}
9911999124
version = super.evaluateVersions(versions);
9912099125
return version;

‎src/distributions/official_builds/official_builds.ts‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import path from 'node:path';
33
import * as core from '@actions/core';
44
import * as exec from '@actions/exec';
55
import * as tc from '@actions/tool-cache';
6+
import semver from 'semver';
67

78
import BaseDistribution from '../base-distribution.js';
89
import {NodeInputs, INodeVersion, INodeVersionInfo} from '../base-models.js';
@@ -195,7 +196,18 @@ export default class OfficialBuilds extends BaseDistribution {
195196

196197
if (this.isLatestSyntax(this.nodeInfo.versionSpec)) {
197198
core.info(`getting latest node version...`);
198-
return versions[0];
199+
200+
const stableVersion = versions.find(
201+
potential => semver.prerelease(potential) === null
202+
);
203+
204+
if (!stableVersion) {
205+
throw new Error(
206+
`Unable to find a stable Node version for '${this.nodeInfo.versionSpec}' on platform ${this.osPlat} and architecture ${this.nodeInfo.arch}.`
207+
);
208+
}
209+
210+
return stableVersion;
199211
}
200212

201213
version = super.evaluateVersions(versions);

0 commit comments

Comments
 (0)