Skip to content
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

Question: Make nvm use usable for my own auto-switching? #186

Closed
wants to merge 1 commit into from

Conversation

lydell
Copy link

@lydell lydell commented May 28, 2022

Hi!

First, thanks for nvm.fish! It’s so fast and easy to use.

Note: I made this as a PR rather than as an issue because it’s easier to show some things using code. I don’t expect this to be merged in its current (ugly, incomplete) form!

I read #132 about the – now closed – suggestion about automatic version switching.

I get that it can be problematic. But I feel that for me, it can be more worth having automatic switching than perfection. So I started playing around with doing automatic switching myself:

# Note: Not completely thought through code yet!

# Run at startup.
nvm use

# Run when the current directory changes.
function foo --on-variable PWD
  nvm use
end

However, there are a few problems with nvm use (with no arguments):

  • It is too slow. It seems to take >200 ms on the “first run”, and like ~90 ms on subsequent runs. (Maybe macOS slows down the first node call?) The slowness comes from _nvm_node_info which invokes node --eval which isn’t the fastest.
  • It prints stuff. I can add /dev/null though. I don’t need any prints, because my prompt shows the version. However, as mentioned in the above point, the prints are slow and /dev/null does not help with performance.
  • It fails if no .nvmrc or .node-version is found. I want it to switch to the default version in that case – which is system for me.

I threw together some quick and bad modifications to nvm.fish to try things out. It looks promising!

  • nvm use now takes 8-12 ms to run, which feels acceptable to me on every cd.
  • Nothing is printed.
  • It switches to system if no .nvmrc or .node-version is found. (It should honor the default though, haven’t looked into that yet).

What do you think? Would you be interested in having an nvm use optimized for this use case?

Alternatively, any tips on how to use nvm effectively? Currently, I “fight” a lot with forgetting to switch version all the time. How do you do it? Would you recommend some other tool if this isn’t ”the nvm.fish philosophy”? (I’ve tried nodenv in the past, which has automatic switching, but it was so clunky to use.)

@lydell
Copy link
Author

lydell commented May 29, 2022

Forgot to search pull requests in addition to issues 🤦 Found this one right now: #181

Copy link

@edouard-lopez edouard-lopez left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the MR, I put some comments

functions/nvm.fish Show resolved Hide resolved
functions/nvm.fish Show resolved Hide resolved
@jorgebucaran jorgebucaran added the enhancement New feature or request label May 30, 2022
@jorgebucaran
Copy link
Owner

It is too slow...
It prints stuff...

👌. We either need a plumbing nvm use command or support this out of the box via a feature flag. Since there has been sustained support for this feature over the years, I'll look into taking a stab at this myself. I'll pull @edouard-lopez's #181 changes first then add my tweaks.

It fails if no .nvmrc or .node-version is found. I want it to switch to the default version in that case – which is system for me.

Just curious, but why isn't your system Node always active?

@lydell
Copy link
Author

lydell commented May 30, 2022

Since there has been sustained support for this feature over the years, I'll look into taking a stab at this myself

Yay! I’m excited 😄 🎉

Just curious, but why isn't your system Node always active?

I usually have the latest LTS installed via brew, and that’s the one I use generally. But then I have some older projects that need a specific older Node.js version (for the time being). In those I like to stick a .node-version file. When entering one of those, I’d like to automatically switch to that Node.js version. When leaving the project folder (cd away from it), I’d like to go back to the system Node.js that I usually use again, rather than stay on that older version just because I happened to be in that project folder before. Some things I thought about now:

  • I could probably create ~/.node-version to achieve the same effect.
  • I probably don’t even need to install Node.js via brew anymore. Feels easier to just install everything with nvm.fish!

@jorgebucaran
Copy link
Owner

How about we introduce a --silent flag that does the job silently, printing nothing. Then consider an official auto-switching feature on a different discussion? 🤓

@lydell
Copy link
Author

lydell commented May 31, 2022

nvm use --silent + me creating ~/.node-version sounds like it should be all I need to implement automatic switching on my own, yes!

Edit: Oh wait, I’ll probably get errors when I (seldomly) go to, say, /usr. Are the errors silenced too? Or should I create /.node-version maybe? 🤔

@jorgebucaran
Copy link
Owner

Yes, but you can redirect to /dev/null in this case since errors won't slow you down as _nvm_node_info does.

@jorgebucaran
Copy link
Owner

Added a --silent option flag that will suppress stdout (it works for nvm install and nvm uninstall too).

Please let me know if this should be all you need to implement automatic switching.

@lydell
Copy link
Author

lydell commented May 31, 2022

I added this to ~/.config/fish/config.fish:

function __nvm_auto --on-variable PWD
  nvm use --silent 2>/dev/null
end
__nvm_auto

And created ~/.node-version with my desired default version.

nvm use --silent seems to take like 9 ms.

It seems to work perfectly so far!* 🎉 Thank you!

@lydell lydell closed this May 31, 2022
@lydell lydell deleted the auto-use branch May 31, 2022 17:26
@jorgebucaran
Copy link
Owner

9ms should do it. One could even get a bit fancier and put __nvm_auto behind a feature flag (#181 (comment)). 🤓

@denvis
Copy link

denvis commented Jun 3, 2022

super fast and reliable so far! thx

@jorgebucaran
Copy link
Owner

Question: we nvm use $nvm_default_version in new shells, but what if there is an .nvmrc file in that directory? Which one should have precedence: nvm_default_version or a local .nvmrc/.node-version file?

cc @lydell @denvis @edouard-lopez

@lydell
Copy link
Author

lydell commented Jun 8, 2022

a local .nvmrc/.node-version file should always win IMO. That’s how my current setup works. In other words, I want no difference between cd foo/ and starting out in foo/ from the start.

@jorgebucaran
Copy link
Owner

What happens when you move to a directory without a .nvmrc (after you've auto-switched versions)? Should you auto-switch to the previous one? Assume such a directory is not a subdirectory of the one that had the .nvmrc file.

@lydell
Copy link
Author

lydell commented Jun 8, 2022

Then I would like the default version. I’ve created ~/.node-version with my desired default to simulate that.

@jorgebucaran
Copy link
Owner

Thank you, that makes sense and it's easy to do in userland too.

djpowers added a commit to djpowers/dotfiles that referenced this pull request Mar 4, 2023
- install nvm.fish (does not use original NVM, does not require bass)
- add function to auto-switch node versions on directory change: jorgebucaran/nvm.fish#186 (comment)
- add ~/.node-version file set to system, which is essentially used as a
  fallback if a version is not stated (otherwise it will stay on the
  last version specified)
djpowers added a commit to djpowers/dotfiles that referenced this pull request Mar 17, 2023
- install nvm.fish (does not use original NVM, does not require bass)
- add function to auto-switch node versions on directory change: jorgebucaran/nvm.fish#186 (comment)
- add ~/.node-version file set to system, which is essentially used as a
  fallback if a version is not stated (otherwise it will stay on the
  last version specified)
djpowers added a commit to djpowers/dotfiles that referenced this pull request Mar 17, 2023
- install nvm.fish (does not use original NVM, does not require bass)
- add function to auto-switch node versions on directory change: jorgebucaran/nvm.fish#186 (comment)
- add ~/.node-version file set to system, which is essentially used as a
  fallback if a version is not stated (otherwise it will stay on the
  last version specified)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants