From a23d29aff8dd7c4319bccfea650078af563261b6 Mon Sep 17 00:00:00 2001 From: dkashyn Date: Mon, 13 Nov 2023 20:19:12 -0500 Subject: [PATCH 1/6] Allow to have non-existent entries in `projectview` that pointing to files that are optional and can be missing for some of the users. --- base/src/META-INF/blaze-base.xml | 1 + .../section/sections/ImportSection.java | 12 +++++++++++- .../parser/ProjectViewParserTest.java | 18 ++++++++++++++++++ .../idea/blaze/base/scope/ErrorCollector.java | 4 ++++ 4 files changed, 34 insertions(+), 1 deletion(-) diff --git a/base/src/META-INF/blaze-base.xml b/base/src/META-INF/blaze-base.xml index 69813037d2a..4f58baeaabf 100644 --- a/base/src/META-INF/blaze-base.xml +++ b/base/src/META-INF/blaze-base.xml @@ -361,6 +361,7 @@ + i.getCategory() == Category.ERROR)).isTrue(); } + public void assertHasNoErrors() { + assertThat(issues.stream().anyMatch(i -> i.getCategory() == Category.ERROR)).isFalse(); + } + public void assertIssues(String... requiredMessages) { List messages = Lists.newArrayList(); for (IssueOutput issue : issues) { From 54a1ff2ce47ae25d917d5c7c6661b321abee2b83 Mon Sep 17 00:00:00 2001 From: dkashyn Date: Tue, 14 Nov 2023 11:12:47 -0500 Subject: [PATCH 2/6] Reordering of conditions for readability --- .../blaze/base/projectview/section/sections/ImportSection.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/base/src/com/google/idea/blaze/base/projectview/section/sections/ImportSection.java b/base/src/com/google/idea/blaze/base/projectview/section/sections/ImportSection.java index d7021a7b9e9..1656adef29e 100644 --- a/base/src/com/google/idea/blaze/base/projectview/section/sections/ImportSection.java +++ b/base/src/com/google/idea/blaze/base/projectview/section/sections/ImportSection.java @@ -54,7 +54,7 @@ protected WorkspacePath parseItem( File projectViewFile = parseContext.getWorkspacePathResolver().resolveToFile(workspacePath); if (projectViewFile != null) { boolean projectViewImportsMandatory = !Registry.is("bazel.projectview.optional.imports"); - if (projectViewFile.exists() || projectViewImportsMandatory) { + if (projectViewImportsMandatory || projectViewFile.exists()) { parser.parseProjectView(projectViewFile); } else { IssueOutput.warn( From ae1503055d94fce0717633a614b5a4f2dc592c70 Mon Sep 17 00:00:00 2001 From: dkashyn Date: Wed, 29 Nov 2023 10:44:33 -0800 Subject: [PATCH 3/6] TryImport POC --- .../base/projectview/ProjectViewSet.java | 8 +++-- .../section/sections/ImportSection.java | 15 +++++++-- .../section/sections/Sections.java | 1 + .../section/sections/TryImportSection.java | 32 +++++++++++++++++++ .../blaze/base/qsync/ProjectStatsLogger.java | 3 +- .../blaze/base/sync/SyncPhaseCoordinator.java | 3 +- .../ui/BlazeEditProjectViewControl.java | 4 +++ .../base/projectview/ProjectViewSetTest.java | 2 ++ .../parser/ProjectViewParserTest.java | 7 +++- 9 files changed, 66 insertions(+), 9 deletions(-) create mode 100644 base/src/com/google/idea/blaze/base/projectview/section/sections/TryImportSection.java diff --git a/base/src/com/google/idea/blaze/base/projectview/ProjectViewSet.java b/base/src/com/google/idea/blaze/base/projectview/ProjectViewSet.java index 1bbb0c23db2..7e9d8002c8d 100644 --- a/base/src/com/google/idea/blaze/base/projectview/ProjectViewSet.java +++ b/base/src/com/google/idea/blaze/base/projectview/ProjectViewSet.java @@ -52,10 +52,12 @@ public List listItems(SectionKey> key) { } /** Returns all values from all scalar sections in the project views, in order */ - public List listScalarItems(SectionKey> key) { + public List listScalarItems(SectionKey>... keys) { List result = Lists.newArrayList(); - for (ScalarSection section : getSections(key)) { - result.add(section.getValue()); + for (SectionKey> key: keys) { + for (ScalarSection section : getSections(key)) { + result.add(section.getValue()); + } } return result; } diff --git a/base/src/com/google/idea/blaze/base/projectview/section/sections/ImportSection.java b/base/src/com/google/idea/blaze/base/projectview/section/sections/ImportSection.java index 1656adef29e..11d4dc5d022 100644 --- a/base/src/com/google/idea/blaze/base/projectview/section/sections/ImportSection.java +++ b/base/src/com/google/idea/blaze/base/projectview/section/sections/ImportSection.java @@ -34,15 +34,25 @@ public class ImportSection { SectionKey.of("import"); public static final SectionParser PARSER = new ImportSectionParser(); - private static class ImportSectionParser extends ScalarSectionParser { + protected static class ImportSectionParser extends ScalarSectionParser { public ImportSectionParser() { - super(KEY, ' '); + this(KEY, ' '); + } + + protected ImportSectionParser(SectionKey> key, char divider) { + super(key, divider); } @Nullable @Override protected WorkspacePath parseItem( ProjectViewParser parser, ParseContext parseContext, String text) { + boolean projectViewImportsMandatory = !Registry.is("bazel.projectview.optional.imports"); + return parseItem(parser, parseContext, text, projectViewImportsMandatory); + } + + @Nullable + protected static WorkspacePath parseItem(ProjectViewParser parser, ParseContext parseContext, String text, boolean projectViewImportsMandatory) { String error = WorkspacePath.validate(text); if (error != null) { parseContext.addError(error); @@ -53,7 +63,6 @@ protected WorkspacePath parseItem( if (parser.isRecursive()) { File projectViewFile = parseContext.getWorkspacePathResolver().resolveToFile(workspacePath); if (projectViewFile != null) { - boolean projectViewImportsMandatory = !Registry.is("bazel.projectview.optional.imports"); if (projectViewImportsMandatory || projectViewFile.exists()) { parser.parseProjectView(projectViewFile); } else { diff --git a/base/src/com/google/idea/blaze/base/projectview/section/sections/Sections.java b/base/src/com/google/idea/blaze/base/projectview/section/sections/Sections.java index 1d73508f256..5aea4db980c 100644 --- a/base/src/com/google/idea/blaze/base/projectview/section/sections/Sections.java +++ b/base/src/com/google/idea/blaze/base/projectview/section/sections/Sections.java @@ -28,6 +28,7 @@ public class Sections { Lists.newArrayList( TextBlockSection.PARSER, ImportSection.PARSER, + TryImportSection.PARSER, DirectorySection.PARSER, AutomaticallyDeriveTargetsSection.PARSER, SyncManualTargetsSection.PARSER, diff --git a/base/src/com/google/idea/blaze/base/projectview/section/sections/TryImportSection.java b/base/src/com/google/idea/blaze/base/projectview/section/sections/TryImportSection.java new file mode 100644 index 00000000000..2942aebed84 --- /dev/null +++ b/base/src/com/google/idea/blaze/base/projectview/section/sections/TryImportSection.java @@ -0,0 +1,32 @@ +package com.google.idea.blaze.base.projectview.section.sections; + +import com.google.idea.blaze.base.model.primitives.WorkspacePath; +import com.google.idea.blaze.base.projectview.parser.ParseContext; +import com.google.idea.blaze.base.projectview.parser.ProjectViewParser; +import com.google.idea.blaze.base.projectview.section.ScalarSection; +import com.google.idea.blaze.base.projectview.section.SectionKey; +import com.google.idea.blaze.base.projectview.section.SectionParser; + +import javax.annotation.Nullable; + +public class TryImportSection extends ImportSection { + + public static final SectionKey> KEY = + SectionKey.of("try-import"); + + public static final SectionParser PARSER = new TryImportSectionParser(); + + private static class TryImportSectionParser extends ImportSectionParser { + + public TryImportSectionParser() { + super(KEY, ' '); + } + + @Nullable + @Override + protected WorkspacePath parseItem( + ProjectViewParser parser, ParseContext parseContext, String text) { + return parseItem(parser, parseContext, text, false); + } + } +} diff --git a/base/src/com/google/idea/blaze/base/qsync/ProjectStatsLogger.java b/base/src/com/google/idea/blaze/base/qsync/ProjectStatsLogger.java index 5f06b14f13e..1b70e5e51cd 100644 --- a/base/src/com/google/idea/blaze/base/qsync/ProjectStatsLogger.java +++ b/base/src/com/google/idea/blaze/base/qsync/ProjectStatsLogger.java @@ -21,6 +21,7 @@ import com.google.idea.blaze.base.model.primitives.WorkspacePath; import com.google.idea.blaze.base.projectview.ProjectViewSet; import com.google.idea.blaze.base.projectview.section.sections.ImportSection; +import com.google.idea.blaze.base.projectview.section.sections.TryImportSection; import com.google.idea.blaze.common.Context; import com.google.idea.blaze.qsync.BlazeProjectListener; import com.google.idea.blaze.qsync.project.BlazeProjectSnapshot; @@ -55,7 +56,7 @@ public void onNewProjectSnapshot(Context context, BlazeProjectSnapshot instan .getProjectInfoStatsBuilder() .setLanguagesActive(instance.queryData().projectDefinition().languageClasses()) .setBlazeProjectFiles( - projectViewSet.listScalarItems(ImportSection.KEY).stream() + projectViewSet.listScalarItems(ImportSection.KEY, TryImportSection.KEY).stream() .map(WorkspacePath::asPath) .collect(toImmutableSet())); scope diff --git a/base/src/com/google/idea/blaze/base/sync/SyncPhaseCoordinator.java b/base/src/com/google/idea/blaze/base/sync/SyncPhaseCoordinator.java index 48453fb3d69..61f2830d3d8 100644 --- a/base/src/com/google/idea/blaze/base/sync/SyncPhaseCoordinator.java +++ b/base/src/com/google/idea/blaze/base/sync/SyncPhaseCoordinator.java @@ -42,6 +42,7 @@ import com.google.idea.blaze.base.projectview.ProjectViewManager; import com.google.idea.blaze.base.projectview.ProjectViewSet; import com.google.idea.blaze.base.projectview.section.sections.ImportSection; +import com.google.idea.blaze.base.projectview.section.sections.TryImportSection; import com.google.idea.blaze.base.scope.BlazeContext; import com.google.idea.blaze.base.scope.Scope; import com.google.idea.blaze.base.scope.output.IssueOutput; @@ -722,7 +723,7 @@ private static void fillInBuildStats( .setWorkspaceType(projectState.getLanguageSettings().getWorkspaceType()) .setLanguagesActive(projectState.getLanguageSettings().getActiveLanguages()) .setBlazeProjectFiles( - projectState.getProjectViewSet().listScalarItems(ImportSection.KEY)); + projectState.getProjectViewSet().listScalarItems(ImportSection.KEY, TryImportSection.KEY)); } if (buildResult != null) { buildResult diff --git a/base/src/com/google/idea/blaze/base/wizard2/ui/BlazeEditProjectViewControl.java b/base/src/com/google/idea/blaze/base/wizard2/ui/BlazeEditProjectViewControl.java index a4174cb65ee..4630fdff3bc 100644 --- a/base/src/com/google/idea/blaze/base/wizard2/ui/BlazeEditProjectViewControl.java +++ b/base/src/com/google/idea/blaze/base/wizard2/ui/BlazeEditProjectViewControl.java @@ -37,6 +37,7 @@ import com.google.idea.blaze.base.projectview.section.sections.ImportSection; import com.google.idea.blaze.base.projectview.section.sections.Sections; import com.google.idea.blaze.base.projectview.section.sections.TargetSection; +import com.google.idea.blaze.base.projectview.section.sections.TryImportSection; import com.google.idea.blaze.base.scope.BlazeContext; import com.google.idea.blaze.base.scope.OutputSink.Propagation; import com.google.idea.blaze.base.scope.Scope; @@ -599,6 +600,9 @@ public void updateBuilder(BlazeNewProjectBuilder builder) { .add( ScalarSection.builder(ImportSection.KEY) .set(selectProjectViewOption.getSharedProjectView())) + .add( + ScalarSection.builder(TryImportSection.KEY) + .set(selectProjectViewOption.getSharedProjectView())) .build(); projectViewSet = ProjectViewSet.builder() diff --git a/base/tests/unittests/com/google/idea/blaze/base/projectview/ProjectViewSetTest.java b/base/tests/unittests/com/google/idea/blaze/base/projectview/ProjectViewSetTest.java index 6f7c4aa71d8..32d95c4e975 100644 --- a/base/tests/unittests/com/google/idea/blaze/base/projectview/ProjectViewSetTest.java +++ b/base/tests/unittests/com/google/idea/blaze/base/projectview/ProjectViewSetTest.java @@ -52,6 +52,7 @@ import com.google.idea.blaze.base.projectview.section.sections.TestSourceSection; import com.google.idea.blaze.base.projectview.section.sections.TextBlock; import com.google.idea.blaze.base.projectview.section.sections.TextBlockSection; +import com.google.idea.blaze.base.projectview.section.sections.TryImportSection; import com.google.idea.blaze.base.projectview.section.sections.WorkspaceTypeSection; import com.google.idea.blaze.base.sync.BlazeSyncPlugin; import com.google.idea.common.experiments.ExperimentService; @@ -85,6 +86,7 @@ public void testProjectViewSetSerializable() { ListSection.builder(TargetSection.KEY) .add(TargetExpression.fromStringSafe("//test:all"))) .add(ScalarSection.builder(ImportSection.KEY).set(new WorkspacePath("test"))) + .add(ScalarSection.builder(TryImportSection.KEY).set(new WorkspacePath("test-optional"))) .add(ListSection.builder(TestSourceSection.KEY).add(new Glob("javatests/*"))) .add(ListSection.builder(ExcludedSourceSection.KEY).add(new Glob("*.java"))) .add(ListSection.builder(BuildFlagsSection.KEY).add("--android_sdk=abcd")) diff --git a/base/tests/unittests/com/google/idea/blaze/base/projectview/parser/ProjectViewParserTest.java b/base/tests/unittests/com/google/idea/blaze/base/projectview/parser/ProjectViewParserTest.java index 51f433a99ae..2bd07ac356e 100644 --- a/base/tests/unittests/com/google/idea/blaze/base/projectview/parser/ProjectViewParserTest.java +++ b/base/tests/unittests/com/google/idea/blaze/base/projectview/parser/ProjectViewParserTest.java @@ -38,6 +38,7 @@ import com.google.idea.blaze.base.projectview.section.sections.TestSourceSection; import com.google.idea.blaze.base.projectview.section.sections.TextBlock; import com.google.idea.blaze.base.projectview.section.sections.TextBlockSection; +import com.google.idea.blaze.base.projectview.section.sections.TryImportSection; import com.google.idea.blaze.base.projectview.section.sections.WorkspaceTypeSection; import com.google.idea.blaze.base.scope.BlazeContext; import com.google.idea.blaze.base.scope.ErrorCollector; @@ -193,6 +194,9 @@ public void testPrint() { .add( ScalarSection.builder(ImportSection.KEY) .set(new WorkspacePath("some/file.blazeproject"))) + .add( + ScalarSection.builder(TryImportSection.KEY) + .set(new WorkspacePath("some/file.blazeproject.optional"))) .build(); String text = ProjectViewParser.projectViewToString(projectView); assertThat(text) @@ -205,7 +209,8 @@ public void testPrint() { "targets:", " //java/com/google:one", " //java/com/google:two", - "import some/file.blazeproject")); + "import some/file.blazeproject", + "try-import some/file.blazeproject.optional")); } @Test From 841a9aa2d06532cbb19b7ff9771492db1b27ff15 Mon Sep 17 00:00:00 2001 From: dkashyn Date: Wed, 29 Nov 2023 11:11:59 -0800 Subject: [PATCH 4/6] `try_import` instead of `import` --- .../base/projectview/section/sections/TryImportSection.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/base/src/com/google/idea/blaze/base/projectview/section/sections/TryImportSection.java b/base/src/com/google/idea/blaze/base/projectview/section/sections/TryImportSection.java index 2942aebed84..76cb76e2fd5 100644 --- a/base/src/com/google/idea/blaze/base/projectview/section/sections/TryImportSection.java +++ b/base/src/com/google/idea/blaze/base/projectview/section/sections/TryImportSection.java @@ -9,10 +9,11 @@ import javax.annotation.Nullable; +/** "try_import" section. */ public class TryImportSection extends ImportSection { public static final SectionKey> KEY = - SectionKey.of("try-import"); + SectionKey.of("try_import"); public static final SectionParser PARSER = new TryImportSectionParser(); From f1377b575ac74407deaafa0f95da3fee4271c0ce Mon Sep 17 00:00:00 2001 From: dkashyn Date: Wed, 29 Nov 2023 11:41:04 -0800 Subject: [PATCH 5/6] Some tests for try_import --- .../ProjectViewLabelReferenceTest.java | 18 ++++++++++++++++++ .../parser/ProjectViewParserTest.java | 8 ++++++++ 2 files changed, 26 insertions(+) diff --git a/base/tests/integrationtests/com/google/idea/blaze/base/lang/projectview/references/ProjectViewLabelReferenceTest.java b/base/tests/integrationtests/com/google/idea/blaze/base/lang/projectview/references/ProjectViewLabelReferenceTest.java index 179bd57a506..f16dd98054b 100644 --- a/base/tests/integrationtests/com/google/idea/blaze/base/lang/projectview/references/ProjectViewLabelReferenceTest.java +++ b/base/tests/integrationtests/com/google/idea/blaze/base/lang/projectview/references/ProjectViewLabelReferenceTest.java @@ -51,6 +51,24 @@ public void testFileReference() { assertThat(importItem.getReference().resolve()).isEqualTo(referencedFile); } + @Test + public void testFileReferenceTryImport() { + PsiFile referencedFile = + workspace.createPsiFile( + new WorkspacePath("ijwb.bazelproject"), + "directories:", + " java/com/google/foo", + "targets:", + " //java/com/google/foo/..."); + PsiFile file = + workspace.createPsiFile(new WorkspacePath(".bazelproject"), "try_import ijwb.bazelproject"); + + ProjectViewPsiSectionItem importItem = + PsiUtils.findFirstChildOfClassRecursive(file, ProjectViewPsiSectionItem.class); + assertThat(importItem).isNotNull(); + assertThat(importItem.getReference().resolve()).isEqualTo(referencedFile); + } + @Test public void testDirectoryReference() { PsiDirectory directory = workspace.createPsiDirectory(new WorkspacePath("foo/bar")); diff --git a/base/tests/unittests/com/google/idea/blaze/base/projectview/parser/ProjectViewParserTest.java b/base/tests/unittests/com/google/idea/blaze/base/projectview/parser/ProjectViewParserTest.java index 2bd07ac356e..14db75e1279 100644 --- a/base/tests/unittests/com/google/idea/blaze/base/projectview/parser/ProjectViewParserTest.java +++ b/base/tests/unittests/com/google/idea/blaze/base/projectview/parser/ProjectViewParserTest.java @@ -331,6 +331,14 @@ public void testImportMissingFileResultsInWarning() { } } + @Test + public void testTryImportMissingFileResultsInWarning() { + projectViewStorageManager.add(".blazeproject", "try_import parent.blazeproject"); + projectViewParser.parseProjectView(new File(".blazeproject")); + errorCollector.assertIssues("Could not load project view file: '/parent.blazeproject'"); + errorCollector.assertHasNoErrors(); + } + @Test public void testMissingSectionResultsInIssue() { projectViewStorageManager.add(".blazeproject", "nosuchsection:", " java/com/google"); From b174bc684bcb8a2a53fa9d0d22b04449556fd978 Mon Sep 17 00:00:00 2001 From: dkashyn Date: Wed, 29 Nov 2023 11:45:10 -0800 Subject: [PATCH 6/6] test fix --- .../blaze/base/projectview/parser/ProjectViewParserTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/base/tests/unittests/com/google/idea/blaze/base/projectview/parser/ProjectViewParserTest.java b/base/tests/unittests/com/google/idea/blaze/base/projectview/parser/ProjectViewParserTest.java index 14db75e1279..90313950d2b 100644 --- a/base/tests/unittests/com/google/idea/blaze/base/projectview/parser/ProjectViewParserTest.java +++ b/base/tests/unittests/com/google/idea/blaze/base/projectview/parser/ProjectViewParserTest.java @@ -210,7 +210,7 @@ public void testPrint() { " //java/com/google:one", " //java/com/google:two", "import some/file.blazeproject", - "try-import some/file.blazeproject.optional")); + "try_import some/file.blazeproject.optional")); } @Test