Skip to content

Commit 2404c7e

Browse files
authored
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: #7726
1 parent 24d5350 commit 2404c7e

File tree

3 files changed

+52
-1
lines changed

3 files changed

+52
-1
lines changed

lib/commands/publish.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,12 @@ class Publish extends BaseCommand {
4646
await this.#publish(args)
4747
}
4848

49-
async execWorkspaces () {
49+
async execWorkspaces (args) {
50+
const useWorkspaces = args.length === 0 || args.includes('.')
51+
if (!useWorkspaces) {
52+
log.warn('Ignoring workspaces for specified package(s)')
53+
return this.exec(args)
54+
}
5055
await this.setWorkspaces()
5156

5257
for (const [name, workspace] of this.workspaces.entries()) {

tap-snapshots/test/lib/commands/publish.js.test.cjs

+4
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,10 @@ exports[`test/lib/commands/publish.js TAP workspaces all workspaces - some marke
393393
+ workspace-a@1.2.3-a
394394
`
395395

396+
exports[`test/lib/commands/publish.js TAP workspaces differet package spec > publish different package spec 1`] = `
397+
+ pkg@1.2.3
398+
`
399+
396400
exports[`test/lib/commands/publish.js TAP workspaces json > all workspaces in json 1`] = `
397401
{
398402
"workspace-a": {

test/lib/commands/publish.js

+42
Original file line numberDiff line numberDiff line change
@@ -700,6 +700,48 @@ t.test('workspaces', t => {
700700
await npm.exec('publish', [])
701701
t.matchSnapshot(joinedOutput(), 'all workspaces in json')
702702
})
703+
704+
t.test('differet package spec', async t => {
705+
const testDir = {
706+
'package.json': JSON.stringify(
707+
{
708+
...pkgJson,
709+
workspaces: ['workspace-a'],
710+
}, null, 2),
711+
'workspace-a': {
712+
'package.json': JSON.stringify({
713+
name: 'workspace-a',
714+
version: '1.2.3-a',
715+
}),
716+
},
717+
'dir/pkg': {
718+
'package.json': JSON.stringify({
719+
name: 'pkg',
720+
version: '1.2.3',
721+
}),
722+
},
723+
}
724+
725+
const { npm, joinedOutput } = await loadMockNpm(t, {
726+
config: {
727+
...auth,
728+
},
729+
prefixDir: testDir,
730+
chdir: ({ prefix }) => path.resolve(prefix, './workspace-a'),
731+
})
732+
const registry = new MockRegistry({
733+
tap: t,
734+
registry: npm.config.get('registry'),
735+
authorization: token,
736+
})
737+
registry.nock
738+
.put('/pkg', body => {
739+
return t.match(body, { name: 'pkg' })
740+
}).reply(200, {})
741+
await npm.exec('publish', ['../dir/pkg'])
742+
t.matchSnapshot(joinedOutput(), 'publish different package spec')
743+
})
744+
703745
t.end()
704746
})
705747

0 commit comments

Comments
 (0)