Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(pom): don't remove excluded deps from upper pom's #282

Merged
merged 3 commits into from
Dec 29, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions pkg/java/pom/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -641,6 +641,51 @@ func TestPom_Parse(t *testing.T) {
},
},
},
{
name: "exclusions in child",
inputFile: filepath.Join("testdata", "exclusions-in-child", "pom.xml"),
local: true,
want: []types.Library{
{
ID: "com.example:example:1.0.0",
Name: "com.example:example",
Version: "1.0.0",
},
{
ID: "org.example:example-api:1.7.30",
Name: "org.example:example-api",
Version: "1.7.30",
Indirect: true,
License: "The Apache Software License, Version 2.0",
},
{
ID: "org.example:example-dependency:1.2.3",
Name: "org.example:example-dependency",
Version: "1.2.3",
Indirect: true,
},
{
ID: "org.example:example-exclusions:4.0.0",
Name: "org.example:example-exclusions",
Version: "4.0.0",
},
},
wantDeps: []types.Dependency{
{
ID: "com.example:example:1.0.0",
DependsOn: []string{
"org.example:example-exclusions:4.0.0",
},
},
{
ID: "org.example:example-exclusions:4.0.0",
DependsOn: []string{
"org.example:example-api:1.7.30",
"org.example:example-dependency:1.2.3",
},
},
},
},
{
name: "exclusions with wildcards",
inputFile: filepath.Join("testdata", "wildcard-exclusions", "pom.xml"),
Expand Down
11 changes: 7 additions & 4 deletions pkg/java/pom/pom.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/xml"
"fmt"
"io"
"maps"
DmitriyLewen marked this conversation as resolved.
Show resolved Hide resolved
"reflect"
"strings"

Expand Down Expand Up @@ -253,10 +254,12 @@ func (d pomDependency) Resolve(props map[string]string, depManagement, rootDepMa

// ToArtifact converts dependency to artifact.
// It should be called after calling Resolve() so that variables can be evaluated.
func (d pomDependency) ToArtifact(exclusions map[string]struct{}) artifact {
if exclusions == nil {
exclusions = map[string]struct{}{}
}
func (d pomDependency) ToArtifact(ex map[string]struct{}) artifact {
// To avoid shadow adding exclusions to top pom's,
// we need to initialize a new map for each new artifact
// See `exclusions in child` test for more information
exclusions := map[string]struct{}{}
maps.Copy(exclusions, ex)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about maps.Clone?

Suggested change
exclusions := map[string]struct{}{}
maps.Copy(exclusions, ex)
exclusions := maps.Clone(ex)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like we will have enough of a hallow clone. Thank you!
Changed in cdc2c7f

for _, e := range d.Exclusions.Exclusion {
exclusions[fmt.Sprintf("%s:%s", e.GroupID, e.ArtifactID)] = struct{}{}
}
Expand Down
17 changes: 17 additions & 0 deletions pkg/java/pom/testdata/exclusions-in-child/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.example</groupId>
<artifactId>example</artifactId>
<version>1.0.0</version>

<dependencies>
<dependency>
<groupId>org.example</groupId>
<artifactId>example-exclusions</artifactId>
<version>4.0.0</version>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.example</groupId>
<artifactId>example-exclusions</artifactId>
<version>4.0.0</version>

<dependencies>
<dependency>
<groupId>org.example</groupId>
<artifactId>example-dependency</artifactId>
<version>1.2.3</version>
<exclusions>
<exclusion>
<groupId>org.example</groupId>
<artifactId>example-api</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.example</groupId>
<artifactId>example-api</artifactId>
<version>1.7.30</version>
</dependency>
</dependencies>

</project>