diff --git a/changelog/@unreleased/pr-736.v2.yml b/changelog/@unreleased/pr-736.v2.yml new file mode 100644 index 000000000..49bb02493 --- /dev/null +++ b/changelog/@unreleased/pr-736.v2.yml @@ -0,0 +1,6 @@ +type: fix +fix: + description: "The `checkImplicitDependencies` task will no longer suggest a fix of the current project." + links: + - https://github.com/palantir/gradle-baseline/pull/736 + - https://github.com/palantir/gradle-baseline/issues/567 diff --git a/gradle-baseline-java/src/main/groovy/com/palantir/baseline/tasks/CheckImplicitDependenciesTask.java b/gradle-baseline-java/src/main/groovy/com/palantir/baseline/tasks/CheckImplicitDependenciesTask.java index 6d5934da8..ab7d885e9 100644 --- a/gradle-baseline-java/src/main/groovy/com/palantir/baseline/tasks/CheckImplicitDependenciesTask.java +++ b/gradle-baseline-java/src/main/groovy/com/palantir/baseline/tasks/CheckImplicitDependenciesTask.java @@ -71,6 +71,7 @@ public final void checkImplicitDependencies() { .map(BaselineExactDependencies.INDEXES::classToDependency) .filter(Optional::isPresent) .map(Optional::get) + .filter(x -> !isArtifactFromCurrentProject(x)) .collect(Collectors.toSet()); Set declaredArtifacts = declaredDependencies.stream() .flatMap(dependency -> dependency.getModuleArtifacts().stream()) @@ -112,6 +113,19 @@ private boolean isProjectArtifact(ResolvedArtifact artifact) { return artifact.getId().getComponentIdentifier() instanceof ProjectComponentIdentifier; } + /** + * Return true if the resolved artifact is derived from a project in the current build rather than an + * external jar. + */ + private boolean isArtifactFromCurrentProject(ResolvedArtifact artifact) { + if (!isProjectArtifact(artifact)) { + return false; + } + return ((ProjectComponentIdentifier) artifact.getId().getComponentIdentifier()).getProjectPath() + .equals(getProject().getPath()); + } + + /** All classes which are mentioned in this project's source code. */ private Set referencedClasses() { return Streams.stream(sourceClasses.get().iterator()) diff --git a/gradle-baseline-java/src/test/groovy/com/palantir/baseline/BaselineExactDependenciesTest.groovy b/gradle-baseline-java/src/test/groovy/com/palantir/baseline/BaselineExactDependenciesTest.groovy index 129854af1..a4d2b4651 100644 --- a/gradle-baseline-java/src/test/groovy/com/palantir/baseline/BaselineExactDependenciesTest.groovy +++ b/gradle-baseline-java/src/test/groovy/com/palantir/baseline/BaselineExactDependenciesTest.groovy @@ -16,11 +16,10 @@ package com.palantir.baseline +import java.nio.file.Files import org.gradle.testkit.runner.BuildResult import org.gradle.testkit.runner.TaskOutcome -import java.nio.file.Files - class BaselineExactDependenciesTest extends AbstractPluginTest { def standardBuildFile = ''' @@ -125,6 +124,15 @@ class BaselineExactDependenciesTest extends AbstractPluginTest { result.output.contains("implementation project(':sub-project-no-deps')") } + def 'checkImplicitDependencies should not report circular dependency on current project'() { + when: + setupMultiProject() + + then: + BuildResult result = with(':sub-project-with-deps:checkImplicitDependencies', ':sub-project-no-deps:checkImplicitDependencies', '--stacktrace').withDebug(true).build() + result.task(':sub-project-no-deps:checkImplicitDependencies').getOutcome() == TaskOutcome.SUCCESS + } + /** * Sets up a multi-module project with 2 sub projects. The root project has a transitive dependency on sub-project-no-deps * and so checkImplicitDependencies should fail on it.