From 8f755d8144e8893ad57a8da7fb1fa4d82c46e602 Mon Sep 17 00:00:00 2001 From: Gunnar Wagenknecht Date: Wed, 25 Oct 2023 10:26:45 +0200 Subject: [PATCH] Prevent self-references from runtime classpath --- .../core/model/discovery/JavaAspectsInfo.java | 40 +++++++++---------- ...ProjectPerPackageProvisioningStrategy.java | 2 +- .../ProjectPerTargetProvisioningStrategy.java | 6 +++ .../eclipse/core/util/jar/BazelJarFile.java | 9 ++++- 4 files changed, 34 insertions(+), 23 deletions(-) diff --git a/bundles/com.salesforce.bazel.eclipse.core/src/com/salesforce/bazel/eclipse/core/model/discovery/JavaAspectsInfo.java b/bundles/com.salesforce.bazel.eclipse.core/src/com/salesforce/bazel/eclipse/core/model/discovery/JavaAspectsInfo.java index 9571643e..4202a92c 100644 --- a/bundles/com.salesforce.bazel.eclipse.core/src/com/salesforce/bazel/eclipse/core/model/discovery/JavaAspectsInfo.java +++ b/bundles/com.salesforce.bazel.eclipse.core/src/com/salesforce/bazel/eclipse/core/model/discovery/JavaAspectsInfo.java @@ -16,6 +16,7 @@ import static java.lang.String.format; import static java.nio.file.Files.isReadable; +import static java.util.Objects.requireNonNull; import static org.eclipse.core.runtime.IPath.fromPath; import java.io.IOException; @@ -163,33 +164,30 @@ public JavaAspectsInfo(ParsedBepOutput aspectsBuildResult, BazelWorkspace bazelW var artifactData = fullArtifactData.get(localJar.getRelativePath()); for (String topLevelTarget : artifactData.topLevelTargets) { - var jarLibrary = libraryByJdepsRootRelativePath.get(classJar.getRelativePath()); - if (jarLibrary == null) { var targetLabel = readTargetLabel(localJar); - if (targetLabel != null) { - var builder = LibraryArtifact.builder(); - builder.setClassJar(classJar); - var sourceJar = SourceJarFinder.findSourceJar(classJar); - if (sourceJar != null) { - builder.addSourceJar(sourceJar); - } - - jarLibrary = new BlazeJarLibrary(builder.build(), TargetKey.forPlainTarget(targetLabel)); - addLibrary(jarLibrary); + if (targetLabel == null) { + LOG.warn( + "Unable to compute target label for runtime jar '{}'. Please check if the rule producing the jar is adding the Target-Label to the jar manifest!", + classJar); + continue; + } + + var builder = LibraryArtifact.builder(); + builder.setClassJar(classJar); + var sourceJar = SourceJarFinder.findSourceJar(classJar); + if (sourceJar != null) { + builder.addSourceJar(sourceJar); } - } - if (jarLibrary != null) { - runtimeJarsByToplevelTarget.putIfAbsent(topLevelTarget, new ArrayList<>()); - runtimeJarsByToplevelTarget.get(topLevelTarget).add(jarLibrary); - } else { - LOG.warn( - "Unable to compute target label for runtime jar '{}'. Please check if the rule producing the jar is adding the Target-Label to the jar manifest!", - localJar); + jarLibrary = new BlazeJarLibrary(builder.build(), TargetKey.forPlainTarget(targetLabel)); + addLibrary(jarLibrary); } + runtimeJarsByToplevelTarget.putIfAbsent(topLevelTarget, new ArrayList<>()); + runtimeJarsByToplevelTarget.get(topLevelTarget) + .add(requireNonNull(jarLibrary, "jarLibrary should not be null here")); } } } @@ -238,7 +236,7 @@ private Label readTargetLabel(LocalFileOutputArtifact localJar) { try (var jarFile = new BazelJarFile(localJar.getPath())) { return jarFile.getTargetLabel(); } catch (IOException e) { - LOG.warn("Error inspecting manifest of jar '{}': {}", localJar, e.getMessage()); + LOG.warn("Error inspecting manifest of jar '{}': {}", localJar, e.getMessage(), e); } return null; } diff --git a/bundles/com.salesforce.bazel.eclipse.core/src/com/salesforce/bazel/eclipse/core/model/discovery/ProjectPerPackageProvisioningStrategy.java b/bundles/com.salesforce.bazel.eclipse.core/src/com/salesforce/bazel/eclipse/core/model/discovery/ProjectPerPackageProvisioningStrategy.java index da76e5a9..82369d44 100644 --- a/bundles/com.salesforce.bazel.eclipse.core/src/com/salesforce/bazel/eclipse/core/model/discovery/ProjectPerPackageProvisioningStrategy.java +++ b/bundles/com.salesforce.bazel.eclipse.core/src/com/salesforce/bazel/eclipse/core/model/discovery/ProjectPerPackageProvisioningStrategy.java @@ -173,7 +173,7 @@ public Map> computeClasspaths(Collectio } // remove references to the project represented by the package - // (this can happen because we have tests and none tests in the same package) + // (this can happen because we have tests and none tests in the same package, also the runtime CP self-reference) classpath.removeIf( entry -> (entry.getEntryKind() == IClasspathEntry.CPE_PROJECT) && entry.getPath().equals(bazelProject.getProject().getFullPath())); diff --git a/bundles/com.salesforce.bazel.eclipse.core/src/com/salesforce/bazel/eclipse/core/model/discovery/ProjectPerTargetProvisioningStrategy.java b/bundles/com.salesforce.bazel.eclipse.core/src/com/salesforce/bazel/eclipse/core/model/discovery/ProjectPerTargetProvisioningStrategy.java index a42dcb7a..999fe528 100644 --- a/bundles/com.salesforce.bazel.eclipse.core/src/com/salesforce/bazel/eclipse/core/model/discovery/ProjectPerTargetProvisioningStrategy.java +++ b/bundles/com.salesforce.bazel.eclipse.core/src/com/salesforce/bazel/eclipse/core/model/discovery/ProjectPerTargetProvisioningStrategy.java @@ -136,6 +136,12 @@ public Map> computeClasspaths(Collectio } } + // remove references to the project + // (the runtime classpath will contain a reference to the project) + classpath.removeIf( + entry -> (entry.getEntryKind() == IClasspathEntry.CPE_PROJECT) + && entry.getPath().equals(bazelProject.getProject().getFullPath())); + classpathsByProject.put(bazelProject, classpath); monitor.worked(1); } diff --git a/bundles/com.salesforce.bazel.eclipse.core/src/com/salesforce/bazel/eclipse/core/util/jar/BazelJarFile.java b/bundles/com.salesforce.bazel.eclipse.core/src/com/salesforce/bazel/eclipse/core/util/jar/BazelJarFile.java index c41a9537..92a0c7b7 100644 --- a/bundles/com.salesforce.bazel.eclipse.core/src/com/salesforce/bazel/eclipse/core/util/jar/BazelJarFile.java +++ b/bundles/com.salesforce.bazel.eclipse.core/src/com/salesforce/bazel/eclipse/core/util/jar/BazelJarFile.java @@ -3,6 +3,8 @@ */ package com.salesforce.bazel.eclipse.core.util.jar; +import static java.lang.String.format; + import java.io.IOException; import java.nio.file.Path; import java.util.jar.JarFile; @@ -42,6 +44,11 @@ public Label getTargetLabel() throws IOException { return null; } - return Label.createIfValid(targetLabel); + var error = Label.validate(targetLabel); + if (error != null) { + throw new IOException(format("Invalid Target-Label in jar file: %s", error)); + } + + return Label.create(targetLabel); } }