Skip to content

Commit

Permalink
fix: Properly support non-Java Bazel 8 projects in IntelliJ (#7006)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
tpasternak authored Nov 19, 2024
1 parent 5fe7245 commit 373eb7a
Showing 1 changed file with 14 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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<String, String> getJavaStringStringMap(BlazeProjectDataManager manager) {
private static @NotNull Map<String, String> 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<String> ruleNamesForLanguageClass(LanguageClass languageClass, ProjectViewSet viewSet) {
Expand Down

0 comments on commit 373eb7a

Please sign in to comment.