GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Read version from .nvmrc or .tool-versions config by matoakley · Pull Request #222 · actions/setup-node

Conversation

@matoakley

@matoakley matoakley commented Jan 1, 2021 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown

NVM and asdf are popular tools for managing node (and other dependency) versions in projects. These tools provide the ability to specify the version number in either a .nvmrc or .tool-versions file and have the management tool automatically install and configure that version.

This patch (shamelessly plagiarised and adapted from setup-ruby) will attempt to set the version from the .nvmrc or .tool-versions file if not specified explicitly. If it is unable to find and parse a version then it will return a blank string and fallback to the original logic to use the architecture's pre-installed node.

To be clear, the order of precedence is:

  1. Use the version explicitly set in the version or node-version option,
  2. Attempt to read the version from .nvmrc if the file is present,
  3. Attempt to read the version from .tool-versions if the file is present,
  4. Fallback to the system node.

By enabling the action to read from these common configuration files we reduce coupling and allow developers to manage their node version from a single location within their codebase.

This patch provides the enhancement discussed in #32 .

Finally, this is the first time that I've worked with Typescript (I'm a rubyist at heart) so I'm open to code review and suggestions to improve the code itself.

Happy New Year! 🎉

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

keabarnes, jamiehaywood, devmattrick, and ruscon reacted with hooray emoji
All reactions
  • 🎉 4 reactions
@matoakley
matoakley force-pushed the read-nvmrc-and-tool-versions branch from 382124c to 4ea5164 Compare January 1, 2021 00:54
Matt Oakley
Read version from .nvmrc or .tool-versions
7fea721
@matoakley
matoakley force-pushed the read-nvmrc-and-tool-versions branch from 4ea5164 to 7fea721 Compare January 1, 2021 00:57
@matoakley
matoakley marked this pull request as ready for review January 1, 2021 01:03
@matoakley matoakley changed the title Read version from .nvmrc or .tool-versions Read version from .nvmrc or .tool-versions config Jan 1, 2021
@bryanmacfarlane bryanmacfarlane added the enhancement New feature or request label Jan 20, 2021
jamiehaywood
jamiehaywood reviewed Feb 1, 2021
View reviewed changes
Comment thread dist/index.js
return ghUrl.hostname.toUpperCase() !== 'GITHUB.COM';
}
function parseNodeVersion() {
let nodeVersion = core.getInput('node-version') || core.getInput('version');

@jamiehaywood jamiehaywood Feb 1, 2021

Copy link
Copy Markdown

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.

just wanted to ask here whether you want to check for a falsy value or just null or undefined? If the latter:

Suggested change
let nodeVersion = core.getInput('node-version') || core.getInput('version');
let nodeVersion = core.getInput('node-version') ?? core.getInput('version');

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
jamiehaywood
jamiehaywood reviewed Feb 1, 2021
View reviewed changes
Comment thread src/main.ts
const nodeLine = toolVersions
.split(/\r?\n/)
.filter(e => e.match(/^nodejs\s/))[0];
nodeVersion = nodeLine.match(/^nodejs\s+(.+)$/)![1];

@jamiehaywood jamiehaywood Feb 1, 2021

Copy link
Copy Markdown

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.

.match() returns RegExpMatchArray | null, so rather than using definite assignment assertion (!), perhaps use the optional chain (?) to query the RegExpMatchArray. If nothing is returned here, it will assign undefined to nodeVersion.

This does then throw into question the flow of these statements, because if I have a .tool-versions with the nodejs key in (I think that's what you're testing?) but no value, then it will return undefined.

I would revisit this by allowing TS to guide you with the types & then making sure that a string is definitely being returned.

Suggested change
nodeVersion = nodeLine.match(/^nodejs\s+(.+)$/)![1];
nodeVersion = nodeLine.match(/^nodejs\s+(.+)$/)?.[1];

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@jasonkarns

jasonkarns commented Feb 7, 2021

Copy link
Copy Markdown

Looks like a dupe of #209 ?

Just commenting since it's highly related: https://github.com/nodenv/actions-node-version is an action that reads .node-version file (leveraging nodenv) and exposes it as an output that can be used as an input to the setup-node action. This would be a workaround until/unless setup-node gains the ability to do it automatically.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ruscon

ruscon commented Mar 13, 2021

Copy link
Copy Markdown

@matoakley @jamiehaywood Is there anything I can do to complete this feature?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@jamiehaywood

jamiehaywood commented Mar 13, 2021

Copy link
Copy Markdown

@ruscon unfortunately I'm not a maintainer, I just added a review because I'm keen to get this feature into setup-node. I think there are 2 or 3 PRs covering the same feature floating around, so it might be worth having a look on them as well, and seeing the status of that.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ruscon

ruscon commented Mar 13, 2021

Copy link
Copy Markdown

this solution is better
#239

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@matoakley

matoakley commented Mar 13, 2021

Copy link
Copy Markdown
Author

@jamiehaywood Thanks for the review it's much appreciated 👍

Sadly I've lost hope on this PR given that the project appears to be all but abandoned by its maintainers. As has been pointed out, there are now at least 3 open PRs offering a solution to this problem and there seems to be high demand for the feature.

@ruscon I agree that #239 would probably be a better all-round solution but whether it is "better" is subjective. For example, it wouldn't have solved my problem as the patch does not lookup the .tool-versions file which https://github.com/asdf-vm/asdf uses.

In the 3 months since I submitted this PR I have migrated my development environment to Docker and have resigned myself to managing the Node version manually for Github Actions.

jamiehaywood reacted with thumbs up emoji jasonkarns reacted with thumbs down emoji
All reactions
  • 👍 1 reaction
  • 👎 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@matoakley matoakley closed this Mar 13, 2021
@ganta ganta mentioned this pull request Dec 2, 2021
Add support for asdf format as Node.js version file #373
Merged
2 tasks done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

1 more reviewer
@jamiehaywood jamiehaywood jamiehaywood left review comments
Reviewers whose approvals may not affect merge requirements

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

enhancement New feature or request

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

5 participants

@matoakley @jasonkarns @ruscon @jamiehaywood @bryanmacfarlane
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.