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

Pre / post activate hooks #1932

Closed
HaleTom opened this issue Oct 28, 2018 · 22 comments
Closed

Pre / post activate hooks #1932

HaleTom opened this issue Oct 28, 2018 · 22 comments
Labels
feature requests I want a new feature in nvm!

Comments

@HaleTom
Copy link

HaleTom commented Oct 28, 2018

  • Operating system and version:

  • nvm debug output:

nvm --version: v0.33.11
$SHELL: /bin/zsh
$SHLVL: 3
$HOME: /home/ravi
$NVM_DIR: '$HOME/.local/share/nvm'
$PATH: $NVM_DIR/versions/node/v6.11.4/bin:$HOME/.gem/ruby/2.3.3/bin:$HOME/.gem/ruby/2.3.3/bin:$HOME/.gem/ruby/2.3.3/bin:$HOME/bin:$HOME/.local/share/miniconda3/bin:$HOME/.local/share/virtualenvs/py3/bin:$HOME/.local/share/npm/bin:$HOME/.local/share/go/bin:$HOME/.gem/ruby/2.3.3/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/lib/jvm/default/bin:/usr/bin/site_perl:/usr/bin/vendor_perl:/usr/bin/core_perl:/usr/lib/systemd:$HOME/.cabal/bin:/usr/lib/jvm/default/bin:/usr/bin/site_perl:/usr/bin/vendor_perl:/usr/bin/core_perl
$PREFIX: ''
$NPM_CONFIG_PREFIX: ''
$NVM_NODEJS_ORG_MIRROR: ''
$NVM_IOJS_ORG_MIRROR: ''
shell version: 'zsh 5.6.2 (x86_64-pc-linux-gnu)'
uname -a: 'Linux 4.16.18-1-MANJARO #1 SMP PREEMPT Tue Jun 26 15:27:59 UTC 2018 x86_64 GNU/Linux'
OS version: Manjaro Linux   () ()
curl: /usr/bin/curl, curl 7.61.1 (x86_64-pc-linux-gnu) libcurl/7.61.1 OpenSSL/1.1.1 zlib/1.2.11 libidn2/2.0.5 libpsl/0.20.2 (+libidn2/2.0.4) libssh2/1.8.0 nghttp2/1.33.0
wget: /usr/bin/wget, GNU Wget 1.19.5 built on linux-gnu.
git: git: aliased to exec_scmb_expand_args hub (exec_scmb_expand_args hub), git version 2.19.1
grep: /home/ravi/bin/grep, grep (GNU grep) 3.1
awk: /usr/bin/awk, GNU Awk 4.2.1, API: 2.0 (GNU MPFR 4.0.1, GNU MP 6.1.2)
sed: /usr/bin/sed, sed (GNU sed) 4.5
cut: /usr/bin/cut, cut (GNU coreutils) 8.30
basename: /usr/bin/basename, basename (GNU coreutils) 8.30
rm: rm: aliased to rm -I (rm -I), rm (GNU coreutils) 8.30
mkdir: /usr/bin/mkdir, mkdir (GNU coreutils) 8.30
xargs: /usr/bin/xargs, xargs (GNU findutils) 4.6.0
nvm current: v6.11.4
which node: $NVM_DIR/versions/node/v6.11.4/bin/node
which iojs: iojs not found
which npm: /usr/bin/npm
npm config get prefix: $NVM_DIR/versions/node/v6.11.4
npm root -g: $NVM_DIR/versions/node/v6.11.4/lib/node_modules
  • nvm ls output:
->      v6.11.4
         system
