Skip to content
Myr

[New] `nvm install`: add `NVM_NO_SOURCE_FALLBACK` to disable the source fallback

Before adoption: this commit came in with the history the project adopted, so no landing records it.

By default a failed binary download falls back to compiling node from source, which is slow and pointless on platforms that always have prebuilt binaries. `-b` already disables the fallback per-invocation; `NVM_NO_SOURCE_FALLBACK=1` makes it the default for every `nvm install`, so CI images and dev setups need not thread `-b` through every callsite. It behaves exactly like `-b` - aborting with a non-zero exit on a failed binary download - and is mutually exclusive with `-s`, for the same reason `-b` is.

authored by Jordan Harband · committed by Jordan Harband · parent aee1f83 · Browse files · 3 files+66 added−0 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+66 added−0 removed

  • tests:+50 added−0 removed
  • other:+16 added−0 removed

3 files+66 added−0 removed

added:test/fast/Unit tests/nvm install NVM_NO_SOURCE_FALLBACK+50 added
1#!/bin/sh23cleanup() {4 [ -n "${NVM_DIR}" ] && [ -d "${NVM_DIR}" ] && rm -rf "${NVM_DIR}"5 unset -f die cleanup nvm_remote_version nvm_has_executable nvm_binary_available \6 nvm_install_binary nvm_install_source nvm_get_make_jobs7 unset NVM_DIR version SOURCE_MARKER OUTPUT EXIT_CODE8}910die() { echo "$@" ; cleanup ; exit 1; }1112: nvm.sh13\. ../../../nvm.sh1415unset npm_config_prefix NPM_CONFIG_PREFIX1617NVM_DIR="$(mktemp -d)"18[ -n "${NVM_DIR}" ] || die 'unable to create temp NVM_DIR'19version='v20.0.0'20SOURCE_MARKER="${NVM_DIR}/source-was-attempted"2122# Stay offline; make the binary download always fail and record any attempt to23# compile from source.24nvm_remote_version() { nvm_echo "${version}"; }25nvm_has_executable() { return 0; }26nvm_binary_available() { return 0; }27nvm_get_make_jobs() { NVM_MAKE_JOBS=1; }28nvm_install_binary() { return 1; }29nvm_install_source() { command touch "${SOURCE_MARKER}"; return 1; }3031# Default: a failed binary download falls back to a source compile.32rm -f "${SOURCE_MARKER}"33nvm install "${version}" > /dev/null 2>&134[ -f "${SOURCE_MARKER}" ] || die 'default behaviour should fall back to source when the binary fails'3536# NVM_NO_SOURCE_FALLBACK=1: a failed binary download must NOT compile from source.37rm -f "${SOURCE_MARKER}"38NVM_NO_SOURCE_FALLBACK=1 nvm install "${version}" > /dev/null 2>&1; EXIT_CODE=$?39[ ! -f "${SOURCE_MARKER}" ] || die 'NVM_NO_SOURCE_FALLBACK=1 should not fall back to source'40[ "${EXIT_CODE}" != "0" ] || die 'a failed binary install with NVM_NO_SOURCE_FALLBACK=1 should be non-zero'4142# NVM_NO_SOURCE_FALLBACK=1 combined with -s would skip both methods: a conflict.43OUTPUT="$(NVM_NO_SOURCE_FALLBACK=1 nvm install -s "${version}" 2>&1)"; EXIT_CODE=$?44[ "${EXIT_CODE}" = "6" ] || die "expected exit code 6 for -s with NVM_NO_SOURCE_FALLBACK=1, got ${EXIT_CODE}"45case "${OUTPUT}" in46 *'skip install from both binary and source'*) ;;47 *) die "expected a conflict message, got >${OUTPUT}<" ;;48esac4950cleanup
modified:nvm.sh+12 added
3360 unmodified lines
3361 nvm_echo ' nvm install [<version>] Download and install a <version>. Uses .nvmrc if version is omitted; otherwise errors.'3362 nvm_echo ' The following optional arguments, if provided, must appear directly after `nvm install`:'3363 nvm_echo ' -s Skip binary download, install from source only.'3364 nvm_echo ' -b Skip source download, install from binary only.'3365 nvm_echo ' (set NVM_NO_SOURCE_FALLBACK=1 to make this the default for every install)'3366 nvm_echo ' --reinstall-packages-from=<version> When installing, reinstall packages installed in <node|iojs|node version number>'3367 nvm_echo ' --lts When installing, only select from LTS (long-term support) versions'3368 nvm_echo ' --lts=<LTS name> When installing, only select from versions for a specific LTS line'3369 nvm_echo ' --skip-default-packages When installing, skip the default-packages file if it exists'
340 unmodified lines
3710 ;;3711 esac3712 done37133714 # NVM_NO_SOURCE_FALLBACK=1 makes `nvm install` behave as if `-b` were always passed:3715 # a failed binary download aborts instead of silently falling back to a from-source compile.3716 # It is a persistent policy so callers need not thread `-b` through every invocation.3717 if [ "${NVM_NO_SOURCE_FALLBACK-}" = '1' ] && [ $nosource -ne 1 ]; then3718 if [ $nobinary -eq 1 ]; then3719 nvm_err '-s cannot be combined with NVM_NO_SOURCE_FALLBACK=1 since that would skip install from both binary and source'3720 return 63721 fi3722 nosource=13723 fi37243725 if [ "${NVM_OFFLINE}" != 1 ] && ! nvm_has_executable "curl" && ! nvm_has_executable "wget"; then3726 nvm_err 'nvm needs curl or wget to proceed.'3727 return 13728 fi
1312 unmodified lines
modified:README.md+4 added
870 unmodified lines
871- `NVM_RC_VERSION` - version from .nvmrc file if being used.872873Additionally, nvm modifies `PATH`, and, if present, `MANPATH` and `NODE_PATH` when changing versions.874875The following environment variables can be set to configure `nvm install`:876877- `NVM_NO_SOURCE_FALLBACK` - when `1`, a failed binary download aborts instead of silently falling back to a (much slower) from-source compile; the persistent equivalent of the `-b` flag, and mutually exclusive with `-s`.878879880## Bash Completion881882To activate, you need to source `bash_completion`:883
329 unmodified lines