[Robustness] handle an overridden type

A `type` function or alias defined before nvm is sourced
hijacked `nvm_has` and `nvm_command_info`.
`nvm_has` now uses `command -v`;
`nvm_command_info` needs `type`'s output,
so it uses `builtin type` in zsh,
whose `command type` runs an external `type` that can't see shell functions.

Since `command -v` makes dash hash the command,
`nvm_command_info` also parses dash's and ksh's "tracked alias" output.

Fixes #2352.
This commit is contained in:
Jordan Harband committed 2020-11-21 13:33:12 -08:00
1 parent a858cc3e2f
commit 3bf6686090
2 files changed
+50 -11

No files matched your search

+24 -11
View File
@@ -69,7 +69,18 @@ nvm_grep() {
}
nvm_has() {
type "${1-}" >/dev/null 2>&1
command -v "${1-}" >/dev/null 2>&1
}
# `type`, ignoring any `type` function or alias
nvm_type() {
if nvm_is_zsh; then
# zsh's `command type` runs an external `type`, which cannot see shell functions or aliases
builtin type "$@"
else
# ksh's `command` alias would expand a `type` alias after it
\command type "$@"
fi
}
# resolves like `command "${1}"` does: only executables on the PATH,
@@ -97,18 +108,20 @@ nvm_command_info() {
local COMMAND
local INFO
COMMAND="${1}"
if type "${COMMAND}" | nvm_grep -q hashed; then
INFO="$(type "${COMMAND}" | command sed -E 's/\(|\)//g' | command awk '{print $4}')"
elif type "${COMMAND}" | nvm_grep -q aliased; then
if nvm_type "${COMMAND}" | nvm_grep -q hashed; then
INFO="$(nvm_type "${COMMAND}" | command sed -E 's/\(|\)//g' | command awk '{print $4}')"
elif nvm_type "${COMMAND}" | nvm_grep -q aliased; then
# shellcheck disable=SC2230
INFO="$(which "${COMMAND}") ($(type "${COMMAND}" | command awk '{ $1=$2=$3=$4="" ;print }' | command sed -e 's/^\ *//g' -Ee "s/\`|'//g"))"
elif type "${COMMAND}" | nvm_grep -q "^${COMMAND} is an alias for"; then
INFO="$(which "${COMMAND}") ($(nvm_type "${COMMAND}" | command awk '{ $1=$2=$3=$4="" ;print }' | command sed -e 's/^\ *//g' -Ee "s/\`|'//g"))"
elif nvm_type "${COMMAND}" | nvm_grep -q "^${COMMAND} is an alias for"; then
# shellcheck disable=SC2230
INFO="$(which "${COMMAND}") ($(type "${COMMAND}" | command awk '{ $1=$2=$3=$4=$5="" ;print }' | command sed 's/^\ *//g'))"
elif type "${COMMAND}" | nvm_grep -q "^${COMMAND} is /"; then
INFO="$(type "${COMMAND}" | command awk '{print $3}')"
INFO="$(which "${COMMAND}") ($(nvm_type "${COMMAND}" | command awk '{ $1=$2=$3=$4=$5="" ;print }' | command sed 's/^\ *//g'))"
elif nvm_type "${COMMAND}" | nvm_grep -q "^${COMMAND} is a tracked alias for /"; then
INFO="$(nvm_type "${COMMAND}" | command awk '{print $NF}')"
elif nvm_type "${COMMAND}" | nvm_grep -q "^${COMMAND} is /"; then
INFO="$(nvm_type "${COMMAND}" | command awk '{print $3}')"
else
INFO="$(type "${COMMAND}")"
INFO="$(nvm_type "${COMMAND}")"
fi
nvm_echo "${INFO}"
}
@@ -7179,7 +7192,7 @@ nvm() {
nvm_print_default_alias nvm_print_formatted_alias nvm_resolve_local_alias \
nvm_replace_literal nvm_sanitize_path nvm_sanitize_url nvm_has_colors nvm_has_italics nvm_process_parameters \
nvm_node_version_has_solaris_binary nvm_iojs_version_has_solaris_binary \
nvm_curl_libz_support nvm_command_info nvm_is_zsh nvm_stdout_is_terminal \
nvm_curl_libz_support nvm_command_info nvm_type nvm_is_zsh nvm_stdout_is_terminal \
nvm_npmrc_bad_news_bears nvm_sanitize_auth_header nvm_auth_header_applies_to \
nvm_wget_is_gnu nvm_url_origin nvm_wget_same_origin nvm_wget_with_auth_header \
nvm_url_has_credentials nvm_wget_with_url_credentials \
+26
View File
@@ -0,0 +1,26 @@
#!/bin/sh
die () { echo "$@" ; exit 1; }
# defined before nvm.sh is sourced, as in a user's profile (#2352)
type() { echo 'hijacked'; return 0; }
if [ -n "${BASH_VERSION-}" ]; then
shopt -s expand_aliases
elif [ -n "${ZSH_VERSION-}" ]; then
setopt aliases
fi
alias type="echo hijacked"
: nvm.sh
\. ../../../nvm.sh
nvm_has cat || die 'nvm_has does not locate "cat" when `type` is overridden'
! nvm_has foobarbaz || die 'nvm_has finds "foobarbaz" when `type` is overridden'
[ "~$(nvm_has cat 2>&1)" = "~" ] || die 'nvm_has has output when `type` is overridden'
INFO="$(nvm_command_info cat)"
EXPECTED="$(command -v cat)"
[ "${INFO}" = "${EXPECTED}" ] || die "nvm_command_info cat with an overridden \`type\`: expected >${EXPECTED}<, got >${INFO}<"