Skip to content
This repository has been archived by the owner on Dec 6, 2023. It is now read-only.

Running with nvm doesn't switch versions consistently #30

Closed
mockdeep opened this issue Nov 25, 2015 · 13 comments
Closed

Running with nvm doesn't switch versions consistently #30

mockdeep opened this issue Nov 25, 2015 · 13 comments

Comments

@mockdeep
Copy link

I'm having some trouble getting up and running with avn. I've installed it for nvm and ran avn setup. Here's what happens when I try to use it, though:

$ node --version
v0.10.32
$ __avn_debug
avn activated 4.2.2 (avn-nvm v4.2.2)
nvm use v4.2.2 > /dev/null;
$ node --version
v0.10.32
$ cd .
$ node --version
v0.10.32
$ nvm use v4.2.2
Now using node v4.2.2 (npm v2.14.7)

It does seem to set the right version sometimes, though. Maybe when I first open a new terminal session and cd to the directory.

@mockdeep
Copy link
Author

It might be helpful if it showed the nvm output without the > /dev/null bit every time.

@wbyoung
Copy link
Owner

wbyoung commented Nov 25, 2015

@mockdeep what shell are you using? Can you post the contents of your ~/.bash_profile or ~/.zshrc?

How did you install nvm? Did you use homebrew or did you install using the instructions on the nvm page?

@mockdeep
Copy link
Author

@wbyoung I'm using bash on linux. Here's my ~/.bash_profile. I'm using rvm so not sure if that might conflict. I installed nvm via the instructions on their page.

[[ -s "$HOME/.profile" ]] && source "$HOME/.profile" # Load the default .profile

[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # Load RVM into a shell session *as a function*
[[ -s "$HOME/.avn/bin/avn.sh" ]] && source "$HOME/.avn/bin/avn.sh" # load avn

@wbyoung
Copy link
Owner

wbyoung commented Nov 30, 2015

Can you add a node version after the eval in avn.sh that's installed within ~/.avn and let me know what the output of changing into a directory is?

@mockdeep
Copy link
Author

Okay, so I dug into it a bit more and it turns out it's erring out when it tries to load:

$ sh .avn/bin/avn.sh
.avn/bin/avn.sh: 5: .avn/bin/avn.sh: [[: not found
.avn/bin/avn.sh: 7: .avn/bin/avn.sh: [[: not found
avn does not yet support this shell
.avn/bin/avn.sh: 16: export: Illegal option -a

I tossed a set -e into the top of the shell script and it crashes the terminal on load. TIL bash is actually a different thing from sh on linux systems, and unless you set a shebang, shell scripts are by default evaluated with sh, which doesn't have quite as much syntactic goodness. After adding #!/usr/bin/env bash to the top of the file it seems to work as expected.

@wbyoung
Copy link
Owner

wbyoung commented Nov 30, 2015

That seems a bit odd. Your login shell is set as sh and sourcing your .bash_profile?

If so, this is something I'd want to try to cover when tackling #26.

Everything's working now, though? If so, let's close this issue.

@ljharb
Copy link

ljharb commented Nov 30, 2015

@mockdeep I think that's a red herring. You're supposed to source avn.sh, not execute it with sh. Try . .avn/bin/avn.sh instead?

@wbyoung
Copy link
Owner

wbyoung commented Nov 30, 2015

Yes, thanks @ljharb. I didn't read fully before. The fact that __avn_debug was previously working shows that the script is being sourced properly.

@mockdeep please proceed with the previous suggestion of node version after eval and report back.

@mockdeep
Copy link
Author

@wbyoung I did that before and didn't get any output. Because I wasn't seeing any output I added set -e (which cancels the script on error) to the top of the script and sourced my .bash_profile, which crashed my terminal. Same thing when I source .avn/bin/avn.sh directly. Which is what prompted me to try to run the script directly and I discovered the error above. Adding the shebang fixed the sourcing issue for me. I don't think the login shell is set as sh. It simply tries to run the script with the most appropriate application and by default unless specified otherwise it assumes .sh files are to be run with sh. If you have a look at the likes of rvm you'll see they add the shebang to the top of their files, though others like nvm use posix compliant syntax to avoid the issue.

@wbyoung
Copy link
Owner

wbyoung commented Dec 1, 2015

Your ~/.bash_profile uses [[ test ]] syntax before sourcing avn.sh as you previously stated. Also, with __avn_debug producing output, it seems clear that avn.sh is actually being loaded properly. Sourcing avn.sh directly should still work via . ~/.avn/bin/avn.sh or source ~/.avn/bin/avn.sh, but ~/.avn/bin/avn.sh is not sourcing it, it's an attempt to execute it (as would be sh ~/.avn/bin/avn.sh).

I just tested my suggestion to add node --version to the ~/.avn/bin/avn.sh file and it does produce output. You'll want to start a new shell and cd to a directory that has a .node-version file in order to see the output, though. Sourcing the script after it's already been loaded may not produce the expected result.

@mockdeep
Copy link
Author

mockdeep commented Dec 1, 2015

Okay, so I've been digging around for a little while now. It appears you're right that .bash_profile sources everything using bash. The real problem seems to be that .bash_profile isn't getting loaded at all, and some combination of the things I was trying to do was masking this fact. Now when I run __avn_debug from a fresh boot I get __avn_debug: command not found. It turns out .bash_profile only gets loaded for login sessions, like you would get when you ssh into a machine, and not when you boot into the OS.

I can confirm that it does load .profile, but for some reason putting the source line there doesn't do the trick. I still get command not found. Only putting it in .bashrc gets avn to work as expected, or manually sourcing .profile or .avn/bin/avn.sh. nvm and rvm both add themselves to the .bashrc, too, which is presumably why they work fine.

@wbyoung
Copy link
Owner

wbyoung commented Dec 1, 2015

Yeah, on a Mac ~/.bash_profile is more like what ~/.bashrc is on Linux. The setup currently only looks for ~/.bash_profile, so we could certainly be smarter about that. I've opened #30 for that and am closing this. I'm assuming everything's working for you now that you've moved it to ~/.bashrc, yes?

@wbyoung wbyoung closed this as completed Dec 1, 2015
@wbyoung wbyoung reopened this Dec 1, 2015
@wbyoung wbyoung closed this as completed Dec 1, 2015
@mockdeep
Copy link
Author

mockdeep commented Dec 1, 2015

Yep, all good. Thanks!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants