Skip to content

Commit

Permalink
Removed resolve virtual includes logic
Browse files Browse the repository at this point in the history
This should be redundant because includes are mapped with the -ibazel flag
  • Loading branch information
LeFrosch committed Feb 19, 2024
1 parent 8f39aaa commit 4b74423
Show file tree
Hide file tree
Showing 6 changed files with 9 additions and 288 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import com.google.idea.blaze.base.sync.data.BlazeProjectDataManager;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.registry.Registry;
import java.io.File;
import java.io.IOException;
import javax.annotation.Nullable;
Expand All @@ -48,20 +47,17 @@ public class ExecutionRootPathResolver {
private final File executionRoot;
private final File outputBase;
private final WorkspacePathResolver workspacePathResolver;
private final TargetMap targetMap;

public ExecutionRootPathResolver(
BuildSystemProvider buildSystemProvider,
WorkspaceRoot workspaceRoot,
File executionRoot,
File outputBase,
WorkspacePathResolver workspacePathResolver,
TargetMap targetMap) {
WorkspacePathResolver workspacePathResolver) {
this.buildArtifactDirectories = buildArtifactDirectories(buildSystemProvider, workspaceRoot);
this.executionRoot = executionRoot;
this.outputBase = outputBase;
this.workspacePathResolver = workspacePathResolver;
this.targetMap = targetMap;
}

@Nullable
Expand All @@ -76,8 +72,7 @@ public static ExecutionRootPathResolver fromProject(Project project) {
WorkspaceRoot.fromProject(project),
projectData.getBlazeInfo().getExecutionRoot(),
projectData.getBlazeInfo().getOutputBase(),
projectData.getWorkspacePathResolver(),
projectData.getTargetMap());
projectData.getWorkspacePathResolver());
}

