-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix node-version-file interprets entire package.json as a version #865
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,31 @@ | ||
import * as core from '@actions/core'; | ||
import * as exec from '@actions/exec'; | ||
|
||
export function parseNodeVersionFile(contents: string): string { | ||
export function parseNodeVersionFile(contents: string): string | null { | ||
let nodeVersion: string | undefined; | ||
|
||
// Try parsing the file as an NPM `package.json` file. | ||
try { | ||
nodeVersion = JSON.parse(contents).volta?.node; | ||
if (!nodeVersion) nodeVersion = JSON.parse(contents).engines?.node; | ||
const manifest = JSON.parse(contents); | ||
|
||
// JSON can parse numbers, but that's handled later | ||
if (typeof manifest === 'object') { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the fix is this typeof x === 'object' check followed by the early return below There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you translate more info please There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. how do you mean? |
||
nodeVersion = manifest.volta?.node; | ||
if (!nodeVersion) nodeVersion = manifest.engines?.node; | ||
|
||
// if contents are an object, we parsed JSON | ||
// this can happen if node-version-file is a package.json | ||
// yet contains no volta.node or engines.node | ||
// | ||
// if node-version file is _not_ json, control flow | ||
// will not have reached these lines. | ||
// | ||
// And because we've reached here, we know the contents | ||
// *are* JSON, so no further string parsing makes sense. | ||
if (!nodeVersion) { | ||
return null; | ||
} | ||
} | ||
} catch { | ||
core.info('Node version file is not JSON file'); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
previously, it was assumed that if a version-file was provided, it would always contain a node version.
This is not the case