Skip to content

Commit f5b9713

Browse files
Rayyan98lukekarrys
andauthored
fix: make omit flags work properly with workspaces (#6549)
Co-authored-by: Luke Karrys <[email protected]>
1 parent 1cd86f9 commit f5b9713

File tree

3 files changed

+130
-11
lines changed

3 files changed

+130
-11
lines changed

workspaces/arborist/lib/arborist/reify.js

+23-11
Original file line numberDiff line numberDiff line change
@@ -483,17 +483,29 @@ module.exports = cls => class Reifier extends cls {
483483

484484
process.emit('time', 'reify:trashOmits')
485485

486-
const filter = node =>
487-
node.top.isProjectRoot &&
488-
(
489-
node.peer && this[_omitPeer] ||
490-
node.dev && this[_omitDev] ||
491-
node.optional && this[_omitOptional] ||
492-
node.devOptional && this[_omitOptional] && this[_omitDev]
493-
)
494-
495-
for (const node of this.idealTree.inventory.filter(filter)) {
496-
this[_addNodeToTrashList](node)
486+
for (const node of this.idealTree.inventory.values()) {
487+
const { top } = node
488+
489+
// if the top is not the root or workspace then we do not want to omit it
490+
if (!top.isProjectRoot && !top.isWorkspace) {
491+
continue
492+
}
493+
494+
// if a diff filter has been created, then we do not omit the node if the
495+
// top node is not in that set
496+
if (this.diff?.filterSet?.size && !this.diff.filterSet.has(top)) {
497+
continue
498+
}
499+
500+
// omit node if the dep type matches any omit flags that were set
501+
if (
502+
node.peer && this[_omitPeer] ||
503+
node.dev && this[_omitDev] ||
504+
node.optional && this[_omitOptional] ||
505+
node.devOptional && this[_omitOptional] && this[_omitDev]
506+
) {
507+
this[_addNodeToTrashList](node)
508+
}
497509
}
498510

499511
process.emit('timeEnd', 'reify:trashOmits')

workspaces/arborist/test/arborist/reify.js

+49
Original file line numberDiff line numberDiff line change
@@ -1187,6 +1187,55 @@ t.test('workspaces', t => {
11871187
t.test('reify simple-workspaces', t =>
11881188
t.resolveMatchSnapshot(printReified(fixture(t, 'workspaces-simple')), 'should reify simple workspaces'))
11891189

1190+
t.test('reify workspaces omit dev dependencies', async t => {
1191+
const runCase = async (t, opts) => {
1192+
const path = fixture(t, 'workspaces-conflicting-dev-deps')
1193+
const rootAjv = resolve(path, 'node_modules/ajv/package.json')
1194+
const ajvOfPkgA = resolve(path, 'a/node_modules/ajv/package.json')
1195+
const ajvOfPkgB = resolve(path, 'b/node_modules/ajv/package.json')
1196+
1197+
t.equal(fs.existsSync(rootAjv), true, 'root ajv exists')
1198+
t.equal(fs.existsSync(ajvOfPkgA), true, 'ajv under package a node_modules exists')
1199+
t.equal(fs.existsSync(ajvOfPkgB), true, 'ajv under package a node_modules exists')
1200+
1201+
await reify(path, { omit: ['dev'], ...opts })
1202+
1203+
return {
1204+
root: { exists: () => fs.existsSync(rootAjv) },
1205+
a: { exists: () => fs.existsSync(ajvOfPkgA) },
1206+
b: { exists: () => fs.existsSync(ajvOfPkgB) },
1207+
}
1208+
}
1209+
1210+
await t.test('default', async t => {
1211+
const { root, a, b } = await runCase(t)
1212+
t.equal(root.exists(), false, 'root')
1213+
t.equal(a.exists(), false, 'a')
1214+
t.equal(b.exists(), false, 'b')
1215+
})
1216+
1217+
await t.test('workspaces only', async t => {
1218+
const { root, a, b } = await runCase(t, { workspaces: ['a'] })
1219+
t.equal(root.exists(), false, 'root')
1220+
t.equal(a.exists(), false, 'a')
1221+
t.equal(b.exists(), true, 'b')
1222+
})
1223+
1224+
await t.test('workspaces + root', async t => {
1225+
const { root, a, b } = await runCase(t, { workspaces: ['a'], includeWorkspaceRoot: true })
1226+
t.equal(root.exists(), false, 'root')
1227+
t.equal(a.exists(), false, 'a')
1228+
t.equal(b.exists(), true, 'b')
1229+
})
1230+
1231+
await t.test('disable workspaces', async t => {
1232+
const { root, a, b } = await runCase(t, { workspacesEnabled: false })
1233+
t.equal(root.exists(), false, 'root')
1234+
t.equal(a.exists(), true, 'a')
1235+
t.equal(b.exists(), true, 'b')
1236+
})
1237+
})
1238+
11901239
t.test('reify workspaces lockfile', async t => {
11911240
const path = fixture(t, 'workspaces-simple')
11921241
await reify(path)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// generated from test/fixtures/workspaces-simple
2+
module.exports = t => {
3+
const path = t.testdir({
4+
"node_modules": {
5+
ajv: {
6+
'package.json': JSON.stringify({
7+
name: 'ajv',
8+
version: '6.10.2',
9+
}),
10+
}
11+
},
12+
"a": {
13+
"package.json": JSON.stringify({
14+
"name": "a",
15+
"version": "1.0.0",
16+
"devDependencies": {
17+
"ajv": "4.11.2"
18+
}
19+
}),
20+
"node_modules": {
21+
ajv: {
22+
'package.json': JSON.stringify({
23+
name: 'ajv',
24+
version: '4.11.2',
25+
}),
26+
}
27+
},
28+
},
29+
"b": {
30+
"package.json": JSON.stringify({
31+
"name": "b",
32+
"version": "1.0.0",
33+
"devDependencies": {
34+
"ajv": "5.11.2"
35+
}
36+
}),
37+
"node_modules": {
38+
ajv: {
39+
'package.json': JSON.stringify({
40+
name: 'ajv',
41+
version: '5.11.2',
42+
}),
43+
}
44+
},
45+
},
46+
"package.json": JSON.stringify({
47+
"name": "workspace-conflicting-dev-deps",
48+
"devDependencies": {
49+
"ajv": "6.10.2"
50+
},
51+
"workspaces": [
52+
"a",
53+
"b"
54+
]
55+
})
56+
})
57+
return path
58+
}

0 commit comments

Comments
 (0)