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

Linkage Checker to throw exception upon missing artifact #2105

Merged
merged 11 commits into from
Jun 18, 2021
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import static com.google.common.collect.ImmutableSet.toImmutableSet;

import com.google.cloud.tools.opensource.dependencies.Bom;
import com.google.cloud.tools.opensource.dependencies.UnresolvableArtifactProblem;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
Expand Down Expand Up @@ -110,6 +111,20 @@ public static LinkageChecker create(Bom bom, Path exclusionFile)
classPathBuilder.resolve(managedDependencies, true, DependencyMediation.MAVEN);
ImmutableList<ClassPathEntry> classpath = classPathResult.getClassPath();

ImmutableList<UnresolvableArtifactProblem> artifactProblems =
classPathResult.getArtifactProblems();
Copy link
Contributor Author

@suztomo suztomo Jun 7, 2021

Choose a reason for hiding this comment

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

The artifact problems were not not reported. For the Cloud SQL case, it was unclear why MaximumLinkageErrorsTest
fails GoogleCloudPlatform/cloud-sql-jdbc-socket-factory#507 (comment) until I ran it with the debugger.

if (!artifactProblems.isEmpty()) {
for (UnresolvableArtifactProblem artifactProblem : artifactProblems) {
logger.severe(artifactProblem.toString());
}
throw new IOException(
"Could not resolve "
+ (artifactProblems.size() == 1
? "1 dependency"
: (artifactProblems.size() + " dependencies"))
+ ". See the message above for details.");
}

// When checking a BOM, entry point classes are the ones in the artifacts listed in the BOM
List<ClassPathEntry> artifactsInBom = classpath.subList(0, managedDependencies.size());
ImmutableSet<ClassPathEntry> entryPoints = ImmutableSet.copyOf(artifactsInBom);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import static org.junit.Assert.fail;

import com.google.cloud.tools.opensource.dependencies.Artifacts;
import com.google.cloud.tools.opensource.dependencies.Bom;
import com.google.cloud.tools.opensource.dependencies.DependencyGraph;
import com.google.cloud.tools.opensource.dependencies.DependencyGraphBuilder;
import com.google.cloud.tools.opensource.dependencies.DependencyPath;
Expand Down Expand Up @@ -1247,4 +1248,27 @@ public void testFindLinkageProblems_shouldNotReportMethodHandleInvoke()

Truth.assertThat(problemsOnMethodHandle).isEmpty();
}

@Test
public void testBomResolutionWithMissingTransitiveDependency() throws Exception {
DefaultArtifact cglib = new DefaultArtifact("cglib:cglib-nodep:jar:2.2");
// cglib-nodep has 2 transitive dependencies, which do not exist in Maven Central
// cglib:cglib-nodep:jar:2.2
// - ant:ant:jar:1.6.2 (compile?)
// - xml-apis:xml-apis:jar:2.6.2 (compile?)
// - xerces:xerces-impl:jar:2.6.2 (compile?)
// https://github.com/GoogleCloudPlatform/cloud-opensource-java/issues/2097
Bom bomWithMissingTransitiveDependency =
new Bom(
"com.google.cloud:bom-with-missing-transitive-dependency:0.1",
ImmutableList.of(cglib));
try {
LinkageChecker.create(bomWithMissingTransitiveDependency);
fail("Linkage Checker should throw IOException");
} catch (IOException expected) {
assertEquals(
"Could not resolve 2 dependencies. See the message above for details.",
expected.getMessage());
}
}
}