lukechilds / zsh-nvm

Zsh plugin for installing, updating and loading nvm

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Attempting to source nvm from within a ruby script does not use parent shell

snowe2010 opened this issue · comments

Working with the nvm guys, we debugged a problem I'm having where attempting to use the zsh-nvm plugin causes a Rakefile that attempts to source the ~/.nvm/nvm.sh to use a different shell than the parent, or it sets some options that are incompatible or something. If I do not load the zsh-nvm plugin but I do source the nvm file myself then everything works as expected.

nvm-sh/nvm#1583

Your example Rakefile is working ok for me with zsh-nvm.

Can you provide the version of zsh/rake/nvm/zsh-nvm you're using and which zsh-nvm env vars you have enabled?

❯ zsh --version
zsh --version
zsh 5.3.1 (x86_64-apple-darwin16.3.0)

~/Documents/dev/code
❯ rake --version
rake --version
rake, version 0.9.6

~/Documents/dev/code
❯ nvm --version
nvm --version
0.33.0

I do not know how to tell the version of zsh-nvm as I'm just using oh-my-zsh's plugin feature
I also have not added any zsh-nvm env vars

hmmnn, can you try nvm upgrade (with zsh-nvm loaded)

I did as you said, there was a ton of output, and then I retried my rake task and I still got the same error

/Users/tylerthrailkill/.nvm/nvm.sh: line 27: cd: -q: invalid option
cd: usage: cd [-L|-P] [dir]
N/A: version "4.5.0 -> N/A" is not yet installed.

oh wait, I have NVM_LAZY_LOAD=true, don't know how I missed that.

Hmmn, I still don't seem to be able to reproduce the nvm.sh: line 27: cd: -q: invalid option warning, even with NVM_LAZY_LOAD=true.

Can you try commenting out your entire .zshrc and just manually sourcing zsh-nvm from wherever omz installs it. Could possibly be a problem with omz + zsh-nvm combo.

Ok with

export ZSH=/Users/tylerthrailkill/.oh-my-zsh
ZSH_THEME=""
plugins=(brew bundler colorize common-aliases fancy-ctrl-z git git-flow git-prompt gitfast grunt gulp jsontools lol mvn nyan spring sublime thefuck themes fasd zsh-autosuggestions zsh-syntax-highlighting)
source $ZSH/oh-my-zsh.sh

as my .zshrc

running rake gives me

tylerthrailkill:~/Documents/dev/code/% rake
rake aborted!
npm missing

as expected

tylerthrailkill:~/Documents/dev/code/blah% ls ~/.oh-my-zsh/plugins/nvm/nvm.plugin.zsh                                                                                                                                                           (feature/add-rakefile|✔)
/Users/tylerthrailkill/.oh-my-zsh/plugins/nvm/nvm.plugin.zsh
tylerthrailkill:~/Documents/dev/code/blah% source ~/.oh-my-zsh/plugins/nvm/nvm.plugin.zsh                                                                                                                                                       (feature/add-rakefile|✔)
tylerthrailkill:~/Documents/dev/code/blah% rake                                                                                                                                                                                                 (feature/add-rakefile|✔)
3.10.10
=============================
Setting up Blah
=============================
Now using node v4.5.0 (npm v2.15.9)
npm WARN package.json borrower-wallet@0.0.0 No license field.
^Crake aborted!

/Users/tylerthrailkill/Documents/dev/code/borrower-wallet-ui/Rakefile:23:in `system'
/Users/tylerthrailkill/Documents/dev/code/borrower-wallet-ui/Rakefile:23:in `block in <top (required)>'
Tasks: TOP => default => setup
(See full trace by running task with --trace)
tylerthrailkill:~/Documents/dev/code/blah%

So it looks like sourcing it manually works.

