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] In workspaces npm run autocomplete only lists root scripts #7114

Closed
2 tasks done
thislooksfun opened this issue Jan 2, 2024 · 1 comment · Fixed by #8135
Closed
2 tasks done

[BUG] In workspaces npm run autocomplete only lists root scripts #7114

thislooksfun opened this issue Jan 2, 2024 · 1 comment · Fixed by #8135
Assignees
Labels
Bug thing that needs fixing cmd:completion Priority 2 secondary priority issue

Comments

@thislooksfun
Copy link

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

When run from a workspace sub-folder, the npm run autocomplete only lists scripts from the workspace root package.json. This is a problem because those aren't the scripts that npm run actually can execute in that context. Trying to run one of the autocompleted script names will fail with "npm ERR! Error: Missing script".

Expected Behavior

It should list scripts from the nearest ancestor package.json, as that is what will actually be executed, not the ones from the root.

Steps To Reproduce

  1. Make a new npm project with at least one workspace
  2. Add a script to the root package.json file
  3. Add a script to the workspace's package.json file
  4. cd to the workspace folder
  5. Type npm run and press Tab

Environment

  • npm: 10.2.5 (also reproducible on 9.8.1)
  • Node.js: 20.10.0 (also reproducible on 18.18.2)
  • OS Name: macOS
  • System Model Name: Macbook Pro
  • npm config:
npm ERR! code ENOWORKSPACES
npm ERR! This command does not support workspaces.
@thislooksfun thislooksfun added Bug thing that needs fixing Needs Triage needs review for next steps Release 10.x labels Jan 2, 2024
@lukekarrys lukekarrys self-assigned this May 14, 2024
@lukekarrys lukekarrys added Priority 2 secondary priority issue cmd:completion and removed Needs Triage needs review for next steps Release 10.x labels May 14, 2024
@lukekarrys
Copy link
Contributor

lukekarrys commented May 14, 2024

The issue is in the static completion method for the run-script command. It always operates on npm.localPrefix which will be the root package.json for workspace.

static async completion (opts, npm) {
const argv = opts.conf.argv.remain
if (argv.length === 2) {
const { content: { scripts = {} } } = await pkgJson.normalize(npm.localPrefix)
.catch(() => ({ content: {} }))
if (opts.isFish) {
return Object.keys(scripts).map(s => `${s}\t${scripts[s].slice(0, 30)}`)
}
return Object.keys(scripts)
}
}

Ideally that method could use the workspace directory if it was the pwd, or even parse the args so npm run -w workspace <tab> would find the directory for workspace.

@lukekarrys lukekarrys removed their assignment May 16, 2024
@owlstronaut owlstronaut self-assigned this Mar 11, 2025
owlstronaut pushed a commit that referenced this issue Mar 11, 2025

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
PR makes `npm run` completion to take workspaces into account in case of
cwd is inside workspace dir it will autocompletes from `scripts` section
of nearest workspace's package.

Former behavoiur was to autocomplete everything from workspace root
package, it has no sense since when you are in workspace directory
script which will be suggested in such manner will not work. So there
was a mismatch between the script launch and the autocomplete hint
areas.

## References
Fixes [#7114](#7114)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug thing that needs fixing cmd:completion Priority 2 secondary priority issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants