Skip to content

Commit b93948f

Browse files
owlstronautreggi
authored andcommitted
fix: use omit when checking ideal tree engine (#8372)
Also centralizes shouldOmit logic
1 parent 6e9dbc6 commit b93948f

File tree

12 files changed

+205
-46
lines changed

12 files changed

+205
-46
lines changed

workspaces/arborist/lib/arborist/build-ideal-tree.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,9 +192,11 @@ module.exports = cls => class IdealTreeBuilder extends cls {
192192
}
193193

194194
async #checkEngineAndPlatform () {
195-
const { engineStrict, npmVersion, nodeVersion } = this.options
195+
const { engineStrict, npmVersion, nodeVersion, omit = [] } = this.options
196+
const omitSet = new Set(omit)
197+
196198
for (const node of this.idealTree.inventory.values()) {
197-
if (!node.optional) {
199+
if (!node.optional && !node.shouldOmit(omitSet)) {
198200
try {
199201
// if devEngines is present in the root node we ignore the engines check
200202
if (!(node.isRoot && node.package.devEngines)) {

workspaces/arborist/lib/arborist/reify.js

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,7 @@ module.exports = cls => class Reifier extends cls {
8484
#bundleUnpacked = new Set() // the nodes we unpack to read their bundles
8585
#dryRun
8686
#nmValidated = new Set()
87-
#omitDev
88-
#omitPeer
89-
#omitOptional
87+
#omit
9088
#retiredPaths = {}
9189
#retiredUnchanged = {}
9290
#savePrefix
@@ -110,10 +108,7 @@ module.exports = cls => class Reifier extends cls {
110108
throw er
111109
}
112110

113-
const omit = new Set(options.omit || [])
114-
this.#omitDev = omit.has('dev')
115-
this.#omitOptional = omit.has('optional')
116-
this.#omitPeer = omit.has('peer')
111+
this.#omit = new Set(options.omit)
117112

118113
// start tracker block
119114
this.addTracker('reify')
@@ -562,12 +557,11 @@ module.exports = cls => class Reifier extends cls {
562557
// adding to the trash list will skip reifying, and delete them
563558
// if they are currently in the tree and otherwise untouched.
564559
[_addOmitsToTrashList] () {
565-
if (!this.#omitDev && !this.#omitOptional && !this.#omitPeer) {
560+
if (!this.#omit.size) {
566561
return
567562
}
568563

569564
const timeEnd = time.start('reify:trashOmits')
570-
571565
for (const node of this.idealTree.inventory.values()) {
572566
const { top } = node
573567

@@ -583,12 +577,7 @@ module.exports = cls => class Reifier extends cls {
583577
}
584578

585579
// omit node if the dep type matches any omit flags that were set
586-
if (
587-
node.peer && this.#omitPeer ||
588-
node.dev && this.#omitDev ||
589-
node.optional && this.#omitOptional ||
590-
node.devOptional && this.#omitOptional && this.#omitDev
591-
) {
580+
if (node.shouldOmit(this.#omit)) {
592581
this[_addNodeToTrashList](node)
593582
}
594583
}

workspaces/arborist/lib/audit-report.js

Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ class AuditReport extends Map {
148148
if (!seen.has(k)) {
149149
const p = []
150150
for (const node of this.tree.inventory.query('packageName', name)) {
151-
if (!shouldAudit(node, this[_omit], this.filterSet)) {
151+
if (!this.shouldAudit(node)) {
152152
continue
153153
}
154154

@@ -282,7 +282,7 @@ class AuditReport extends Map {
282282

283283
const timeEnd = time.start('auditReport:getReport')
284284
try {
285-
const body = prepareBulkData(this.tree, this[_omit], this.filterSet)
285+
const body = this.prepareBulkData()
286286
log.silly('audit', 'bulk request', body)
287287

288288
// no sense asking if we don't have anything to audit,
@@ -309,37 +309,33 @@ class AuditReport extends Map {
309309
timeEnd()
310310
}
311311
}
312-
}
313312

314-
// return true if we should audit this one
315-
const shouldAudit = (node, omit, filterSet) =>
316-
!node.version ? false
317-
: node.isRoot ? false
318-
: filterSet && filterSet.size !== 0 && !filterSet.has(node) ? false
319-
: omit.size === 0 ? true
320-
: !( // otherwise, just ensure we're not omitting this one
321-
node.dev && omit.has('dev') ||
322-
node.optional && omit.has('optional') ||
323-
node.devOptional && omit.has('dev') && omit.has('optional') ||
324-
node.peer && omit.has('peer')
325-
)
326-
327-
const prepareBulkData = (tree, omit, filterSet) => {
328-
const payload = {}
329-
for (const name of tree.inventory.query('packageName')) {
330-
const set = new Set()
331-
for (const node of tree.inventory.query('packageName', name)) {
332-
if (!shouldAudit(node, omit, filterSet)) {
333-
continue
334-
}
313+
// return true if we should audit this one
314+
shouldAudit (node) {
315+
return !node.version ? false
316+
: node.isRoot ? false
317+
: this.filterSet && this.filterSet.size !== 0 && !this.filterSet.has(node) ? false
318+
: this[_omit].size === 0 ? true
319+
: !node.shouldOmit(this[_omit])
320+
}
335321

336-
set.add(node.version)
337-
}
338-
if (set.size) {
339-
payload[name] = [...set]
322+
prepareBulkData () {
323+
const payload = {}
324+
for (const name of this.tree.inventory.query('packageName')) {
325+
const set = new Set()
326+
for (const node of this.tree.inventory.query('packageName', name)) {
327+
if (!this.shouldAudit(node)) {
328+
continue
329+
}
330+
331+
set.add(node.version)
332+
}
333+
if (set.size) {
334+
payload[name] = [...set]
335+
}
340336
}
337+
return payload
341338
}
342-
return payload
343339
}
344340

345341
module.exports = AuditReport

workspaces/arborist/lib/node.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,15 @@ class Node {
489489
return false
490490
}
491491

492+
shouldOmit (omitSet) {
493+
return (
494+
this.peer && omitSet.has('peer') ||
495+
this.dev && omitSet.has('dev') ||
496+
this.optional && omitSet.has('optional') ||
497+
this.devOptional && omitSet.has('optional') && omitSet.has('dev')
498+
)
499+
}
500+
492501
getBundler (path = []) {
493502
// made a cycle, definitely not bundled!
494503
if (path.includes(this)) {

workspaces/arborist/test/arborist/build-ideal-tree.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4091,3 +4091,47 @@ t.test('should take devEngines in account', async t => {
40914091
const tree = await buildIdeal(path)
40924092
t.matchSnapshot(String(tree.meta))
40934093
})
4094+
4095+
t.test('engine checking respects omit flags', async t => {
4096+
const testFixture = resolve(fixtures, 'engine-omit-test')
4097+
4098+
t.test('fail on engine mismatch in devDependencies without omit=dev', async t => {
4099+
await t.rejects(buildIdeal(testFixture, {
4100+
nodeVersion: '12.18.4',
4101+
engineStrict: true,
4102+
}),
4103+
{ code: 'EBADENGINE' },
4104+
'should fail with EBADENGINE when devDependencies have engine mismatch'
4105+
)
4106+
})
4107+
4108+
t.test('skip engine check for devDependencies with omit=dev', async t => {
4109+
// This should NOT throw an EBADENGINE error
4110+
await buildIdeal(testFixture, {
4111+
nodeVersion: '12.18.4',
4112+
engineStrict: true,
4113+
omit: ['dev'],
4114+
})
4115+
t.pass('should succeed when omitting dev dependencies with engine mismatches')
4116+
})
4117+
4118+
t.test('skip engine check for optionalDependencies with omit=optional', async t => {
4119+
const optionalFixture = resolve(fixtures, 'optional-engine-omit-test')
4120+
await buildIdeal(optionalFixture, {
4121+
nodeVersion: '12.18.4',
4122+
engineStrict: true,
4123+
omit: ['optional'],
4124+
})
4125+
t.pass('should succeed when omitting optional dependencies with engine mismatches')
4126+
})
4127+
4128+
t.test('skip engine check for peerDependencies with omit=peer', async t => {
4129+
const peerFixture = resolve(fixtures, 'peer-engine-omit-test')
4130+
await buildIdeal(peerFixture, {
4131+
nodeVersion: '12.18.4',
4132+
engineStrict: true,
4133+
omit: ['peer'],
4134+
})
4135+
t.pass('should succeed when omitting peer dependencies with engine mismatches')
4136+
})
4137+
})

workspaces/arborist/test/fixtures/engine-omit-test/node_modules/strict-engine-dev/package.json

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"name": "engine-omit-test",
3+
"version": "1.0.0",
4+
"devDependencies": {
5+
"strict-engine-dev": "1.0.0"
6+
}
7+
}

workspaces/arborist/test/fixtures/optional-engine-omit-test/node_modules/strict-engine-optional/package.json

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"name": "optional-engine-omit-test",
3+
"version": "1.0.0",
4+
"optionalDependencies": {
5+
"strict-engine-optional": "1.0.0"
6+
}
7+
}

workspaces/arborist/test/fixtures/peer-engine-omit-test/node_modules/strict-engine-peer/package.json

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)