Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve performance when resolving the workspace root #6820

Open
wants to merge 1 commit into
base: google
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@
package com.google.idea.blaze.base.sync.workspace;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.ImmutableMap;
import com.google.idea.blaze.base.bazel.BuildSystemProvider;
import com.google.idea.blaze.base.io.FileOperationProvider;
import com.google.idea.blaze.base.model.BlazeProjectData;
import com.google.idea.blaze.base.model.primitives.Label;
import com.google.idea.blaze.base.model.primitives.TargetName;
Expand All @@ -27,20 +25,27 @@
import com.google.idea.blaze.base.settings.Blaze;
import com.google.idea.blaze.base.settings.BuildSystemName;
import com.google.idea.blaze.base.sync.SyncCache;
import com.google.idea.blaze.base.sync.data.BlazeProjectDataManager;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.progress.ProgressManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.io.FileUtil;
import java.io.File;
import java.util.Arrays;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Map;
import java.util.Map.Entry;
import java.util.stream.Collectors;
import java.util.concurrent.ConcurrentHashMap;
import javax.annotation.Nullable;

/** External-workspace-aware resolution of workspace paths. */
public class WorkspaceHelper {

private static BlazeProjectData blazeProjectData;
private static final Logger logger = Logger.getInstance(WorkspaceHelper.class);

private static class Workspace {

private final WorkspaceRoot root;
@Nullable private final String externalWorkspaceName;

Expand All @@ -50,25 +55,29 @@ private Workspace(WorkspaceRoot root, @Nullable String externalWorkspaceName) {
}
}

private static synchronized BlazeProjectData getBlazeProjectData(Project project) {
if (blazeProjectData == null) {
blazeProjectData = BlazeProjectDataManager.getInstance(project).getBlazeProjectData();
}
return blazeProjectData;
}

@Nullable
public static WorkspaceRoot resolveExternalWorkspace(Project project, String workspaceName) {
Map<String, WorkspaceRoot> externalWorkspaces = getExternalWorkspaceRoots(project);
return externalWorkspaces != null ? externalWorkspaces.get(workspaceName) : null;
return getExternalWorkspaceRootsFile(workspaceName, project);
}

/** Resolves the parent blaze package corresponding to this label. */
@Nullable
public static File resolveBlazePackage(Project project, Label label) {
logger.debug("resolveBlazePackage: " + label + " in project " + project.getName());
if (!label.isExternal()) {
WorkspacePathResolver pathResolver =
WorkspacePathResolverProvider.getInstance(project).getPathResolver();
return pathResolver != null ? pathResolver.resolveToFile(label.blazePackage()) : null;
}
Map<String, WorkspaceRoot> externalWorkspaces = getExternalWorkspaceRoots(project);
if (externalWorkspaces == null) {
return null;
}
WorkspaceRoot root = externalWorkspaces.get(label.externalWorkspaceName());

WorkspaceRoot root = getExternalWorkspaceRootsFile(label.externalWorkspaceName(), project);
return root != null ? root.fileForPath(label.blazePackage()) : null;
}

Expand All @@ -81,6 +90,7 @@ public static WorkspacePath resolveWorkspacePath(Project project, File absoluteF
/** Converts a file to the corresponding BUILD label for this project, if valid. */
@Nullable
public static Label getBuildLabel(Project project, File absoluteFile) {
logger.debug("getBuildLabel for file " + absoluteFile.getAbsolutePath());
Workspace workspace = resolveWorkspace(project, absoluteFile);
if (workspace == null) {
return null;
Expand All @@ -103,18 +113,30 @@ private static Workspace resolveWorkspace(Project project, File absoluteFile) {
// try project workspace first
WorkspaceRoot root = pathResolver.findWorkspaceRoot(absoluteFile);
if (root != null) {
logger.debug("resolveWorkspace: " + root.directory().getAbsolutePath());
return new Workspace(root, null);
}

Map<String, WorkspaceRoot> externalWorkspaces = getExternalWorkspaceRoots(project);
if (externalWorkspaces == null) {
BlazeProjectData blazeProjectData = getBlazeProjectData(project);
Path bazelRootPath =
Paths.get(blazeProjectData.getBlazeInfo().getOutputBase().getAbsolutePath(), "external")
.normalize();

logger.debug("the bazelRootPath is " + bazelRootPath);
Path path = Paths.get(absoluteFile.getAbsolutePath()).normalize();

// Check if the file path starts with the root directory path
if (!path.startsWith(bazelRootPath)) {
return null;
}
for (Entry<String, WorkspaceRoot> entry : externalWorkspaces.entrySet()) {
root = entry.getValue();
WorkspacePath workspacePath = root.workspacePathForSafe(absoluteFile);
if (workspacePath != null) {
return new Workspace(root, entry.getKey());

Path relativePath = bazelRootPath.relativize(path);
if (relativePath.getNameCount() > 0) {
String firstFolder = relativePath.getName(0).toString();
Path workspaceRootPath = bazelRootPath.resolve(firstFolder);
if (workspaceRootPath.toFile().exists() || isInTestMode()) {
logger.debug("resolveWorkspace: " + workspaceRootPath + " firstFolder: " + firstFolder);
return new Workspace(new WorkspaceRoot(workspaceRootPath.toFile()), firstFolder);
}
}
return null;
Expand Down Expand Up @@ -156,30 +178,47 @@ private static WorkspacePath getPackagePath(
}

@Nullable
private static synchronized Map<String, WorkspaceRoot> getExternalWorkspaceRoots(
Project project) {
private static synchronized WorkspaceRoot getExternalWorkspaceRootsFile(
String workspaceName, Project project) {
if (Blaze.getBuildSystemName(project) == BuildSystemName.Blaze) {
return ImmutableMap.of();
return null;
}
logger.debug("getExternalWorkspaceRootsFile for " + workspaceName);
Map<String, WorkspaceRoot> workspaceRootCache =
SyncCache.getInstance(project)
.get(
WorkspaceHelper.class, (p, data) -> new ConcurrentHashMap<String, WorkspaceRoot>());

// the null cache value case could happen when the blazeProjectData is null.
if (workspaceRootCache == null) {
return null;
}
return SyncCache.getInstance(project)
.get(WorkspaceHelper.class, WorkspaceHelper::enumerateExternalWorkspaces);
}

@SuppressWarnings("unused")
private static Map<String, WorkspaceRoot> enumerateExternalWorkspaces(
Project project, BlazeProjectData blazeProjectData) {
FileOperationProvider provider = FileOperationProvider.getInstance();
File[] children = provider.listFiles(getExternalSourceRoot(blazeProjectData));
if (children == null) {
return ImmutableMap.of();
WorkspaceRoot root = null;
if (workspaceRootCache.containsKey(workspaceName)) {
root = workspaceRootCache.get(workspaceName);
} else if (getBlazeProjectData(project) != null) {
File externalDir =
new File(
getBlazeProjectData(project).getBlazeInfo().getOutputBase(),
"external/" + workspaceName);

if (externalDir.exists() || isInTestMode()) {
root = new WorkspaceRoot(externalDir);
workspaceRootCache.put(workspaceName, root);
}
}
return Arrays.stream(children)
.filter(provider::isDirectory)
.collect(Collectors.toMap(File::getName, WorkspaceRoot::new));

return root;
}

@VisibleForTesting
public static File getExternalSourceRoot(BlazeProjectData projectData) {
return new File(projectData.getBlazeInfo().getOutputBase(), "external");
}

// The unit test use the TempFileSystem to create VirtualFile which does not exist on disk.
private static boolean isInTestMode() {
return ApplicationManager.getApplication().isUnitTestMode();
}
}