Skip to content

Commit f920c6d

Browse files
committed
fix handling of transitive dependencies
This fixes issue with missing dependencies like: ``` > Could not find com.google.protobuf:protobuf-bom:3.10.0. ``` Which was not reported for `com.google.protobuf:protobuf-java:3.10.0`. This is necessary for Gradle 6+, but for now I'm leaving this disabled. It's not enough to track `dependencies` field. We also need to scan the `dependencyManagement` field for transitive dependencies. Most of them are in `test` scope or without a scope, but some like `import` are needed. Dependency Management allows to consolidate and centralize the management of dep. versions without adding deps which are inherited by all children. Useful when you have a set of projects that inherits a common parent. For more details see: - https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html#Dependency_Management Signed-off-by: Jakub Sokołowski <jakub@status.im>
1 parent 4844d95 commit f920c6d

File tree

4 files changed

+43
-21
lines changed

4 files changed

+43
-21
lines changed

finder/finder.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@ import (
1111
)
1212

1313
type Options struct {
14-
IgnoreScopes []string /* list of dependency scopes to ignore */
15-
IgnoreOptional bool /* if optional dependencies should be ignored */
16-
RecursiveSearch bool /* recursive dependency resolution switch */
14+
IgnoreScopes []string /* list of dependency scopes to ignore */
15+
IgnoreOptional bool /* if optional dependencies should be ignored */
16+
IgnoreTransitive bool /* managed dependencies can be often ignored */
17+
RecursiveSearch bool /* recursive dependency resolution switch */
1718
}
1819

1920
type Finder struct {
@@ -76,6 +77,14 @@ func (f *Finder) ResolveDep(dep pom.Dependency) (string, *pom.Project, error) {
7677
}
7778

7879
func (f *Finder) InvalidDep(dep pom.Dependency) bool {
80+
if dep.Transitive {
81+
if f.opts.IgnoreTransitive {
82+
return true
83+
} else if dep.Scope == "none" {
84+
/* Unscoped transitive deps are mostly useless trash. */
85+
return true
86+
}
87+
}
7988
/* Check if the scope matches any of the ignored ones. */
8089
for i := range f.opts.IgnoreScopes {
8190
if dep.Scope == f.opts.IgnoreScopes[i] {

main.go

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,15 @@ import (
1515
var l *log.Logger
1616

1717
var (
18-
workersNum int
19-
requestRetries int
20-
requestTimeout int
21-
reposFile string
22-
ignoreScopes string
23-
ignoreOptional bool
24-
recursive bool
25-
exitCode bool
18+
workersNum int
19+
requestRetries int
20+
requestTimeout int
21+
reposFile string
22+
ignoreScopes string
23+
ignoreOptional bool
24+
ignoreTransitive bool
25+
recursive bool
26+
exitCode bool
2627
)
2728

2829
const helpMessage string = `
@@ -53,6 +54,7 @@ func flagsInit() {
5354
flag.StringVar(&reposFile, "reposFile", "", "Path file with repo URLs to check.")
5455
flag.StringVar(&ignoreScopes, "ignoreScopes", "provided,system,test", "Scopes to ignore.")
5556
flag.BoolVar(&ignoreOptional, "ignoreOptional", true, "Ignore optional dependencies.")
57+
flag.BoolVar(&ignoreTransitive, "ignoreTransitive", false, "Ignore transitive dependencies.")
5658
flag.BoolVar(&exitCode, "exitCode", true, "Set exit code on any resolving failures.")
5759
flag.Parse()
5860
}
@@ -75,9 +77,10 @@ func main() {
7577

7678
/* Controls which dependencies are resolved. */
7779
finderOpts := finder.Options{
78-
IgnoreScopes: strings.Split(ignoreScopes, ","),
79-
IgnoreOptional: ignoreOptional,
80-
RecursiveSearch: recursive,
80+
IgnoreScopes: strings.Split(ignoreScopes, ","),
81+
IgnoreOptional: ignoreOptional,
82+
IgnoreTransitive: ignoreTransitive,
83+
RecursiveSearch: recursive,
8184
}
8285

8386
/* A separate pool of fetcher workers prevents running out of sockets */

pom/dependency.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ type Dependency struct {
1313
Version string `xml:"version"`
1414
Scope string `xml:"scope"`
1515
Optional bool `xml:"optional"`
16+
/* Indirect dependencies */
17+
Transitive bool
1618
}
1719

1820
/* Maven uses a special format for dependency identifiers:
@@ -40,6 +42,9 @@ func (d Dependency) FixFields(parent Project) Dependency {
4042
if d.Version == "${project.version}" {
4143
d.Version = parent.Version
4244
}
45+
if d.Scope == "" {
46+
d.Scope = "none"
47+
}
4348
return d
4449
}
4550

pom/project.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,14 @@ import (
88

99
/* Root object in XML POM files defining packages. */
1010
type Project struct {
11-
GroupId string `xml:"groupId"`
12-
ArtifactId string `xml:"artifactId"`
13-
Name string `xml:"name"`
14-
Version string `xml:"version"`
15-
Parent Dependency `xml:"parent"`
16-
Dependencies []Dependency `xml:"dependencies>dependency"`
17-
Build struct {
11+
GroupId string `xml:"groupId"`
12+
ArtifactId string `xml:"artifactId"`
13+
Name string `xml:"name"`
14+
Version string `xml:"version"`
15+
Parent Dependency `xml:"parent"`
16+
Dependencies []Dependency `xml:"dependencies>dependency"`
17+
DependenciesMgm []Dependency `xml:"dependencyManagement>dependencies>dependency"`
18+
Build struct {
1819
Plugins []Dependency `xml:"plugins>plugin"`
1920
}
2021
}
@@ -51,6 +52,10 @@ func (p Project) GetDependencies() []Dependency {
5152
for _, dep := range p.Dependencies {
5253
deps = append(deps, dep.FixFields(p))
5354
}
55+
for _, dep := range p.DependenciesMgm {
56+
dep.Transitive = true
57+
deps = append(deps, dep.FixFields(p))
58+
}
5459
for _, dep := range p.Build.Plugins {
5560
deps = append(deps, dep.FixFields(p))
5661
}

0 commit comments

Comments
 (0)