Skip to content

Commit 23df96d

Browse files
nlfwraithgar
authored andcommittedJan 14, 2021
fix(link): already linked packages w/ global prefix
correctly identify already linked packages when global prefix is a symlink PR-URL: #2486 Credit: @nlf Close: #2486 Reviewed-by: @wraithgar
1 parent b0b0edf commit 23df96d

File tree

3 files changed

+66
-0
lines changed

3 files changed

+66
-0
lines changed
 

‎lib/link.js

+3
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

+5
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

+58
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,64 @@ 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+
'global-prefix': t.fixture('symlink', './real-global-prefix'),
267+
'real-global-prefix': {
268+
lib: {
269+
node_modules: {
270+
'@myscope': {
271+
linked: t.fixture('symlink', '../../../../scoped-linked'),
272+
},
273+
},
274+
},
275+
},
276+
'scoped-linked': {
277+
'package.json': JSON.stringify({
278+
name: '@myscope/linked',
279+
version: '1.0.0',
280+
}),
281+
},
282+
'my-project': {
283+
'package.json': JSON.stringify({
284+
name: 'my-project',
285+
version: '1.0.0',
286+
}),
287+
},
288+
})
289+
npm.globalDir = resolve(testdir, 'global-prefix', 'lib', 'node_modules')
290+
npm.prefix = resolve(testdir, 'my-project')
291+
292+
npm.config.find = () => 'default'
293+
294+
const _cwd = process.cwd()
295+
process.chdir(npm.prefix)
296+
297+
reifyOutput = async () => {
298+
reifyOutput = undefined
299+
process.chdir(_cwd)
300+
npm.config.find = () => null
301+
302+
const links = await printLinks({
303+
path: npm.prefix,
304+
})
305+
306+
t.equal(
307+
require(resolve(testdir, 'my-project', 'package.json')).dependencies,
308+
undefined,
309+
'should not save to package.json upon linking'
310+
)
311+
312+
t.matchSnapshot(links, 'should create a local symlink to global pkg')
313+
}
314+
315+
link(['@myscope/linked'], (err) => {
316+
t.ifError(err, 'should not error out')
317+
})
318+
})
319+
262320
t.test('completion', (t) => {
263321
const testdir = t.testdir({
264322
'global-prefix': {

0 commit comments

Comments
 (0)
Please sign in to comment.