-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresolver.go
More file actions
634 lines (568 loc) · 16.8 KB
/
resolver.go
File metadata and controls
634 lines (568 loc) · 16.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
package pom
import (
"context"
"fmt"
"maps"
"slices"
"strings"
)
const (
maxParentDepth = 32
maxBOMDepth = 16
)
// Resolution classifies how (or whether) a dependency's version was
// determined. Missing-version and unresolved-property are the same
// underlying problem so they share this taxonomy.
type Resolution string
const (
// Resolved means a concrete version string was produced.
Resolved Resolution = "resolved"
// UnresolvedProperty means a ${name} expression remained after
// interpolation and no source defines it.
UnresolvedProperty Resolution = "unresolved_property"
// UnresolvedEnv means the version references ${env.X} which can never
// be resolved statically.
UnresolvedEnv Resolution = "unresolved_env"
// UnresolvedParent means a parent POM in the chain could not be
// fetched, so the whole resolution is suspect.
UnresolvedParent Resolution = "unresolved_parent"
// UnresolvedProfileGated means the version is only defined inside a
// profile that was not activated under the current mode.
UnresolvedProfileGated Resolution = "unresolved_profile_gated"
// UnresolvedMissing means no version was declared anywhere reachable:
// not on the dependency, not in dependencyManagement, not in any BOM.
UnresolvedMissing Resolution = "unresolved_missing"
)
// ProfileMode controls which <profile> sections contribute to the merge.
type ProfileMode int
const (
// OnlyDefault activates only profiles with <activeByDefault>true</activeByDefault>.
OnlyDefault ProfileMode = iota
// Pessimistic activates every profile, on the basis that for vuln
// scanning a false positive is preferable to a false negative.
Pessimistic
// Explicit activates only the named profile IDs (plus activeByDefault).
Explicit
)
// ProfileActivation configures profile selection for a Resolve call.
type ProfileActivation struct {
Mode ProfileMode
IDs []string
}
// Fetcher retrieves a parsed POM for a coordinate. Implementations are
// expected to be safe for concurrent use; the resolver itself is
// synchronous but callers may share a Fetcher across goroutines.
type Fetcher interface {
Fetch(ctx context.Context, gav GAV) (*POM, error)
}
// Resolver computes effective POMs. It owns a Fetcher and uses it for the
// root, every parent in the chain, and every imported BOM, so all I/O goes
// through one place.
type Resolver struct {
fetcher Fetcher
cache map[GAV]*EffectivePOM
}
// NewResolver constructs a Resolver around f. Resolved POMs are memoised
// for the lifetime of the Resolver since released coordinates are
// immutable.
func NewResolver(f Fetcher) *Resolver {
return &Resolver{fetcher: f, cache: map[GAV]*EffectivePOM{}}
}
// Options tunes a single Resolve call.
type Options struct {
Profiles ProfileActivation
}
// EffectivePOM is the merged, interpolated view of a coordinate.
type EffectivePOM struct {
GAV GAV
Packaging string
Name string
Description string
URL string
Licenses []License
SCM SCM
// Relocation is set when the root POM declares a
// <distributionManagement><relocation>. Callers may want to follow it
// and re-resolve.
Relocation *Relocation
// Properties is the merged property map after parent inheritance,
// profile contribution, project.* synthesis, and self-interpolation.
Properties map[string]string
// Dependencies are the project's direct dependencies with versions
// filled from dependencyManagement and interpolated.
Dependencies []ResolvedDep
// DependencyManagement is the merged managed-dependency table, after
// BOM expansion, keyed by management key.
DependencyManagement map[string]Dep
// Parents lists the parent chain from immediate parent to root.
Parents []GAV
// ActiveProfiles lists profile IDs that contributed to the merge.
ActiveProfiles []string
// Warnings collects non-fatal issues encountered during resolution
// (parent fetch failures, BOM fetch failures, depth limits).
Warnings []string
}
// ResolvedDep is a dependency after merge and interpolation, tagged with
// how its version was (or wasn't) determined.
type ResolvedDep struct {
GroupID string
ArtifactID string
Version string
Type string
Classifier string
Scope string
Optional bool
Exclusions []Exclusion
Resolution Resolution
// Expression holds the original unresolved ${...} string when
// Resolution is one of the unresolved_* values.
Expression string
// Profile is set when this dependency was contributed by a profile.
Profile string
}
func (d ResolvedDep) GAV() GAV {
return GAV{GroupID: d.GroupID, ArtifactID: d.ArtifactID, Version: d.Version}
}
// Resolve fetches gav and computes its effective POM under opts.
func (r *Resolver) Resolve(ctx context.Context, gav GAV, opts Options) (*EffectivePOM, error) {
if ep, ok := r.cache[gav]; ok {
return ep, nil
}
root, err := r.fetcher.Fetch(ctx, gav)
if err != nil {
return nil, fmt.Errorf("pom: fetch %s: %w", gav, err)
}
ep, err := r.ResolvePOM(ctx, root, opts)
if err != nil {
return nil, err
}
r.cache[gav] = ep
return ep, nil
}
// ResolvePOM computes the effective POM for an already-parsed root POM.
// Useful when the caller holds a pom.xml from a source checkout that is
// not itself fetchable by coordinate.
func (r *Resolver) ResolvePOM(ctx context.Context, root *POM, opts Options) (*EffectivePOM, error) {
chain, warnings := r.parentChain(ctx, root)
m := newMerger(opts.Profiles)
parentFailed := len(warnings) > 0
for _, p := range chain {
m.apply(p)
}
rootGAV := root.EffectiveGAV()
m.synthesiseProjectProps(root, rootGAV)
m.interpolateProps()
bomWarnings := r.expandBOMs(ctx, m, 0)
warnings = append(warnings, bomWarnings...)
m.interpolateDepMgmt()
deps := m.resolveDeps(parentFailed)
ep := &EffectivePOM{
GAV: rootGAV,
Packaging: defaultType(root.Packaging),
Name: interpolate(m.meta.name, m.props),
Description: interpolate(strings.TrimSpace(m.meta.description), m.props),
URL: interpolate(m.meta.url, m.props),
Licenses: m.meta.licenses,
SCM: m.interpolateSCM(),
Relocation: root.DistributionManagement.Relocation,
Properties: m.props,
Dependencies: deps,
DependencyManagement: m.depMgmt,
Parents: parentsOf(chain),
ActiveProfiles: m.activeProfiles,
Warnings: warnings,
}
return ep, nil
}
// parentChain fetches the inheritance chain and returns it ordered root
// first, child last, so a linear merge naturally gives child-wins.
func (r *Resolver) parentChain(ctx context.Context, root *POM) ([]*POM, []string) {
chain := []*POM{root}
var warnings []string
seen := map[GAV]bool{root.EffectiveGAV(): true}
cur := root
for depth := 0; cur.Parent != nil; depth++ {
if depth >= maxParentDepth {
warnings = append(warnings, fmt.Sprintf("parent chain exceeds %d levels at %s", maxParentDepth, cur.Parent.GAV()))
break
}
pgav := cur.Parent.GAV()
if seen[pgav] {
warnings = append(warnings, fmt.Sprintf("parent cycle detected at %s", pgav))
break
}
seen[pgav] = true
p, err := r.fetcher.Fetch(ctx, pgav)
if err != nil {
warnings = append(warnings, fmt.Sprintf("fetch parent %s: %v", pgav, err))
break
}
chain = append(chain, p)
cur = p
}
slices.Reverse(chain)
return chain, warnings
}
func parentsOf(chain []*POM) []GAV {
if len(chain) <= 1 {
return nil
}
parents := chain[:len(chain)-1]
out := make([]GAV, 0, len(parents))
for i := len(parents) - 1; i >= 0; i-- {
out = append(out, parents[i].EffectiveGAV())
}
return out
}
// expandBOMs recursively resolves <scope>import</scope> entries in the
// merged dependencyManagement, replacing each with the managed entries of
// the imported BOM. First-declared wins on conflict.
func (r *Resolver) expandBOMs(ctx context.Context, m *merger, depth int) []string {
var warnings []string
if depth >= maxBOMDepth {
return []string{fmt.Sprintf("BOM import depth exceeds %d", maxBOMDepth)}
}
imports := m.takeImports()
for _, imp := range imports {
gav := GAV{
GroupID: interpolate(imp.GroupID, m.props),
ArtifactID: interpolate(imp.ArtifactID, m.props),
Version: interpolate(imp.Version, m.props),
}
if containsExpr(gav.Version) || gav.Version == "" {
warnings = append(warnings, fmt.Sprintf("BOM import %s:%s has unresolvable version %q", gav.GroupID, gav.ArtifactID, imp.Version))
continue
}
ep, err := r.resolveBOM(ctx, gav, depth+1)
if err != nil {
warnings = append(warnings, fmt.Sprintf("fetch BOM %s: %v", gav, err))
continue
}
m.importBOM(ep.DependencyManagement)
warnings = append(warnings, ep.Warnings...)
}
return warnings
}
func (r *Resolver) resolveBOM(ctx context.Context, gav GAV, depth int) (*EffectivePOM, error) {
if ep, ok := r.cache[gav]; ok {
return ep, nil
}
root, err := r.fetcher.Fetch(ctx, gav)
if err != nil {
return nil, err
}
chain, warnings := r.parentChain(ctx, root)
m := newMerger(ProfileActivation{Mode: OnlyDefault})
for _, p := range chain {
m.apply(p)
}
m.synthesiseProjectProps(root, root.EffectiveGAV())
m.interpolateProps()
warnings = append(warnings, r.expandBOMs(ctx, m, depth)...)
m.interpolateDepMgmt()
ep := &EffectivePOM{
GAV: root.EffectiveGAV(),
Properties: m.props,
DependencyManagement: m.depMgmt,
Warnings: warnings,
}
r.cache[gav] = ep
return ep, nil
}
type metadata struct {
name string
description string
url string
licenses []License
scm SCM
}
// merger accumulates state across the parent chain.
type merger struct {
activation ProfileActivation
props map[string]string
meta metadata
depMgmt map[string]Dep
imports []Dep
deps []Dep
depKeys map[string]int
depProf map[string]string
profDefs map[string]bool
activeProfiles []string
}
func newMerger(act ProfileActivation) *merger {
return &merger{
activation: act,
props: map[string]string{},
depMgmt: map[string]Dep{},
depKeys: map[string]int{},
depProf: map[string]string{},
profDefs: map[string]bool{},
}
}
// apply merges one POM into the accumulator. Called root-first, so later
// calls (children) overwrite earlier ones (parents).
func (m *merger) apply(p *POM) {
maps.Copy(m.props, p.Properties)
m.mergeMeta(p)
m.mergeDepMgmt(p.DependencyManagement.Dependencies, true)
m.mergeDeps(p.Dependencies, "")
for i := range p.Profiles {
pr := &p.Profiles[i]
active := m.activation.active(pr)
if !active {
m.recordProfileGated(pr)
continue
}
m.activeProfiles = append(m.activeProfiles, pr.ID)
maps.Copy(m.props, pr.Properties)
m.mergeDepMgmt(pr.DependencyManagement.Dependencies, true)
m.mergeDeps(pr.Dependencies, pr.ID)
}
}
func (a ProfileActivation) active(p *Profile) bool {
def := strings.EqualFold(strings.TrimSpace(p.Activation.ActiveByDefault), "true")
switch a.Mode {
case Pessimistic:
return true
case Explicit:
return def || slices.Contains(a.IDs, p.ID)
case OnlyDefault:
fallthrough
default:
return def
}
}
func (m *merger) mergeMeta(p *POM) {
if p.Name != "" {
m.meta.name = p.Name
}
if strings.TrimSpace(p.Description) != "" {
m.meta.description = p.Description
}
if p.URL != "" {
m.meta.url = p.URL
}
if len(p.Licenses) > 0 {
m.meta.licenses = p.Licenses
}
if !p.SCM.empty() {
m.meta.scm = p.SCM
}
}
func (m *merger) interpolateSCM() SCM {
return SCM{
URL: interpolate(m.meta.scm.URL, m.props),
Connection: interpolate(m.meta.scm.Connection, m.props),
DeveloperConnection: interpolate(m.meta.scm.DeveloperConnection, m.props),
}
}
func (m *merger) recordProfileGated(pr *Profile) {
for k := range pr.Properties {
m.profDefs[k] = true
}
}
// mergeDepMgmt folds managed entries into the accumulator. childWins
// controls precedence: true for parent->child inheritance (child overrides
// parent), false for BOM imports (existing entry wins).
func (m *merger) mergeDepMgmt(entries []Dep, childWins bool) {
for _, d := range entries {
if d.Scope == scopeImport {
m.imports = append(m.imports, d)
continue
}
k := d.managementKey()
if !childWins {
if _, exists := m.depMgmt[k]; exists {
continue
}
}
m.depMgmt[k] = d
}
}
func (m *merger) mergeDeps(entries []Dep, profile string) {
for _, d := range entries {
k := d.managementKey()
if i, ok := m.depKeys[k]; ok {
m.deps[i] = overlayDep(m.deps[i], d)
if profile != "" {
m.depProf[k] = profile
}
continue
}
m.depKeys[k] = len(m.deps)
m.deps = append(m.deps, d)
if profile != "" {
m.depProf[k] = profile
}
}
}
func overlayDep(base, over Dep) Dep {
if over.Version != "" {
base.Version = over.Version
}
if over.Scope != "" {
base.Scope = over.Scope
}
if over.Optional != "" {
base.Optional = over.Optional
}
if len(over.Exclusions) > 0 {
base.Exclusions = over.Exclusions
}
return base
}
func (m *merger) takeImports() []Dep {
out := m.imports
m.imports = nil
return out
}
func (m *merger) importBOM(managed map[string]Dep) {
for k, d := range managed {
if _, exists := m.depMgmt[k]; exists {
continue
}
m.depMgmt[k] = d
}
}
func (m *merger) synthesiseProjectProps(root *POM, gav GAV) {
set := func(k, v string) {
if v == "" {
return
}
if _, ok := m.props[k]; !ok {
m.props[k] = v
}
}
set("project.groupId", gav.GroupID)
set("project.artifactId", gav.ArtifactID)
set("project.version", gav.Version)
if root.Parent != nil {
set("project.parent.groupId", root.Parent.GroupID)
set("project.parent.artifactId", root.Parent.ArtifactID)
set("project.parent.version", root.Parent.Version)
}
if root.Packaging != "" {
set("project.packaging", root.Packaging)
}
}
func (m *merger) interpolateProps() {
for range maxInterpolationPasses {
changed := false
for k, v := range m.props {
nv := interpolate(v, m.props)
if nv != v {
m.props[k] = nv
changed = true
}
}
if !changed {
break
}
}
}
func (m *merger) interpolateDepMgmt() {
out := make(map[string]Dep, len(m.depMgmt))
for _, d := range m.depMgmt {
d.GroupID = interpolate(d.GroupID, m.props)
d.ArtifactID = interpolate(d.ArtifactID, m.props)
d.Version = interpolate(d.Version, m.props)
d.Scope = interpolate(d.Scope, m.props)
d.Type = interpolate(d.Type, m.props)
d.Classifier = interpolate(d.Classifier, m.props)
out[d.managementKey()] = d
}
m.depMgmt = out
}
func (m *merger) resolveDeps(parentFailed bool) []ResolvedDep {
out := make([]ResolvedDep, 0, len(m.deps))
for _, d := range m.deps {
rd := m.resolveDep(d, parentFailed)
out = append(out, rd)
}
return out
}
func (m *merger) resolveDep(d Dep, parentFailed bool) ResolvedDep {
rawKey := d.managementKey()
d.GroupID = interpolate(d.GroupID, m.props)
d.ArtifactID = interpolate(d.ArtifactID, m.props)
d.Type = interpolate(d.Type, m.props)
d.Classifier = interpolate(d.Classifier, m.props)
rawVersion := d.Version
managed, haveManaged := m.lookupManaged(d)
if d.Version == "" && haveManaged {
d.Version = managed.Version
}
if d.Scope == "" && haveManaged && managed.Scope != "" {
d.Scope = managed.Scope
}
if len(d.Exclusions) == 0 && haveManaged && len(managed.Exclusions) > 0 {
d.Exclusions = managed.Exclusions
}
d.Version = interpolate(d.Version, m.props)
d.Scope = interpolate(d.Scope, m.props)
rd := ResolvedDep{
GroupID: d.GroupID,
ArtifactID: d.ArtifactID,
Version: d.Version,
Type: defaultType(d.Type),
Classifier: d.Classifier,
Scope: defaultScope(d.Scope),
Optional: strings.EqualFold(strings.TrimSpace(d.Optional), "true"),
Exclusions: d.Exclusions,
Profile: m.depProf[rawKey],
}
rd.Resolution, rd.Expression = m.classify(d, rawVersion, parentFailed)
return rd
}
func (m *merger) lookupManaged(d Dep) (Dep, bool) {
if md, ok := m.depMgmt[d.managementKey()]; ok {
return md, true
}
if d.Type == "" && d.Classifier == "" {
return Dep{}, false
}
fallback := Dep{GroupID: d.GroupID, ArtifactID: d.ArtifactID}
md, ok := m.depMgmt[fallback.managementKey()]
return md, ok
}
func (m *merger) classify(d Dep, rawVersion string, parentFailed bool) (Resolution, string) {
switch {
case d.Version == "" && parentFailed:
return UnresolvedParent, rawVersion
case d.Version == "":
return UnresolvedMissing, rawVersion
case containsExpr(d.Version):
return m.classifyExpr(d.Version, parentFailed)
}
for _, f := range []string{d.GroupID, d.ArtifactID, d.Classifier} {
if containsExpr(f) {
return m.classifyExpr(f, parentFailed)
}
}
return Resolved, ""
}
func (m *merger) classifyExpr(s string, parentFailed bool) (Resolution, string) {
name := firstExpr(s)
switch {
case strings.HasPrefix(name, "env."):
return UnresolvedEnv, s
case m.profDefs[name]:
return UnresolvedProfileGated, s
case parentFailed:
return UnresolvedParent, s
default:
return UnresolvedProperty, s
}
}
func defaultType(t string) string {
if t == "" {
return "jar"
}
return t
}
func defaultScope(s string) string {
if s == "" {
return "compile"
}
return s
}