Skip to content

Commit 14a3d95

Browse files
authored
fix: resolve workspace paths from cwd when possible (#4265)
1 parent c99c215 commit 14a3d95

File tree

3 files changed

+12
-4
lines changed

3 files changed

+12
-4
lines changed

lib/base-command.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
// Base class for npm commands
2+
3+
const { relative } = require('path')
4+
25
const usageUtil = require('./utils/usage.js')
36
const ConfigDefinitions = require('./utils/config/definitions.js')
47
const getWorkspaces = require('./workspaces/get-workspaces.js')
@@ -78,9 +81,14 @@ class BaseCommand {
7881
this.includeWorkspaceRoot = false
7982
}
8083

84+
const relativeFrom = relative(this.npm.localPrefix, process.cwd()).startsWith('..')
85+
? this.npm.localPrefix
86+
: process.cwd()
87+
8188
const ws = await getWorkspaces(filters, {
8289
path: this.npm.localPrefix,
8390
includeWorkspaceRoot: this.includeWorkspaceRoot,
91+
relativeFrom,
8492
})
8593
this.workspaces = ws
8694
this.workspaceNames = [...ws.keys()]

lib/workspaces/get-workspaces.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const rpj = require('read-package-json-fast')
55

66
// Returns an Map of paths to workspaces indexed by workspace name
77
// { foo => '/path/to/foo' }
8-
const getWorkspaces = async (filters, { path, includeWorkspaceRoot }) => {
8+
const getWorkspaces = async (filters, { path, includeWorkspaceRoot, relativeFrom }) => {
99
// TODO we need a better error to be bubbled up here if this rpj call fails
1010
const pkg = await rpj(resolve(path, 'package.json'))
1111
const workspaces = await mapWorkspaces({ cwd: path, pkg })
@@ -21,8 +21,8 @@ const getWorkspaces = async (filters, { path, includeWorkspaceRoot }) => {
2121
for (const filterArg of filters) {
2222
for (const [workspaceName, workspacePath] of workspaces.entries()) {
2323
if (filterArg === workspaceName
24-
|| resolve(path, filterArg) === workspacePath
25-
|| minimatch(workspacePath, `${resolve(path, filterArg)}/*`)) {
24+
|| resolve(relativeFrom || path, filterArg) === workspacePath
25+
|| minimatch(workspacePath, `${resolve(relativeFrom || path, filterArg)}/*`)) {
2626
res.set(workspaceName, workspacePath)
2727
}
2828
}

test/lib/arborist-cmd.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ t.test('handle getWorkspaces raising an error', async t => {
9898
})
9999
class TestCmd extends ArboristCmd {}
100100
const cmd = new TestCmd()
101-
cmd.npm = {}
101+
cmd.npm = { localPrefix: t.testdir() }
102102

103103
await t.rejects(
104104
cmd.execWorkspaces(['foo'], ['a']),

0 commit comments

Comments
 (0)