Skip to content

Commit ffdadda

Browse files
committed
correctly identify already linked packages when global prefix is a symlink
1 parent eb061ca commit ffdadda

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed

lib/link.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ const link = async args => {
4545
// Returns a list of items that can't be fulfilled by
4646
// things found in the current arborist inventory
4747
const missingArgsFromTree = (tree, args) => {
48+
if (tree.isLink)
49+
return missingArgsFromTree(tree.target, args)
50+
4851
const foundNodes = []
4952
const missing = args.filter(a => {
5053
const arg = npa(a)

tap-snapshots/test-lib-link.js-TAP.test.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ exports[`test/lib/link.js TAP link pkg already in global space > should create a
1919
2020
`
2121

22+
exports[`test/lib/link.js TAP link pkg already in global space when prefix is a symlink > should create a local symlink to global pkg 1`] = `
23+
{CWD}/test/lib/link-link-pkg-already-in-global-space-when-prefix-is-a-symlink/my-project/node_modules/@myscope/linked -> {CWD}/test/lib/link-link-pkg-already-in-global-space-when-prefix-is-a-symlink/scoped-linked
24+
25+
`
26+
2227
exports[`test/lib/link.js TAP link to globalDir when in current working dir of pkg and no args > should create a global link to current pkg 1`] = `
2328
{CWD}/test/lib/link-link-to-globalDir-when-in-current-working-dir-of-pkg-and-no-args/global-prefix/lib/node_modules/test-pkg-link -> {CWD}/test/lib/link-link-to-globalDir-when-in-current-working-dir-of-pkg-and-no-args/test-pkg-link
2429

test/lib/link.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,63 @@ t.test('link pkg already in global space', (t) => {
259259
})
260260
})
261261

262+
t.test('link pkg already in global space when prefix is a symlink', (t) => {
263+
t.plan(3)
264+
265+
const testdir = t.testdir({
266+
'real-global-prefix': {
267+
lib: {
268+
node_modules: {
269+
'@myscope': {
270+
linked: t.fixture('symlink', '../../../../scoped-linked'),
271+
},
272+
},
273+
},
274+
},
275+
'scoped-linked': {
276+
'package.json': JSON.stringify({
277+
name: '@myscope/linked',
278+
version: '1.0.0',
279+
}),
280+
},
281+
'my-project': {
282+
'package.json': JSON.stringify({
283+
name: 'my-project',
284+
version: '1.0.0',
285+
}),
286+
},
287+
})
288+
npm.globalDir = resolve(testdir, 'global-prefix', 'lib', 'node_modules')
289+
npm.prefix = resolve(testdir, 'my-project')
290+
291+
npm.config.find = () => 'default'
292+
293+
const _cwd = process.cwd()
294+
process.chdir(npm.prefix)
295+
296+
reifyOutput = async () => {
297+
reifyOutput = undefined
298+
process.chdir(_cwd)
299+
npm.config.find = () => null
300+
301+
const links = await printLinks({
302+
path: npm.prefix,
303+
})
304+
305+
t.equal(
306+
require(resolve(testdir, 'my-project', 'package.json')).dependencies,
307+
undefined,
308+
'should not save to package.json upon linking'
309+
)
310+
311+
t.matchSnapshot(links, 'should create a local symlink to global pkg')
312+
}
313+
314+
link(['@myscope/linked'], (err) => {
315+
t.ifError(err, 'should not error out')
316+
})
317+
})
318+
262319
t.test('completion', (t) => {
263320
const testdir = t.testdir({
264321
'global-prefix': {

0 commit comments

Comments
 (0)