Skip to content

Commit c52cf6b

Browse files
nlflukekarrys
authored andcommitted
fix: properly handle directory, file, git and alias specs in overrides
1 parent ec09474 commit c52cf6b

File tree

2 files changed

+136
-1
lines changed

2 files changed

+136
-1
lines changed

workspaces/arborist/lib/override-set.js

+28-1
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,36 @@ class OverrideSet {
5050
continue
5151
}
5252

53-
if (semver.intersects(edge.spec, rule.keySpec)) {
53+
// if keySpec is * we found our override
54+
if (rule.keySpec === '*') {
5455
return rule
5556
}
57+
58+
let spec = npa(`${edge.name}@${edge.spec}`)
59+
if (spec.type === 'alias') {
60+
spec = spec.subSpec
61+
}
62+
63+
if (spec.type === 'git') {
64+
if (spec.gitRange && semver.intersects(spec.gitRange, rule.keySpec)) {
65+
return rule
66+
}
67+
68+
continue
69+
}
70+
71+
if (spec.type === 'range' || spec.type === 'version') {
72+
if (semver.intersects(spec.fetchSpec, rule.keySpec)) {
73+
return rule
74+
}
75+
76+
continue
77+
}
78+
79+
// if we got this far, the spec type is one of tag, directory or file
80+
// which means we have no real way to make version comparisons, so we
81+
// just accept the override
82+
return rule
5683
}
5784

5885
return this

workspaces/arborist/test/override-set.js

+108
Original file line numberDiff line numberDiff line change
@@ -163,4 +163,112 @@ t.test('constructor', async (t) => {
163163
const barEdgeRule = overrides.getEdgeRule({ name: 'bar', spec: '^1' })
164164
t.equal(barEdgeRule.value, '*', 'when rule is omitted entirely value is *')
165165
})
166+
167+
t.test('version specs work', async (t) => {
168+
const overrides = new OverrideSet({
169+
overrides: {
170+
foo: {
171+
bar: '$bar',
172+
},
173+
'baz@^1.0.0': {
174+
'buzz@^1.0.0': '$buzz',
175+
},
176+
},
177+
})
178+
179+
const fooEdgeRule = overrides.getEdgeRule({ name: 'foo', spec: '^1.0.0' })
180+
const barEdgeRule = fooEdgeRule.getEdgeRule({ name: 'bar', spec: '1.0.0' })
181+
t.equal(barEdgeRule.value, '$bar', 'got a rule back')
182+
183+
const bazEdgeRule = overrides.getEdgeRule({ name: 'baz', spec: '^1.0.0' })
184+
const buzzEdgeRule = bazEdgeRule.getEdgeRule({ name: 'buzz', spec: '1.0.0' })
185+
t.equal(buzzEdgeRule.value, '$buzz', 'got a rule back')
186+
})
187+
188+
t.test('directory specs work', async (t) => {
189+
const overrides = new OverrideSet({
190+
overrides: {
191+
foo: {
192+
bar: '$bar',
193+
},
194+
'baz@^1.0.0': {
195+
'buzz@^1.0.0': '$buzz',
196+
},
197+
},
198+
})
199+
200+
const fooEdgeRule = overrides.getEdgeRule({ name: 'foo', spec: '^1.0.0' })
201+
const barEdgeRule = fooEdgeRule.getEdgeRule({ name: 'bar', spec: 'file:../bar' })
202+
t.equal(barEdgeRule.value, '$bar', 'got a rule back')
203+
204+
const bazEdgeRule = overrides.getEdgeRule({ name: 'baz', spec: '^1.0.0' })
205+
const buzzEdgeRule = bazEdgeRule.getEdgeRule({ name: 'buzz', spec: 'file:../buzz' })
206+
t.equal(buzzEdgeRule.value, '$buzz', 'got a rule back')
207+
})
208+
209+
t.test('file specs work', async (t) => {
210+
const overrides = new OverrideSet({
211+
overrides: {
212+
foo: {
213+
bar: '$bar',
214+
},
215+
'baz@^1.0.0': {
216+
'buzz@^1.0.0': '$buzz',
217+
},
218+
},
219+
})
220+
221+
const fooEdgeRule = overrides.getEdgeRule({ name: 'foo', spec: '^1.0.0' })
222+
const barEdgeRule = fooEdgeRule.getEdgeRule({ name: 'bar', spec: 'file:../bar.tgz' })
223+
t.equal(barEdgeRule.value, '$bar', 'got a rule back')
224+
225+
const bazEdgeRule = overrides.getEdgeRule({ name: 'baz', spec: '^1.0.0' })
226+
const buzzEdgeRule = bazEdgeRule.getEdgeRule({ name: 'buzz', spec: 'file:../buzz.tgz' })
227+
t.equal(buzzEdgeRule.value, '$buzz', 'got a rule back')
228+
})
229+
230+
t.test('alias specs work', async (t) => {
231+
const overrides = new OverrideSet({
232+
overrides: {
233+
foo: {
234+
bar: '$bar',
235+
},
236+
'baz@^1.0.0': {
237+
'buzz@^1.0.0': '$buzz',
238+
},
239+
},
240+
})
241+
242+
const fooEdgeRule = overrides.getEdgeRule({ name: 'foo', spec: '^1.0.0' })
243+
const barEdgeRule = fooEdgeRule.getEdgeRule({ name: 'bar', spec: 'npm:bar2@^1.0.0' })
244+
t.equal(barEdgeRule.value, '$bar', 'got a rule back')
245+
246+
const bazEdgeRule = overrides.getEdgeRule({ name: 'baz', spec: '^1.0.0' })
247+
const buzzEdgeRule = bazEdgeRule.getEdgeRule({ name: 'buzz', spec: 'npm:buzz2@^1.0.0' })
248+
t.equal(buzzEdgeRule.value, '$buzz', 'got a rule back')
249+
})
250+
251+
t.test('git specs work', async (t) => {
252+
const overrides = new OverrideSet({
253+
overrides: {
254+
foo: {
255+
bar: '$bar',
256+
},
257+
'baz@^1.0.0': {
258+
'buzz@^1.0.0': '$buzz',
259+
},
260+
},
261+
})
262+
263+
const fooEdgeRule = overrides.getEdgeRule({ name: 'foo', spec: '^1.0.0' })
264+
const barEdgeRule = fooEdgeRule.getEdgeRule({ name: 'bar', spec: 'github:foo/bar' })
265+
t.equal(barEdgeRule.value, '$bar', 'got a rule back')
266+
267+
const bazEdgeRule = overrides.getEdgeRule({ name: 'baz', spec: '^1.0.0' })
268+
const buzzEdgeRule = bazEdgeRule.getEdgeRule({ name: 'buzz', spec: 'github:baz/buzz#semver:^1.0.0' })
269+
t.equal(buzzEdgeRule.value, '$buzz', 'got a rule back')
270+
271+
const outOfRangeRule = bazEdgeRule.getEdgeRule({ name: 'buzz', spec: 'github:baz/buzz#semver:^2.0.0' })
272+
t.equal(outOfRangeRule.name, 'baz', 'no match - returned parent')
273+
})
166274
})

0 commit comments

Comments
 (0)