cli icon indicating copy to clipboard operation
cli copied to clipboard

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

Open thislooksfun opened this issue 1 year ago • 1 comments

Is there an existing issue for this?

  • [X] I have searched the existing issues

This issue exists in the latest npm version

  • [X] 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 avatar Jan 02 '24 16:01 thislooksfun

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.

https://github.com/npm/cli/blob/badeac28faf9fde5f8c05d235219be840999a646/lib/commands/run-script.js#L23-L34

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 avatar May 14 '24 22:05 lukekarrys