Nvm: NVM doesn't support closed file handles for STDOUT/STDERR

Created on 21 Apr 2019  Â·  7Comments  Â·  Source: nvm-sh/nvm

  • Operating system and version: macOS High Sierra 10.13.6 (17G6030)

  • nvm debug output:

nvm --version: v0.34.0
$TERM_PROGRAM: iTerm.app
$SHELL: /bin/bash
$SHLVL: 1
$HOME: /Users/jcayzac
$NVM_DIR: '$HOME/.nvm'
$PATH: $HOME/.sdkman/candidates/kotlin/current/bin:$HOME/.sdkman/candidates/gradle/current/bin:$NVM_DIR/versions/node/v10.15.3/bin:$HOME/.rbenv/shims:$HOME/.yarn/bin:$HOME/.prefix/bin:$HOME/.go/bin:/usr/local/opt/go/libexec/bin:$HOME/Library/Android/sdk/tools:$HOME/Library/Android/sdk/tools/bin:$HOME/Library/Android/sdk/platform-tools:/usr/local/share/dotnet:/Library/Frameworks/Mono.framework/Versions/Current/bin:/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin
$PREFIX: ''
$NPM_CONFIG_PREFIX: ''
$NVM_NODEJS_ORG_MIRROR: ''
$NVM_IOJS_ORG_MIRROR: ''
shell version: 'GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin17)'
uname -a: 'Darwin 17.7.0 Darwin Kernel Version 17.7.0: Wed Feb 27 00:43:23 PST 2019; root:xnu-4570.71.35~1/RELEASE_X86_64 x86_64'
OS version: Mac 10.13.6 17G6030
curl: /usr/bin/curl, curl 7.54.0 (x86_64-apple-darwin17.0) libcurl/7.54.0 LibreSSL/2.0.20 zlib/1.2.11 nghttp2/1.24.0
wget: /usr/local/bin/wget, GNU Wget 1.20.3 built on darwin17.7.0.
git: /usr/local/bin/git, git version 2.21.0
grep: /usr/bin/grep (grep --color), grep (BSD grep) 2.5.1-FreeBSD
awk: /usr/local/bin/awk, GNU Awk 5.0.0, API: 2.0 (GNU MPFR 4.0.2, GNU MP 6.1.2)
sed: illegal option -- -
usage: sed script [-Ealn] [-i extension] [file ...]
       sed [-Ealn] [-i extension] [-e script] ... [-f script_file] ... [file ...]
sed: /usr/bin/sed, 
cut: illegal option -- -
usage: cut -b list [-n] [file ...]
       cut -c list [file ...]
       cut -f list [-s] [-d delim] [file ...]
cut: /usr/bin/cut, 
bash: basename: --: invalid option
basename: usage: basename string [suffix]
basename: basename is a shell builtin, 
rm: illegal option -- -
usage: rm [-f | -i] [-dPRrvW] file ...
       unlink file
rm: /bin/rm, 
mkdir: illegal option -- -
usage: mkdir [-pv] [-m mode] directory ...
mkdir: /bin/mkdir, 
xargs: illegal option -- -
usage: xargs [-0opt] [-E eofstr] [-I replstr [-R replacements]] [-J replstr]
             [-L number] [-n number [-x]] [-P maxprocs] [-s size]
             [utility [argument ...]]
xargs: /usr/bin/xargs, 
nvm current: v10.15.3
which node: $NVM_DIR/versions/node/v10.15.3/bin/node
which iojs: 
which npm: $NVM_DIR/versions/node/v10.15.3/bin/npm
npm config get prefix: $NVM_DIR/versions/node/v10.15.3
npm root -g: $NVM_DIR/versions/node/v10.15.3/lib/node_modules

  • nvm ls output:
        v10.9.0
->     v10.15.3
         system
default -> lts/* (-> v10.15.3)
node -> stable (-> v10.15.3) (default)
stable -> 10.15 (-> v10.15.3) (default)
iojs -> N/A (default)
unstable -> N/A (default)
lts/* -> lts/dubnium (-> v10.15.3)
lts/argon -> v4.9.1 (-> N/A)
lts/boron -> v6.17.1 (-> N/A)
lts/carbon -> v8.16.0 (-> N/A)
lts/dubnium -> v10.15.3

  • How did you install nvm? (e.g. install script in readme, Homebrew):
    curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.34.0/install.sh | bash

  • What steps did you perform?
    nvm use --lts >&- ; echo $?

  • What happened?
    1

  • What did you expect to happen?
    0, like when using the longer nvm use --lts >/dev/null ; echo $?.

Mac OS bugs needs followup bassh

All 7 comments

Can you clarify the difference? I’ve never seen >&- before. What would nvm need to do differently to handle that, considering it’s a shell thing handling nvm’s output?

&- is a special “closed file handle” commonly used instead of /dev/null because it’s much shorter to write.

As for bash, I realized only after posting this that the output was being handled by its printf builtin and confirmed that it’s what cause the command to fail.

Maybe wrapping it in a more lenient function would do the trick? e.g.

lenient_printf() {
  printf ${1+"$@"} || true
}

If you'd be interested in writing up some tests in a PR, with or without an implementation change, that would be very helpful. I've never seen this shorthand used personally ever, so i'm not sure how common it is, but it seems reasonable to support.

I tried but I'm really not sure about redirections with non-bash shells (I think redirection to pipes is a bash thing).

Is there a way to run all the tests on all the shells without installing those? Or should I just send a PR and CI will test everything?

this only fixes the problem for STDOUT. STDERR will still fail.

It kind of seems like, if the builtin printf on bash doesn't work with this, that it's not actually a safe substitute for /dev/null :-/

I've investigated, and it turns out that >&- is not meant to be used as an alternative for >/dev/null, since they do different things. Closing a descriptor is typically meant for custom descriptors you open, not for stdout or stderr.

Thus, I'm going to close this. Thanks for helping me learn about this bash syntax!

Was this page helpful?
0 / 5 - 0 ratings