From ba34ae1351304c62f4a111e9182ff1ffc2e2c689 Mon Sep 17 00:00:00 2001 From: Eric Sword Date: Mon, 5 Aug 2019 14:32:09 -0400 Subject: [PATCH 1/3] detect if dependency is actually the current project. Fixes https://github.com/palantir/gradle-baseline/issues/567 --- .../tasks/CheckImplicitDependenciesTask.java | 14 ++++++++++++++ .../baseline/BaselineExactDependenciesTest.groovy | 12 ++++++++++-- 2 files changed, 24 insertions(+), 2 deletions(-) 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. From 75f9556f3396518e09f3571b805b009770f2b777 Mon Sep 17 00:00:00 2001 From: Eric Sword Date: Mon, 5 Aug 2019 15:23:18 -0400 Subject: [PATCH 2/3] changelog --- changelog/@unreleased/pr-736.v2.yml | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 changelog/@unreleased/pr-736.v2.yml diff --git a/changelog/@unreleased/pr-736.v2.yml b/changelog/@unreleased/pr-736.v2.yml new file mode 100644 index 000000000..cfac50b10 --- /dev/null +++ b/changelog/@unreleased/pr-736.v2.yml @@ -0,0 +1,6 @@ +type: fix +fix: + description: detect if dependency is actually the current project + links: + - https://github.com/palantir/gradle-baseline/pull/736 + - https://github.com/palantir/gradle-baseline/issues/567 From a8cf3bd0f888b14ba40e5482ef44e405da6085de Mon Sep 17 00:00:00 2001 From: iamdanfox Date: Tue, 6 Aug 2019 12:10:27 +0100 Subject: [PATCH 3/3] Update pr-736.v2.yml --- changelog/@unreleased/pr-736.v2.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changelog/@unreleased/pr-736.v2.yml b/changelog/@unreleased/pr-736.v2.yml index cfac50b10..49bb02493 100644 --- a/changelog/@unreleased/pr-736.v2.yml +++ b/changelog/@unreleased/pr-736.v2.yml @@ -1,6 +1,6 @@ type: fix fix: - description: detect if dependency is actually the current project + 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