Skip to content

Commit

Permalink
Prevent self-references from runtime classpath
Browse files Browse the repository at this point in the history
  • Loading branch information
guw committed Oct 25, 2023
1 parent 6648826 commit 8f755d8
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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"));
}
}
}
Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ public Map<BazelProject, Collection<ClasspathEntry>> 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()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,12 @@ public Map<BazelProject, Collection<ClasspathEntry>> 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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}

0 comments on commit 8f755d8

Please sign in to comment.