Skip to content

Commit e3dc695

Browse files
committed
Correctly classify external dependencies by checking Bazel path
1 parent 9e21eb0 commit e3dc695

1 file changed

Lines changed: 60 additions & 11 deletions

File tree

  • bundles/com.salesforce.bazel.eclipse.core/src/com/salesforce/bazel/eclipse/core/model/discovery

bundles/com.salesforce.bazel.eclipse.core/src/com/salesforce/bazel/eclipse/core/model/discovery/JavaAspectsInfo.java

Lines changed: 60 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.io.IOException;
2323
import java.io.UnsupportedEncodingException;
2424
import java.net.URLDecoder;
25+
import java.nio.file.Path;
2526
import java.util.ArrayList;
2627
import java.util.HashMap;
2728
import java.util.List;
@@ -194,18 +195,36 @@ public JavaAspectsInfo(ParsedBepOutput aspectsBuildResult, BazelWorkspace bazelW
194195
"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!",
195196
classJar);
196197
}
197-
targetLabel = Label.create(format("@_unknown_jar_//:%s", sanitizePathForLabel(classJar.getRelativePath())));
198-
} else if (!targetLabel.isExternal()
199-
&& !bazelWorkspace.getBazelPackage(new BazelLabel(targetLabel)).exists()) {
200-
// possibly an external jar produced within an external repo
201-
// see https://github.com/eclipseguru/bazel-eclipse/issues/34
202-
if (LOG.isDebugEnabled()) {
203-
LOG.debug(
204-
"The target '{}' of the runtime JAR file '{}' does not exist in the workspace.",
205-
targetLabel,
206-
classJar);
198+
targetLabel = Label.create(
199+
format("@_unknown_jar_//:%s", sanitizePathForLabel(classJar.getRelativePath())));
200+
} else if (!targetLabel.isExternal()) {
201+
// Check if this is actually an external JAR by examining its physical location
202+
// External dependencies are typically in external/ directory even if their
203+
// Target-Label looks like a local package (e.g., //3rdparty/java)
204+
var isExternalJar = isJarFromExternalRepository(localJar.getPath());
205+
var shouldTreatAsUnknown = isExternalJar;
206+
207+
if (!isExternalJar) {
208+
// Not in external directory, verify the package exists
209+
var bazelPackage = bazelWorkspace.getBazelPackage(new BazelLabel(targetLabel));
210+
if (!bazelPackage.exists()) {
211+
shouldTreatAsUnknown = true;
212+
}
213+
}
214+
215+
if (shouldTreatAsUnknown) {
216+
// possibly an external jar produced within an external repo
217+
// see https://github.com/eclipseguru/bazel-eclipse/issues/34
218+
if (LOG.isDebugEnabled()) {
219+
LOG.debug(
220+
"The JAR '{}' with target '{}' is {} in the workspace.",
221+
classJar,
222+
targetLabel,
223+
isExternalJar ? "from external repository" : "not found");
224+
}
225+
targetLabel = Label.create(
226+
format("@_unknown_jar_//:%s", sanitizePathForLabel(classJar.getRelativePath())));
207227
}
208-
targetLabel = Label.create(format("@_unknown_jar_//:%s", sanitizePathForLabel(classJar.getRelativePath())));
209228
}
210229

211230
var builder = LibraryArtifact.builder();
@@ -259,6 +278,36 @@ public BlazeJarLibrary getLibraryByJdepsRootRelativePath(String relativePath) {
259278
return libraryByJdepsRootRelativePath.get(relativePath);
260279
}
261280

281+
/**
282+
* Checks if a JAR file comes from an external Bazel repository.
283+
* <p>
284+
* External dependencies (e.g., from Maven) are placed in the {@code external/} directory, even if their
285+
* Target-Label in the manifest looks like a local package (e.g., {@code //3rdparty/java}). This method examines the
286+
* physical file path to determine if the JAR originates from an external repository.
287+
* </p>
288+
*
289+
* @param jarPath
290+
* the absolute path to the JAR file
291+
* @return {@code true} if the JAR is from an external repository, {@code false} otherwise
292+
*/
293+
private boolean isJarFromExternalRepository(Path jarPath) {
294+
try {
295+
var executionRoot = getBlazeInfo().getExecutionRoot();
296+
var relativeToExecRoot = executionRoot.relativize(jarPath);
297+
var pathString = relativeToExecRoot.toString();
298+
299+
// External JARs are in paths like:
300+
// - bazel-out/<config>/bin/external/<repo_name>/...
301+
return pathString.startsWith("bazel-out/") && pathString.contains("/external/");
302+
} catch (IllegalArgumentException e) {
303+
// Path is not relative to execution root, assume it's external
304+
if (LOG.isDebugEnabled()) {
305+
LOG.debug("JAR path '{}' is not under execution root, treating as external", jarPath);
306+
}
307+
return true;
308+
}
309+
}
310+
262311
public List<BlazeJarLibrary> getRuntimeClasspath(TargetKey targetKey) {
263312
if (targetKey.isPlainTarget()) {
264313
var outputGroupArtifacts = aspectsBuildResult

0 commit comments

Comments
 (0)