Can you try the manual source again but with NVM_LAZY_LOAD=true just to see if it's lazy loading that's causing the issue (although I think that's unlikely).

By the way, here is the output of my plugins directory, I don't see zsh-nvm there so I assumed it was nvm

tylerthrailkill:~/Documents/dev/code/% ls ~/.oh-my-zsh/plugins/nvm 
_nvm           nvm.plugin.zsh
tylerthrailkill:~/Documents/dev/code/% ls ~/.oh-my-zsh/plugins  
adb                      cakephp3                 cp                       fasd                     gitignore                jump                     mvn                      pod                      rust                     swiftpm                  urltools
ant                      capistrano               cpanm                    fastfile                 glassfish                kate                     mysql-macports           postgres                 rvm                      symfony                  vagrant
apache2-macports         cargo                    debian                   fbterm                   globalias                kitchen                  n98-magerun              pow                      safe-paste               symfony2                 vault
arcanist                 cask                     dircycle                 fedora                   gnu-utils                knife                    nanoc                    powder                   sbt                      systemadmin              vi-mode
archlinux                catimg                   dirhistory               firewalld                go                       knife_ssh                ng                       powify                   scala                    systemd                  vim-interaction
asdf                     celery                   dirpersist               forklift                 golang                   kubectl                  nmap                     profiles                 scd                      taskwarrior              virtualenv
autoenv                  chruby                   django                   fossil                   gpg-agent                laravel                  node                     pyenv                    screen                   terminalapp              virtualenvwrapper
autojump                 chucknorris              dnf                      frontend-search          gradle                   laravel4                 nomad                    pylint                   scw                      terminitor               vundle
autopep8                 cloudapp                 docker                   gas                      grails                   laravel5                 npm                      python                   sfffe                    terraform                wakeonlan
aws                      codeclimate              docker-compose           gb                       grunt                    last-working-dir         nvm                      rails                    shrink-path              textastic                wd
battery                  coffee                   dotenv                   geeknote                 gulp                     lein                     nyan                     rake                     singlechar               textmate                 web-search
bbedit                   colemak                  droplr                   gem                      heroku                   lighthouse               osx                      rake-fast                spring                   thefuck                  wp-cli
bgnotify                 colored-man-pages        emacs                    git                      history                  lol                      pass                     rand-quote               sprunge                  themes                   xcode
boot2docker              colorize                 ember-cli                git-extras               history-substring-search macports                 paver                    rbenv                    ssh-agent                thor                     yarn
bower                    command-not-found        emoji                    git-flow                 httpie                   man                      pep8                     rbfu                     stack                    tig                      yii
branch                   common-aliases           emoji-clock              git-flow-avh             iwhois                   marked2                  per-directory-history    react-native             sublime                  tmux                     yii2
brew                     compleat                 emotty                   git-hubflow              jake-node                mercurial                perl                     rebar                    sudo                     tmux-cssh                yum
bundler                  composer                 encode64                 git-prompt               jhbuild                  meteor                   perms                    redis-cli                supervisor               tmuxinator               z
bwana                    copybuffer               extract                  git-remote-branch        jira                     mix                      phing                    repo                     suse                     torrent                  zeus
cabal                    copydir                  fabric                   gitfast                  jruby                    mix-fast                 pip                      rsync                    svn                      tugboat                  zsh-navigation-tools
cake                     copyfile                 fancy-ctrl-z             github                   jsontools                mosh                     pj                       ruby                     svn-fast-info            ubuntu                   zsh_reload
tylerthrailkill:~/Documents/dev/code/%
tylerthrailkill:~/Documents/dev/code/blah% rake                                                                                                                                                                                                 (feature/add-rakefile|✔)
rake aborted!
npm missing
/Users/tylerthrailkill/Documents/dev/code/blah/Rakefile:16:in `block in <top (required)>'
Tasks: TOP => default => setup => npm
(See full trace by running task with --trace)
tylerthrailkill:~/Documents/dev/code/blah% export NVM_LAZY_LOAD=true                                                                                                                                                                            (feature/add-rakefile|✔)
tylerthrailkill:~/Documents/dev/code/blah% source ~/.oh-my-zsh/plugins/nvm/nvm.plugin.zsh                                                                                                                                                       (feature/add-rakefile|✔)
tylerthrailkill:~/Documents/dev/code/blah% rake                                                                                                                                                                                                 (feature/add-rakefile|✔)
3.10.10
=============================
Setting up Blah
=============================
Now using node v4.5.0 (npm v2.15.9)
npm WARN package.json borrower-wallet@0.0.0 No license field.
^Crake aborted!

/Users/tylerthrailkill/Documents/dev/code/blah/Rakefile:23:in `system'
/Users/tylerthrailkill/Documents/dev/code/blah/Rakefile:23:in `block in <top (required)>'
Tasks: TOP => default => setup
(See full trace by running task with --trace)
tylerthrailkill:~/Documents/dev/code/blah%

doesn't seem to be the issue

I don't see zsh-nvm there so I assumed it was nvm

You should have a zsh-nvm dir. Are you sure you've installed zsh-nvm in the plugins dir?

git clone https://github.com/lukechilds/zsh-nvm ~/.oh-my-zsh/custom/plugins/zsh-nvm

oops i wasn't in the custom directory

Ok so the zsh-nvm source does fail

tylerthrailkill:~/Documents/dev/code% cd blah
tylerthrailkill:~/Documents/dev/code/blah% rake
rake aborted!
npm missing
/Users/tylerthrailkill/Documents/dev/code/blah/Rakefile:16:in `block in <top (required)>'
Tasks: TOP => default => setup => npm
(See full trace by running task with --trace)
tylerthrailkill:~/Documents/dev/code/blah% source ~/.oh-my-zsh/custom/plugins/zsh-nvm/zsh-nvm.plugin.zsh
tylerthrailkill:~/Documents/dev/code/blah% rake
3.10.10
=============================
Setting up Blah
=============================
/Users/tylerthrailkill/.nvm/nvm.sh: line 27: cd: -q: invalid option
cd: usage: cd [-L|-P] [dir]
N/A: version "4.5.0 -> N/A" is not yet installed.

You need to run "nvm install 4.5.0" to install it before using it.
^Crake aborted!

/Users/tylerthrailkill/Documents/dev/code/blah/Rakefile:23:in `system'
/Users/tylerthrailkill/Documents/dev/code/blah/Rakefile:23:in `block in <top (required)>'
Tasks: TOP => default => setup
(See full trace by running task with --trace)
tylerthrailkill:~/Documents/dev/code/blah%

Interesting, can you try cd ~/.oh-my-zsh/custom/plugins/zsh-nvm/ && git pull to update to the latest version of zsh-nvm.

Ok so I modified the zsh-nvm.plugin.zsh script so I could see what is happening. I added some echo statements.

 34 _zsh_nvm_load() {
 35 echo 'hi reloaded'
 36   # Source nvm
 37   source "$NVM_DIR/nvm.sh"
 38
 39   # Rename main nvm function
 40   _zsh_nvm_rename_function nvm _zsh_nvm_nvm
 41
 42   # Wrap nvm in our own function
 43   nvm() {
 44           echo "HIIIII"
 45     case $1 in
 46       'upgrade')
 47         _zsh_nvm_upgrade
 48         ;;
 49       'revert')
 50         _zsh_nvm_revert
 51         ;;
 52       *)
 53               echo "HI"
 54         _zsh_nvm_nvm "$@"
 55         ;;
 56     esac
 57   }
 58 }