default -> lts/* (-> v6.11.4)
node -> stable (-> v6.11.4) (default)
stable -> 6.11 (-> v6.11.4) (default)
iojs -> N/A (default)
lts/* -> lts/boron (-> v6.11.4)
lts/argon -> v4.8.4 (-> N/A)
lts/boron -> v6.11.4
  • How did you install nvm? (e.g. install script in readme, Homebrew):

curl

Feature request: Pre / post activate hooks

Can the pre- and post- activation scripts be called with the names of the previous and next environment?:

Eg: when sourcing nvm from .bashrc, the following could be called

$NVM_DIR/pre-hook "" "system"
... system is added to path, etc...
$NVM_DIR/post-hook "" system"

(note the "previous" version is "" as there was none)

If then switching from system to v6.11.4 via nvm use v6.11.4:
$NVM_DIR/pre-hook "system" "v6.11.4"
$NVM_DIR/post-hook "system" "v6.11.4"

Note: both conda and virtualenvwrapper support hook scripts.

@HaleTom
Copy link
Author

HaleTom commented Oct 28, 2018

For now, a work-around is:

# Copy / rename a function
# https://stackoverflow.com/a/18839557/5353461
func_copy() {
  test -n "$(declare -f "$1")" || return 
  eval "${_/$1/$2}"
}
func_rename() {
  func_copy "$@" || return
  unset -f "$1"
}

# Wrap nvm function
func_rename nvm nvm_wrapped
nvm() { pre-hook; nvm_wrapped "$@"; post-hook; }

However this doesn't cover the case of detecting environment changes, which is an important missing.

@ljharb
Copy link
Member

ljharb commented Oct 28, 2018

What’s the use case? Do you want hooks for any invocation of nvm, or only a use command?

@ljharb ljharb added the feature requests I want a new feature in nvm! label Oct 28, 2018
@HaleTom
Copy link
Author

HaleTom commented Oct 29, 2018

Updating my prompt is my use case, so changing environments would be sufficient for me.

My workaround doesn't detect environment changes but naively runs the prompt update every time nvm is run. (I only utilise the post-hook myself)

@ljharb
Copy link
Member

ljharb commented Oct 29, 2018

I'm confused; i have my prompt set up to call nvm current on every line, and it works fine with no perf issues. Why do you need to hook into the change itself?

@HaleTom
Copy link
Author

HaleTom commented Oct 29, 2018

The performance figures I get are as follows:

$ time nvm_wrapped current
system

real    0m0.188s
user    0m0.149s
sys     0m0.042s
 $

Delaying a prompt by almost 0.2 seconds every time doesn't work, especially if its cumulative with conda, virtualenvwrapper, chruby, etc.

The best way I've seen it done in other environment managers is where they set an environment variable denoting the version currently activated.

I work around nvms lack of current environment variable by using the following in my prompt:

:$ time [[ $NVM_BIN =~ ([^/]+)/bin$ ]] && echo "${BASH_REMATCH[2]}" || echo "system"

real    0m0.009s
user    0m0.002s
sys     0m0.007s
system

Using a variable makes the prompt delay possible far more palatable: 0.009 vs 0.188 on an idle system.

If nvm set the environment variable, it would be even better, as it wouldn't have to be calculated each time the prompt is displayed.

@waldyrious
Copy link
Contributor

I was looking for such a feature, but my use case was to be able to specify commands to be run after each install or use command. For example, npm update -g to make sure I always have the latest versions of my globally installed packages. The --latest-npm flag does this for npm, but I'd like to do that for all my global packages.

@ljharb
Copy link
Member

ljharb commented Jan 11, 2019

@waldyrious given that having pretty much anything installed globally is an antipattern, i'd love to hear more about your use case - what global packages do you have that need frequent updating?

@waldyrious
Copy link
Contributor

waldyrious commented Jan 13, 2019

@ljharb in my case I use yarn instead of npm, so the use case is pretty similar to what motivated --latest-npm. But I assume there are other similarly "meta" packages that would make sense to keep installed globally, and always up to date unless when explicitly required by some package's dependencies.

@ljharb
Copy link
Member

ljharb commented Jan 14, 2019

@waldyrious
Copy link
Contributor

Yes, but IIUC that only works when installing, not when useing, right?

@ljharb
Copy link
Member

ljharb commented Jan 14, 2019

Sure, but install also uses, so you can run nvm install X at all times rather than nvm use X.

@waldyrious
Copy link
Contributor

Sure, that can work :)

@ljharb
Copy link
Member

ljharb commented Feb 25, 2019

@waldyrious ok so, with nvm install X instead of nvm use X, and with default-packages, do you still have a use case for install hooks?

@waldyrious
Copy link
Contributor

@ljharb semantically, I think it would make more sense to run nvm use rather than nvm install; Always using the latter does work, but feels like a workaround to what would be the logical (IMHO) behavior.

That said, I wouldn't object if you prefer ruling out implementing the same behavior for nvm use.

@ljharb
Copy link
Member

ljharb commented Feb 25, 2019

Which, the default packages behavior?

If so it'd have to be with a flag, like nvm use --install-default-packages X, which might be too verbose to be worth it.

@waldyrious
Copy link
Contributor

I was thinking more of nvm use installing the packages by default, just like nvm install does. That said, I feel like I probably haven't yet understood the full rationale for a separate use and install command. In my naive view, I'd expect there to be only nvm use, which would automatically install the node versions that aren't yet locally available (and do the same to the default packages, if specified).

But I'm sure there are stronger justifications for the separation. Again, resorting to running nvm install every time works for me, so I if you think it is not worth implementing a way to tell nvm use to install the default packages, I'd be fine with that.

@ljharb
Copy link
Member

ljharb commented Feb 26, 2019

nvm use won't ever touch the network, nor create any files, so it can work on a completely full disk, or on a disk where there's insufficient permissions to create files, or offline. nvm install is an explicit action to do both of those.

@ljharb
Copy link
Member

ljharb commented Feb 26, 2019

OK, it seems like this is addressed then :-) if there's new use cases, I'll be happy to reopen :-)

@ljharb ljharb closed this as completed Feb 26, 2019
@HaleTom
Copy link
Author

HaleTom commented Nov 5, 2020

I'm revisiting this issue as I'm developing JavaScript again.

Approx 0.2 seconds per prompt is quite slow.

If I press enter a bunch of times, I have 'gaps' where the enter is echoed to the screen faster than the prompt can be redrawn. And it's not just nvm's 0.2 seconds per prompt, it's a cumulative experience for all the other elements that go into a prompt also. If conda, chruby etc all take 0.2 sec, then the delay after pressing 'enter' is very palpable.

Perhaps as a work-around, nvm could set a shell variable to cache the current version, so that it doesn't get queried at a 0.2 second cost whenever that information is needed?

@ljharb
Copy link
Member

ljharb commented Nov 5, 2020

@HaleTom if your prompt is doing something slow, that’s not part of nvm - nvm doesn’t touch your prompt, and is only ever slow on a “use” command (which #2317 should address)

@HaleTom
Copy link
Author

HaleTom commented Nov 7, 2020

Sadly performance improvements to nvm use won't help.

My prompt is slow because nvm current is slow, which is the very command you recommended using in every prompt. 😕

@ljharb
Copy link
Member

ljharb commented Nov 7, 2020

@HaleTom you're the first person to ever report that; could you please file a new issue about it and fill out the full template? I’d love to resolve it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature requests I want a new feature in nvm!
Projects
None yet
Development

No branches or pull requests

3 participants