jorgebucaran / nvm.fish

The Node.js version manager you'll adore, crafted just for Fish
https://git.io/nvm.fish
MIT License
2.08k stars 69 forks source link

nvm 2.0 with direnv - add instructions in direnv wiki #131

Closed thernstig closed 3 years ago

thernstig commented 3 years ago

Discussion was started in https://github.com/jorgebucaran/nvm.fish/pull/123, more specifically check https://github.com/jorgebucaran/nvm.fish/pull/123#issuecomment-738731666. This is a continuation to finalize a PR for the direnv repo's wiki on how to best use direnv with nvm 2.0.

In short, direnv is an extension for your shell. It augments existing shells with a new feature that can load and unload environment variables depending on the current directory. In combination with nvm.fish, it could be used to automatically load the node version for a specific project when entering that directory, and unloading it when leaving that directory.

For reference, check https://github.com/direnv/direnv/wiki/Node. It currently has a version for nvm.fish and nvm.sh combined, but it is outdated and not great. I intend to update it and add a nvm.fish 2.0 only version of nvm_use(), but need some guidance. If you read that page, the recommendation is to create a use_nvm() bash function in ~/.config/direnv/direnvrc. That function can then be used by nvm.fish 2.0 users.

  1. It should use nvm use or nvm use <version> instead of altering the path.
  2. It should check if nvm 2.0 is installed on the system. What is the best way to check if nvm.fish 2.0 is installed on the system? The current version in the wiki does:
    elif [[ -f ~/.config/nvm/index ]]; then
  3. I believe it is a good idea that if nvm use fails, the function should issue an nvm install <version>.
jorgebucaran commented 3 years ago
  1. 👍
  2. In Fish? I'd go with type --query nvm.
  3. POSIX-compatible nvm doesn't issue an nvm install if nvm use fails and neither do we. But it's up to you to suggest that feature to the direnv developers.
thernstig commented 3 years ago
  1. You meant type --quiet nvm, right? Should I check which version of nvm.fish is installed as well? I suppose I can skip that check and just mention that it is a pre-requisite to use version 2? Or maybe not mention it at all, since it might work with version 1 as well?
  2. I know the direnv version of pipenv (Python package manager) + pyenv (Python version manager) combo does install a version if it does not exist. It is quite nice. I will have to think about it.
  3. I just noticed that https://direnv.net/CHANGELOG.html#highlights has a new functionality for third-party tools to place files in .config/direnv/lib/*.sh. This mean, we could potentially add that functionality to this repo to add a file to .config/direnv/lib/nvm.fish.sh which means it "just works" for direnv users. However, that also opens the door for many other direnv-like tools to want to add their own code to this repo, which might not be ideal. It is up to you @jorgebucaran.
jorgebucaran commented 3 years ago

Nope, I meant type --query nvm. Like:

type --query nvm && nvm use $node_version

I'd say forget the version check. It's too much work.

It is up to you...

I'll pass on this as I may choose to add automatic, async version switching in the future here instead.

thernstig commented 3 years ago

https://fishshell.com/docs/current/cmds/type.html does not have --query (set does though)?

thernstig commented 3 years ago

I just realized I can only use bash functionality to check for the existence of nvm.fish. The best option I could think of is to check if $nvm_data exists, but it is not exported so that is not possible.

jorgebucaran commented 3 years ago

Okay, turns out type --quiet is now type --query in Fish 3.2, the version I'm using, so it's probably better to use type -q to keep backward compatibility, since --quiet will be removed in the future.

I just realized I can only use bash functionality to check for the existence of nvm.fish

I don't understand that. How are you planning to run nvm use then?

You do realize that nvm is not a binary, but a shell function (so it works kind of like a builtin). Just checking.

thernstig commented 3 years ago

I don't understand that. How are you planning to run nvm use then?

I also just realized this is not possible... I should have thought this through better. Let me contemplate.

edit: Yes of course I realize nvm is not a binary, I did review the 2.0 update. I just did not think this through entirely.

thernstig commented 3 years ago

I cannot come up with a sound solution, since I cannot use nvm use. As such, I believe the best approach is to create an issue to clean up the current recommendation at https://github.com/direnv/direnv/wiki/Node#using-nvm-with-automatic-discovery and then just wait on:

I may choose to add automatic, async version switching in the future here instead.

5t111111 commented 3 years ago

I tried to get recent versions of nvm.fish and direnv work together, but I eventually gave up to update to 2.0 or above and pin jorgebucaran/nvm.fish@1.1.0.

I really hope that alternative auto switching of node versions come.