Skip to content
This repository was archived by the owner on Jan 20, 2022. It is now read-only.

Commit a376f02

Browse files
committed
Install peerOptionals if explicitly requested, or dev
Fix npm/cli#2005
1 parent 9bb36dc commit a376f02

File tree

6 files changed

+133
-9
lines changed

6 files changed

+133
-9
lines changed

lib/add-rm-pkg-deps.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ const removeFromOthers = (name, type, pkg) => {
1515
break
1616
case 'dev':
1717
others.delete('devDependencies')
18+
others.delete('peerDependencies')
19+
others.delete('peerDependenciesMeta')
1820
break
1921
case 'optional':
2022
others.delete('optionalDependencies')

lib/arborist/build-ideal-tree.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -856,9 +856,12 @@ This is a one-time fix-up, please be patient...
856856
if (edge.to && edge.to.inShrinkwrap)
857857
return false
858858

859-
// If the edge has no destination, that's a problem.
860-
if (!edge.to)
861-
return edge.type !== 'peerOptional'
859+
// If the edge has no destination, that's a problem, unless
860+
// if it's peerOptional and not explicitly requested.
861+
if (!edge.to) {
862+
return edge.type !== 'peerOptional' ||
863+
this[_explicitRequests].has(edge.name)
864+
}
862865

863866
// If the edge has an error, there's a problem.
864867
if (!edge.valid)

lib/node.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,12 @@ class Node {
502502
this[_loadDepType](this.package.optionalDependencies, 'optional')
503503
this[_loadDepType](this.package.dependencies, 'prod')
504504

505+
// Linked targets that are disconnected from the tree are tops,
506+
// but don't have a 'path' field, only a 'realpath', because we
507+
// don't know their canonical location. We don't need their devDeps.
508+
if (this.isTop && this.path)
509+
this[_loadDepType](this.package.devDependencies, 'dev')
510+
505511
const pd = this.package.peerDependencies
506512
if (pd && typeof pd === 'object' && !this.legacyPeerDeps) {
507513
const pm = this.package.peerDependenciesMeta || {}
@@ -516,12 +522,6 @@ class Node {
516522
this[_loadDepType](peerDependencies, 'peer')
517523
this[_loadDepType](peerOptional, 'peerOptional')
518524
}
519-
520-
// Linked targets that are disconnected from the tree are tops,
521-
// but don't have a 'path' field, only a 'realpath', because we
522-
// don't know their canonical location. We don't need their devDeps.
523-
if (this.isTop && this.path)
524-
this[_loadDepType](this.package.devDependencies, 'dev')
525525
}
526526

527527
[_loadDepType] (obj, type) {

tap-snapshots/test-arborist-build-ideal-tree.js-TAP.test.js

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65110,6 +65110,97 @@ Node {
6511065110
}
6511165111
`
6511265112

65113+
exports[`test/arborist/build-ideal-tree.js TAP peerOptionals that are devDeps or explicit request > should install the abbrev dep 1`] = `
65114+
Node {
65115+
"children": Map {
65116+
"abbrev" => Node {
65117+
"edgesIn": Set {
65118+
Edge {
65119+
"from": "",
65120+
"name": "abbrev",
65121+
"spec": "",
65122+
"type": "peerOptional",
65123+
},
65124+
},
65125+
"location": "node_modules/abbrev",
65126+
"name": "abbrev",
65127+
"optional": true,
65128+
"peer": true,
65129+
"resolved": "https://registry.npmjs.org/abbrev/-/abbrev-1.1.1.tgz",
65130+
},
65131+
"wrappy" => Node {
65132+
"dev": true,
65133+
"edgesIn": Set {
65134+
Edge {
65135+
"from": "",
65136+
"name": "wrappy",
65137+
"spec": "",
65138+
"type": "dev",
65139+
},
65140+
},
65141+
"location": "node_modules/wrappy",
65142+
"name": "wrappy",
65143+
"resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz",
65144+
},
65145+
},
65146+
"edgesOut": Map {
65147+
"abbrev" => Edge {
65148+
"name": "abbrev",
65149+
"spec": "",
65150+
"to": "node_modules/abbrev",
65151+
"type": "peerOptional",
65152+
},
65153+
"wrappy" => Edge {
65154+
"name": "wrappy",
65155+
"spec": "",
65156+
"to": "node_modules/wrappy",
65157+
"type": "dev",
65158+
},
65159+
},
65160+
"location": "",
65161+
"name": "peer-optional-installs",
65162+
"resolved": null,
65163+
}
65164+
`
65165+
65166+
exports[`test/arborist/build-ideal-tree.js TAP peerOptionals that are devDeps or explicit request > should install the wrappy dep, and not remove from peerDeps 1`] = `
65167+
Node {
65168+
"children": Map {
65169+
"wrappy" => Node {
65170+
"dev": true,
65171+
"edgesIn": Set {
65172+
Edge {
65173+
"from": "",
65174+
"name": "wrappy",
65175+
"spec": "",
65176+
"type": "dev",
65177+
},
65178+
},
65179+
"location": "node_modules/wrappy",
65180+
"name": "wrappy",
65181+
"resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz",
65182+
},
65183+
},
65184+
"edgesOut": Map {
65185+
"abbrev" => Edge {
65186+
"name": "abbrev",
65187+
"spec": "",
65188+
"to": null,
65189+
"type": "peerOptional",
65190+
},
65191+
"wrappy" => Edge {
65192+
"name": "wrappy",
65193+
"spec": "",
65194+
"to": "node_modules/wrappy",
65195+
"type": "dev",
65196+
},
65197+
},
65198+
"location": "",
65199+
"name": "peer-optional-installs",
65200+
"resolved": null,
65201+
}
65202+
`
65203+
6511365204
exports[`test/arborist/build-ideal-tree.js TAP push conflicted peer deps deeper in to the tree to solve > must match snapshot 1`] = `
6511465205
Node {
6511565206
"children": Map {

test/arborist/build-ideal-tree.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2114,3 +2114,14 @@ t.test('carbonium eslint conflicts', async t => {
21142114
],
21152115
}))
21162116
})
2117+
2118+
t.test('peerOptionals that are devDeps or explicit request', async t => {
2119+
const path = resolve(fixtures, 'peer-optional-installs')
2120+
t.matchSnapshot(await printIdeal(path, {
2121+
add: ['abbrev'],
2122+
}), 'should install the abbrev dep')
2123+
t.matchSnapshot(await printIdeal(path, {
2124+
add: ['wrappy'],
2125+
saveType: 'dev',
2126+
}), 'should install the wrappy dep, and not remove from peerDeps')
2127+
})
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"peerDependencies": {
3+
"abbrev": "",
4+
"wrappy": ""
5+
},
6+
"devDependencies": {
7+
"wrappy": ""
8+
},
9+
"peerDependenciesMeta": {
10+
"wrappy": {
11+
"optional": true
12+
},
13+
"abbrev": {
14+
"optional": true
15+
}
16+
}
17+
}

0 commit comments

Comments
 (0)