cli icon indicating copy to clipboard operation
cli copied to clipboard

[BUG] wrong version used when publishing from a workspace

Open Badisi opened this issue 3 years ago • 6 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

Considering the following project structure and code:

my-project
├── dist
│   └── a
│       └── package.json # { "name: "a", "version": "1.0.0" }
├── projects
│   └── a
│       └── package.json # { "name: "a", "version": "0.0.0-semantically-released" }
├── package.json # { "name: "my-project", "workspaces": [ "projects/a" ] }
└── index.js
// index.js
const { exec } = require('node:child_process');
const cmd = exec(
    `npm publish ${process.cwd()}/dist/a --dry-run`,
    { cwd: `${process.cwd()}/projects/a` }
);
cmd.stdout.pipe(process.stdout, { end: false });
cmd.stderr.pipe(process.stderr, { end: false });

Running node index.js from ./my-project will result in:

 npm notice filename: a-0.0.0-semantically-released.tgz

instead of

 npm notice filename: a-1.0.0.tgz

The issue seems to be related to the workspaces attribute from the root package.json.

Because removing this attribute will produce the proper result.

Expected Behavior

I would expect that the package.json file used during the command npm publish {path} is the one located in the given path argument and not another one from somewhere else (being cwd or whatsoever).

Steps To Reproduce

  1. git clone https://github.com/Badisi/issue-npm-publish-with-workspaces
  2. npm install
  3. npm run start

Environment

  • platform: macOS Monterey 12.5.1 (21G83)
  • node: v16.14.2
  • npm: 8.19.2 (EDIT: still exists in 10.2.0, Node 18 & 20)

Badisi avatar Oct 25 '22 16:10 Badisi

This issue still exists in [email protected] and, as such, label should be updated to Release 9x. Thanks

Badisi avatar Dec 19 '22 12:12 Badisi

This issue still exists in [email protected].

Badisi avatar Mar 02 '23 09:03 Badisi

This issue still exists in [email protected].

Badisi avatar Jul 10 '23 10:07 Badisi

I think I ran into this problem as well, however in a slightly different manner and before opening a new issue, I will add my finding to this one.

It seems that - within a workspace - npm publish <tarball> disregards that <tarball> argument and will fall back to what it finds in the workspace:

image

This showcases the behavior inside of a workspace. Notice the lines:

npm info found workspace root at ***/cdk-constructs/dist 
...
npm verb publish   '***/cdk-constructs/dist/packages/athena'

When I move the package outside of the workspace, I get the expected outcome, where the existing tarball is used:

image

I can force the expected behavior, if I pass --workspaces false to npm publish inside of the workspace, but this is just a hack for me right now.

@npm/cli-team do you have any comment on this issue?


Edit: before anyone asks, I have also tested this with Node v20.8.0 and NPM v10.1.0 and the issue still exists.

bweigel avatar Oct 04 '23 17:10 bweigel

I also confirm that this issue still exists in [email protected] (Node 18 & 20).

Badisi avatar Oct 19 '23 15:10 Badisi

Still an issue in [email protected] (Node 20).

Shakeskeyboarde avatar Mar 17 '24 06:03 Shakeskeyboarde

Still an issue in [email protected] (Node 20).


I also found (like @bweigel answer) that it's no really related to the version itself but to the <package-spec> argument that is not taken into account. See https://github.com/npm/cli/issues/7726 for more info.

Badisi avatar Aug 15 '24 15:08 Badisi

Closing this, as newly reported issue https://github.com/npm/cli/issues/7726 is the similer if not duplicate. Keeping the newer one open for better visibility.

milaninfy avatar Aug 16 '24 14:08 milaninfy