From 4cdfa8841d6a18717e3b92f8b809b0e7a37349f5 Mon Sep 17 00:00:00 2001 From: Mihai Toader Date: Fri, 30 Aug 2024 09:55:39 -0700 Subject: [PATCH 1/9] [#6664] Resolve a workspace label across external repositories - 4/n --- .../buildfile/references/LabelReference.java | 3 + .../base/model/ExternalWorkspaceData.java | 10 ++- .../base/sync/workspace/WorkspaceHelper.java | 38 ++++++--- .../completion/RuleTargetCompletionTest.java | 6 ++ .../ModuleRepositoryCompletionTest.java | 85 +++++++++++++++++++ .../BuildFileIntegrationTestCase.java | 62 ++++++++++++++ 6 files changed, 189 insertions(+), 15 deletions(-) create mode 100644 base/tests/integrationtests/com/google/idea/blaze/base/lang/buildfile/references/ModuleRepositoryCompletionTest.java diff --git a/base/src/com/google/idea/blaze/base/lang/buildfile/references/LabelReference.java b/base/src/com/google/idea/blaze/base/lang/buildfile/references/LabelReference.java index a3457bb56ea..514747a98db 100644 --- a/base/src/com/google/idea/blaze/base/lang/buildfile/references/LabelReference.java +++ b/base/src/com/google/idea/blaze/base/lang/buildfile/references/LabelReference.java @@ -32,9 +32,12 @@ import com.intellij.openapi.vfs.VirtualFileFilter; import com.intellij.psi.PsiElement; import com.intellij.psi.PsiFile; +import com.intellij.psi.PsiReference; import com.intellij.psi.PsiReferenceBase; import com.intellij.util.ArrayUtil; import com.intellij.util.IncorrectOperationException; +import org.jetbrains.annotations.NotNull; + import javax.annotation.Nullable; /** Converts a blaze label into an absolute path, then resolves that path to a PsiElements */ diff --git a/base/src/com/google/idea/blaze/base/model/ExternalWorkspaceData.java b/base/src/com/google/idea/blaze/base/model/ExternalWorkspaceData.java index 888b22c99f6..df5dac5a332 100644 --- a/base/src/com/google/idea/blaze/base/model/ExternalWorkspaceData.java +++ b/base/src/com/google/idea/blaze/base/model/ExternalWorkspaceData.java @@ -3,10 +3,13 @@ import com.google.common.base.Functions; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; +import com.google.common.collect.Maps; import com.google.devtools.intellij.model.ProjectData; import com.google.idea.blaze.base.ideinfo.ProtoWrapper; import com.google.idea.blaze.base.model.primitives.ExternalWorkspace; +import javax.annotation.Nullable; + public final class ExternalWorkspaceData implements ProtoWrapper { public ImmutableMap workspaces; @@ -22,7 +25,7 @@ public static ExternalWorkspaceData create(ImmutableList work .stream() .collect( ImmutableMap.toImmutableMap( - ExternalWorkspace::name, + ExternalWorkspace::repoName, Functions.identity())) ); } @@ -43,4 +46,9 @@ public ProjectData.ExternalWorkspaceData toProto() { public static ExternalWorkspaceData fromProto(ProjectData.ExternalWorkspaceData proto) { return new ExternalWorkspaceData(proto.getWorkspacesList().stream().map(ExternalWorkspace::fromProto).collect(ImmutableList.toImmutableList())); } + + @Nullable + public ExternalWorkspace getByRepoName(String name) { + return Maps.filterValues(workspaces, w -> w.repoName() != null && w.repoName().equals(name)).values().stream().findFirst().orElse(null); + } } diff --git a/base/src/com/google/idea/blaze/base/sync/workspace/WorkspaceHelper.java b/base/src/com/google/idea/blaze/base/sync/workspace/WorkspaceHelper.java index 0ad3eafe857..0dca30e615c 100644 --- a/base/src/com/google/idea/blaze/base/sync/workspace/WorkspaceHelper.java +++ b/base/src/com/google/idea/blaze/base/sync/workspace/WorkspaceHelper.java @@ -18,6 +18,8 @@ import com.google.common.annotations.VisibleForTesting; import com.google.idea.blaze.base.bazel.BuildSystemProvider; import com.google.idea.blaze.base.model.BlazeProjectData; +import com.google.idea.blaze.base.model.ExternalWorkspaceData; +import com.google.idea.blaze.base.model.primitives.ExternalWorkspace; 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; @@ -169,8 +171,8 @@ private static Label deriveLabel( TargetName.createIfValid( FileUtil.getRelativePath(workspace.root.fileForPath(packagePath), file)); return targetName != null - ? Label.create(workspace.externalWorkspaceName, packagePath, targetName) - : null; + ? Label.create(workspace.externalWorkspaceName, packagePath, targetName) + : null; } private static WorkspacePath getPackagePath( @@ -193,33 +195,41 @@ public static File getExternalSourceRoot(BlazeProjectData projectData) { @Nullable private static synchronized WorkspaceRoot getExternalWorkspaceRootsFile(String workspaceName, - Project project) { + Project project) { if (Blaze.getBuildSystemName(project) == BuildSystemName.Blaze) { return null; } logger.debug("getExternalWorkspaceRootsFile for " + workspaceName); - Map workspaceRootCache = SyncCache.getInstance(project) - .get(WorkspaceHelper.class, (p, data) -> new ConcurrentHashMap()); + Map workspaceRootCache = + SyncCache.getInstance(project) + .get(WorkspaceHelper.class, (p, data) -> new ConcurrentHashMap<>()); //the null cache value case could happen when the blazeProjectData is null. - if(workspaceRootCache == null) { + if (workspaceRootCache == null) { return null; } - 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); + return workspaceRootCache.get(workspaceName); + } + + BlazeProjectData blazeProjectData = getBlazeProjectData(project); + if (blazeProjectData != null) { + File workspaceDir = new File(blazeProjectData.getBlazeInfo().getOutputBase(), "external/" + workspaceName); - if (externalDir.exists() || isInTestMode()) { - root = new WorkspaceRoot(externalDir); + ExternalWorkspace workspace = blazeProjectData.getExternalWorkspaceData().getByRepoName(workspaceName); + if (workspace != null) { + workspaceDir = new File(blazeProjectData.getBlazeInfo().getOutputBase(), "external/" + workspace.name()); + } + + if (workspaceDir.exists() || isInTestMode()) { + WorkspaceRoot root = new WorkspaceRoot(workspaceDir); workspaceRootCache.put(workspaceName, root); + return root; } } - return root; + return null; } //The unit test use the TempFileSystem to create VirtualFile which does not exist on disk. diff --git a/base/tests/integrationtests/com/google/idea/blaze/base/lang/buildfile/completion/RuleTargetCompletionTest.java b/base/tests/integrationtests/com/google/idea/blaze/base/lang/buildfile/completion/RuleTargetCompletionTest.java index 5ada9f4a3e3..20328d2a493 100644 --- a/base/tests/integrationtests/com/google/idea/blaze/base/lang/buildfile/completion/RuleTargetCompletionTest.java +++ b/base/tests/integrationtests/com/google/idea/blaze/base/lang/buildfile/completion/RuleTargetCompletionTest.java @@ -24,6 +24,7 @@ import com.google.idea.blaze.base.lang.buildfile.language.semantics.RuleDefinition; import com.google.idea.blaze.base.lang.buildfile.psi.BuildFile; import com.google.idea.blaze.base.model.primitives.WorkspacePath; +import com.google.idea.blaze.base.sync.data.BlazeProjectDataManager; import com.intellij.codeInsight.lookup.LookupElement; import com.intellij.openapi.editor.Editor; import javax.annotation.Nullable; @@ -188,6 +189,11 @@ public void testLocalPathIgnoredForNonLocalLabels() throws Throwable { assertThat(completionItems).asList().doesNotContain("'//java/com/google:other_rule'"); } + @Test + public void testExternalRepoCompletion() throws Throwable { + + } + private static void setBuildLanguageSpecRules( MockBuildLanguageSpecProvider specProvider, String... ruleNames) { ImmutableMap.Builder rules = ImmutableMap.builder(); diff --git a/base/tests/integrationtests/com/google/idea/blaze/base/lang/buildfile/references/ModuleRepositoryCompletionTest.java b/base/tests/integrationtests/com/google/idea/blaze/base/lang/buildfile/references/ModuleRepositoryCompletionTest.java new file mode 100644 index 00000000000..fcbb3c6480f --- /dev/null +++ b/base/tests/integrationtests/com/google/idea/blaze/base/lang/buildfile/references/ModuleRepositoryCompletionTest.java @@ -0,0 +1,85 @@ +package com.google.idea.blaze.base.lang.buildfile.references; + +import com.google.common.collect.ImmutableList; +import com.google.idea.blaze.base.lang.buildfile.BuildFileIntegrationTestCase; +import com.google.idea.blaze.base.lang.buildfile.psi.BuildFile; +import com.google.idea.blaze.base.model.ExternalWorkspaceData; +import com.google.idea.blaze.base.model.primitives.ExternalWorkspace; +import com.google.idea.blaze.base.model.primitives.WorkspacePath; +import com.google.idea.blaze.base.model.primitives.WorkspaceRoot; +import com.intellij.codeInsight.navigation.actions.GotoDeclarationAction; +import com.intellij.openapi.editor.Editor; +import com.intellij.psi.PsiElement; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +import static com.google.common.truth.Truth.assertThat; +import static org.junit.Assert.assertNotNull; + +@RunWith(JUnit4.class) +public class ModuleRepositoryCompletionTest extends BuildFileIntegrationTestCase { + + final ExternalWorkspaceFixture unmappedWorkspace = + new ExternalWorkspaceFixture(ExternalWorkspace.create("workspace_one", "workspace_one")); + + final ExternalWorkspaceFixture remappedWorkspace = + new ExternalWorkspaceFixture(ExternalWorkspace.create("workspace_one", "com_workspace_one")); + + @Override + protected ExternalWorkspaceData mockExternalWorkspaceData() { + return ExternalWorkspaceData.create(ImmutableList.of(unmappedWorkspace.workspace, remappedWorkspace.workspace)); + } + + @Test + public void testUnmappedExternalWorkspaceCompletion() throws Throwable { + WorkspaceRoot externalWorkspaceRoot = unmappedWorkspace.getWorkspaceRoot(); + assertNotNull(externalWorkspaceRoot); + + BuildFile otherPackage = + unmappedWorkspace.createBuildFile(new WorkspacePath( "p1/p2/BUILD"), "java_library(name = 'rule1')"); + + String targetRule = "@" + unmappedWorkspace.workspace.repoName() + "//p1/p2:rule1"; + + BuildFile file = createBuildFile( + new WorkspacePath("java/BUILD"), + "java_library(", + " name = 'lib',", + " deps = ['" + targetRule + "']"); + + Editor editor = editorTest.openFileInEditor(file); + editorTest.setCaretPosition(editor, 2, (" deps = ['" + targetRule).length()); + + PsiElement target = + GotoDeclarationAction.findTargetElement( + getProject(), editor, editor.getCaretModel().getOffset()); + + assertThat(target).isNotNull(); + } + + @Test + public void testRemappedExternalWorkspaceCompletion() throws Throwable { + WorkspaceRoot externalWorkspaceRoot = remappedWorkspace.getWorkspaceRoot(); + assertNotNull(externalWorkspaceRoot); + + BuildFile otherPackage = + remappedWorkspace.createBuildFile(new WorkspacePath("p1/p2/BUILD"), "java_library(name = 'rule1')"); + + String targetRule = "@" + remappedWorkspace.workspace.repoName() + "//p1/p2:rule1"; + + BuildFile file = createBuildFile( + new WorkspacePath("java/BUILD"), + "java_library(", + " name = 'lib',", + " deps = ['" + targetRule + "']"); + + Editor editor = editorTest.openFileInEditor(file); + editorTest.setCaretPosition(editor, 2, (" deps = ['" + targetRule).length()); + + PsiElement target = + GotoDeclarationAction.findTargetElement( + getProject(), editor, editor.getCaretModel().getOffset()); + + assertThat(target).isNotNull(); + } +} diff --git a/base/tests/utils/integration/com/google/idea/blaze/base/lang/buildfile/BuildFileIntegrationTestCase.java b/base/tests/utils/integration/com/google/idea/blaze/base/lang/buildfile/BuildFileIntegrationTestCase.java index d7bca78144a..072d733c9a6 100644 --- a/base/tests/utils/integration/com/google/idea/blaze/base/lang/buildfile/BuildFileIntegrationTestCase.java +++ b/base/tests/utils/integration/com/google/idea/blaze/base/lang/buildfile/BuildFileIntegrationTestCase.java @@ -16,18 +16,29 @@ package com.google.idea.blaze.base.lang.buildfile; import static com.google.common.truth.Truth.assertThat; +import static org.junit.Assert.assertNotNull; import com.google.common.base.Joiner; import com.google.idea.blaze.base.BlazeIntegrationTestCase; import com.google.idea.blaze.base.EditorTestHelper; +import com.google.idea.blaze.base.WorkspaceFileSystem; import com.google.idea.blaze.base.lang.buildfile.psi.BuildFile; +import com.google.idea.blaze.base.model.BlazeProjectData; +import com.google.idea.blaze.base.model.ExternalWorkspaceData; import com.google.idea.blaze.base.model.MockBlazeProjectDataBuilder; import com.google.idea.blaze.base.model.MockBlazeProjectDataManager; +import com.google.idea.blaze.base.model.primitives.ExternalWorkspace; import com.google.idea.blaze.base.model.primitives.WorkspacePath; +import com.google.idea.blaze.base.model.primitives.WorkspaceRoot; import com.google.idea.blaze.base.sync.data.BlazeProjectDataManager; import com.intellij.openapi.vfs.VirtualFile; import com.intellij.psi.PsiFile; + +import java.io.File; +import java.nio.file.Path; +import java.nio.file.Paths; import java.util.List; + import org.junit.Before; /** BUILD file specific integration test base */ @@ -40,11 +51,16 @@ public final void doSetup() { new MockBlazeProjectDataManager( MockBlazeProjectDataBuilder.builder(workspaceRoot) .setOutputBase(fileSystem.getRootDir() + "/output_base") + .setExternalWorkspaceData(mockExternalWorkspaceData()) .build()); registerProjectService(BlazeProjectDataManager.class, mockProjectDataManager); editorTest = new EditorTestHelper(getProject(), testFixture); } + protected ExternalWorkspaceData mockExternalWorkspaceData() { + return ExternalWorkspaceData.EMPTY; + } + /** * Creates a file with the specified contents and file path in the test project, and asserts that * it's parsed as a BuildFile @@ -68,4 +84,50 @@ protected void assertFileContents(PsiFile file, List contentLines) { String contents = Joiner.on('\n').join(contentLines); assertThat(file.getText()).isEqualTo(contents); } + + protected final class ExternalWorkspaceFixture { + public final ExternalWorkspace workspace; + WorkspaceFileSystem workspaceFileSystem; + + public ExternalWorkspaceFixture(ExternalWorkspace workspace) { + this.workspace = workspace; + } + + WorkspaceFileSystem getWorkspaceFileSystem() { + if (workspaceFileSystem == null) { + BlazeProjectData blazeProjectData = BlazeProjectDataManager.getInstance(getProject()).getBlazeProjectData(); + assertNotNull(blazeProjectData); + + File outputBase = blazeProjectData.getBlazeInfo().getOutputBase(); + WorkspaceRoot workspaceRoot = new WorkspaceRoot(Paths.get( + blazeProjectData.getBlazeInfo().getOutputBase().getAbsolutePath(), + "external", workspace.name()).normalize().toFile()); + + File workspaceRootFile = workspaceRoot.directory(); + assertThat(workspaceRootFile).isNotNull(); + workspaceFileSystem = new WorkspaceFileSystem(workspaceRoot, BuildFileIntegrationTestCase.this.fileSystem); + } + + return workspaceFileSystem; + } + + public WorkspaceRoot getWorkspaceRoot() { + BlazeProjectData blazeProjectData = BlazeProjectDataManager.getInstance(getProject()).getBlazeProjectData(); + assertThat(blazeProjectData).isNotNull(); + + File outputBase = blazeProjectData.getBlazeInfo().getOutputBase(); + + Path workspaceRootPath = Paths.get( + blazeProjectData.getBlazeInfo().getOutputBase().getAbsolutePath(), + "external", workspace.name()); + + return new WorkspaceRoot(workspaceRootPath.normalize().toFile()); + } + + public BuildFile createBuildFile(WorkspacePath workspacePath, String... contentLines) { + PsiFile file = getWorkspaceFileSystem().createPsiFile(workspacePath, contentLines); + assertThat(file).isInstanceOf(BuildFile.class); + return (BuildFile) file; + } + } } From b3255479528c3cd69893fea6221fd4e7da159bad Mon Sep 17 00:00:00 2001 From: Mihai Toader Date: Fri, 30 Aug 2024 13:42:03 -0700 Subject: [PATCH 2/9] Fix tests execution --- .../base/sync/workspace/WorkspaceHelper.java | 4 +- ...ava => ModuleRepositoryReferenceTest.java} | 21 ++++-- .../blaze/base/ExternalWorkspaceFixture.java | 69 +++++++++++++++++++ .../BuildFileIntegrationTestCase.java | 59 +--------------- .../model/MockBlazeProjectDataBuilder.java | 3 +- 5 files changed, 88 insertions(+), 68 deletions(-) rename base/tests/integrationtests/com/google/idea/blaze/base/lang/buildfile/references/{ModuleRepositoryCompletionTest.java => ModuleRepositoryReferenceTest.java} (79%) create mode 100644 base/tests/utils/integration/com/google/idea/blaze/base/ExternalWorkspaceFixture.java diff --git a/base/src/com/google/idea/blaze/base/sync/workspace/WorkspaceHelper.java b/base/src/com/google/idea/blaze/base/sync/workspace/WorkspaceHelper.java index 0dca30e615c..91a235e505d 100644 --- a/base/src/com/google/idea/blaze/base/sync/workspace/WorkspaceHelper.java +++ b/base/src/com/google/idea/blaze/base/sync/workspace/WorkspaceHelper.java @@ -18,7 +18,6 @@ import com.google.common.annotations.VisibleForTesting; import com.google.idea.blaze.base.bazel.BuildSystemProvider; import com.google.idea.blaze.base.model.BlazeProjectData; -import com.google.idea.blaze.base.model.ExternalWorkspaceData; import com.google.idea.blaze.base.model.primitives.ExternalWorkspace; import com.google.idea.blaze.base.model.primitives.Label; import com.google.idea.blaze.base.model.primitives.TargetName; @@ -37,7 +36,6 @@ import java.io.File; import java.nio.file.Path; import java.nio.file.Paths; -import java.util.HashMap; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import javax.annotation.Nullable; @@ -213,7 +211,7 @@ private static synchronized WorkspaceRoot getExternalWorkspaceRootsFile(String w return workspaceRootCache.get(workspaceName); } - BlazeProjectData blazeProjectData = getBlazeProjectData(project); + BlazeProjectData blazeProjectData = BlazeProjectDataManager.getInstance(project).getBlazeProjectData(); if (blazeProjectData != null) { File workspaceDir = new File(blazeProjectData.getBlazeInfo().getOutputBase(), "external/" + workspaceName); diff --git a/base/tests/integrationtests/com/google/idea/blaze/base/lang/buildfile/references/ModuleRepositoryCompletionTest.java b/base/tests/integrationtests/com/google/idea/blaze/base/lang/buildfile/references/ModuleRepositoryReferenceTest.java similarity index 79% rename from base/tests/integrationtests/com/google/idea/blaze/base/lang/buildfile/references/ModuleRepositoryCompletionTest.java rename to base/tests/integrationtests/com/google/idea/blaze/base/lang/buildfile/references/ModuleRepositoryReferenceTest.java index fcbb3c6480f..5d41258cefa 100644 --- a/base/tests/integrationtests/com/google/idea/blaze/base/lang/buildfile/references/ModuleRepositoryCompletionTest.java +++ b/base/tests/integrationtests/com/google/idea/blaze/base/lang/buildfile/references/ModuleRepositoryReferenceTest.java @@ -1,6 +1,7 @@ package com.google.idea.blaze.base.lang.buildfile.references; import com.google.common.collect.ImmutableList; +import com.google.idea.blaze.base.ExternalWorkspaceFixture; import com.google.idea.blaze.base.lang.buildfile.BuildFileIntegrationTestCase; import com.google.idea.blaze.base.lang.buildfile.psi.BuildFile; import com.google.idea.blaze.base.model.ExternalWorkspaceData; @@ -18,17 +19,23 @@ import static org.junit.Assert.assertNotNull; @RunWith(JUnit4.class) -public class ModuleRepositoryCompletionTest extends BuildFileIntegrationTestCase { +public class ModuleRepositoryReferenceTest extends BuildFileIntegrationTestCase { - final ExternalWorkspaceFixture unmappedWorkspace = - new ExternalWorkspaceFixture(ExternalWorkspace.create("workspace_one", "workspace_one")); - - final ExternalWorkspaceFixture remappedWorkspace = - new ExternalWorkspaceFixture(ExternalWorkspace.create("workspace_one", "com_workspace_one")); + protected ExternalWorkspaceFixture unmappedWorkspace; + protected ExternalWorkspaceFixture remappedWorkspace; @Override protected ExternalWorkspaceData mockExternalWorkspaceData() { - return ExternalWorkspaceData.create(ImmutableList.of(unmappedWorkspace.workspace, remappedWorkspace.workspace)); + System.out.println("mockExternalWorkspaceData called"); + new Exception().printStackTrace(); + unmappedWorkspace = new ExternalWorkspaceFixture( + ExternalWorkspace.create("workspace_one", "workspace_one"), fileSystem); + + remappedWorkspace = new ExternalWorkspaceFixture( + ExternalWorkspace.create("workspace_two", "com_workspace_two"), fileSystem); + + return ExternalWorkspaceData.create( + ImmutableList.of(unmappedWorkspace.workspace, remappedWorkspace.workspace)); } @Test diff --git a/base/tests/utils/integration/com/google/idea/blaze/base/ExternalWorkspaceFixture.java b/base/tests/utils/integration/com/google/idea/blaze/base/ExternalWorkspaceFixture.java new file mode 100644 index 00000000000..2db9859cf4d --- /dev/null +++ b/base/tests/utils/integration/com/google/idea/blaze/base/ExternalWorkspaceFixture.java @@ -0,0 +1,69 @@ +package com.google.idea.blaze.base; + +import com.google.idea.blaze.base.lang.buildfile.psi.BuildFile; +import com.google.idea.blaze.base.model.BlazeProjectData; +import com.google.idea.blaze.base.model.primitives.ExternalWorkspace; +import com.google.idea.blaze.base.model.primitives.WorkspacePath; +import com.google.idea.blaze.base.model.primitives.WorkspaceRoot; +import com.google.idea.blaze.base.sync.data.BlazeProjectDataManager; +import com.intellij.psi.PsiFile; + +import java.io.File; +import java.nio.file.Path; +import java.nio.file.Paths; + +import static com.google.common.truth.Truth.assertThat; +import static com.google.idea.blaze.base.settings.ui.ProjectViewUi.getProject; +import static org.junit.Assert.assertNotNull; + +public class ExternalWorkspaceFixture { + public final ExternalWorkspace workspace; + + final TestFileSystem fileSystem; + WorkspaceFileSystem workspaceFileSystem; + + public ExternalWorkspaceFixture(ExternalWorkspace workspace, TestFileSystem fileSystem) { + this.workspace = workspace; + this.fileSystem = fileSystem; + } + + public WorkspaceRoot getWorkspaceRoot() { + BlazeProjectData blazeProjectData = BlazeProjectDataManager.getInstance(getProject()).getBlazeProjectData(); + assertThat(blazeProjectData).isNotNull(); + + File outputBase = blazeProjectData.getBlazeInfo().getOutputBase(); + + Path workspaceRootPath = Paths.get( + blazeProjectData.getBlazeInfo().getOutputBase().getAbsolutePath(), + "external", workspace.name()); + + return new WorkspaceRoot(workspaceRootPath.normalize().toFile()); + } + + public BuildFile createBuildFile(WorkspacePath workspacePath, String... contentLines) { + PsiFile file = getWorkspaceFileSystem().createPsiFile(workspacePath, contentLines); + assertThat(file).isInstanceOf(BuildFile.class); + return (BuildFile) file; + } + + WorkspaceFileSystem getWorkspaceFileSystem() { + if (workspaceFileSystem == null) { + BlazeProjectData blazeProjectData = BlazeProjectDataManager.getInstance(getProject()).getBlazeProjectData(); + assertNotNull(blazeProjectData); + + File outputBase = blazeProjectData.getBlazeInfo().getOutputBase(); + + + WorkspaceRoot workspaceRoot = new WorkspaceRoot(Paths.get( + blazeProjectData.getBlazeInfo().getOutputBase().getAbsolutePath(), + "external", workspace.name()).normalize().toFile()); + + File workspaceRootFile = workspaceRoot.directory(); + assertThat(workspaceRootFile).isNotNull(); + workspaceFileSystem = new WorkspaceFileSystem(workspaceRoot, fileSystem); + } + + return workspaceFileSystem; + } + +} \ No newline at end of file diff --git a/base/tests/utils/integration/com/google/idea/blaze/base/lang/buildfile/BuildFileIntegrationTestCase.java b/base/tests/utils/integration/com/google/idea/blaze/base/lang/buildfile/BuildFileIntegrationTestCase.java index 072d733c9a6..8b2f2662f5e 100644 --- a/base/tests/utils/integration/com/google/idea/blaze/base/lang/buildfile/BuildFileIntegrationTestCase.java +++ b/base/tests/utils/integration/com/google/idea/blaze/base/lang/buildfile/BuildFileIntegrationTestCase.java @@ -15,31 +15,22 @@ */ package com.google.idea.blaze.base.lang.buildfile; -import static com.google.common.truth.Truth.assertThat; -import static org.junit.Assert.assertNotNull; - import com.google.common.base.Joiner; import com.google.idea.blaze.base.BlazeIntegrationTestCase; import com.google.idea.blaze.base.EditorTestHelper; -import com.google.idea.blaze.base.WorkspaceFileSystem; import com.google.idea.blaze.base.lang.buildfile.psi.BuildFile; -import com.google.idea.blaze.base.model.BlazeProjectData; import com.google.idea.blaze.base.model.ExternalWorkspaceData; import com.google.idea.blaze.base.model.MockBlazeProjectDataBuilder; import com.google.idea.blaze.base.model.MockBlazeProjectDataManager; -import com.google.idea.blaze.base.model.primitives.ExternalWorkspace; import com.google.idea.blaze.base.model.primitives.WorkspacePath; -import com.google.idea.blaze.base.model.primitives.WorkspaceRoot; import com.google.idea.blaze.base.sync.data.BlazeProjectDataManager; import com.intellij.openapi.vfs.VirtualFile; import com.intellij.psi.PsiFile; +import org.junit.Before; -import java.io.File; -import java.nio.file.Path; -import java.nio.file.Paths; import java.util.List; -import org.junit.Before; +import static com.google.common.truth.Truth.assertThat; /** BUILD file specific integration test base */ public abstract class BuildFileIntegrationTestCase extends BlazeIntegrationTestCase { @@ -84,50 +75,4 @@ protected void assertFileContents(PsiFile file, List contentLines) { String contents = Joiner.on('\n').join(contentLines); assertThat(file.getText()).isEqualTo(contents); } - - protected final class ExternalWorkspaceFixture { - public final ExternalWorkspace workspace; - WorkspaceFileSystem workspaceFileSystem; - - public ExternalWorkspaceFixture(ExternalWorkspace workspace) { - this.workspace = workspace; - } - - WorkspaceFileSystem getWorkspaceFileSystem() { - if (workspaceFileSystem == null) { - BlazeProjectData blazeProjectData = BlazeProjectDataManager.getInstance(getProject()).getBlazeProjectData(); - assertNotNull(blazeProjectData); - - File outputBase = blazeProjectData.getBlazeInfo().getOutputBase(); - WorkspaceRoot workspaceRoot = new WorkspaceRoot(Paths.get( - blazeProjectData.getBlazeInfo().getOutputBase().getAbsolutePath(), - "external", workspace.name()).normalize().toFile()); - - File workspaceRootFile = workspaceRoot.directory(); - assertThat(workspaceRootFile).isNotNull(); - workspaceFileSystem = new WorkspaceFileSystem(workspaceRoot, BuildFileIntegrationTestCase.this.fileSystem); - } - - return workspaceFileSystem; - } - - public WorkspaceRoot getWorkspaceRoot() { - BlazeProjectData blazeProjectData = BlazeProjectDataManager.getInstance(getProject()).getBlazeProjectData(); - assertThat(blazeProjectData).isNotNull(); - - File outputBase = blazeProjectData.getBlazeInfo().getOutputBase(); - - Path workspaceRootPath = Paths.get( - blazeProjectData.getBlazeInfo().getOutputBase().getAbsolutePath(), - "external", workspace.name()); - - return new WorkspaceRoot(workspaceRootPath.normalize().toFile()); - } - - public BuildFile createBuildFile(WorkspacePath workspacePath, String... contentLines) { - PsiFile file = getWorkspaceFileSystem().createPsiFile(workspacePath, contentLines); - assertThat(file).isInstanceOf(BuildFile.class); - return (BuildFile) file; - } - } } diff --git a/base/tests/utils/unit/com/google/idea/blaze/base/model/MockBlazeProjectDataBuilder.java b/base/tests/utils/unit/com/google/idea/blaze/base/model/MockBlazeProjectDataBuilder.java index 1d226a61060..08718043b1f 100644 --- a/base/tests/utils/unit/com/google/idea/blaze/base/model/MockBlazeProjectDataBuilder.java +++ b/base/tests/utils/unit/com/google/idea/blaze/base/model/MockBlazeProjectDataBuilder.java @@ -107,7 +107,8 @@ public MockBlazeProjectDataBuilder setWorkspaceLanguageSettings( } @CanIgnoreReturnValue - public MockBlazeProjectDataBuilder setExternalWorkspaceData(ExternalWorkspaceData externalWorkspaceData) { + public MockBlazeProjectDataBuilder setExternalWorkspaceData( + ExternalWorkspaceData externalWorkspaceData) { this.externalWorkspaceData = externalWorkspaceData; return this; } From 8e6791cef67f40d205fbd689ea702ec74866bd50 Mon Sep 17 00:00:00 2001 From: Mihai Toader Date: Fri, 30 Aug 2024 15:09:15 -0700 Subject: [PATCH 3/9] Minor simplification --- .../idea/blaze/base/sync/workspace/WorkspaceHelper.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/base/src/com/google/idea/blaze/base/sync/workspace/WorkspaceHelper.java b/base/src/com/google/idea/blaze/base/sync/workspace/WorkspaceHelper.java index 91a235e505d..99b87633183 100644 --- a/base/src/com/google/idea/blaze/base/sync/workspace/WorkspaceHelper.java +++ b/base/src/com/google/idea/blaze/base/sync/workspace/WorkspaceHelper.java @@ -213,11 +213,12 @@ private static synchronized WorkspaceRoot getExternalWorkspaceRootsFile(String w BlazeProjectData blazeProjectData = BlazeProjectDataManager.getInstance(project).getBlazeProjectData(); if (blazeProjectData != null) { - File workspaceDir = new File(blazeProjectData.getBlazeInfo().getOutputBase(), "external/" + workspaceName); + File externalBase = new File(blazeProjectData.getBlazeInfo().getOutputBase(), "external"); + File workspaceDir = new File(externalBase, workspaceName); ExternalWorkspace workspace = blazeProjectData.getExternalWorkspaceData().getByRepoName(workspaceName); if (workspace != null) { - workspaceDir = new File(blazeProjectData.getBlazeInfo().getOutputBase(), "external/" + workspace.name()); + workspaceDir = new File(externalBase, workspace.name()); } if (workspaceDir.exists() || isInTestMode()) { From 243d06ee710fb44fd7f523419d8af3adb819dfc4 Mon Sep 17 00:00:00 2001 From: Mihai Toader Date: Tue, 3 Sep 2024 09:51:47 -0700 Subject: [PATCH 4/9] Small cleanups --- .../references/ModuleRepositoryReferenceTest.java | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/base/tests/integrationtests/com/google/idea/blaze/base/lang/buildfile/references/ModuleRepositoryReferenceTest.java b/base/tests/integrationtests/com/google/idea/blaze/base/lang/buildfile/references/ModuleRepositoryReferenceTest.java index 5d41258cefa..6140febcd1d 100644 --- a/base/tests/integrationtests/com/google/idea/blaze/base/lang/buildfile/references/ModuleRepositoryReferenceTest.java +++ b/base/tests/integrationtests/com/google/idea/blaze/base/lang/buildfile/references/ModuleRepositoryReferenceTest.java @@ -27,7 +27,6 @@ public class ModuleRepositoryReferenceTest extends BuildFileIntegrationTestCase @Override protected ExternalWorkspaceData mockExternalWorkspaceData() { System.out.println("mockExternalWorkspaceData called"); - new Exception().printStackTrace(); unmappedWorkspace = new ExternalWorkspaceFixture( ExternalWorkspace.create("workspace_one", "workspace_one"), fileSystem); @@ -44,7 +43,9 @@ public void testUnmappedExternalWorkspaceCompletion() throws Throwable { assertNotNull(externalWorkspaceRoot); BuildFile otherPackage = - unmappedWorkspace.createBuildFile(new WorkspacePath( "p1/p2/BUILD"), "java_library(name = 'rule1')"); + unmappedWorkspace.createBuildFile( + new WorkspacePath( "p1/p2/BUILD"), + "java_library(name = 'rule1')"); String targetRule = "@" + unmappedWorkspace.workspace.repoName() + "//p1/p2:rule1"; @@ -70,7 +71,9 @@ public void testRemappedExternalWorkspaceCompletion() throws Throwable { assertNotNull(externalWorkspaceRoot); BuildFile otherPackage = - remappedWorkspace.createBuildFile(new WorkspacePath("p1/p2/BUILD"), "java_library(name = 'rule1')"); + remappedWorkspace.createBuildFile( + new WorkspacePath("p1/p2/BUILD"), + "java_library(name = 'rule1')"); String targetRule = "@" + remappedWorkspace.workspace.repoName() + "//p1/p2:rule1"; From 6b5b92653b1cb98b0a7a422f76fa844c76f6f79c Mon Sep 17 00:00:00 2001 From: Mihai Toader Date: Tue, 3 Sep 2024 10:40:34 -0700 Subject: [PATCH 5/9] Set the `bazel.read.external.workspace.data` registry key to true --- base/src/META-INF/blaze-base.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/base/src/META-INF/blaze-base.xml b/base/src/META-INF/blaze-base.xml index ba8931a0b07..4c93d7ed3ac 100644 --- a/base/src/META-INF/blaze-base.xml +++ b/base/src/META-INF/blaze-base.xml @@ -411,7 +411,7 @@ - From f968d8f116c47afe443a8e7658acb601fd644c69 Mon Sep 17 00:00:00 2001 From: Mihai Toader Date: Tue, 3 Sep 2024 15:05:27 -0700 Subject: [PATCH 6/9] Improve fixtures and simplify tests --- .../base/model/ExternalWorkspaceData.java | 28 ++++---- .../model/primitives/ExternalWorkspace.java | 24 ++++--- .../ExternalWorkspaceFindUsagesTest.java | 13 +--- ...xternalWorkspaceReferenceBzlModeTest.java} | 68 +++++++++---------- .../ExternalWorkspaceReferenceTest.java | 19 ++---- .../blaze/base/ExternalWorkspaceFixture.java | 18 +++-- .../BuildFileIntegrationTestCase.java | 18 +++++ 7 files changed, 102 insertions(+), 86 deletions(-) rename base/tests/integrationtests/com/google/idea/blaze/base/lang/buildfile/references/{ModuleRepositoryReferenceTest.java => ExternalWorkspaceReferenceBzlModeTest.java} (58%) diff --git a/base/src/com/google/idea/blaze/base/model/ExternalWorkspaceData.java b/base/src/com/google/idea/blaze/base/model/ExternalWorkspaceData.java index df5dac5a332..9d5424a259b 100644 --- a/base/src/com/google/idea/blaze/base/model/ExternalWorkspaceData.java +++ b/base/src/com/google/idea/blaze/base/model/ExternalWorkspaceData.java @@ -3,13 +3,13 @@ import com.google.common.base.Functions; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; -import com.google.common.collect.Maps; import com.google.devtools.intellij.model.ProjectData; import com.google.idea.blaze.base.ideinfo.ProtoWrapper; import com.google.idea.blaze.base.model.primitives.ExternalWorkspace; import javax.annotation.Nullable; + public final class ExternalWorkspaceData implements ProtoWrapper { public ImmutableMap workspaces; @@ -25,30 +25,32 @@ public static ExternalWorkspaceData create(ImmutableList work .stream() .collect( ImmutableMap.toImmutableMap( - ExternalWorkspace::repoName, + ExternalWorkspace::repositoryName, Functions.identity())) ); } @Override public ProjectData.ExternalWorkspaceData toProto() { - ImmutableList protoWorkspaces = workspaces - .values() - .stream() - .map(ExternalWorkspace::toProto) - .collect(ImmutableList.toImmutableList()); - - return ProjectData.ExternalWorkspaceData.newBuilder() - .addAllWorkspaces(protoWorkspaces) - .build(); + ProjectData.ExternalWorkspaceData.Builder builder = ProjectData.ExternalWorkspaceData.newBuilder(); + + for (ExternalWorkspace externalWorkspace : workspaces.values()) { + builder = builder.addWorkspaces(externalWorkspace.toProto()); + } + + return builder.build(); } public static ExternalWorkspaceData fromProto(ProjectData.ExternalWorkspaceData proto) { - return new ExternalWorkspaceData(proto.getWorkspacesList().stream().map(ExternalWorkspace::fromProto).collect(ImmutableList.toImmutableList())); + return new ExternalWorkspaceData( + proto.getWorkspacesList() + .stream() + .map(ExternalWorkspace::fromProto) + .collect(ImmutableList.toImmutableList())); } @Nullable public ExternalWorkspace getByRepoName(String name) { - return Maps.filterValues(workspaces, w -> w.repoName() != null && w.repoName().equals(name)).values().stream().findFirst().orElse(null); + return workspaces.get(name); } } diff --git a/base/src/com/google/idea/blaze/base/model/primitives/ExternalWorkspace.java b/base/src/com/google/idea/blaze/base/model/primitives/ExternalWorkspace.java index 2fec441c34b..ce4b1fe9308 100644 --- a/base/src/com/google/idea/blaze/base/model/primitives/ExternalWorkspace.java +++ b/base/src/com/google/idea/blaze/base/model/primitives/ExternalWorkspace.java @@ -13,7 +13,11 @@ public abstract class ExternalWorkspace implements ProtoWrapper contentLines) { String contents = Joiner.on('\n').join(contentLines); assertThat(file.getText()).isEqualTo(contents); } + + protected PsiFile createFileInExternalWorkspace( + String workspaceName, WorkspacePath path, String... contents) { + String filePath = + Paths.get(getExternalSourceRoot().getPath(), workspaceName, path.relativePath()).toString(); + return fileSystem.createPsiFile(filePath, contents); + } + + protected File getExternalSourceRoot() { + BlazeProjectData blazeProjectData = BlazeProjectDataManager.getInstance(getProject()).getBlazeProjectData(); + assertThat(blazeProjectData).isNotNull(); + + return WorkspaceHelper.getExternalSourceRoot(blazeProjectData).toFile(); + } } From ad0c325429d426c4a04fa2c044aec55978c32547 Mon Sep 17 00:00:00 2001 From: Mihai Toader Date: Tue, 3 Sep 2024 15:44:36 -0700 Subject: [PATCH 7/9] Fix compilation --- .../blaze/base/lang/buildfile/BuildFileIntegrationTestCase.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/base/tests/utils/integration/com/google/idea/blaze/base/lang/buildfile/BuildFileIntegrationTestCase.java b/base/tests/utils/integration/com/google/idea/blaze/base/lang/buildfile/BuildFileIntegrationTestCase.java index 3728c040bdb..a2f8ce53d7f 100644 --- a/base/tests/utils/integration/com/google/idea/blaze/base/lang/buildfile/BuildFileIntegrationTestCase.java +++ b/base/tests/utils/integration/com/google/idea/blaze/base/lang/buildfile/BuildFileIntegrationTestCase.java @@ -91,6 +91,6 @@ protected File getExternalSourceRoot() { BlazeProjectData blazeProjectData = BlazeProjectDataManager.getInstance(getProject()).getBlazeProjectData(); assertThat(blazeProjectData).isNotNull(); - return WorkspaceHelper.getExternalSourceRoot(blazeProjectData).toFile(); + return WorkspaceHelper.getExternalSourceRoot(blazeProjectData); } } From 704a4adedb0f2ea213e07b417da07d823e99ee4c Mon Sep 17 00:00:00 2001 From: Mihai Toader Date: Wed, 4 Sep 2024 00:11:45 -0700 Subject: [PATCH 8/9] Address some comments --- MODULE.bazel.lock | 4 ++-- .../ExternalWorkspaceReferenceBzlModeTest.java | 4 ++-- .../ExternalWorkspaceReferenceTest.java | 5 ----- .../blaze/base/ExternalWorkspaceFixture.java | 17 ----------------- 4 files changed, 4 insertions(+), 26 deletions(-) diff --git a/MODULE.bazel.lock b/MODULE.bazel.lock index ed4f1241a45..27bae869b77 100644 --- a/MODULE.bazel.lock +++ b/MODULE.bazel.lock @@ -67,12 +67,12 @@ "https://bcr.bazel.build/modules/rules_java/4.0.0/MODULE.bazel": "5a78a7ae82cd1a33cef56dc578c7d2a46ed0dca12643ee45edbb8417899e6f74", "https://bcr.bazel.build/modules/rules_java/6.0.0/MODULE.bazel": "8a43b7df601a7ec1af61d79345c17b31ea1fedc6711fd4abfd013ea612978e39", "https://bcr.bazel.build/modules/rules_java/7.1.0/MODULE.bazel": "30d9135a2b6561c761bd67bd4990da591e6bdc128790ce3e7afd6a3558b2fb64", + "https://bcr.bazel.build/modules/rules_java/7.10.0/MODULE.bazel": "530c3beb3067e870561739f1144329a21c851ff771cd752a49e06e3dc9c2e71a", + "https://bcr.bazel.build/modules/rules_java/7.10.0/source.json": "01e9e64c170c3698ea82539f406ef04a1583b5b1a2458a91f3f7ea9f2bf8b553", "https://bcr.bazel.build/modules/rules_java/7.2.0/MODULE.bazel": "06c0334c9be61e6cef2c8c84a7800cef502063269a5af25ceb100b192453d4ab", "https://bcr.bazel.build/modules/rules_java/7.4.0/MODULE.bazel": "a592852f8a3dd539e82ee6542013bf2cadfc4c6946be8941e189d224500a8934", "https://bcr.bazel.build/modules/rules_java/7.6.1/MODULE.bazel": "2f14b7e8a1aa2f67ae92bc69d1ec0fa8d9f827c4e17ff5e5f02e91caa3b2d0fe", "https://bcr.bazel.build/modules/rules_java/7.6.5/MODULE.bazel": "481164be5e02e4cab6e77a36927683263be56b7e36fef918b458d7a8a1ebadb1", - "https://bcr.bazel.build/modules/rules_java/7.9.0/MODULE.bazel": "aa1ebea5c5e2a4b31fbabe07bf3e9951ccc0f4e92499c42d325580f86c456020", - "https://bcr.bazel.build/modules/rules_java/7.9.0/source.json": "41ba541cffadec937a872c9cdfe67e2936afcb560b6957d60c2bf313a868e4b2", "https://bcr.bazel.build/modules/rules_jvm_external/4.4.2/MODULE.bazel": "a56b85e418c83eb1839819f0b515c431010160383306d13ec21959ac412d2fe7", "https://bcr.bazel.build/modules/rules_jvm_external/5.1/MODULE.bazel": "33f6f999e03183f7d088c9be518a63467dfd0be94a11d0055fe2d210f89aa909", "https://bcr.bazel.build/modules/rules_jvm_external/5.2/MODULE.bazel": "d9351ba35217ad0de03816ef3ed63f89d411349353077348a45348b096615036", diff --git a/base/tests/integrationtests/com/google/idea/blaze/base/lang/buildfile/references/ExternalWorkspaceReferenceBzlModeTest.java b/base/tests/integrationtests/com/google/idea/blaze/base/lang/buildfile/references/ExternalWorkspaceReferenceBzlModeTest.java index 796c2307819..50dec6c661c 100644 --- a/base/tests/integrationtests/com/google/idea/blaze/base/lang/buildfile/references/ExternalWorkspaceReferenceBzlModeTest.java +++ b/base/tests/integrationtests/com/google/idea/blaze/base/lang/buildfile/references/ExternalWorkspaceReferenceBzlModeTest.java @@ -50,7 +50,7 @@ public void doSetupExternalWorkspaces() { } @Test - public void testUnmappedExternalWorkspaceCompletion() throws Throwable { + public void testUnmappedExternalWorkspace() throws Throwable { Label targetLabel = workspaceOne.createLabel(new WorkspacePath("p1/p2"), TargetName.create("rule1")); BuildFile file = createBuildFile( @@ -72,7 +72,7 @@ public void testUnmappedExternalWorkspaceCompletion() throws Throwable { } @Test - public void testRemappedExternalWorkspaceCompletion() throws Throwable { + public void testRemappedExternalWorkspace() throws Throwable { Label targetLabel = workspaceTwoMapped.createLabel(new WorkspacePath("p1/p2"), TargetName.create("rule1")); BuildFile file = createBuildFile( diff --git a/base/tests/integrationtests/com/google/idea/blaze/base/lang/buildfile/references/ExternalWorkspaceReferenceTest.java b/base/tests/integrationtests/com/google/idea/blaze/base/lang/buildfile/references/ExternalWorkspaceReferenceTest.java index 775824a9130..1c3d0b85a06 100644 --- a/base/tests/integrationtests/com/google/idea/blaze/base/lang/buildfile/references/ExternalWorkspaceReferenceTest.java +++ b/base/tests/integrationtests/com/google/idea/blaze/base/lang/buildfile/references/ExternalWorkspaceReferenceTest.java @@ -224,9 +224,4 @@ public void testRelativeLoadInExternalWorkspace() { protected ExternalWorkspaceData mockExternalWorkspaceData() { return super.mockExternalWorkspaceData(); } - - @Test - public void testBzlModExternalWorkspaceReference() { - - } } diff --git a/base/tests/utils/integration/com/google/idea/blaze/base/ExternalWorkspaceFixture.java b/base/tests/utils/integration/com/google/idea/blaze/base/ExternalWorkspaceFixture.java index 37c0632237b..b95abc23e33 100644 --- a/base/tests/utils/integration/com/google/idea/blaze/base/ExternalWorkspaceFixture.java +++ b/base/tests/utils/integration/com/google/idea/blaze/base/ExternalWorkspaceFixture.java @@ -1,8 +1,6 @@ package com.google.idea.blaze.base; -import com.google.idea.blaze.base.ideinfo.ProjectDataInterner; import com.google.idea.blaze.base.lang.buildfile.psi.BuildFile; -import com.google.idea.blaze.base.lang.buildfile.references.LabelUtils; import com.google.idea.blaze.base.model.BlazeProjectData; import com.google.idea.blaze.base.model.primitives.ExternalWorkspace; import com.google.idea.blaze.base.model.primitives.Label; @@ -12,9 +10,7 @@ import com.google.idea.blaze.base.sync.data.BlazeProjectDataManager; import com.intellij.psi.PsiFile; -import javax.annotation.Nullable; import java.io.File; -import java.nio.file.Path; import java.nio.file.Paths; import static com.google.common.truth.Truth.assertThat; @@ -32,19 +28,6 @@ public ExternalWorkspaceFixture(ExternalWorkspace w, TestFileSystem fileSystem) this.fileSystem = fileSystem; } - public WorkspaceRoot getWorkspaceRoot() { - BlazeProjectData blazeProjectData = BlazeProjectDataManager.getInstance(getProject()).getBlazeProjectData(); - assertThat(blazeProjectData).isNotNull(); - - File outputBase = blazeProjectData.getBlazeInfo().getOutputBase(); - - Path workspaceRootPath = Paths.get( - blazeProjectData.getBlazeInfo().getOutputBase().getAbsolutePath(), - "external", w.name()); - - return new WorkspaceRoot(workspaceRootPath.normalize().toFile()); - } - public BuildFile createBuildFile(WorkspacePath workspacePath, String... contentLines) { PsiFile file = getWorkspaceFileSystem().createPsiFile(workspacePath, contentLines); assertThat(file).isInstanceOf(BuildFile.class); From 9b11f25dfd35514cfcc9bc010aae6a57403a7c52 Mon Sep 17 00:00:00 2001 From: Mihai Toader Date: Wed, 4 Sep 2024 00:21:32 -0700 Subject: [PATCH 9/9] Replace the length based caret setting with the `configureByText` --- ...ExternalWorkspaceReferenceBzlModeTest.java | 25 ++++++++----------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/base/tests/integrationtests/com/google/idea/blaze/base/lang/buildfile/references/ExternalWorkspaceReferenceBzlModeTest.java b/base/tests/integrationtests/com/google/idea/blaze/base/lang/buildfile/references/ExternalWorkspaceReferenceBzlModeTest.java index 50dec6c661c..8c870e7f54c 100644 --- a/base/tests/integrationtests/com/google/idea/blaze/base/lang/buildfile/references/ExternalWorkspaceReferenceBzlModeTest.java +++ b/base/tests/integrationtests/com/google/idea/blaze/base/lang/buildfile/references/ExternalWorkspaceReferenceBzlModeTest.java @@ -12,6 +12,7 @@ import com.intellij.codeInsight.navigation.actions.GotoDeclarationAction; import com.intellij.openapi.editor.Editor; import com.intellij.psi.PsiElement; +import com.intellij.psi.PsiFile; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -51,18 +52,16 @@ public void doSetupExternalWorkspaces() { @Test public void testUnmappedExternalWorkspace() throws Throwable { - Label targetLabel = workspaceOne.createLabel(new WorkspacePath("p1/p2"), TargetName.create("rule1")); + Label targetLabel = workspaceOne.createLabel( + new WorkspacePath("p1/p2"), TargetName.create("rule1")); - BuildFile file = createBuildFile( - new WorkspacePath("java/BUILD"), + PsiFile file = testFixture.configureByText("BUILD", String.format(""" java_library( name = 'lib', - deps = ['%s'] + deps = ['%s'] )""", targetLabel)); - - Editor editor = editorTest.openFileInEditor(file); - editorTest.setCaretPosition(editor, 2, (" deps = ['" + targetLabel).length()); + Editor editor = editorTest.openFileInEditor(file.getVirtualFile()); PsiElement target = GotoDeclarationAction.findTargetElement( @@ -73,18 +72,16 @@ public void testUnmappedExternalWorkspace() throws Throwable { @Test public void testRemappedExternalWorkspace() throws Throwable { - Label targetLabel = workspaceTwoMapped.createLabel(new WorkspacePath("p1/p2"), TargetName.create("rule1")); + Label targetLabel = workspaceTwoMapped.createLabel( + new WorkspacePath("p1/p2"), TargetName.create("rule1")); - BuildFile file = createBuildFile( - new WorkspacePath("java/BUILD"), + PsiFile file = testFixture.configureByText("BUILD", String.format(""" java_library( name = 'lib', - deps = ['%s'] + deps = ['%s'] )""", targetLabel)); - - Editor editor = editorTest.openFileInEditor(file); - editorTest.setCaretPosition(editor, 2, (" deps = ['" + targetLabel).length()); + Editor editor = editorTest.openFileInEditor(file.getVirtualFile()); PsiElement target = GotoDeclarationAction.findTargetElement(