From 2404c7e991e1a4962608a1e6b2453a7ccee4e0a5 Mon Sep 17 00:00:00 2001 From: milaninfy <111582375+milaninfy@users.noreply.github.com> Date: Wed, 21 Aug 2024 10:48:39 -0400 Subject: [PATCH] fix(publish): consider package-spec when inside workspace dir (#7738) This PR fixes an issue where the `npm publish` command would fail when run from within a workspace directory with package-spec fixes: https://github.com/npm/cli/issues/7726 --- lib/commands/publish.js | 7 +++- .../test/lib/commands/publish.js.test.cjs | 4 ++ test/lib/commands/publish.js | 42 +++++++++++++++++++ 3 files changed, 52 insertions(+), 1 deletion(-) diff --git a/lib/commands/publish.js b/lib/commands/publish.js index 3fe337a7b1c43..c2cd3cf9c927b 100644 --- a/lib/commands/publish.js +++ b/lib/commands/publish.js @@ -46,7 +46,12 @@ class Publish extends BaseCommand { await this.#publish(args) } - async execWorkspaces () { + async execWorkspaces (args) { + const useWorkspaces = args.length === 0 || args.includes('.') + if (!useWorkspaces) { + log.warn('Ignoring workspaces for specified package(s)') + return this.exec(args) + } await this.setWorkspaces() for (const [name, workspace] of this.workspaces.entries()) { diff --git a/tap-snapshots/test/lib/commands/publish.js.test.cjs b/tap-snapshots/test/lib/commands/publish.js.test.cjs index bad7355fb9076..78395c5cbca63 100644 --- a/tap-snapshots/test/lib/commands/publish.js.test.cjs +++ b/tap-snapshots/test/lib/commands/publish.js.test.cjs @@ -393,6 +393,10 @@ exports[`test/lib/commands/publish.js TAP workspaces all workspaces - some marke + workspace-a@1.2.3-a ` +exports[`test/lib/commands/publish.js TAP workspaces differet package spec > publish different package spec 1`] = ` ++ pkg@1.2.3 +` + exports[`test/lib/commands/publish.js TAP workspaces json > all workspaces in json 1`] = ` { "workspace-a": { diff --git a/test/lib/commands/publish.js b/test/lib/commands/publish.js index fa43994679b97..4dea3e2fa06a0 100644 --- a/test/lib/commands/publish.js +++ b/test/lib/commands/publish.js @@ -700,6 +700,48 @@ t.test('workspaces', t => { await npm.exec('publish', []) t.matchSnapshot(joinedOutput(), 'all workspaces in json') }) + + t.test('differet package spec', async t => { + const testDir = { + 'package.json': JSON.stringify( + { + ...pkgJson, + workspaces: ['workspace-a'], + }, null, 2), + 'workspace-a': { + 'package.json': JSON.stringify({ + name: 'workspace-a', + version: '1.2.3-a', + }), + }, + 'dir/pkg': { + 'package.json': JSON.stringify({ + name: 'pkg', + version: '1.2.3', + }), + }, + } + + const { npm, joinedOutput } = await loadMockNpm(t, { + config: { + ...auth, + }, + prefixDir: testDir, + chdir: ({ prefix }) => path.resolve(prefix, './workspace-a'), + }) + const registry = new MockRegistry({ + tap: t, + registry: npm.config.get('registry'), + authorization: token, + }) + registry.nock + .put('/pkg', body => { + return t.match(body, { name: 'pkg' }) + }).reply(200, {}) + await npm.exec('publish', ['../dir/pkg']) + t.matchSnapshot(joinedOutput(), 'publish different package spec') + }) + t.end() })