mirror of
https://github.com/nvm-sh/nvm.git
synced 2026-12-06 22:52:23 +03:00
[Fix] nvm.sh: use NVM_-prefixed locals in the sourcing path
Sourcing nvm.sh fails when the caller has made readonly a variable that nvm.sh declares as a local, such as `readonly VERSION` in a CI build script: bash rejects the `local`, and dash exits. Prefix the locals that sourcing and its `nvm use` reach. The test derives the unprefixed local names from nvm.sh, so a new collision fails it. `nvm_install_default_packages` reported an invalid default-packages file by assigning `nvm`'s `EXIT_CODE` without declaring it. It now declares its own, and `nvm install` uses its return status instead, except 3, which means npm failed to install the packages and, as before, does not fail the install. Fixes #3584 Signed-off-by: NUAAwqy <146524024+NUAAwqy@users.noreply.github.com> Co-authored-by: NUAAwqy <146524024+NUAAwqy@users.noreply.github.com> Co-authored-by: Jordan Harband <ljharb@gmail.com>
This commit is contained in:
1 parent
585b6ec915
commit
a66a5ae5d2
6 files changed
+1017
-783
No files matched your search
@@ -0,0 +1,61 @@
|
||||
#!/bin/sh
|
||||
|
||||
# `nvm install` fails with nvm_install_default_packages' status,
|
||||
# except 3 (npm failing to install the packages), which it only reports.
|
||||
|
||||
TMP_DIR=''
|
||||
cleanup() {
|
||||
case "${TMP_DIR}" in
|
||||
/*) rm -rf "${TMP_DIR}" ;;
|
||||
esac
|
||||
}
|
||||
|
||||
die() { echo "$@" ; cleanup ; exit 1; }
|
||||
|
||||
: nvm.sh
|
||||
\. ../../../nvm.sh
|
||||
|
||||
unset npm_config_prefix NPM_CONFIG_PREFIX
|
||||
|
||||
TMP_DIR="$(mktemp -d)"
|
||||
[ -n "${TMP_DIR}" ] || die 'unable to create a temp dir'
|
||||
NVM_DIR="${TMP_DIR}/nvm"
|
||||
mkdir -p "${NVM_DIR}" || die 'unable to create NVM_DIR'
|
||||
|
||||
version='v20.0.0'
|
||||
version_path="${NVM_DIR}/versions/node/${version}"
|
||||
|
||||
# Stay offline, and keep everything but the default packages inert.
|
||||
nvm_remote_version() { nvm_echo "${version}"; }
|
||||
nvm_has_executable() { return 0; }
|
||||
nvm_binary_available() { return 0; }
|
||||
nvm_install_source() { return 1; }
|
||||
nvm_use_if_needed() { return 0; }
|
||||
nvm_install_npm_if_needed() { return 0; }
|
||||
nvm_ensure_default_set() { return 0; }
|
||||
nvm_install_binary() {
|
||||
mkdir -p "${version_path}/bin"
|
||||
printf '#!/bin/sh\necho "%s"\n' "${version}" > "${version_path}/bin/node"
|
||||
chmod +x "${version_path}/bin/node"
|
||||
}
|
||||
nvm_install_default_packages() { return "${DEFAULT_PACKAGES_STATUS}"; }
|
||||
|
||||
for DEFAULT_PACKAGES_STATUS in 0 1 2 3; do
|
||||
case "${DEFAULT_PACKAGES_STATUS}" in
|
||||
3) EXPECTED=0 ;;
|
||||
*) EXPECTED="${DEFAULT_PACKAGES_STATUS}" ;;
|
||||
esac
|
||||
|
||||
rm -rf "${version_path}"
|
||||
OUTPUT="$(nvm install -b "${version}" 2>&1)" && STATUS=0 || STATUS=$?
|
||||
[ "${STATUS}" = "${EXPECTED}" ] || die "a new install with nvm_install_default_packages returning ${DEFAULT_PACKAGES_STATUS}: expected ${EXPECTED}, got ${STATUS}; output >${OUTPUT}<"
|
||||
|
||||
OUTPUT="$(nvm install -b "${version}" 2>&1)" && STATUS=0 || STATUS=$?
|
||||
case "${OUTPUT}" in
|
||||
*'is already installed'*) ;;
|
||||
*) die "expected the second install to find ${version} already installed; got >${OUTPUT}<" ;;
|
||||
esac
|
||||
[ "${STATUS}" = "${EXPECTED}" ] || die "an existing install with nvm_install_default_packages returning ${DEFAULT_PACKAGES_STATUS}: expected ${EXPECTED}, got ${STATUS}; output >${OUTPUT}<"
|
||||
done
|
||||
|
||||
cleanup
|
||||
@@ -14,7 +14,7 @@ cleanup() {
|
||||
\. ../../common.sh
|
||||
|
||||
nvm_remote_version() {
|
||||
echo "NVM_VERSION_ONLY:${NVM_VERSION_ONLY-},NVM_LTS:${NVM_LTS-},PATTERN:${PATTERN-}"
|
||||
echo "NVM_VERSION_ONLY:${NVM_VERSION_ONLY-},NVM_LTS:${NVM_LTS-},PATTERN:${NVM_PATTERN-}"
|
||||
}
|
||||
|
||||
OUTPUT="$(nvm version-remote foo)"
|
||||
|
||||
@@ -0,0 +1,93 @@
|
||||
#!/bin/sh
|
||||
|
||||
# Regression test for https://github.com/nvm-sh/nvm/issues/3584
|
||||
#
|
||||
# A caller (very often a CI build script) may make a variable readonly, most
|
||||
# notably `readonly VERSION=...` used to pin a version. When nvm.sh declared a
|
||||
# local of the same name, bash rejected the declaration and dash aborted the
|
||||
# script outright, so sourcing failed and the build broke.
|
||||
#
|
||||
# Sourcing also runs `nvm use` for the `default` alias, so a collision can
|
||||
# leave sourcing returning 0 while the default is never activated. The names
|
||||
# are derived from nvm.sh itself, so an unprefixed local added there fails this
|
||||
# test instead of failing a user's shell.
|
||||
|
||||
die () { echo "$@" ; exit 1; }
|
||||
|
||||
NVM_SH_PATH="$(CDPATH= cd -- "$(dirname -- "$0")/../../.." && pwd)/nvm.sh"
|
||||
[ -r "${NVM_SH_PATH}" ] || die "cannot find nvm.sh at ${NVM_SH_PATH}"
|
||||
|
||||
# The shell urchin is running this test in.
|
||||
if [ -n "${BASH_VERSION-}" ]; then
|
||||
NVM_TEST_SHELL=bash
|
||||
elif [ -n "${ZSH_VERSION-}" ]; then
|
||||
NVM_TEST_SHELL=zsh
|
||||
elif [ -n "${KSH_VERSION-}" ]; then
|
||||
NVM_TEST_SHELL=mksh
|
||||
else
|
||||
NVM_TEST_SHELL=sh
|
||||
fi
|
||||
command -v "${NVM_TEST_SHELL}" >/dev/null 2>&1 || die "shell ${NVM_TEST_SHELL} is not available"
|
||||
|
||||
NVM_TEST_DIR="$(mktemp -d)" || die 'could not create a temporary directory'
|
||||
cleanup () {
|
||||
case "${NVM_TEST_DIR-}" in
|
||||
/*) rm -rf "${NVM_TEST_DIR}" ;;
|
||||
esac
|
||||
}
|
||||
trap cleanup 0
|
||||
|
||||
# A fixture with an installed version and a `default` alias, so that sourcing
|
||||
# runs the startup `nvm use` and has something to activate.
|
||||
NVM_FAKE_DIR="${NVM_TEST_DIR}/nvm"
|
||||
mkdir -p "${NVM_FAKE_DIR}/versions/node/v14.21.3/bin" "${NVM_FAKE_DIR}/alias" || die 'fixture setup failed'
|
||||
command printf '#!/bin/sh\necho v14.21.3\n' > "${NVM_FAKE_DIR}/versions/node/v14.21.3/bin/node" || die 'fixture setup failed'
|
||||
chmod +x "${NVM_FAKE_DIR}/versions/node/v14.21.3/bin/node" || die 'fixture setup failed'
|
||||
command printf 'v14.21.3' > "${NVM_FAKE_DIR}/alias/default" || die 'fixture setup failed'
|
||||
# the startup `nvm use --only=alpha,lts,non-lts,iojs` classifies the default
|
||||
# version through this file, so record the shape a real `nvm ls-remote` writes
|
||||
command printf 'v14.21.3 -\n' > "${NVM_FAKE_DIR}/.lts-status" || die 'fixture setup failed'
|
||||
|
||||
# Every unprefixed local name nvm.sh declares. `NVM_`-prefixed names are nvm's
|
||||
# own namespace; IFS, PREFIX and TMPDIR are shell or install contracts that a
|
||||
# caller may reasonably make readonly, and nvm cannot use other names for them.
|
||||
NVM_NAMES="$(command grep -E '^[[:space:]]*local[[:space:]]' "${NVM_SH_PATH}" \
|
||||
| command sed -e 's/^[[:space:]]*local[[:space:]]\{1,\}//' \
|
||||
| command sed -e "s/'[^']*'//g" -e 's/"[^"]*"//g' -e 's/#.*//' \
|
||||
| command tr ' \t' '\n\n' \
|
||||
| command sed -e 's/=.*//' \
|
||||
| command grep -E '^[A-Za-z_][A-Za-z0-9_]*$' \
|
||||
| command grep -v '^NVM_' \
|
||||
| command sort -u \
|
||||
| command grep -vE '^(IFS|PREFIX|TMPDIR)$')"
|
||||
|
||||
[ -n "${NVM_NAMES}" ] || die 'could not derive local names from nvm.sh'
|
||||
|
||||
NVM_EXPECTED='after-source=v14.21.3 use=0 after-use=v14.21.3'
|
||||
|
||||
nvm_source_with_readonly () {
|
||||
(cd "${NVM_TEST_DIR}" && env -i \
|
||||
PATH="${PATH}" HOME="${NVM_TEST_DIR}" NVM_DIR="${NVM_FAKE_DIR}" \
|
||||
"${NVM_TEST_SHELL}" -c "
|
||||
${1}
|
||||
. '${NVM_SH_PATH}' >/dev/null
|
||||
printf 'after-source=%s ' \"\$(nvm current)\"
|
||||
nvm use default >/dev/null 2>&1
|
||||
printf 'use=%s ' \"\$?\"
|
||||
printf 'after-use=%s' \"\$(nvm current)\"
|
||||
" 2>/dev/null) ||:
|
||||
}
|
||||
|
||||
# All names at once, since one shell per name is too slow; on failure, find which.
|
||||
NVM_GOT="$(nvm_source_with_readonly "$(command printf '%s\n' "${NVM_NAMES}" | command sed -e 's/.*/readonly &=caller_value/')")"
|
||||
if [ "${NVM_GOT}" != "${NVM_EXPECTED}" ]; then
|
||||
NVM_FAILING=''
|
||||
while IFS= read -r NVM_NAME; do
|
||||
if [ "$(nvm_source_with_readonly "readonly ${NVM_NAME}=caller_value")" != "${NVM_EXPECTED}" ]; then
|
||||
NVM_FAILING="${NVM_FAILING} ${NVM_NAME}"
|
||||
fi
|
||||
done <<EOF
|
||||
${NVM_NAMES}
|
||||
EOF
|
||||
die "with readonly caller variables, sourcing nvm.sh and \`nvm use default\` did not activate the default version in ${NVM_TEST_SHELL}: got >${NVM_GOT}<; failing names:${NVM_FAILING}"
|
||||
fi
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
#!/bin/sh
|
||||
|
||||
# `nvm_install_default_packages` used to assign `EXIT_CODE` without declaring
|
||||
# it, so it landed in `nvm()`'s `local EXIT_CODE`; once that local was
|
||||
# prefixed, the assignment fell through to the caller's shell and overwrote
|
||||
# their own `EXIT_CODE`.
|
||||
|
||||
die () { echo "$@" ; exit 1; }
|
||||
|
||||
: nvm.sh
|
||||
\. ../../../nvm.sh
|
||||
|
||||
nvm_get_default_packages() {
|
||||
return 0
|
||||
}
|
||||
|
||||
EXIT_CODE=caller_value
|
||||
nvm_install_default_packages >/dev/null 2>&1
|
||||
[ "${EXIT_CODE}" = 'caller_value' ] || die "nvm_install_default_packages set EXIT_CODE in the caller's shell: >${EXIT_CODE}<"
|
||||
@@ -0,0 +1,49 @@
|
||||
#!/bin/sh
|
||||
|
||||
# `nvm install` fails when the default-packages file is invalid,
|
||||
# but only reports npm failing to install the packages:
|
||||
# so a failure from nvm_get_default_packages is returned as-is, and an npm failure is 3.
|
||||
|
||||
die () { echo "$@" ; exit 1; }
|
||||
|
||||
: nvm.sh
|
||||
\. ../../../nvm.sh
|
||||
|
||||
TMP_DIR="$(mktemp -d)" || die 'could not create a temporary directory'
|
||||
cleanup () {
|
||||
case "${TMP_DIR-}" in
|
||||
/*) rm -rf "${TMP_DIR}" ;;
|
||||
esac
|
||||
}
|
||||
trap cleanup 0
|
||||
|
||||
# a real executable, since `xargs` cannot run a shell function
|
||||
NPM_CALLS="${TMP_DIR}/npm-calls"
|
||||
command printf '#!/bin/sh\necho "$*" >> "%s"\nexit "${NPM_STATUS:-0}"\n' "${NPM_CALLS}" > "${TMP_DIR}/npm" || die 'could not create the npm stub'
|
||||
chmod +x "${TMP_DIR}/npm" || die 'could not create the npm stub'
|
||||
PATH="${TMP_DIR}:${PATH}"
|
||||
|
||||
check () {
|
||||
rm -f "${NPM_CALLS}"
|
||||
nvm_install_default_packages >/dev/null 2>&1 && STATUS=0 || STATUS=$?
|
||||
[ "${STATUS}" = "$2" ] || die "$1: expected status $2, got >${STATUS}<"
|
||||
CALLS="$(cat "${NPM_CALLS}" 2>/dev/null)"
|
||||
[ "${CALLS}" = "$3" ] || die "$1: expected npm calls >$3<, got >${CALLS}<"
|
||||
}
|
||||
|
||||
nvm_get_default_packages() { echo 'a b'; }
|
||||
NPM_STATUS=0
|
||||
export NPM_STATUS
|
||||
check 'npm succeeds' 0 'install -g --quiet a b'
|
||||
|
||||
NPM_STATUS=1
|
||||
check 'npm fails' 3 'install -g --quiet a b'
|
||||
|
||||
nvm_get_default_packages() { return 0; }
|
||||
check 'no default packages' 0 ''
|
||||
|
||||
nvm_get_default_packages() { echo 'a b'; return 1; }
|
||||
check 'an invalid default-packages file' 1 ''
|
||||
|
||||
nvm_get_default_packages() { return 2; }
|
||||
check 'an unreadable default-packages file' 2 ''
|
||||
Reference in new issue
Block a user