Skip to content

Commit

Permalink
Resolve external projects if they exist
Browse files Browse the repository at this point in the history
  • Loading branch information
guw committed Aug 13, 2023
1 parent 2143f6e commit 01a0aee
Showing 1 changed file with 64 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,18 @@

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;
import java.util.List;
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;
Expand Down Expand Up @@ -372,6 +377,10 @@ private BazelWorkspace findExternalWorkspace(Label label) throws CoreException {
return null;
}

IWorkspaceRoot getEclipseWorkspaceRoot() {
return ResourcesPlugin.getWorkspace().getRoot();
}

private List<JdepsDependency> loadJdeps(TargetIdeInfo targetIdeInfo) throws CoreException {
// load jdeps file
var jdepsFile = resolveJdepsOutput(targetIdeInfo);
Expand Down Expand Up @@ -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;
}

}

0 comments on commit 01a0aee

Please sign in to comment.