[BUG] `npm completion` errors when run on cygwin terminals
Open
Nobody has claimed this yet.
Bug
Needs Triage
platform:windows
- Dominant language
- JavaScript
- Stars
- 10.1k
- Forks
- 4.7k
- Avg merge
- 2d 2h
- Merged PRs (30d)
- 19
Description
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:
- Locally, I commented out https://github.com/npm/cli/blob/846118686849f821b084775f7891038013f7ba97/lib/commands/completion.js#L74-L79
- I ran
source <(npm completion)on my cygwin terminal - I typed
npm install --legacy<tab key>on the CLI and it expanded tonpm install --legacy-bundling.
In addition:
- 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 - When I run
npm completionin 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 - When I run
npm completionin 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 - When I run
npm completionin a windows cmd terminal and embed cygwin viac:\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
- In windows 10
- In a cygwin terminal
- 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.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with lib/commands/completion.js and lib/utils/is-windows.js, especially the Windows-shell detection linked in the report. Reproduce npm completion in Cygwin and the listed Windows terminals, then verify that completion output is produced there without breaking the existing MINGW and Git Bash behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript, node.js
- Domain
- cli
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 55/100