npm / cli

the package manager for JavaScript
https://docs.npmjs.com/cli/
Other
8.3k stars 3.05k forks source link

[BUG] Different behavior for npm pkg for root package and workspaces #5481

Open ThePlenkov opened 1 year ago

ThePlenkov commented 1 year ago

Is there an existing issue for this?

This issue exists in the latest npm version

Current Behavior

I have a project with packages defined as workspaces

If i run the following command in the root folder - it works as expected

/gitlab-ci-cli (main) $ npm pkg get name
"gitlaber"

if i run same command for workspace - it always responds with json:

gitpod /workspace/gitlab-ci-cli/packages/commands (main) $ npm pkg get name
{
  "@gitlaber/commands": "@gitlaber/commands"
}

Using --json=false doesn't help. So it looks like somewhere --json is used internally

Expected Behavior

This command should work identically for all packages respecting --json parameter

Steps To Reproduce

  1. Init project
  2. Add workspace project
  3. run npm pkg get name in the root folder
  4. run npm pkg get name in the workspace folder

Output format is different

Environment

boeckMt commented 1 year ago

I'm getting a different output for workspaces. First it prints the workspace name as expected and then it adds json output.


$ npm pkg get name --workspace=<workspace>

"<workspace-name>"
{
  "<workspace-name>": "<workspace-name>"
}

Maybe that is related to https://github.com/npm/cli/issues/5508#issuecomment-1326181655

Environment

npm: 9.3.1 Node.js: 19.4.0

ljharb commented 1 year ago

what about with npm 9.5?

boeckMt commented 1 year ago

what about with npm 9.5?

I get the same output like https://github.com/npm/cli/issues/5481#issuecomment-1442042779

Environment

npm: 9.5.1 Node.js: 19.4.0

lukekarrys commented 3 months ago

The possible bug here is that npm treats being inside a workspace directory the same as using the --workspace flag.

Currently npm pkg get always returns json for workspaces. This is how the command has always worked (outside of some other bugs) so changing that would be a breaking change. For example:

# from root
❯ npm pkg get name
"npm"

❯ npm pkg get name -w mock-registry
{
  "@npmcli/mock-registry": "@npmcli/mock-registry"
}

# change to workspace directory
❯ cd mock-registry
❯ npm pkg get name
{
  "@npmcli/mock-registry": "@npmcli/mock-registry"
}

I think that second example should return "@npmcli/mock-registry" the same way running it from the root returns "npm". This would require npm to differentiate between the workspace flag and the directory being a workspace.

ThePlenkov commented 3 months ago

To my opinion --json option is not consistent across all npm commands. There are so many commands which return not parseable output even using this flag. So the semantics should be very straightforward. If it's --json - always return parseable json, otherwise output formatted response. There should not be difference to my opinion we run npm command for root or workspace - result should be also consistent respecting --json flag too