Skip to content

Commit b39b52d

Browse files
authoredDec 14, 2023
Fix node-version-file interprets entire package.json as a version (#865)
1 parent 7247617 commit b39b52d

File tree

5 files changed

+78
-11
lines changed

5 files changed

+78
-11
lines changed
 

‎__tests__/main.test.ts

+1
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ describe('main tests', () => {
101101
${' 14.1.0 '} | ${'14.1.0'}
102102
${'{"volta": {"node": ">=14.0.0 <=17.0.0"}}'}| ${'>=14.0.0 <=17.0.0'}
103103
${'{"engines": {"node": "17.0.0"}}'} | ${'17.0.0'}
104+
${'{}'} | ${null}
104105
`.it('parses "$contents"', ({contents, expected}) => {
105106
expect(util.parseNodeVersionFile(contents)).toBe(expected);
106107
});

‎dist/cache-save/index.js

+19-3
Original file line numberDiff line numberDiff line change
@@ -83338,9 +83338,25 @@ function parseNodeVersionFile(contents) {
8333883338
let nodeVersion;
8333983339
// Try parsing the file as an NPM `package.json` file.
8334083340
try {
83341-
nodeVersion = (_a = JSON.parse(contents).volta) === null || _a === void 0 ? void 0 : _a.node;
83342-
if (!nodeVersion)
83343-
nodeVersion = (_b = JSON.parse(contents).engines) === null || _b === void 0 ? void 0 : _b.node;
83341+
const manifest = JSON.parse(contents);
83342+
// JSON can parse numbers, but that's handled later
83343+
if (typeof manifest === 'object') {
83344+
nodeVersion = (_a = manifest.volta) === null || _a === void 0 ? void 0 : _a.node;
83345+
if (!nodeVersion)
83346+
nodeVersion = (_b = manifest.engines) === null || _b === void 0 ? void 0 : _b.node;
83347+
// if contents are an object, we parsed JSON
83348+
// this can happen if node-version-file is a package.json
83349+
// yet contains no volta.node or engines.node
83350+
//
83351+
// if node-version file is _not_ json, control flow
83352+
// will not have reached these lines.
83353+
//
83354+
// And because we've reached here, we know the contents
83355+
// *are* JSON, so no further string parsing makes sense.
83356+
if (!nodeVersion) {
83357+
return null;
83358+
}
83359+
}
8334483360
}
8334583361
catch (_d) {
8334683362
core.info('Node version file is not JSON file');

‎dist/setup/index.js

+26-4
Original file line numberDiff line numberDiff line change
@@ -93728,7 +93728,13 @@ function resolveVersionInput() {
9372893728
if (!fs_1.default.existsSync(versionFilePath)) {
9372993729
throw new Error(`The specified node version file at: ${versionFilePath} does not exist`);
9373093730
}
93731-
version = (0, util_1.parseNodeVersionFile)(fs_1.default.readFileSync(versionFilePath, 'utf8'));
93731+
const parsedVersion = (0, util_1.parseNodeVersionFile)(fs_1.default.readFileSync(versionFilePath, 'utf8'));
93732+
if (parsedVersion) {
93733+
version = parsedVersion;
93734+
}
93735+
else {
93736+
core.warning(`Could not determine node version from ${versionFilePath}. Falling back`);
93737+
}
9373293738
core.info(`Resolved ${versionFileInput} as ${version}`);
9373393739
}
9373493740
return version;
@@ -93783,9 +93789,25 @@ function parseNodeVersionFile(contents) {
9378393789
let nodeVersion;
9378493790
// Try parsing the file as an NPM `package.json` file.
9378593791
try {
93786-
nodeVersion = (_a = JSON.parse(contents).volta) === null || _a === void 0 ? void 0 : _a.node;
93787-
if (!nodeVersion)
93788-
nodeVersion = (_b = JSON.parse(contents).engines) === null || _b === void 0 ? void 0 : _b.node;
93792+
const manifest = JSON.parse(contents);
93793+
// JSON can parse numbers, but that's handled later
93794+
if (typeof manifest === 'object') {
93795+
nodeVersion = (_a = manifest.volta) === null || _a === void 0 ? void 0 : _a.node;
93796+
if (!nodeVersion)
93797+
nodeVersion = (_b = manifest.engines) === null || _b === void 0 ? void 0 : _b.node;
93798+
// if contents are an object, we parsed JSON
93799+
// this can happen if node-version-file is a package.json
93800+
// yet contains no volta.node or engines.node
93801+
//
93802+
// if node-version file is _not_ json, control flow
93803+
// will not have reached these lines.
93804+
//
93805+
// And because we've reached here, we know the contents
93806+
// *are* JSON, so no further string parsing makes sense.
93807+
if (!nodeVersion) {
93808+
return null;
93809+
}
93810+
}
9378993811
}
9379093812
catch (_d) {
9379193813
core.info('Node version file is not JSON file');

‎src/main.ts

+11-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,17 @@ function resolveVersionInput(): string {
105105
);
106106
}
107107

108-
version = parseNodeVersionFile(fs.readFileSync(versionFilePath, 'utf8'));
108+
const parsedVersion = parseNodeVersionFile(
109+
fs.readFileSync(versionFilePath, 'utf8')
110+
);
111+
112+
if (parsedVersion) {
113+
version = parsedVersion;
114+
} else {
115+
core.warning(
116+
`Could not determine node version from ${versionFilePath}. Falling back`
117+
);
118+
}
109119

110120
core.info(`Resolved ${versionFileInput} as ${version}`);
111121
}

‎src/util.ts

+21-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,31 @@
11
import * as core from '@actions/core';
22
import * as exec from '@actions/exec';
33

4-
export function parseNodeVersionFile(contents: string): string {
4+
export function parseNodeVersionFile(contents: string): string | null {
55
let nodeVersion: string | undefined;
66

77
// Try parsing the file as an NPM `package.json` file.
88
try {
9-
nodeVersion = JSON.parse(contents).volta?.node;
10-
if (!nodeVersion) nodeVersion = JSON.parse(contents).engines?.node;
9+
const manifest = JSON.parse(contents);
10+
11+
// JSON can parse numbers, but that's handled later
12+
if (typeof manifest === 'object') {
13+
nodeVersion = manifest.volta?.node;
14+
if (!nodeVersion) nodeVersion = manifest.engines?.node;
15+
16+
// if contents are an object, we parsed JSON
17+
// this can happen if node-version-file is a package.json
18+
// yet contains no volta.node or engines.node
19+
//
20+
// if node-version file is _not_ json, control flow
21+
// will not have reached these lines.
22+
//
23+
// And because we've reached here, we know the contents
24+
// *are* JSON, so no further string parsing makes sense.
25+
if (!nodeVersion) {
26+
return null;
27+
}
28+
}
1129
} catch {
1230
core.info('Node version file is not JSON file');
1331
}

0 commit comments

Comments
 (0)
Failed to load comments.