I get no output when running nvm from inside the rakefile, but I get it when running nvm by itself.

tylerthrailkill:~/Documents/dev/code/blah% vim ~/.oh-my-zsh/custom/plugins/zsh-nvm/zsh-nvm.plugin.zsh
tylerthrailkill:~/Documents/dev/code/blah% source ~/.oh-my-zsh/custom/plugins/zsh-nvm/zsh-nvm.plugin.zsh
tylerthrailkill:~/Documents/dev/code/blah% rake
3.10.10
=============================
Setting up Borrower Wallet UI
=============================
/Users/tylerthrailkill/.nvm/nvm.sh: line 27: cd: -q: invalid option
cd: usage: cd [-L|-P] [dir]
N/A: version "4.5.0 -> N/A" is not yet installed.

You need to run "nvm install 4.5.0" to install it before using it.
^Crake aborted!

/Users/tylerthrailkill/Documents/dev/code/blah/Rakefile:23:in `system'
/Users/tylerthrailkill/Documents/dev/code/blah/Rakefile:23:in `block in <top (required)>'
Tasks: TOP => default => setup


(See full trace by running task with --trace)
tylerthrailkill:~/Documents/dev/code/blah% source ~/.oh-my-zsh/custom/plugins/zsh-nvm/zsh-nvm.plugin.zsh
tylerthrailkill:~/Documents/dev/code/blah% vim ~/.oh-my-zsh/custom/plugins/zsh-nvm/zsh-nvm.plugin.zsh
tylerthrailkill:~/Documents/dev/code/blah% source ~/.oh-my-zsh/custom/plugins/zsh-nvm/zsh-nvm.plugin.zsh
hi reloaded
tylerthrailkill:~/Documents/dev/code/blah% nvm -h
HIIIII
HI
HIIIII
HI

@lukechilds Ok it did pull something, but still errors

