From 553365bd575c918d8c57d17f9dd1821886941752 Mon Sep 17 00:00:00 2001 From: runchen0919 Date: Tue, 23 Dec 2025 09:58:43 +0800 Subject: [PATCH] Correctly classify external dependencies by checking Bazel path --- .../core/model/discovery/JavaAspectsInfo.java | 67 ++++++++++++++++--- 1 file changed, 57 insertions(+), 10 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 569bb8c3..b5ff4ec2 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 @@ -20,6 +20,7 @@ import static org.eclipse.core.runtime.IPath.fromPath; import java.io.IOException; +import java.nio.file.Path; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -169,17 +170,33 @@ public JavaAspectsInfo(ParsedBepOutput aspectsBuildResult, BazelWorkspace bazelW classJar); } targetLabel = Label.create(format("@_unknown_jar_//:%s", classJar.getRelativePath())); - } else if (!targetLabel.isExternal() - && !bazelWorkspace.getBazelPackage(new BazelLabel(targetLabel)).exists()) { - // possibly an external jar produced within an external repo - // see https://github.com/eclipseguru/bazel-eclipse/issues/34 - if (LOG.isDebugEnabled()) { - LOG.debug( - "The target '{}' of the runtime JAR file '{}' does not exist in the workspace.", - targetLabel, - classJar); + } else if (!targetLabel.isExternal()) { + // Check if this is actually an external JAR by examining its physical location + // External dependencies are typically in external/ directory even if their + // Target-Label looks like a local package (e.g., //3rdparty/java) + var isExternalJar = isJarFromExternalRepository(localJar.getPath()); + var shouldTreatAsUnknown = isExternalJar; + + if (!isExternalJar) { + // Not in external directory, verify the package exists + var bazelPackage = bazelWorkspace.getBazelPackage(new BazelLabel(targetLabel)); + if (!bazelPackage.exists()) { + shouldTreatAsUnknown = true; + } + } + + if (shouldTreatAsUnknown) { + // possibly an external jar produced within an external repo + // see https://github.com/eclipseguru/bazel-eclipse/issues/34 + if (LOG.isDebugEnabled()) { + LOG.debug( + "The JAR '{}' with target '{}' is {} in the workspace.", + classJar, + targetLabel, + isExternalJar ? "from external repository" : "not found"); + } + targetLabel = Label.create(format("@_unknown_jar_//:%s", classJar.getRelativePath())); } - targetLabel = Label.create(format("@_unknown_jar_//:%s", classJar.getRelativePath())); } var builder = LibraryArtifact.builder(); @@ -233,6 +250,36 @@ public BlazeJarLibrary getLibraryByJdepsRootRelativePath(String relativePath) { return libraryByJdepsRootRelativePath.get(relativePath); } + /** + * Checks if a JAR file comes from an external Bazel repository. + *

+ * External dependencies (e.g., from Maven) are placed in the {@code external/} directory, even if their + * Target-Label in the manifest looks like a local package (e.g., {@code //3rdparty/java}). This method examines the + * physical file path to determine if the JAR originates from an external repository. + *

+ * + * @param jarPath + * the absolute path to the JAR file + * @return {@code true} if the JAR is from an external repository, {@code false} otherwise + */ + private boolean isJarFromExternalRepository(Path jarPath) { + try { + var executionRoot = getBlazeInfo().getExecutionRoot(); + var relativeToExecRoot = executionRoot.relativize(jarPath); + var pathString = relativeToExecRoot.toString(); + + // External JARs are in paths like: + // - bazel-out//bin/external//... + return pathString.startsWith("bazel-out/") && pathString.contains("/external/"); + } catch (IllegalArgumentException e) { + // Path is not relative to execution root, assume it's external + if (LOG.isDebugEnabled()) { + LOG.debug("JAR path '{}' is not under execution root, treating as external", jarPath); + } + return true; + } + } + public List getRuntimeClasspath(TargetKey targetKey) { if (targetKey.isPlainTarget()) { var outputGroupArtifacts = aspectsBuildResult