Skip to content

Commit

Permalink
fix(link): already linked packages w/ global prefix
Browse files Browse the repository at this point in the history
correctly identify already linked packages when global prefix is a symlink

PR-URL: npm#2486
Credit: @nlf
Close: npm#2486
Reviewed-by: @wraithgar
  • Loading branch information
nlf authored and wraithgar committed Jan 14, 2021
1 parent b0b0edf commit 23df96d
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/link.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ const link = async args => {
// Returns a list of items that can't be fulfilled by
// things found in the current arborist inventory
const missingArgsFromTree = (tree, args) => {
if (tree.isLink)
return missingArgsFromTree(tree.target, args)

const foundNodes = []
const missing = args.filter(a => {
const arg = npa(a)
Expand Down
5 changes: 5 additions & 0 deletions tap-snapshots/test-lib-link.js-TAP.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ exports[`test/lib/link.js TAP link pkg already in global space > should create a
`

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`] = `
{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
`

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`] = `
{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
Expand Down
58 changes: 58 additions & 0 deletions test/lib/link.js
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,64 @@ t.test('link pkg already in global space', (t) => {
})
})

t.test('link pkg already in global space when prefix is a symlink', (t) => {
t.plan(3)

const testdir = t.testdir({
'global-prefix': t.fixture('symlink', './real-global-prefix'),
'real-global-prefix': {
lib: {
node_modules: {
'@myscope': {
linked: t.fixture('symlink', '../../../../scoped-linked'),
},
},
},
},
'scoped-linked': {
'package.json': JSON.stringify({
name: '@myscope/linked',
version: '1.0.0',
}),
},
'my-project': {
'package.json': JSON.stringify({
name: 'my-project',
version: '1.0.0',
}),
},
})
npm.globalDir = resolve(testdir, 'global-prefix', 'lib', 'node_modules')
npm.prefix = resolve(testdir, 'my-project')

npm.config.find = () => 'default'

const _cwd = process.cwd()
process.chdir(npm.prefix)

reifyOutput = async () => {
reifyOutput = undefined
process.chdir(_cwd)
npm.config.find = () => null

const links = await printLinks({
path: npm.prefix,
})

t.equal(
require(resolve(testdir, 'my-project', 'package.json')).dependencies,
undefined,
'should not save to package.json upon linking'
)

t.matchSnapshot(links, 'should create a local symlink to global pkg')
}

link(['@myscope/linked'], (err) => {
t.ifError(err, 'should not error out')
})
})

t.test('completion', (t) => {
const testdir = t.testdir({
'global-prefix': {
Expand Down

0 comments on commit 23df96d

Please sign in to comment.