From 373eb7a8a7b10ac5aa75c29f6f7f2f951790b4ca Mon Sep 17 00:00:00 2001 From: Tomasz Pasternak Date: Tue, 19 Nov 2024 12:01:11 +0100 Subject: [PATCH] fix: Properly support non-Java Bazel 8 projects in IntelliJ (#7006) Previously, the condition to load rules_java was based on the "additional languages" field, which led to issues as Java is always present in IntelliJ (unlike PyCharm). This change modifies the condition to explicitly check if rules_java is actually loaded in the project, ensuring proper handling of non-Java projects in IntelliJ. --- .../strategy/SyncAspectTemplateProvider.java | 24 +++++++++++-------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/base/src/com/google/idea/blaze/base/sync/aspects/strategy/SyncAspectTemplateProvider.java b/base/src/com/google/idea/blaze/base/sync/aspects/strategy/SyncAspectTemplateProvider.java index 7f56bb247be..020146ae02d 100644 --- a/base/src/com/google/idea/blaze/base/sync/aspects/strategy/SyncAspectTemplateProvider.java +++ b/base/src/com/google/idea/blaze/base/sync/aspects/strategy/SyncAspectTemplateProvider.java @@ -77,7 +77,7 @@ private void prepareProjectAspect(Project project) throws SyncFailedException { final var templateAspects = AspectRepositoryProvider.findAspectTemplateDirectory() .orElseThrow(() -> new SyncFailedException("Couldn't find aspect template directory")); - writeJavaInfo(manager, realizedAspectsPath, templateAspects); + writeJavaInfo(manager, realizedAspectsPath, templateAspects, project); writeCodeGeneratorInfo(manager, project, realizedAspectsPath, templateAspects); } @@ -106,25 +106,29 @@ private void writeCodeGeneratorInfo( } private void writeJavaInfo( - BlazeProjectDataManager manager, - Path realizedAspectsPath, - File templateAspects) throws SyncFailedException { + BlazeProjectDataManager manager, + Path realizedAspectsPath, + File templateAspects, Project project) throws SyncFailedException { var realizedFile = realizedAspectsPath.resolve(REALIZED_JAVA); var templateWriter = new TemplateWriter(templateAspects.toPath()); - var templateVariableMap = getJavaStringStringMap(manager); + var templateVariableMap = getJavaStringStringMap(manager, project); if (!templateWriter.writeToFile(TEMPLATE_JAVA, realizedFile, templateVariableMap)) { throw new SyncFailedException("Could not create template for: " + REALIZED_JAVA); } } - private static @NotNull Map getJavaStringStringMap(BlazeProjectDataManager manager) { + private static @NotNull Map getJavaStringStringMap(BlazeProjectDataManager manager, Project project) throws SyncFailedException { var projectData = Optional.ofNullable(manager.getBlazeProjectData()); // It can be empty on intial sync. Fall back to no lauguage support + var blazeProjectData = BlazeProjectDataManager.getInstance(project).getBlazeProjectData(); var activeLanguages = projectData.map(it -> it.getWorkspaceLanguageSettings().getActiveLanguages()).orElse(ImmutableSet.of()); + var isJavaEnabled = activeLanguages.contains(LanguageClass.JAVA) + && blazeProjectData != null + && blazeProjectData.getExternalWorkspaceData().getByRepoName("rules_java") != null; var isAtLeastBazel8 = projectData.map(it -> it.getBlazeVersionData().bazelIsAtLeastVersion(8, 0, 0)).orElse(false); - return Map.of( - "bazel8OrAbove", isAtLeastBazel8 ? "true" : "false", - "isJavaEnabled", activeLanguages.contains(LanguageClass.JAVA) ? "true" : "false" - ); + return Map.of( + "bazel8OrAbove", isAtLeastBazel8 ? "true" : "false", + "isJavaEnabled", isJavaEnabled ? "true" : "false" + ); } private static List ruleNamesForLanguageClass(LanguageClass languageClass, ProjectViewSet viewSet) {