nvm: sourcing nvm.sh is slow even with `--no-use`
I start up a lot of terminal windows on my Mac and for some reason the commands
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" --no-use
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"
that I had in my .bashrc
would sometimes take a noticeable and annoying amount of time to run. (Maybe as long as 2 seconds.) My solution was to setup environment variables like NVM_DIR
as usual but to fully defer all other nvm
setup until first use by adding these definitions to my .bash_profile
script:
function _install_nvm() {
unset -f nvm
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This sets up nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # load nvm bash_completion
nvm "$@"
}
function nvm() {
_install_nvm "$@"
}
I am not familiar enough with other shells to say how portable that is (I am using GNU bash
), but I suggest you at least add it to the documentation (if not the install script) so that people can have an easy way to install deferred setup of nvm
that is truly deferred.
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 14
- Comments: 21 (8 by maintainers)
Commits related to this issue
- [Fix] `nvm_supports_source_options`: work around a bug in bash 3.2 See https://github.com/creationix/nvm/issues/1978#issuecomment-453480231 Fixes #1978. — committed to ljharb/nvm by ljharb 5 years ago
- [Fix] `nvm_supports_source_options`: work around a bug in bash 3.2 See https://github.com/creationix/nvm/issues/1978#issuecomment-453480231 Fixes #1978. — committed to ljharb/nvm by ljharb 5 years ago
- [Fix] `nvm_supports_source_options`: work around a bug in bash 3.2 See https://github.com/creationix/nvm/issues/1978#issuecomment-453480231 Fixes #1978. — committed to ljharb/nvm by ljharb 5 years ago
I use this for zsh:
@ljharb Thanks for re-opening this.
It appears
--no-use
is sometimes slow becausenvm_supports_source_options
is not reliable, at least not underGNU bash version 3.2.57(1)
on my Mac, and whennvm_supports_source_options
isfalse
, the--no-use
option is ignored.The current implementation of
nvm_supports_source_options
writes to a pipe that then tries to source that pipe as a shell script via/dev/stdin
. I guess there is some race condition resulting in the output of the pipe being discarded rather than processed.FWIW, I updated my
.bash_profile
script to this:That suits me better, because there is no loading of anything until I need it, but when I need
node
, it is (almost) immediately available.With this solution:
I still losing
0.01 - 0.03s
of shell speed. but thanks anyway.Looks like this is a bug in Gnu bash 3.2.x, which is what Apple ships, apparently because of licensing issues. (
bash 3.2.57(1)
is what shipped with El Capitan in 2015 and it is still what is shipping with Catalina in 2020, although starting with Catalina the default shell was switched tozsh
.) It appears the receiving side of the pipe does not wait for the sending side of the pipe to start producing output before deciding that is has reached EOF. The best workaround I can give you so far is to use a here-is document instead ofecho
to produce the script. I’m not a portability expert and do not know what it might break, so I do not want to offer this as a PR, but feel free to take it.Spent a hour of debugging my .zshrc file, thanks god I found why
@ljharb Here is the new issue https://github.com/nvm-sh/nvm/issues/2387
Thanks @ljharb for your quick replies. I will do that