tylerthrailkill:~/Documents/dev/code/blah% vim ~/.oh-my-zsh/custom/plugins/zsh-nvm/zsh-nvm.plugin.zsh
tylerthrailkill:~/Documents/dev/code/blah% cd ~/.oh-my-zsh/custom/plugins/zsh-nvm
tylerthrailkill:~/.oh-my-zsh/custom/plugins/zsh-nvm% git pull
error: cannot pull with rebase: You have unstaged changes.
error: please commit or stash them.
tylerthrailkill:~/.oh-my-zsh/custom/plugins/zsh-nvm% gst
zsh: command not found: gst
tylerthrailkill:~/.oh-my-zsh/custom/plugins/zsh-nvm% git status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)

modified:   zsh-nvm.plugin.zsh

no changes added to commit (use "git add" and/or "git commit -a")
tylerthrailkill:~/.oh-my-zsh/custom/plugins/zsh-nvm% git stash
Saved working directory and index state WIP on master: 7a3747f Update usage in readme for new features
HEAD is now at 7a3747f Update usage in readme for new features
tylerthrailkill:~/.oh-my-zsh/custom/plugins/zsh-nvm% git pull
remote: Counting objects: 406, done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 406 (delta 1), reused 3 (delta 1), pack-reused 401
Receiving objects: 100% (406/406), 57.62 KiB | 0 bytes/s, done.
Resolving deltas: 100% (225/225), done.
From https://github.com/lukechilds/zsh-nvm
7a3747f..5c59940  master            -> origin/master
* [new branch]      auto-use          -> origin/auto-use
* [new branch]      bugfix-19         -> origin/bugfix-19
* [new branch]      install-keywords  -> origin/install-keywords
* [new branch]      tests             -> origin/tests
* [new branch]      yarn-lazy         -> origin/yarn-lazy
* [new branch]      yarn-unset-notice -> origin/yarn-unset-notice
First, rewinding head to replay your work on top of it...
Fast-forwarded master to 5c59940a6d66568203b4f0acb99b8fe183fb89a1.
tylerthrailkill:~/.oh-my-zsh/custom/plugins/zsh-nvm% nvm upgrade
HIIIII
Installed version is v0.33.2
Checking latest version of nvm...
You're already up to date
tylerthrailkill:~/.oh-my-zsh/custom/plugins/zsh-nvm% source ~/.oh-my-zsh/custom/plugins/zsh-nvm/zsh-nvm.plugin.zsh
tylerthrailkill:~/.oh-my-zsh/custom/plugins/zsh-nvm% cd ../../../
tylerthrailkill:~/.oh-my-zsh% cd ../
tylerthrailkill:~% cd Documents/dev/code/blah
tylerthrailkill:~/Documents/dev/code/blah% rake
3.10.10
=============================
Setting up Blah
=============================
/Users/tylerthrailkill/.nvm/nvm.sh: line 27: cd: -q: invalid option
cd: usage: cd [-L|-P] [dir]
N/A: version "4.5.0 -> N/A" is not yet installed.

You need to run "nvm install 4.5.0" to install it before using it.
^Crake aborted!

/Users/tylerthrailkill/Documents/dev/code/blah/Rakefile:23:in `system'
/Users/tylerthrailkill/Documents/dev/code/blah/Rakefile:23:in `block in <top (required)>'
Tasks: TOP => default => setup
(See full trace by running task with --trace)
tylerthrailkill:~/Documents/dev/code/blah%

False alarm! Reloading my entire profile after git pulling seems to have fixed the issue!

Thanks for the help! It appears the plugin was just out of date, though those are very strange errors for an out of date plugin haha. Thank you for helping me resolve that.

@snowe2010 No problem, glad you got it sorted. I'm not too convinced it was a problem with zsh-nvm tbh, it appears to be this section in nvm that was causing the warning: https://github.com/creationix/nvm/blob/master/nvm.sh#L217-L223

It's possible that previously I was doing something in zsh-nvm that changed some zsh option that was causing nvm to fail in your specific environment but then got rid of that in an update. Although I think that's unlikely, I had a quick look through my commit history and didn't see anything suspect.

Oh well, at least it's working now ¯\_(ツ)_/¯

@lukechilds Yeah I was trying to debug that section over the whole weekend, but nothing seemed to be wrong when just sourcing the nvm.sh file itself, so I think it was just some incompatibility between the two in a previous release!