nvm-sh / nvm

Node Version Manager - POSIX-compliant bash script to manage multiple active node.js versions
MIT License
79.08k stars 7.92k forks source link

`nvm ls` outputs color codes when piped to another command #2497

Open unikitty37 opened 3 years ago

unikitty37 commented 3 years ago

Operating system and version: macOS 10.14.6 (18G8022)

nvm debug output:

```sh nvm --version: v0.35.0 $TERM_PROGRAM: iTerm.app $SHELL: /usr/local/bin/zsh $SHLVL: 1 ${HOME}: /Users/user ${NVM_DIR}: '${HOME}/.nvm' ${PATH}: ${HOME}/.nodenv/shims:${HOME}/.rbenv/shims:${HOME}/.rbenv/bin:${NVM_DIR}/versions/node/v16.0.0/bin:${HOME}/.rbenv/shims:${HOME}/bin:/usr/local/git/bin:/opt/local/bin:/opt/local/sbin:${HOME}/go/bin:/usr/local/sbin:/usr/local/bin:/usr/local/opt/gnu-sed/libexec/gnubin:/usr/local/opt/sqlite/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Applications/VMware Fusion.app/Contents/Public:/usr/local/mysql/bin:${HOME}/.composer/vendor/bin:/usr/local/opt/go/libexec/bin $PREFIX: '' ${NPM_CONFIG_PREFIX}: '' $NVM_NODEJS_ORG_MIRROR: '' $NVM_IOJS_ORG_MIRROR: '' shell version: 'zsh 5.8 (x86_64-apple-darwin18.7.0)' uname -a: 'Darwin 18.7.0 Darwin Kernel Version 18.7.0: Tue Jan 12 22:04:47 PST 2021; root:xnu-4903.278.56~1/RELEASE_X86_64 x86_64' OS version: Mac 10.14.6 18G8022 curl: /usr/bin/curl, curl 7.54.0 (x86_64-apple-darwin18.0) libcurl/7.54.0 LibreSSL/2.6.5 zlib/1.2.11 nghttp2/1.24.1 wget: not found sed: /usr/local/opt/gnu-sed/libexec/gnubin/sed cut: /usr/bin/cut basename: /usr/bin/basename rm: /bin/rm mkdir: /bin/mkdir xargs: /usr/bin/xargs git: /usr/local/bin/git, git version 2.30.2 ls: grep:: No such file or directory grep: grep: aliased to grep --color=auto --exclude-dir={.bzr,CVS,.git,.hg,.svn,.idea,.tox} (grep --color=auto --exclude-dir={.bzr,CVS,.git,.hg,.svn,.idea,.tox}), grep (BSD grep) 2.5.1-FreeBSD awk: /usr/bin/awk, awk version 20070501 nvm current: system which node: ${HOME}/.nodenv/shims/node which iojs: iojs not found which npm: ${HOME}/.nodenv/shims/npm npm config get prefix: ${HOME}/.nodenv/versions/14.9.0 npm root -g: ${HOME}/.nodenv/versions/14.9.0/lib/node_modules ```

nvm ls output:

```sh  v16.0.0 -> system default -> v16.0.0 node -> stable (-> v16.0.0) (default) stable -> 16.0 (-> v16.0.0) (default) iojs -> N/A (default) unstable -> N/A (default) lts/* -> lts/fermium (-> N/A) lts/argon -> v4.9.1 (-> N/A) lts/boron -> v6.17.1 (-> N/A) lts/carbon -> v8.17.0 (-> N/A) lts/dubnium -> v10.24.1 (-> N/A) lts/erbium -> v12.22.1 (-> N/A) lts/fermium -> v14.16.1 (-> N/A) ```

How did you install nvm?

Homebrew

What steps did you perform?

What happened?

All the ANSI codes were copied to the clipboard, as you can see from the section above.

What did you expect to happen?

nvm ls should omit ANSI colour codes and outputs plain text when being piped to another command. This is in line with how other colour-using commands work, so would be generally expected behaviour. Having to add --no-colors each time is a workaround, but it does not mirror the behaviour of other commands, which do the right thing automatically.

Perhaps the default behaviour could be to use colours when outputting to a TTY, and no colours when not outputting to a TTY, and adding a new --colors flag which could be used to force the issue when colours are required in a pipe?

Is there anything in any of your profile files that modifies the PATH?

Yes; they all execute after eval "$(nodenv init -)", which is at the top of the file.

ljharb commented 3 years ago

nvm is not supported when installed from homebrew (altho this issue seems unrelated); please brew uninstall it and reinstall it from the only supported install method (the install script in the readme).

I'm not sure why nodenv init would matter, since this is nvm, not nodenv? Similarly, I'm not sure why there's nodenv in your path anywhere if you're using nvm. Can you elaborate?


I can reproduce this issue, and I completely agree that when it's in a pipe, it shouldn't output colors. I don't think "TTY-ness" should dictate that, since it's not about interactivity (but your suggestion would certainly work, albeit with a lot of added complexity in nvm).

Using some form of this technique should work here - when I naively use it in nvm_has_colors, the "ls" part of the nvm ls output works properly, but in an actual terminal it causes the "alias" part of the nvm ls output to drop its colorization, so some finesse might be needed. tagging in @gitburd and @naomiquinones for help.

unikitty37 commented 3 years ago

Thanks — I'm not 100% sure why nodenv was installed; it must have been left over from some time ago. For some reason zsh initialisation was working with it at the end of the file right up until I installed nvm, but removing nodenv entirely fixed it, and seems to be the more sensible option :)

heinthanth commented 2 years ago

I've cover help, ls, ls-remote and alias. Let me know if some issue with this PR.

ljharb commented 1 month ago

Unfortunately the PR's fork was deleted, so this issue still needs a PR.

ljharb commented 1 month ago

I've been exploring a fix to this. It's of course a breaking change, which is fine. It's also a highly disruptive one, however.

Currently you can do nvm ls --no-colors | whatever or TERM=dumb nvm ls | whatever and skip the colors.

With this change, you'd need do do nvm ls --colors | whatever.

I think this change would be much less disruptive if it was confined to pipes - but "$(nvm ls)" seems to be indistinguishable from nvm ls | echo from nvm ls's POV.