Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
2 tasks done
DanKaplanSES opened this issue Mar 1, 2025 · 1 comment
Open
2 tasks done

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

DanKaplanSES opened this issue Mar 1, 2025 · 1 comment
Labels
Bug thing that needs fixing Needs Triage needs review for next steps platform:windows is Windows-specific

Comments

@DanKaplanSES
Copy link
Contributor

DanKaplanSES commented Mar 1, 2025

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.
@DanKaplanSES DanKaplanSES added Bug thing that needs fixing Needs Triage needs review for next steps labels Mar 1, 2025
@DanKaplanSES DanKaplanSES changed the title [BUG] npm completion unnecessarily errors when run on cygwin terminals [BUG] npm completion errors when run on cygwin terminals Mar 1, 2025
@DanKaplanSES
Copy link
Contributor Author

Here are some surefire ways to detect if node is running in a cygwin terminal:

  1. Parse the output of uname.
  2. Run cygpath --version in node, since that command should only be on the path when cygwin is running:
    const { execSync } = require('child_process');
    
    try {
        execSync('cygpath --version', { stdio: 'ignore' });
        console.log("Running in Cygwin terminal.");
    } catch (error) {
        console.log("Not running in Cygwin.");
    }
    (Note: I got this answer from ChatGPT)

There may be something better than either of these but it didn't show up in my search. I could submit a PR for solution 1., but I don't know if it's acceptable run external commands in https://github.com/npm/cli/blob/846118686849f821b084775f7891038013f7ba97/lib/utils/is-windows.js or any part of the node project.

@reggi reggi added the platform:windows is Windows-specific label Mar 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug thing that needs fixing Needs Triage needs review for next steps platform:windows is Windows-specific
Projects
None yet
Development

No branches or pull requests

2 participants