Skip to content

Commit

Permalink
Sync only allowed external workspace target from project
Browse files Browse the repository at this point in the history
Require external target directory within project to be listed
in directories section of project view file. Also introduces
registry key "bazel.cpp.sync.external.targets.from.directories"
which can be used to disable mentioned external targets sync.
  • Loading branch information
ujohnny committed Sep 22, 2023
1 parent 97e2b87 commit 8946c22
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.google.idea.blaze.base.ideinfo.Tags;
import com.google.idea.blaze.base.ideinfo.TargetIdeInfo;
import com.google.idea.blaze.base.model.primitives.Label;
import com.google.idea.blaze.base.model.primitives.WorkspacePath;
import com.google.idea.blaze.base.model.primitives.WorkspaceRoot;
import com.google.idea.blaze.base.projectview.ProjectViewSet;
import com.google.idea.blaze.base.projectview.section.sections.ExcludeTargetSection;
Expand All @@ -45,6 +46,10 @@ public boolean isSourceTarget(TargetIdeInfo target) {
return importRoots.importAsSource(target.getKey().getLabel()) && !importTargetOutput(target);
}

public boolean containsWorkspacePath(WorkspacePath workspacePath) {
return importRoots.containsWorkspacePath(workspacePath);
}

private boolean importTargetOutput(TargetIdeInfo target) {
return target.getTags().contains(Tags.TARGET_TAG_IMPORT_TARGET_OUTPUT)
|| target.getTags().contains(Tags.TARGET_TAG_IMPORT_AS_LIBRARY_LEGACY)
Expand Down
1 change: 1 addition & 0 deletions cpp/src/META-INF/blaze-cpp.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
overrides="true"
/>
<registryKey defaultValue="false" description="Disable absolute path trimming in debug clang builds" key="bazel.trim.absolute.path.disabled"/>
<registryKey defaultValue="true" description="Allow external targets from source directories be imported in" key="bazel.cpp.sync.external.targets.from.directories"/>
<applicationService serviceInterface="com.google.idea.blaze.cpp.CompilerVersionChecker"
serviceImplementation="com.google.idea.blaze.cpp.CompilerVersionCheckerImpl"/>
<applicationService serviceInterface="com.google.idea.blaze.cpp.CompilerWrapperProvider"
Expand Down
26 changes: 15 additions & 11 deletions cpp/src/com/google/idea/blaze/cpp/BlazeConfigurationResolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import com.google.idea.blaze.base.ideinfo.TargetKey;
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.WorkspacePath;
import com.google.idea.blaze.base.model.primitives.WorkspaceRoot;
import com.google.idea.blaze.base.projectview.ProjectViewSet;
import com.google.idea.blaze.base.scope.BlazeContext;
Expand All @@ -47,6 +48,7 @@
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.io.FileUtilRt;
import com.intellij.openapi.util.registry.Registry;
import java.io.IOException;
import java.nio.file.Path;
import java.util.Optional;
Expand All @@ -65,6 +67,8 @@
import java.util.stream.Collectors;

final class BlazeConfigurationResolver {
static final String SYNC_EXTERNAL_TARGETS_FROM_DIRECTORIES_KEY = "bazel.cpp.sync.external.targets.from.directories";

private static final Logger logger = Logger.getInstance(BlazeConfigurationResolver.class);

private final Project project;
Expand Down Expand Up @@ -142,20 +146,22 @@ private static ImmutableMap<String, String> getTargetToVersionMap(ImmutableMap<T

private static Predicate<TargetIdeInfo> getTargetFilter(
ProjectViewTargetImportFilter projectViewFilter,
Project project, WorkspacePathResolver workspacePathResolver) {
Project project,
WorkspacePathResolver workspacePathResolver) {
return target -> {
boolean targetFromExternalWorkspaceInProject = isExternalWorkspaceTargetInProject(target,
project,
workspacePathResolver
);
WorkspacePath pathForExternalTarget = getWorkspacePathForExternalTarget(target, project, workspacePathResolver);

boolean allowExternalTargetSync =
Registry.is(SYNC_EXTERNAL_TARGETS_FROM_DIRECTORIES_KEY) && pathForExternalTarget != null;

return target.getcIdeInfo() != null
&& (targetFromExternalWorkspaceInProject || projectViewFilter.isSourceTarget(target))
&& (projectViewFilter.isSourceTarget(target) ||
allowExternalTargetSync && projectViewFilter.containsWorkspacePath(pathForExternalTarget))
&& containsCompiledSources(target);
};
}

private static boolean isExternalWorkspaceTargetInProject(
private static WorkspacePath getWorkspacePathForExternalTarget(
TargetIdeInfo target,
Project project,
WorkspacePathResolver workspacePathResolver) {
Expand All @@ -166,15 +172,13 @@ private static boolean isExternalWorkspaceTargetInProject(
if (externalWorkspace != null) {
try {
Path externalWorkspaceRealPath = externalWorkspace.directory().toPath().toRealPath();
if (workspacePathResolver.getWorkspacePath(externalWorkspaceRealPath.toFile()) != null) {
return true;
}
return workspacePathResolver.getWorkspacePath(externalWorkspaceRealPath.toFile());
} catch (IOException ioException) {
logger.warn("Failed to resolve real external workspace location", ioException);
}
}
}
return false;
return null;
}

private static boolean containsCompiledSources(TargetIdeInfo target) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
import com.intellij.openapi.extensions.impl.ExtensionPointImpl;
import com.intellij.openapi.progress.ProgressManager;
import com.intellij.openapi.progress.impl.ProgressManagerImpl;
import com.intellij.openapi.util.registry.Registry;
import com.intellij.openapi.vfs.LocalFileSystem;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.openapi.vfs.VirtualFileManager;
Expand Down Expand Up @@ -116,6 +117,8 @@ protected void initTest(Container applicationServices, Container projectServices
VirtualFileSystemProvider.class, mock(VirtualFileSystemProvider.class));
when(VirtualFileSystemProvider.getInstance().getSystem()).thenReturn(mockFileSystem);

Registry.get(BlazeConfigurationResolver.SYNC_EXTERNAL_TARGETS_FROM_DIRECTORIES_KEY).setValue(true);

ExtensionPointImpl<Kind.Provider> ep =
registerExtensionPoint(Kind.Provider.EP_NAME, Kind.Provider.class);
ep.registerExtension(new CppBlazeRules());
Expand Down Expand Up @@ -796,7 +799,10 @@ public void multipleToolchainsNoIssue() {

@Test
public void testExternalDependencyResolvedWhenIsPartOfProject() throws IOException {
ProjectView projectView = projectView(directories("test"), targets("//test:target"));
ProjectView projectView = projectView(
directories("test", "external_dependency"),
targets("//test:target"));

TargetMap targetMap =
TargetMapBuilder.builder()
.addTarget(createCcToolchain())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
import com.intellij.openapi.extensions.impl.ExtensionPointImpl;
import com.intellij.openapi.progress.ProgressManager;
import com.intellij.openapi.progress.impl.ProgressManagerImpl;
import com.intellij.openapi.util.registry.Registry;
import com.intellij.openapi.vfs.LocalFileSystem;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.openapi.vfs.VirtualFileManager;
Expand Down Expand Up @@ -116,6 +117,8 @@ protected void initTest(Container applicationServices, Container projectServices
projectServices.register(
BlazeImportSettingsManager.class, new BlazeImportSettingsManager(project));

Registry.get(BlazeConfigurationResolver.SYNC_EXTERNAL_TARGETS_FROM_DIRECTORIES_KEY).setValue(true);

BlazeImportSettingsManager.getInstance(getProject())
.setImportSettings(
new BlazeImportSettings(
Expand Down

0 comments on commit 8946c22

Please sign in to comment.