From a27a8b7da8e1624390d71899d0fe676011135f72 Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Fri, 13 Mar 2026 16:13:41 -0400 Subject: [PATCH] [Fix] `nvm alias`: fix colors not showing by default Colors were lost because `nvm_has_colors` checks `[ -t 1 ]`, which is false inside the `(...) | sort` pipeline in `nvm_list_aliases`. Evaluate `nvm_has_colors` before the pipe and propagate via `NVM_HAS_COLORS`, matching the approach used by `nvm_print_versions`. Bug introduced in https://github.com/nvm-sh/nvm/commit/35212c134652197c779dffd42b9567a270355987. --- nvm.sh | 14 +++++--- ...propagates NVM_HAS_COLORS through pipeline | 27 ++++++++++++++++ ..._alias respects NVM_HAS_COLORS in pipeline | 32 +++++++++++++++++++ 3 files changed, 69 insertions(+), 4 deletions(-) create mode 100755 test/fast/Aliases/nvm_list_aliases propagates NVM_HAS_COLORS through pipeline create mode 100755 test/fast/Aliases/nvm_print_formatted_alias respects NVM_HAS_COLORS in pipeline diff --git a/nvm.sh b/nvm.sh index d2b622b..accae0b 100755 --- a/nvm.sh +++ b/nvm.sh @@ -1153,7 +1153,7 @@ nvm_print_formatted_alias() { fi local ARROW ARROW='->' - if nvm_has_colors; then + if [ "${NVM_HAS_COLORS-}" = 1 ] || nvm_has_colors; then ARROW='\033[0;90m->\033[0m' if [ "_${DEFAULT}" = '_true' ]; then NEWLINE=" \033[${DEFAULT_COLOR}(default)\033[0m\n" @@ -1254,11 +1254,17 @@ nvm_list_aliases() { return $? fi + local NVM_HAS_COLORS + NVM_HAS_COLORS=0 + if nvm_has_colors; then + NVM_HAS_COLORS=1 + fi + nvm_is_zsh && unsetopt local_options nomatch ( local ALIAS_PATH for ALIAS_PATH in "${NVM_ALIAS_DIR}/${ALIAS}"*; do - NVM_NO_COLORS="${NVM_NO_COLORS-}" NVM_CURRENT="${NVM_CURRENT}" nvm_print_alias_path "${NVM_ALIAS_DIR}" "${ALIAS_PATH}" & + NVM_NO_COLORS="${NVM_NO_COLORS-}" NVM_HAS_COLORS="${NVM_HAS_COLORS}" NVM_CURRENT="${NVM_CURRENT}" nvm_print_alias_path "${NVM_ALIAS_DIR}" "${ALIAS_PATH}" & done wait ) | command sort @@ -1269,7 +1275,7 @@ nvm_list_aliases() { { # shellcheck disable=SC2030,SC2031 # (https://github.com/koalaman/shellcheck/issues/2217) if [ ! -f "${NVM_ALIAS_DIR}/${ALIAS_NAME}" ] && { [ -z "${ALIAS}" ] || [ "${ALIAS_NAME}" = "${ALIAS}" ]; }; then - NVM_NO_COLORS="${NVM_NO_COLORS-}" NVM_CURRENT="${NVM_CURRENT}" nvm_print_default_alias "${ALIAS_NAME}" + NVM_NO_COLORS="${NVM_NO_COLORS-}" NVM_HAS_COLORS="${NVM_HAS_COLORS}" NVM_CURRENT="${NVM_CURRENT}" nvm_print_default_alias "${ALIAS_NAME}" fi } & done @@ -1281,7 +1287,7 @@ nvm_list_aliases() { # shellcheck disable=SC2030,SC2031 # (https://github.com/koalaman/shellcheck/issues/2217) for ALIAS_PATH in "${NVM_ALIAS_DIR}/lts/${ALIAS}"*; do { - LTS_ALIAS="$(NVM_NO_COLORS="${NVM_NO_COLORS-}" NVM_LTS=true nvm_print_alias_path "${NVM_ALIAS_DIR}" "${ALIAS_PATH}")" + LTS_ALIAS="$(NVM_NO_COLORS="${NVM_NO_COLORS-}" NVM_HAS_COLORS="${NVM_HAS_COLORS}" NVM_LTS=true nvm_print_alias_path "${NVM_ALIAS_DIR}" "${ALIAS_PATH}")" if [ -n "${LTS_ALIAS}" ]; then nvm_echo "${LTS_ALIAS}" fi diff --git a/test/fast/Aliases/nvm_list_aliases propagates NVM_HAS_COLORS through pipeline b/test/fast/Aliases/nvm_list_aliases propagates NVM_HAS_COLORS through pipeline new file mode 100755 index 0000000..a53f3f0 --- /dev/null +++ b/test/fast/Aliases/nvm_list_aliases propagates NVM_HAS_COLORS through pipeline @@ -0,0 +1,27 @@ +#!/bin/sh + +: nvm.sh +\. ../../../nvm.sh +\. ../../common.sh + +die () { echo "$@" ; exit 1; } + +set -e + +# Force nvm_has_colors to return true so nvm_list_aliases sets NVM_HAS_COLORS=1 +nvm_has_colors() { return 0; } + +nvm_alias_path() { + nvm_echo "../../../alias" +} + +# nvm_list_aliases pipes through `sort`, which makes [ -t 1 ] false. +# Before the fix in 2eb8bbd0, colors were lost inside the pipeline. +# With the fix, NVM_HAS_COLORS is evaluated before the pipe and propagated. +OUTPUT=$(nvm_list_aliases test-stable-1) +COLORED_ARROW="$(command printf %b '\033[0;90m->\033[0m')" + +case "${OUTPUT}" in + *"${COLORED_ARROW}"*) : ;; # pass - colored arrow survived the pipeline + *) die "Expected colored arrow in nvm_list_aliases output through pipeline, got >${OUTPUT}<" ;; +esac diff --git a/test/fast/Aliases/nvm_print_formatted_alias respects NVM_HAS_COLORS in pipeline b/test/fast/Aliases/nvm_print_formatted_alias respects NVM_HAS_COLORS in pipeline new file mode 100755 index 0000000..c2c597b --- /dev/null +++ b/test/fast/Aliases/nvm_print_formatted_alias respects NVM_HAS_COLORS in pipeline @@ -0,0 +1,32 @@ +#!/bin/sh + +: nvm.sh +\. ../../../nvm.sh + +die () { + echo "$@" + exit 1 +} + +set -e + +# Override nvm_has_colors to always return false, simulating a pipeline context +# where [ -t 1 ] is false (the bug condition from 2eb8bbd0) +nvm_has_colors() { return 1; } + +# Without NVM_HAS_COLORS, output should have no color codes (plain arrow) +OUTPUT="$(nvm_print_formatted_alias fakealias fakedest)" +case "${OUTPUT}" in + *'\033['*) die "Expected no color codes without NVM_HAS_COLORS, got >${OUTPUT}<" ;; +esac + +# With NVM_HAS_COLORS=1, output should contain color codes even though +# nvm_has_colors returns false (regression test for the pipeline fix) +NVM_HAS_COLORS=1 +export NVM_HAS_COLORS +OUTPUT="$(nvm_print_formatted_alias fakealias fakedest)" +ARROW="$(command printf %b '\033[0;90m->\033[0m')" +case "${OUTPUT}" in + *"${ARROW}"*) : ;; # pass - colored arrow present + *) die "Expected colored arrow with NVM_HAS_COLORS=1, got >${OUTPUT}<" ;; +esac