When I source nvm.sh like . "$HOME/.nvm/nvm.sh" for manual install and like . $(brew --prefix nvm)/nvm.sh, the exit code is 3.
Even though nvm works after that, in bash scripts with set -e, the script stops there, since the command exits with a non-zero value.
Is there a reason this fails?
Is there a reason it exits with 3, if it's not an actual failure?
What can we do about that?
Some extra info:
. nvm.sh with set -x for "tracing":Operating system and version: macOS 10.14.2 (mojave)
nvm debug output:
Details
锘縩vm --version: v0.34.0
$TERM_PROGRAM: iTerm.app
$SHELL: /bin/zsh
$SHLVL: 1
$HOME: /Users/pvinis
$NVM_DIR: '$HOME/.nvm'
$PATH: $HOME/.gem/ruby/2.5.1/bin:$HOME/.rubies/ruby-2.5.1/lib/ruby/gems/2.5.0/bin:$HOME/.rubies/ruby-2.5.1/bin:$NVM_DIR/versions/node/v11.0.0/bin:$HOME/.config/yarn/global/node_modules/.bin:$HOME/.yarn/bin:$HOME/Library/Python/3.7/bin:$HOME/.fastlane/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/MacGPG2/bin:/opt/X11/bin:$HOME/.rvm/bin
$PREFIX: ''
$NPM_CONFIG_PREFIX: ''
$NVM_NODEJS_ORG_MIRROR: ''
$NVM_IOJS_ORG_MIRROR: ''
shell version: 'zsh 5.3 (x86_64-apple-darwin18.0)'
uname -a: 'Darwin 18.2.0 Darwin Kernel Version 18.2.0: Mon Nov 12 20:24:46 PST 2018; root:xnu-4903.231.4~2/RELEASE_X86_64 x86_64'
OS version: Mac 10.14.2 18C54
curl: /usr/bin/curl, curl 7.54.0 (x86_64-apple-darwin18.0) libcurl/7.54.0 LibreSSL/2.6.4 zlib/1.2.11 nghttp2/1.24.1
wget: not found
git: /usr/local/bin/git, git version 2.19.1
grep: /usr/bin/grep, grep (BSD grep) 2.5.1-FreeBSD
awk: /usr/bin/awk, awk version 20070501
sed: illegal option -- -
usage: sed script [-Ealn] [-i extension] [file ...]
sed [-Ealn] [-i extension] [-e script] ... [-f script_file] ... [file ...]
sed: /usr/bin/sed,
cut: illegal option -- -
usage: cut -b list [-n] [file ...]
cut -c list [file ...]
cut -f list [-s] [-d delim] [file ...]
cut: /usr/bin/cut,
basename: illegal option -- -
usage: basename string [suffix]
basename [-a] [-s suffix] string [...]
basename: /usr/bin/basename,
rm: illegal option -- -
usage: rm [-f | -i] [-dPRrvW] file ...
unlink file
rm: /bin/rm,
mkdir: illegal option -- -
usage: mkdir [-pv] [-m mode] directory ...
mkdir: /bin/mkdir,
xargs: illegal option -- -
usage: xargs [-0opt] [-E eofstr] [-I replstr [-R replacements]] [-J replstr]
[-L number] [-n number [-x]] [-P maxprocs] [-s size]
[utility [argument ...]]
xargs: /usr/bin/xargs,
nvm current: v11.0.0
which node: $NVM_DIR/versions/node/v11.0.0/bin/node
which iojs: iojs not found
which npm: $NVM_DIR/versions/node/v11.0.0/bin/npm
npm config get prefix: $NVM_DIR/versions/node/v11.0.0
npm root -g: $NVM_DIR/versions/node/v11.0.0/lib/node_modules
nvm ls output: v8.12.0
v10.12.0
-> v11.0.0
system
default -> v11 (-> v11.0.0)
node -> stable (-> v11.0.0) (default)
stable -> 11.0 (-> v11.0.0) (default)
iojs -> N/A (default)
unstable -> N/A (default)
lts/* -> lts/dubnium (-> N/A)
lts/argon -> v4.9.1 (-> N/A)
lts/boron -> v6.14.4 (-> N/A)
lts/carbon -> v8.12.0
lts/dubnium -> v10.13.0 (-> N/A)
How did you install nvm? (e.g. install script in readme, Homebrew):
Tried both. I mostly do from brew though, since it's easier.
What steps did you perform?
source the file
What happened?
worked, but exits with non-zero value
What did you expect to happen?
to exit with a 0 value since everything seemingly went well
Is there anything in any of your profile files (.bashrc, .bash_profile, .zshrc, etc) that modifies the PATH?
I ran this without any $PATH alterations. In general I don't have anything in there that touch nvm. Only to include brew stuff.
The issue template does not work well with the details.. I tried to fix it but I failed.
Note that nvm is not supported with homebrew - if you brew uninstall it, and install it the proper way (in the readme), do you see the same behavior?
Thanks for the -x output, but it's not clear to me where it's erroring :-/ it's getting to the nvm_tree_contains_path call on line 2178 (via the nvm_die_on_prefix call on line 2995), and the only variable compared with -n after that is NVM_USE_OUTPUT - which is declared with local on line 2977.
The exit code 3 is returned from nvm in a few places:
nvm ls-remote fails via nvm_remote_versionsnvm ls failsnvm_version_dir fails (called internally, unlikely)nvm_version_path fails (called internally, unlikely)nvm_version failsOne possibility is, could you edit nvm.sh to change every return 3 to a different 2-digit number, and then see which callsite is causing the error?
brew or manual, both give me this exit code.
that's a good idea. I'll try that tomorrow morning since I'm on my phone now.
Is there a reason actually that it returns the same number Ina few places? why not use different numbers for different places and different errors by default?
Typically I use unique numbers for each code path; but as nvm has gotten more complex, they've blurred together a bit. It would be a reasonable (but technically breaking) change to make sure every exit code in the app is unique (at least for things more complex than simple 0/1 predicates).
omg I changed all numbers to unique ones (except the 312 :p)

and it exits with 0.
I'll start putting them back to 3 and see how when it breaks again.
Hm, ok on my home macbook its 0 exit even with a freshly installed one. That's good. Then I'll check tomorrow at work with the problematic machine, and see what could be the issue.
Thanks for your efforts! I鈥檓 looking forward to fixing this, and perhaps convincing me it鈥檚 time for unique exit codes :-D
I found the reproducible way!
With nvm not in the system, uninstalled, cleared etc, no rc files, no nodes installed, try this:
.nvmrcnvm.sh file.3 that comes from https://github.com/creationix/nvm/blob/master/nvm.sh#L2944.nvm install which makes sense, but also returns with 3.Basically, the problem here is sourcing nvm in a dir that has a .nvmrc in it. If I install and source outside, then go in the dir, it's fine, and when I do nvm use it will complain and return 3, which is great.
I don't know how this should be handled. I am basically trying to setup a project with ci, and in the ci I am already in the dir, and I install nvm, then source nvm, then nvm install, then nvm use. This order would work if sourcing would not try to do nvm use inside it I'm guessing.
What do you think?
Interesting. You can source with --no-use, or with --install to autoinstall - does this address it for you?
(I'd expect nvm to be sourced long before any CI system cd's into a project directory)
I'll try it.
Well, if the ci is using a node image, usually nvm is there already. If it's booting a macos image though, nvm is probably missing, so the init happens, cds in my project and then I get control, when I would start installing nvm and other things that I need.
This might be due to a bug in bash 3 (which ships on Macs) which makes set -e exit even on tests that exit nonzero.
To be honest, I switched to nodenv since last month. Feel free to keep this issue open to solve this if it's still a problem, or close it if you feel it's not relevant anymore.
nvm, thank you for your service :D
Sorry to hear that :-)
I believe I've solved it with 58d0933f72c3b819d8180e3fe68b66201a87c0a4, so I'll close.
I will try master out of curiosity :D
Most helpful comment
I found the reproducible way!
With nvm not in the system, uninstalled, cleared etc, no rc files, no nodes installed, try this:
.nvmrcnvm.shfile.it exits with code
3that comes from https://github.com/creationix/nvm/blob/master/nvm.sh#L2944.nvm is correctly sourced, and works fine, but it prints that i need run
nvm installwhich makes sense, but also returns with3.Basically, the problem here is sourcing nvm in a dir that has a
.nvmrcin it. If I install and source outside, then go in the dir, it's fine, and when I donvm useit will complain and return3, which is great.I don't know how this should be handled. I am basically trying to setup a project with ci, and in the ci I am already in the dir, and I install nvm, then source nvm, then nvm install, then nvm use. This order would work if sourcing would not try to do nvm use inside it I'm guessing.
What do you think?