Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Add "current" symlink to NVM_DIR #447

Closed
wants to merge 4 commits into from

4 participants

@danielb2

Adresses #430

nvm.sh
@@ -526,6 +526,7 @@ nvm() {
export NODE_PATH
export NVM_PATH="$NVM_DIR/$VERSION/lib/node"
export NVM_BIN="$NVM_DIR/$VERSION/bin"
+ rm -rf "$NVM_DIR/current" && ln -sf "$NVM_DIR/$VERSION" "$NVM_DIR/current"
@ljharb Collaborator
ljharb added a note

If you're using -f on ln, why rm first? Alternatively, if you've just rmd it, why -f?

The -f doesn't actually force a new link (I tried). The -fon both rm and ln make sure there's no noise or warnings if they fail. I can change it to non-silent fail if that's preferred.

@ljharb Collaborator
ljharb added a note

Ah yes, that makes sense. This is fine.

-r is not needed here though, and -f can be left out for ln if you remove the link first

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@ljharb
Collaborator

Could you try to add a test? You should be able to write a fast test, that does something like mkdir v0.0.1 && nvm use 0.0.1 and then verify that the symlink exists, and that it maps to v0.0.1?

@danielb2

Sure

@danielb2

OK, added test. But test seems to fail. Although test seems to fail on main master as well, so not sure what to do about that.

@ljharb
Collaborator

This looks great, but we need the test to fail on master and pass on your branch in order for it to be valid.

@danielb2

That makes no sense. The test passes when I run it locally. A bunch of other tests fail locally however.

@koenpunt

You should unload nvm (nvm unload) or set $NVM_DIR explicitly to the repo directory before running the tests.

@ljharb ljharb closed this in 8f66273
@jokeyrhyme

I have a use case where I install NVM to /opt/nvm, owned by root, and load nvm.sh by regular (non-root) users. Node.JS versions and global NPM packages are installed by root, but regular users can use the executables for their own projects. I went this route to prevent having multiple copies of the same version of Node.JS (and save time having to compile the same version multiple times).

The change in this PR breaks NVM for my regular users, as they are now unable to select a Node.JS version for their current session. Users get the following error:

rm: cannot remove ‘/opt/nvm/current’: Permission denied

Any ideas?

@ljharb
Collaborator

NVM_DIR isn't meant to be shared. I think it would make more sense to give each user their own NVM_DIR, and hard link in (from a shared location) each of the node versions you want.

@danielb2

The failing rm -f shouldn't impact anything except the failure to create the symlink. Are you saying it's halting execution and not changing the version of node?

@jokeyrhyme

Sorry, you're correct in that it isn't breaking anything in NVM. It does cause the script to exit with a non-zero exit code, which causes some of my other scripts to fail (e.g. Vagrant, Ansible, etc).

Node.JS's presence in distribution package managers is now much better than it was when I first resorted to using NVM, so I've switched to that where I can. In the remaining areas (e.g. Node.JS on ARM), I'll transition to per-user NVM_DIR with sym-links to globally installed versions.

@danielb2

This is what I'm seeing. Are you sure this is the cause of the non-zero exit code?

$ nvm use 0.10
rm: /Users/daniel/tmp/nvm/current: Permission denied
Now using node v0.10.29
[~/tmp/nvm]$ echo $?
0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jun 24, 2014
  1. @danielb2
Commits on Jun 27, 2014
  1. @danielb2
Commits on Jul 5, 2014
  1. @danielb2
  2. @danielb2
This page is out of date. Refresh to see the latest.
Showing with 8 additions and 0 deletions.
  1. +1 −0  nvm.sh
  2. +7 −0 test/fast/Running "nvm use" should link current path.
View
1  nvm.sh
@@ -526,6 +526,7 @@ nvm() {
export NODE_PATH
export NVM_PATH="$NVM_DIR/$VERSION/lib/node"
export NVM_BIN="$NVM_DIR/$VERSION/bin"
+ rm -f "$NVM_DIR/current" && ln -s "$NVM_DIR/$VERSION" "$NVM_DIR/current"
echo "Now using node $VERSION"
;;
"run" )
View
7 test/fast/Running "nvm use" should link current path.
@@ -0,0 +1,7 @@
+#!/bin/bash
+
+cd ../..
+. ./nvm.sh
+mkdir v0.10.10
+nvm use 0.10
+[ "$(ls -ld current | cut -d ' ' -f 13,15 && rm -rf current v0.10.10)" = "current $NVM_DIR/v0.10.10" ]
Something went wrong with that request. Please try again.