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 inclusion/exclusion inconsistency #342

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -555,13 +555,13 @@ public void resolveDependencies() {
}

for (ArtifactCoords coords : getProjectArtifacts()) {
if (isIncluded(coords) || !isExcluded(coords)) {
if (isIncluded(coords)) {
processRootArtifact(coords);
}
}

for (ArtifactCoords coords : toSortedCoords(config.getIncludeArtifacts())) {
if (isIncluded(coords) || !isExcluded(coords)) {
if (isIncluded(coords)) {
processRootArtifact(coords);
}
}
Expand All @@ -575,6 +575,17 @@ public void resolveDependencies() {
}
}

/**
* An artefact is included if it is in the inclusion list or it is not in the
* exclusion list. Inclusion beats exclusion.
*
* @param coords coords
* @return included
*/
private boolean isIncluded(ArtifactCoords coords) {
return isExplicitlyIncluded(coords) || !isExplicitlyExcluded(coords);
}

private static List<ArtifactCoords> toSortedCoords(Collection<ArtifactCoords> col) {
if (col.isEmpty()) {
return List.of();
Expand Down Expand Up @@ -644,9 +655,9 @@ protected Iterable<ArtifactCoords> getProjectArtifacts() {
for (ArtifactCoords d : projectBomConstraints) {
final boolean collect;
if (includeSet.isEmpty()) {
collect = d.getGroupId().startsWith(config.getProjectBom().getGroupId()) && !isExcluded(d);
collect = d.getGroupId().startsWith(config.getProjectBom().getGroupId()) && !isExplicitlyExcluded(d);
} else {
collect = !isExcluded(d) && isIncluded(d);
collect = !isExplicitlyExcluded(d) && isExplicitlyIncluded(d);
Copy link
Author

Choose a reason for hiding this comment

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

why do we check if the projectBomConstraint is explicitly included in this case? When discovering top level artefacts we check if they are included or not excluded.

}
if (collect) {
result.add(d);
Expand Down Expand Up @@ -752,7 +763,7 @@ private void processRootArtifact(ArtifactCoords rootArtifact) {
}
for (DependencyNode d : root.getChildren()) {
if (d.getDependency().isOptional()
&& !(config.isIncludeOptionalDeps() || isIncluded(toCoords(d.getArtifact())))) {
&& !(config.isIncludeOptionalDeps() || isExplicitlyIncluded(toCoords(d.getArtifact())))) {
Copy link
Author

@robobario robobario Aug 27, 2024

Choose a reason for hiding this comment

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

why do we only consult the include list when deciding if we should recurse into optional deps?

continue;
}
processNodes(d, 1, false);
Expand Down Expand Up @@ -1238,7 +1249,7 @@ private void processNodes(DependencyNode node, int level, boolean remaining) {
if (winner != null) {
final ArtifactCoords coords = toCoords(winner.getArtifact());
// linked dependencies should also be checked for exclusions
if (!isExcluded(coords)) {
if (!isExplicitlyExcluded(coords)) {
Copy link
Author

Choose a reason for hiding this comment

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

should the inclusion list be considered here too?

for (DependencyTreeVisitor v : treeVisitors) {
v.linkDependency(coords);
}
Expand All @@ -1247,7 +1258,7 @@ private void processNodes(DependencyNode node, int level, boolean remaining) {
}

final ArtifactCoords coords = toCoords(node.getArtifact());
if (isExcluded(coords)) {
if (!isIncluded(coords)) {
Copy link
Author

Choose a reason for hiding this comment

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

this is the actual logic change, checking if the coords are in the inclusion list or not in the exclusion list.

return;
}
ResolvedDependency visit = null;
Expand Down Expand Up @@ -1297,7 +1308,7 @@ private ResolvedDependency addArtifactToBuild(ArtifactCoords coords, List<Remote

if (managed
|| config.isIncludeNonManaged()
|| isIncluded(coords)
|| isExplicitlyIncluded(coords)
|| coords.getType().equals(ArtifactCoords.TYPE_POM)
&& (!config.isExcludeParentPoms()
|| projectGavs.contains(toGav(coords)))) {
Expand Down Expand Up @@ -1363,7 +1374,7 @@ private Map<String, String> addImportedBomsAndParentPomToBuild(ResolvedDependenc
if (parentVersion != null) {
final ArtifactCoords parentPomCoords = ArtifactCoords.pom(parent.getGroupId(), parent.getArtifactId(),
parentVersion);
if (!isExcluded(parentPomCoords)) {
if (!isExplicitlyExcluded(parentPomCoords)) {
final ResolvedDependency resolvedParent = addArtifactToBuild(parentPomCoords, dependency.getRepositories());
artDep.setParentPom(getOrCreateArtifactDep(resolvedParent));
parentPomProps = addImportedBomsAndParentPomToBuild(resolvedParent);
Expand Down Expand Up @@ -1416,7 +1427,7 @@ private void addImportedBomsToBuild(ArtifactDependency pomArtDep, Model model, M
continue;
}
final ArtifactCoords bomCoords = ArtifactCoords.pom(groupId, artifactId, version);
if (!isExcluded(bomCoords)) {
if (!isExplicitlyExcluded(bomCoords)) {
final ResolvedDependency resolvedImport = addArtifactToBuild(bomCoords,
pomArtDep.resolved.getRepositories());
if (resolvedImport != null) {
Expand Down Expand Up @@ -1448,7 +1459,7 @@ private void addToRemaining(ArtifactCoords coords) {
}
}

private boolean isExcluded(ArtifactCoords coords) {
private boolean isExplicitlyExcluded(ArtifactCoords coords) {
for (ArtifactCoordsPattern pattern : excludeSet) {
if (pattern.matches(coords)) {
return true;
Expand All @@ -1457,7 +1468,7 @@ private boolean isExcluded(ArtifactCoords coords) {
return false;
}

private boolean isIncluded(ArtifactCoords coords) {
private boolean isExplicitlyIncluded(ArtifactCoords coords) {
for (ArtifactCoordsPattern pattern : includeSet) {
if (pattern.matches(coords)) {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,24 @@ public void singleJarExcludeDependencyKey() throws Exception {
assertThat(release.getArtifacts()).containsKey(ArtifactCoords.jar("org.acme", "acme-library", "1.0"));
}

@Test
public void singleJarInclusionBeatsExclusion() throws Exception {

var rc = getReleaseCollection(TestUtils.getResource("projects/multimodule-with-bom"),
List.of(ArtifactCoords.jar("org.acme", "acme-library", "1.0")),
config -> config.setExcludeKeys(List.of(ArtifactKey.ga("org.acme", "acme-api")))
.setIncludeKeys(List.of(ArtifactKey.ga("org.acme", "acme-api"))));

assertThat(rc.isEmpty()).isFalse();
assertThat(rc.size()).isEqualTo(1);
var release = rc.iterator().next();
assertThat(release.artifacts).hasSize(4);
assertThat(release.getArtifacts()).containsKey(ArtifactCoords.pom("org.acme", "acme-parent", "1.0"));
assertThat(release.getArtifacts()).containsKey(ArtifactCoords.pom("org.acme", "acme-bom", "1.0"));
assertThat(release.getArtifacts()).containsKey(ArtifactCoords.jar("org.acme", "acme-api", "1.0"));
assertThat(release.getArtifacts()).containsKey(ArtifactCoords.jar("org.acme", "acme-library", "1.0"));
}

@Test
public void singleJarArtifactIncludingParentPomsAndBoms() throws Exception {

Expand Down