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

How to automatically switch to default version when I'm not near an .fnmrc? #58

Closed
corysimmons opened this issue Nov 27, 2017 · 13 comments

Comments

@corysimmons
Copy link

corysimmons commented Nov 27, 2017

node -v
9.0.0

cd foo
'1.0.0' > .fnrmc
refresh
node -v
1.0.0

cd ..
node -v
1.0.0 ❗️

I feel like .fnmrc should only temporarily swap node version. :\

@davinkevin

This comment has been minimized.

@corysimmons

This comment has been minimized.

@davinkevin

This comment has been minimized.

@jorgebucaran
Copy link
Owner

jorgebucaran commented Jan 9, 2019

The new version of this package will not feature automatic version switching (no custom node.fish function). How you can write one yourself will be documented (I'll include several examples). I have no plans on adding one, so we can close.

I understand @corysimmons sentiment of using .nvmrc only to temporarily swap node versions, but I don't know if everyone would agree that should be the default.

@jorgebucaran jorgebucaran added enhancement New feature or request Wontfix labels Jan 9, 2019
@corysimmons
Copy link
Author

Oh hey Jorge! I didn't even notice this was your lib. :D

@caillou
Copy link

caillou commented Sep 19, 2019

@jorgebucaran You wrote:

How you can write one yourself will be documented (I'll include several examples).

I searched the repsitory, yet couldn't find such a documentation or example.

Could you hint me at a direction, on how to do so, as I am fairly new to fish.

Once I figure it out, I could make a PR to the readme.md with this documentation.

@jorgebucaran
Copy link
Owner

jorgebucaran commented Sep 19, 2019

@caillou Here's a simple/naive implementation:

touch ~/.config/fish/functions/node.fish
function node
  nvm && command node $argv
end

@caillou
Copy link

caillou commented Sep 19, 2019

@jorgebucaran Thanks for your quick answer.

Unfortunately I usually use node implicitly, meaning I use a global cli function installed through nvm. Or run a cli function within the local ./node_modules/.bin folder, e.g.

# This is my main use case
$ npm install tldr
$ tldr curl

# This is the one that usually requires an out-dated node verion
$ ./node_modules/.bin/gulp --dev

I am looking for a way to execute fish-nvm when changing directory.

@caillou
Copy link

caillou commented Sep 19, 2019

@jorgebucaran I found an answer to my problem, will send a PR to the readme.md.

function __check_nvm --on-variable PWD --description 'Check nvm version on directoy change'
  status --is-command-substitution; and return
  nvm
end

@caillou
Copy link

caillou commented Sep 19, 2019

@jorgebucaran Sorry to disturb one more time.

Given the __check_nvm function defined in my previous comment, the Node version gets updated when I change directory.

This being said, it does not work in a new session. I need to call __check_nvm for it to react to directory changes.

So I went ahead and naively added __check_nvm in my ~/.config/fish/config.fish. This works but introduces a new bug:

Functions are not shown anymore in the web interface of fish_config. If I remove the call to nvm in the __check_nvm function, the web interface is working again.

Do you have an idea why this is so? What am I doing wrong? Do I need to call __check_nvm in order to register it as a callback to the change of PWD?

@jorgebucaran
Copy link
Owner

This being said, it does not work in a new session. I need to call __check_nvm for it to react to directory changes.

Makes sense. Not what you want, though.

Do you have an idea why this is so? What am I doing wrong? Do I need to call __check_nvm in order to register it as a callback to the change of PWD?

Nope. I couldn't reproduced it either. I added the following to my config.fish (I actually don't use one, but created one to try this):

function on_pwd_change_do --on-variable PWD
    status --is-command-substitution && return
    echo foo
end

...switched directories a few times, ran fish_config and tried other combinations of that and functions are still shown in the web interface.

@caillou
Copy link

caillou commented Sep 20, 2019

@jorgebucaran Thank you for your quick and exhaustive answer.

Your approach works, but does not call nvm when I start a new session.

Executing nvm within the config.fish is what breaks the functions tab in the fish_config web interface.

It makes sense to run nvm when I start a new session, as fish-nvm uses the last used version when starting up a new session.

I tried running nvm & in the backgroud, but this is unfortunately not possible with fish functions.

I also noticed that fish-nvm globally changes the Node version. This means, it is not possible to have a session with lts while having another session with v9.0.0.

@jorgebucaran
Copy link
Owner

jorgebucaran commented Sep 20, 2019

@caillou Executing nvm within the config.fish is what breaks the functions tab in the fish_config web interface.

Couldn't repro that either.

Here's my suggestion: create a configuration snippet like ~/.config/fish/conf.d/nvm.fish (you can use any name you like, I'll use nvm.fish) and run nvm:

mkdir -p ~/.config/fish/conf.d
echo nvm > ~/.config/fish/conf.d/nvm.fish

I tried running nvm & in the background, but this is ... not possible with fish functions.

Correct.

I also noticed that fish-nvm globally changes the Node version.

Yes. In this way, it's similar to tj/n as far as I remember (don't know if they changed it recently).

@jorgebucaran jorgebucaran removed enhancement New feature or request Wontfix labels Jan 5, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants