|
22 | 22 | import java.io.IOException; |
23 | 23 | import java.io.UnsupportedEncodingException; |
24 | 24 | import java.net.URLDecoder; |
| 25 | +import java.nio.file.Path; |
25 | 26 | import java.util.ArrayList; |
26 | 27 | import java.util.HashMap; |
27 | 28 | import java.util.List; |
@@ -194,18 +195,36 @@ public JavaAspectsInfo(ParsedBepOutput aspectsBuildResult, BazelWorkspace bazelW |
194 | 195 | "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!", |
195 | 196 | classJar); |
196 | 197 | } |
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()))); |
207 | 227 | } |
208 | | - targetLabel = Label.create(format("@_unknown_jar_//:%s", sanitizePathForLabel(classJar.getRelativePath()))); |
209 | 228 | } |
210 | 229 |
|
211 | 230 | var builder = LibraryArtifact.builder(); |
@@ -259,6 +278,36 @@ public BlazeJarLibrary getLibraryByJdepsRootRelativePath(String relativePath) { |
259 | 278 | return libraryByJdepsRootRelativePath.get(relativePath); |
260 | 279 | } |
261 | 280 |
|
| 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 | + |
262 | 311 | public List<BlazeJarLibrary> getRuntimeClasspath(TargetKey targetKey) { |
263 | 312 | if (targetKey.isPlainTarget()) { |
264 | 313 | var outputGroupArtifacts = aspectsBuildResult |
|
0 commit comments