Skip to content

Commit 73a513c

Browse files
committed
[Fix] allow nvm unalias x when x is a default alias, but shadowed
Fixes #2122.
1 parent 04ad1b5 commit 73a513c

File tree

2 files changed

+34
-7
lines changed

2 files changed

+34
-7
lines changed

nvm.sh

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3372,14 +3372,24 @@ nvm() {
33723372
local NVM_NODE_PREFIX
33733373
NVM_IOJS_PREFIX="$(nvm_iojs_prefix)"
33743374
NVM_NODE_PREFIX="$(nvm_node_prefix)"
3375-
case "$1" in
3376-
"stable" | "unstable" | "${NVM_IOJS_PREFIX}" | "${NVM_NODE_PREFIX}" | "system")
3377-
nvm_err "${1-} is a default (built-in) alias and cannot be deleted."
3378-
return 1
3379-
;;
3380-
esac
3375+
local NVM_ALIAS_EXISTS
3376+
NVM_ALIAS_EXISTS=0
3377+
if [ -f "${NVM_ALIAS_DIR}/${1-}" ]; then
3378+
NVM_ALIAS_EXISTS=1
3379+
fi
3380+
3381+
if [ $NVM_ALIAS_EXISTS -eq 0 ]; then
3382+
case "$1" in
3383+
"stable" | "unstable" | "${NVM_IOJS_PREFIX}" | "${NVM_NODE_PREFIX}" | "system")
3384+
nvm_err "${1-} is a default (built-in) alias and cannot be deleted."
3385+
return 1
3386+
;;
3387+
esac
3388+
3389+
nvm_err "Alias ${1-} doesn't exist!"
3390+
return
3391+
fi
33813392

3382-
[ ! -f "${NVM_ALIAS_DIR}/${1-}" ] && nvm_err "Alias ${1-} doesn't exist!" && return
33833393
local NVM_ALIAS_ORIGINAL
33843394
NVM_ALIAS_ORIGINAL="$(nvm_alias "${1}")"
33853395
command rm -f "${NVM_ALIAS_DIR}/${1}"
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/bin/sh
2+
3+
\. ../../../nvm.sh
4+
5+
die () { echo "$@" ; exit 1; }
6+
7+
OUTPUT="$(nvm unalias node 2>&1)"
8+
EXPECTED_OUTPUT="node is a default (built-in) alias and cannot be deleted."
9+
[ "$OUTPUT" = "$EXPECTED_OUTPUT" ] || die "trying to remove a built-in alias should fail with '$EXPECTED_OUTPUT', got '$OUTPUT'"
10+
11+
nvm alias node stable || die '`nvm alias node stable` failed'
12+
13+
nvm unalias node || die '`nvm unalias node` failed'
14+
15+
OUTPUT="$(nvm unalias node 2>&1)"
16+
EXPECTED_OUTPUT="node is a default (built-in) alias and cannot be deleted."
17+
[ "$OUTPUT" = "$EXPECTED_OUTPUT" ] || die "trying to remove a built-in alias should fail with '$EXPECTED_OUTPUT', got '$OUTPUT'"

0 commit comments

Comments
 (0)