Skip to content

[BUG] npm completion errors when run on cygwin terminals #8137

Open
@DanKaplanSES

Description

@DanKaplanSES

Is there an existing issue for this?

  • I have searched the existing issues

This issue exists in the latest npm version

  • I am using the latest npm

Current Behavior

In a cygwin terminal:

$ npm completion
npm error code ENOTSUP
npm error npm completion supported only in MINGW / Git bash on Windows
npm error Log files were not written due to an error writing to the directory: C:\Users\User\AppData\Local\npm-cache\_logs
npm error You can rerun the command with `--loglevel=verbose` to see the logs in your terminal

Expected Behavior

I would expect this behavior:

$ npm completion
###-begin-npm-completion-###
#
# npm command completion script
#
# Installation: npm completion >> ~/.bashrc  (or ~/.zshrc)
# Or, maybe: npm completion > /usr/local/etc/bash_completion.d/npm
#

if type complete &>/dev/null; then
  _npm_completion () {
    local words cword
    if type _get_comp_words_by_ref &>/dev/null; then
      _get_comp_words_by_ref -n = -n @ -n : -w words -i cword
    else
      cword="$COMP_CWORD"
      words=("${COMP_WORDS[@]}")
    fi

    local si="$IFS"
    if ! IFS=$'\n' COMPREPLY=($(COMP_CWORD="$cword" \
                           COMP_LINE="$COMP_LINE" \
                           COMP_POINT="$COMP_POINT" \
                           npm completion -- "${words[@]}" \
                           2>/dev/null)); then
      local ret=$?
      IFS="$si"
      return $ret
    fi
    IFS="$si"
    if type __ltrim_colon_completions &>/dev/null; then
      __ltrim_colon_completions "${words[cword]}"
    fi
  }
  complete -o default -F _npm_completion npm
elif type compdef &>/dev/null; then
  _npm_completion() {
    local si=$IFS
    compadd -- $(COMP_CWORD=$((CURRENT-1)) \
                 COMP_LINE=$BUFFER \
                 COMP_POINT=0 \
                 npm completion -- "${words[@]}" \
                 2>/dev/null)
    IFS=$si
  }
  compdef _npm_completion npm
elif type compctl &>/dev/null; then
  _npm_completion () {
    local cword line point words si
    read -Ac words
    read -cn cword
    let cword-=1
    read -l line
    read -ln point
    si="$IFS"
    if ! IFS=$'\n' reply=($(COMP_CWORD="$cword" \
                       COMP_LINE="$line" \
                       COMP_POINT="$point" \
                       npm completion -- "${words[@]}" \
                       2>/dev/null)); then

      local ret=$?
      IFS="$si"
      return $ret
    fi
    IFS="$si"
  }
  compctl -K _npm_completion npm
fi
###-end-npm-completion-###

The reason I expect this behavior is because the output works as expected when sourced. Here's how I tested that:

  1. Locally, I commented out
    if (isWindowsShell) {
    const msg = 'npm completion supported only in MINGW / Git bash on Windows'
    throw Object.assign(new Error(msg), {
    code: 'ENOTSUP',
    })
    }
  2. I ran source <(npm completion) on my cygwin terminal
  3. I typed npm install --legacy<tab key> on the CLI and it expanded to npm install --legacy-bundling.

In addition:

  1. Locally, I edited https://github.com/npm/cli/blob/846118686849f821b084775f7891038013f7ba97/lib/utils/is-windows.js like so:
    const isWindowsShell = (process.platform === 'win32') &&
      !/^MINGW(32|64)$/.test(process.env.MSYSTEM) && process.env.TERM !== 'cygwin'
    
    console.log(`process.platform`, process.platform);
    console.log(`process.env.MSYSTEM`, process.env.MSYSTEM);
    console.log(`!/^MINGW(32|64)$/.test(process.env.MSYSTEM)`, !/^MINGW(32|64)$/.test(process.env.MSYSTEM));
    console.log(`process.env.TERM`, process.env.TERM);
    console.log(`isWindowsShell`, isWindowsShell);
    
    exports.isWindowsShell = isWindowsShell
  2. When I run npm completion in vscode's integrated terminal, it outputs:
    $ npm completion
    process.platform win32
    process.env.MSYSTEM undefined
    !/^MINGW(32|64)$/.test(process.env.MSYSTEM) true
    process.env.TERM xterm-256color # <-------
    isWindowsShell true
    npm error code ENOTSUP
    
  3. When I run npm completion in a cygwin terminal, it outputs:
    $ npm completion
    process.platform win32
    process.env.MSYSTEM undefined
    !/^MINGW(32|64)$/.test(process.env.MSYSTEM) true
    process.env.TERM xterm # <-------
    isWindowsShell true
    npm error code ENOTSUP
    
  4. When I run npm completion in a windows cmd terminal and embed cygwin via c:\cygwin64\bin\bash.exe --login -i, it outputs:
    $ npm completion
    process.platform win32
    process.env.MSYSTEM undefined
    !/^MINGW(32|64)$/.test(process.env.MSYSTEM) true
    process.env.TERM xterm-256color # <-------
    isWindowsShell true
    npm error code ENOTSUP
    

In other words, process.env.TERM !== 'cygwin' does not catch all cygwin environments.

Steps To Reproduce

  1. In windows 10
  2. In a cygwin terminal
  3. Type npm completion

Environment

  • npm: 11.1.0
  • Node.js: v22.13.0
  • OS Name: Windows 10
  • System Model Name:
  • npm config:
; "builtin" config from C:\Users\User\AppData\Roaming\npm\node_modules\npm\npmrc

prefix = "C:\\Users\\User\\AppData\\Roaming\\npm"

; "global" config from C:\Users\User\AppData\Roaming\npm\etc\npmrc

save-exact = true
save-prefix = ""

; "user" config from C:\cygwin64\home\User\.npmrc

//registry.npmjs.org/:_authToken = (protected)
registry = "https://registry.npmjs.org/"
script-shell = "C:\\cygwin64\\bin\\bash.exe"

; "project" config from C:\App\.npmrc

install-links = true

; node bin location = C:\Program Files\nodejs\node.exe
; node version = v22.13.0
; npm local prefix = C:\App
; npm version = 11.1.0
; cwd = C:\App
; HOME = C:\cygwin64\home\User
; Run `npm config ls -l` to show all defaults.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Bugthing that needs fixingNeeds Triageneeds review for next stepsplatform:windowsis Windows-specific

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions