diff --git a/bundles/com.salesforce.bazel.eclipse.core/src/com/salesforce/bazel/eclipse/core/model/discovery/JavaAspectsClasspathInfo.java b/bundles/com.salesforce.bazel.eclipse.core/src/com/salesforce/bazel/eclipse/core/model/discovery/JavaAspectsClasspathInfo.java index f4fb589a..9673ddc0 100644 --- a/bundles/com.salesforce.bazel.eclipse.core/src/com/salesforce/bazel/eclipse/core/model/discovery/JavaAspectsClasspathInfo.java +++ b/bundles/com.salesforce.bazel.eclipse.core/src/com/salesforce/bazel/eclipse/core/model/discovery/JavaAspectsClasspathInfo.java @@ -20,6 +20,8 @@ import java.io.IOException; import java.io.InputStream; +import java.net.URI; +import java.net.URISyntaxException; import java.util.Collection; import java.util.LinkedHashMap; import java.util.LinkedHashSet; @@ -27,6 +29,9 @@ import java.util.Map; import java.util.Set; +import org.eclipse.core.resources.IResource; +import org.eclipse.core.resources.IWorkspaceRoot; +import org.eclipse.core.resources.ResourcesPlugin; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IPath; import org.eclipse.core.runtime.Path; @@ -372,6 +377,10 @@ private BazelWorkspace findExternalWorkspace(Label label) throws CoreException { return null; } + IWorkspaceRoot getEclipseWorkspaceRoot() { + return ResourcesPlugin.getWorkspace().getRoot(); + } + private List loadJdeps(TargetIdeInfo targetIdeInfo) throws CoreException { // load jdeps file var jdepsFile = resolveJdepsOutput(targetIdeInfo); @@ -438,34 +447,67 @@ private ClasspathEntry resolveLibrary(BlazeJarLibrary library) throws CoreExcept } protected ClasspathEntry resolveProject(TargetKey targetKey) throws CoreException { - if (targetKey.isPlainTarget()) { - var workspace = bazelWorkspace; - if (targetKey.getLabel().isExternal()) { - workspace = findExternalWorkspace(targetKey.getLabel()); - if (workspace == null) { - return null; + if (!targetKey.isPlainTarget()) { + return null; + } + + var workspace = bazelWorkspace; + + // check for project mapping (it trumps everything) + var projectMapping = workspace.getBazelProjectView().projectMappings().get(targetKey.getLabel().toString()); + if (null != projectMapping) { + try { + var path = new URI(projectMapping).getPath(); + LOG.debug( + "Discovered project mapping for target '{}': {} (path '{}')", + targetKey.getLabel(), + projectMapping, + path); + if (path != null) { + var member = getEclipseWorkspaceRoot().findMember(IPath.forPosix(path)); + if ((member != null) && (member.getType() == IResource.PROJECT)) { + if (LOG.isDebugEnabled()) { + LOG.debug("Found project reference for '{}': {}", targetKey, member.getProject()); + } + return ClasspathEntry.newProjectEntry(member.getProject()); + } } + } catch (URISyntaxException e) { + LOG.error( + "Unparsable project mapping for target '{}': {} (check project view of '{}')", + targetKey.getLabel(), + projectMapping, + workspace, + e); } - var bazelPackage = workspace.getBazelPackage(forPosix(targetKey.getLabel().blazePackage().relativePath())); - var bazelTarget = bazelPackage.getBazelTarget(targetKey.getLabel().targetName().toString()); - if (bazelTarget.hasBazelProject() && bazelTarget.getBazelProject().getProject().isAccessible()) { - // a direct target match is preferred - return newProjectReference(targetKey, bazelTarget.getBazelProject()); + } + + if (targetKey.getLabel().isExternal()) { + workspace = findExternalWorkspace(targetKey.getLabel()); + if (workspace == null) { + return null; } - if (bazelPackage.hasBazelProject() && bazelPackage.getBazelProject().getProject().isAccessible()) { - // we have to check the target name is part of the enabled project list - // otherwise it might be a special jar by some generator target we don't support for import - var targetName = targetKey.getLabel().targetName().toString(); - if (bazelPackage.getBazelProject() - .getBazelTargets() - .stream() - .anyMatch(t -> t.getTargetName().equals(targetName))) { - return newProjectReference(targetKey, bazelPackage.getBazelProject()); - } + } + var bazelPackage = workspace.getBazelPackage(forPosix(targetKey.getLabel().blazePackage().relativePath())); + var bazelTarget = bazelPackage.getBazelTarget(targetKey.getLabel().targetName().toString()); + if (bazelTarget.hasBazelProject() && bazelTarget.getBazelProject().getProject().isAccessible()) { + // a direct target match is preferred + return newProjectReference(targetKey, bazelTarget.getBazelProject()); + } + if (bazelPackage.hasBazelProject() && bazelPackage.getBazelProject().getProject().isAccessible()) { + // we have to check the target name is part of the enabled project list + // otherwise it might be a special jar by some generator target we don't support for import + var targetName = targetKey.getLabel().targetName().toString(); + if (bazelPackage.getBazelProject() + .getBazelTargets() + .stream() + .anyMatch(t -> t.getTargetName().equals(targetName))) { + return newProjectReference(targetKey, bazelPackage.getBazelProject()); } } - return null; + // nothing found + return null; } }