private static ImmutableList<String> buildArtifactDirectories(
Expand Down Expand Up @@ -114,25 +109,7 @@ public ImmutableList<File> resolveToIncludeDirectories(ExecutionRootPath path) {
}
String firstPathComponent = getFirstPathComponent(path.getAbsoluteOrRelativeFile().getPath());
if (buildArtifactDirectories.contains(firstPathComponent)) {
// Build artifacts accumulate under the execution root, independent of symlink settings

if(Registry.is("bazel.sync.resolve.virtual.includes") &&
VirtualIncludesHandler.containsVirtualInclude(path)) {
// Resolve virtual_include from execution root either to local or external workspace for correct code insight
ImmutableList<File> resolved = ImmutableList.of();
try {
resolved = VirtualIncludesHandler.resolveVirtualInclude(path, outputBase,
workspacePathResolver, targetMap);
} catch (Throwable throwable) {
LOG.error("Failed to resolve virtual includes for " + path, throwable);
}

return resolved.isEmpty()
? ImmutableList.of(path.getFileRootedAt(executionRoot))
: resolved;
} else {
return ImmutableList.of(path.getFileRootedAt(executionRoot));
}
return ImmutableList.of(path.getFileRootedAt(executionRoot));
}
if (firstPathComponent.equals(externalPrefix)) { // In external workspace
// External workspaces accumulate under the output base.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,22 @@

import com.google.common.base.Stopwatch;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import com.google.idea.blaze.base.ideinfo.ArtifactLocation;
import com.google.idea.blaze.base.ideinfo.CIdeInfo;
import com.google.idea.blaze.base.ideinfo.Dependency;
import com.google.idea.blaze.base.ideinfo.TargetIdeInfo;
import com.google.idea.blaze.base.ideinfo.TargetKey;
import com.google.idea.blaze.base.ideinfo.TargetMap;
import com.google.idea.blaze.base.model.BlazeProjectData;
import com.google.idea.blaze.base.model.primitives.ExecutionRootPath;
import com.google.idea.blaze.base.model.primitives.Label;
import com.google.idea.blaze.base.model.primitives.TargetName;
import com.google.idea.blaze.base.model.primitives.WorkspacePath;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.progress.ProgressIndicator;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.util.registry.Registry;
import java.io.File;
import java.nio.file.InvalidPathException;
import java.nio.file.Path;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.Stack;
import java.util.concurrent.TimeUnit;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
Expand All @@ -44,131 +34,8 @@
* {@code bazel-out/.../bin/.../_virtual_includes/...}
*/
public class VirtualIncludesHandler {
private static final Logger LOG = Logger.getInstance(VirtualIncludesHandler.class);
final static Path VIRTUAL_INCLUDES_DIRECTORY = Path.of("_virtual_includes");
private static final int EXTERNAL_DIRECTORY_IDX = 3;
private static final int EXTERNAL_WORKSPACE_NAME_IDX = 4;
private static final int WORKSPACE_PATH_START_FOR_EXTERNAL_WORKSPACE = 5;
private static final int WORKSPACE_PATH_START_FOR_LOCAL_WORKSPACE = 3;
private static final String ABSOLUTE_LABEL_PREFIX = "//";
private static final int ABSOLUTE_LABEL_PREFIX_LENGTH = ABSOLUTE_LABEL_PREFIX.length();

private static List<Path> splitExecutionPath(ExecutionRootPath executionRootPath) {
return Lists.newArrayList(executionRootPath.getAbsoluteOrRelativeFile().toPath().iterator());
}

static boolean containsVirtualInclude(ExecutionRootPath executionRootPath) {
return splitExecutionPath(executionRootPath).contains(VIRTUAL_INCLUDES_DIRECTORY);
}

/**
* Resolves execution root path to {@code _virtual_includes} directory to the matching workspace location
*
* @return list of resolved paths if required information is obtained from execution root path and target data
* or empty list if resolution has failed
*/
@NotNull
static ImmutableList<File> resolveVirtualInclude(ExecutionRootPath executionRootPath,
File externalWorkspacePath,
WorkspacePathResolver workspacePathResolver,
TargetMap targetMap)
{
TargetKey key = null;
try {
key = guessTargetKey(executionRootPath);
} catch (IndexOutOfBoundsException exception) {
// report to intellij EA
LOG.error(
"Failed to detect target from execution root path: " + executionRootPath,
exception);
}

if (key == null) {
return ImmutableList.of();
}

TargetIdeInfo info = targetMap.get(key);
if (info == null) {
return ImmutableList.of();
}

CIdeInfo cIdeInfo = info.getcIdeInfo();
if (cIdeInfo == null) {
return ImmutableList.of();
}

if (!info.getcIdeInfo().getIncludePrefix().isEmpty()) {
LOG.debug(
"_virtual_includes cannot be handled for targets with include_prefix attribute");
return ImmutableList.of();
}

String stripPrefix = info.getcIdeInfo().getStripIncludePrefix();
if (!stripPrefix.isEmpty()) {
if (stripPrefix.endsWith("/")) {
stripPrefix = stripPrefix.substring(0, stripPrefix.length() - 1);
}
String externalWorkspaceName = key.getLabel().externalWorkspaceName();
WorkspacePath stripPrefixWorkspacePath = stripPrefix.startsWith(ABSOLUTE_LABEL_PREFIX) ?
new WorkspacePath(stripPrefix.substring(ABSOLUTE_LABEL_PREFIX_LENGTH)) :
new WorkspacePath(key.getLabel().blazePackage(), stripPrefix);
if (key.getLabel().externalWorkspaceName() != null) {
ExecutionRootPath external = new ExecutionRootPath(
ExecutionRootPathResolver.externalPath.toPath()
.resolve(externalWorkspaceName)
.resolve(stripPrefixWorkspacePath.toString()).toString());

return ImmutableList.of(external.getFileRootedAt(externalWorkspacePath));
} else {
return workspacePathResolver.resolveToIncludeDirectories(
stripPrefixWorkspacePath);
}
} else {
return ImmutableList.of();
}

}

/**
* @throws IndexOutOfBoundsException if executionRootPath has _virtual_includes but
* its content is unexpected
*/
@Nullable
private static TargetKey guessTargetKey(ExecutionRootPath executionRootPath) {
List<Path> split = splitExecutionPath(executionRootPath);
int virtualIncludesIdx = split.indexOf(VIRTUAL_INCLUDES_DIRECTORY);

if (virtualIncludesIdx > -1) {
String externalWorkspaceName =
split.get(EXTERNAL_DIRECTORY_IDX)
.equals(ExecutionRootPathResolver.externalPath.toPath())
? split.get(EXTERNAL_WORKSPACE_NAME_IDX).toString()
: null;

int workspacePathStart = externalWorkspaceName != null
? WORKSPACE_PATH_START_FOR_EXTERNAL_WORKSPACE
: WORKSPACE_PATH_START_FOR_LOCAL_WORKSPACE;

List<Path> workspacePaths = (workspacePathStart <= virtualIncludesIdx)
? split.subList(workspacePathStart, virtualIncludesIdx)
: Collections.emptyList();

String workspacePathString =
FileUtil.toSystemIndependentName(
workspacePaths.stream().reduce(Path.of(""), Path::resolve).toString());

TargetName target = TargetName.create(split.get(virtualIncludesIdx + 1).toString());
WorkspacePath workspacePath = WorkspacePath.createIfValid(workspacePathString);
if (workspacePath == null) {
return null;
}

return TargetKey.forPlainTarget(
Label.create(externalWorkspaceName, workspacePath, target));
} else {
return null;
}
}
private static final Logger LOG = Logger.getInstance(VirtualIncludesHandler.class);
private static final String ABSOLUTE_LABEL_PREFIX = "//";

private static Path trimStart(Path value, @Nullable Path prefix) {
if (prefix == null || !value.startsWith(prefix)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,33 +108,6 @@ private static TargetIdeInfo getTargetIdeInfo(TargetName targetName) {
.build();
}

@NotNull
private static TargetMap getTargetMap() {
ImmutableMap.Builder<TargetKey, TargetIdeInfo> builder = new ImmutableMap.Builder<>();

for (String workspaceName : WORKSPACES) {
for (WorkspacePath workspacePath : WORKSPACE_PATHS) {
for (TargetName targetName : TARGET_NAMES) {
builder.put(
TargetKey.forPlainTarget(Label.create(workspaceName, workspacePath, targetName)),
getTargetIdeInfo(targetName));
}
}

builder.put(
TargetKey.forPlainTarget(
Label.create(workspaceName, ABSOLUTE_STRIP_PREFIX_WORKSPACE_PATH, TARGET_WITH_ABSOLUTE_STRIP_PREFIX)),
getTargetIdeInfo(TARGET_WITH_ABSOLUTE_STRIP_PREFIX));
}

builder.put(
TargetKey.forPlainTarget(
Label.create(WORKSPACES.get(0), WORKSPACE_PATHS.get(0), TARGET_WITH_INCLUDE_PREFIX)),
getTargetIdeInfo(TARGET_WITH_INCLUDE_PREFIX));

return new TargetMap(builder.build());
}

@Override
protected void initTest(Container applicationServices, Container projectServices) {
Registry.get("bazel.sync.resolve.virtual.includes").setValue(true);
Expand All @@ -145,8 +118,7 @@ protected void initTest(Container applicationServices, Container projectServices
WORKSPACE_ROOT,
new File(EXECUTION_ROOT),
new File(OUTPUT_BASE),
new WorkspacePathResolverImpl(WORKSPACE_ROOT),
getTargetMap());
new WorkspacePathResolverImpl(WORKSPACE_ROOT));
}

@Test
Expand Down Expand Up @@ -189,98 +161,6 @@ public void testIllegalWorkspacePaths() {
assertThat(files).isEmpty();
}

@Test
public void testVirtualIncludes() {
for (String workspaceName : WORKSPACES) {
for (WorkspacePath workspacePath : WORKSPACE_PATHS) {
for (TargetName targetName : TARGET_NAMES) {
String workspaceNameString = workspaceName != null ? workspaceName : "";

ExecutionRootPath generatedPath = new ExecutionRootPath(Path.of(
"bazel-out/k8-fastbuild/bin",
(workspaceName == null ? "" : ExecutionRootPathResolver.externalPath.getPath()),
workspaceNameString,
workspacePath.toString(),
VirtualIncludesHandler.VIRTUAL_INCLUDES_DIRECTORY.toString(),
targetName.toString()).toFile());

ImmutableList<File> files =
pathResolver.resolveToIncludeDirectories(generatedPath);

String stripPrefix = getStripPrefix(targetName);
if (stripPrefix.endsWith("/")) {
stripPrefix = stripPrefix.substring(0, stripPrefix.length() - 1);
}
String expectedPath = Path.of(workspaceNameString,
workspacePath.toString(),
stripPrefix).toString();

if (workspaceName != null) {
// check external workspace
assertThat(files).containsExactly(
Path.of(OUTPUT_BASE, ExecutionRootPathResolver.externalPath.getPath(), expectedPath)
.toFile());
} else {
// check local
assertThat(files).containsExactly(
WORKSPACE_ROOT.fileForPath(new WorkspacePath(expectedPath)));
}
}
}
}
}

@Test
public void testVirtualIncludesWithAbsoluteStripPrefix() {
for (String workspaceName : WORKSPACES) {
String workspaceNameString = workspaceName != null ? workspaceName : "";

ExecutionRootPath generatedPath = new ExecutionRootPath(Path.of(
"bazel-out/k8-fastbuild/bin",
(workspaceName == null ? "" : ExecutionRootPathResolver.externalPath.getPath()),
workspaceNameString,
ABSOLUTE_STRIP_PREFIX_WORKSPACE_PATH.toString(),
VirtualIncludesHandler.VIRTUAL_INCLUDES_DIRECTORY.toString(),
TARGET_WITH_ABSOLUTE_STRIP_PREFIX.toString()).toFile());

ImmutableList<File> files =
pathResolver.resolveToIncludeDirectories(generatedPath);

String expectedPath = Path.of(workspaceNameString,
ABSOLUTE_STRIP_PREFIX_PATH).toString();

if (workspaceName != null) {
// check external workspace
assertThat(files).containsExactly(
Path.of(OUTPUT_BASE, ExecutionRootPathResolver.externalPath.getPath(), expectedPath)
.toFile());
} else {
// check local
assertThat(files).containsExactly(
WORKSPACE_ROOT.fileForPath(new WorkspacePath(expectedPath)));
}
}
}

@Test
public void testVirtualIncludesWithIncludePrefix() {
File fileToBeResolved = Path.of(
"bazel-out/k8-fastbuild/bin",
WORKSPACE_PATHS.get(0).toString(),
VirtualIncludesHandler.VIRTUAL_INCLUDES_DIRECTORY.toString(),
TARGET_WITH_INCLUDE_PREFIX.toString(),
INCLUDE_PREFIX).toFile();

ExecutionRootPath generatedPath = new ExecutionRootPath(fileToBeResolved);

ImmutableList<File> files =
pathResolver.resolveToIncludeDirectories(generatedPath);

// check that include path for target with "include_prefix" attribute is resolved to execution root
assertThat(files).containsExactly(
new File(EXECUTION_ROOT, fileToBeResolved.toString()));
}

@Test
public void testExternalWorkspaceSymlinkToProject() throws IOException {
Path expectedPath = Path.of(WORKSPACE_ROOT.toString(), "guava", "src");
Expand Down
3 changes: 1 addition & 2 deletions cpp/src/com/google/idea/blaze/cpp/BlazeCWorkspace.java
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,7 @@ private OCWorkspaceImpl.ModifiableModel calculateConfigurations(
workspaceRoot,
blazeProjectData.getBlazeInfo().getExecutionRoot(),
blazeProjectData.getBlazeInfo().getOutputBase(),
blazeProjectData.getWorkspacePathResolver(),
blazeProjectData.getTargetMap());
blazeProjectData.getWorkspacePathResolver());

int progress = 0;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,7 @@ public BlazeConfigurationResolverResult update(
WorkspaceRoot.fromProject(project),
blazeProjectData.getBlazeInfo().getExecutionRoot(),
blazeProjectData.getBlazeInfo().getOutputBase(),
blazeProjectData.getWorkspacePathResolver(),
blazeProjectData.getTargetMap());
blazeProjectData.getWorkspacePathResolver());
ImmutableMap<TargetKey, CToolchainIdeInfo> toolchainLookupMap =
BlazeConfigurationToolchainResolver.buildToolchainLookupMap(
context, blazeProjectData.getTargetMap());
Expand Down
Loading

0 comments on commit 4b74423

Please sign in to comment.