Skip to content

Commit 1a9ce8e

Browse files
fix(link): scope npm link <path> --workspace to the workspace, not the root (#9592)
`npm link <path> --workspace=<ws>` did not scope the operation to the target workspace. The linked dependency was attached to the root project instead, and with `--save` (or `--save-dev`/`--save-optional`/etc.) the `file:` spec was written to the root `package.json` rather than `<ws>/package.json`. `npm install <path> --workspace=<ws> --save` behaved correctly, so only `npm link` was affected. The cause is in `Link.linkInstall()` in `lib/commands/link.js`, which built the local Arborist without the `workspaces` option and passed `workspaces` only to `reify()`. Arborist decides which node receives the `add` request from `this.options.workspaces`, which is captured at construction time, not from the reify-time option. `buildIdealTree` merges reify options into a local variable and `#parseSettings` never copies `options.workspaces` into `this.options`, so the reify-time value never reached `#applyUserRequests`. With `this.options.workspaces` left empty, the `add` and the save were applied to the root node. The fix passes `workspaces: this.workspaceNames` to the local Arborist constructor, matching how `lib/commands/install.js` already does it. Physical placement is unchanged: under the default `hoisted` strategy the symlink still hoists to the root `node_modules`, identical to `npm install --workspace`. ## References <!-- List any relevant issues, pull requests, or external references --> Fixes #9590 Related #9589
1 parent 00b9f9f commit 1a9ce8e

2 files changed

Lines changed: 40 additions & 0 deletions

File tree

‎lib/commands/link.js‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,8 @@ class Link extends ArboristWorkspaceCmd {
125125
prune: false,
126126
path: this.npm.prefix,
127127
save,
128+
// Arborist reads this.options.workspaces (set at construction) to decide which node receives the add, so it must be set here, not only at reify time.
129+
workspaces: this.workspaceNames,
128130
allowScripts: allowScriptsPolicy,
129131
})
130132
await localArb.reify({

‎test/lib/commands/link.js‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,44 @@ t.test('link global linked pkg to local workspace using args', async t => {
289289
t.matchSnapshot(await printLinks(), 'should create a local symlink to global pkg')
290290
})
291291

292+
t.test('link --workspace --save targets the workspace manifest, not the root', async t => {
293+
const { link, prefix } = await mockLink(t, {
294+
globalPrefixDir: {
295+
node_modules: {
296+
a: {
297+
'package.json': JSON.stringify({
298+
name: 'a',
299+
version: '1.0.0',
300+
}),
301+
},
302+
},
303+
},
304+
prefixDir: {
305+
'package.json': JSON.stringify({
306+
name: 'my-project',
307+
version: '1.0.0',
308+
workspaces: ['packages/*'],
309+
}),
310+
packages: {
311+
x: {
312+
'package.json': JSON.stringify({
313+
name: 'x',
314+
version: '1.0.0',
315+
}),
316+
},
317+
},
318+
},
319+
config: { workspace: 'x', save: true },
320+
})
321+
322+
await link.exec(['a'])
323+
324+
const root = JSON.parse(fs.readFileSync(join(prefix, 'package.json'), 'utf8'))
325+
const ws = JSON.parse(fs.readFileSync(join(prefix, 'packages', 'x', 'package.json'), 'utf8'))
326+
t.notOk(root.dependencies, 'root manifest should not get the dependency')
327+
t.match(ws.dependencies, { a: /^file:/ }, 'workspace manifest should get the file: dependency')
328+
})
329+
292330
t.test('link pkg already in global space', async t => {
293331
const { npm, link, printLinks, prefix } = await mockLink(t, {
294332
globalPrefixDir: {

0 commit comments

Comments
 (0)