Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions workspaces/arborist/lib/arborist/build-ideal-tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -1465,19 +1465,19 @@ This is a one-time fix-up, please be patient...
const needPrune = metaFromDisk && (mutateTree || flagsSuspect)
if (this.#prune && needPrune) {
this.#idealTreePrune()
for (const node of this.idealTree.inventory.values()) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note: I snuck #8430 in because I feel otherwise technically I should apply the fix to this loop too as they're the same, and technically this loop has a performance cost so removing it is a little nicer

if (node.extraneous) {
node.parent = null
}
}
}

timeEnd()
}

#idealTreePrune () {
for (const node of this.idealTree.inventory.values()) {
if (node.extraneous) {
// optional peer dependencies are meant to be added to the tree
// through an explicit required dependency (most commonly in the
// root package.json), at which point they won't be optional so
// any dependencies still marked as both optional and peer at
// this point can be pruned as a special kind of extraneous
if (node.extraneous || (node.peer && node.optional)) {
node.parent = null
}
}
Expand Down
18 changes: 18 additions & 0 deletions workspaces/arborist/test/arborist/pruner.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const { resolve } = require('node:path')
const fs = require('node:fs')

const t = require('tap')
const Arborist = require('../../lib/arborist/index.js')
Expand Down Expand Up @@ -39,6 +40,23 @@ t.test('prune with lockfile', async t => {
t.matchSnapshot(printTree(tree))
})

t.test('prune with lockfile with implicit optional peer dependencies', async t => {
const path = fixture(t, 'prune-lockfile-optional-peer')
const tree = await pruneTree(path, { audit: false })

const dep = tree.children.get('dedent')
t.ok(dep, 'required prod dep was not pruned from tree')

const optionalPeerDep = tree.children.get('babel-plugin-macros')
t.notOk(optionalPeerDep, 'optional peer dep was pruned from tree')

t.notMatch(
fs.readFileSync(path + '/package-lock.json'),
'node_modules/babel-plugin-macros',
'should remove optional peer dep from package-lock.json'
)
})

t.test('prune with actual tree omit dev', async t => {
const path = fixture(t, 'prune-actual-omit-dev')
const tree = await pruneTree(path, { omit: ['dev'] })
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "prune-lockfile-optional-peer",
"version": "1.0.0",
"dependencies": {
"dedent": "^1.6.0"
}
}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// generated from test/fixtures/prune-lockfile-optional-peer
module.exports = t => {
const path = t.testdir({
"node_modules": {
"dedent": {
"package.json": JSON.stringify({
"name": "dedent",
"version": "1.6.0",
"peerDependencies": {
"babel-plugin-macros": "^3.1.0"
},
"peerDependenciesMeta": {
"babel-plugin-macros": {
"optional": true
}
}
})
}
},
"package-lock.json": JSON.stringify({
"name": "prune-lockfile-optional-peer",
"version": "1.0.0",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "prune-lockfile-optional-peer",
"version": "1.0.0",
"dependencies": {
"dedent": "^1.6.0"
}
},
"node_modules/babel-plugin-macros": {
"version": "3.1.0",
"optional": true,
"peer": true
},
"node_modules/dedent": {
"version": "1.6.0",
"peerDependencies": {
"babel-plugin-macros": "^3.1.0"
},
"peerDependenciesMeta": {
"babel-plugin-macros": {
"optional": true
}
}
}
}
}),
"package.json": JSON.stringify({
"name": "prune-lockfile-optional-peer",
"version": "1.0.0",
"dependencies": {
"dedent": "^1.6.0"
}
})
})
return path
}
Loading