Skip to content

Commit

Permalink
V2024.11.05 hotfix (#7053)
Browse files Browse the repository at this point in the history
* Fixed Java enabled condition in SyncAspectTemplateProvider (#6987)

Removed the check for GENERIC language class when determining if Java is enabled. This change ensures that only the presence of JAVA language class sets the "isJavaEnabled" flag to true.

* 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.

* support aspect template for Python (#7011)

* fix issues with aspect templates not available in query sync mode (#7048)

* fix issues with aspect templates not available in query sync mode

* revise based on review

* fix: Don't require rules_java and rules_python for bazel 7 and older (#7052)

* doc: Update changelog

* Update CHANGELOG

---------

Co-authored-by: Daniel Brauner <daniel.brauner@jetbrains.com>
Co-authored-by: Jason Trinh <xuanson.trinh@jetbrains.com>
Co-authored-by: Mai Hussien <70515749+mai93@users.noreply.github.com>
  • Loading branch information
4 people authored Nov 25, 2024
1 parent 95b750b commit 3c6e732
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 19 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
v2024.11.05.0.2
===============
Fixes:
* Don't require rules_java and rules_python for bazel 7 and older (#7052)
* Fix issues with aspect templates not available in query sync mode (#7048)
* Support aspect template for Python (#7011)
* Properly support non-Java Bazel 8 projects in IntelliJ (#7006)
* Fixed Java enabled condition in SyncAspectTemplateProvider (#6987)

v2024.11.05
===========
Features:
Expand Down
10 changes: 6 additions & 4 deletions aspect/intellij_info_impl.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ load(":flag_hack.bzl", "FlagHackInfo")

load("@intellij_aspect_template//:java_info.bzl", "get_java_info", "java_info_in_target", "java_info_reference")

load("@intellij_aspect_template//:python_info.bzl", "get_py_info", "py_info_in_target")

load("@intellij_aspect_template//:code_generator_info.bzl", "CODE_GENERATOR_RULE_NAMES")

load(
Expand Down Expand Up @@ -341,7 +343,7 @@ def _do_starlark_string_expansion(ctx, name, strings, extra_targets = []):

def collect_py_info(target, ctx, semantics, ide_info, ide_info_file, output_groups):
"""Updates Python-specific output groups, returns false if not a Python target."""
if not PyInfo in target or _is_language_specific_proto_library(ctx, target, semantics):
if not py_info_in_target(target) or _is_language_specific_proto_library(ctx, target, semantics):
return False

py_semantics = getattr(semantics, "py", None)
Expand All @@ -351,7 +353,7 @@ def collect_py_info(target, ctx, semantics, ide_info, ide_info_file, output_grou
py_launcher = None

sources = sources_from_target(ctx)
to_build = target[PyInfo].transitive_sources
to_build = get_py_info(target).transitive_sources
args = getattr(ctx.rule.attr, "args", [])
data_deps = getattr(ctx.rule.attr, "data", [])
args = _do_starlark_string_expansion(ctx, "args", args, data_deps)
Expand Down Expand Up @@ -397,14 +399,14 @@ def collect_py_info(target, ctx, semantics, ide_info, ide_info_file, output_grou
def provider_imports_to_attr_imports():
result = []

for provider_import in target[PyInfo].imports.to_list():
for provider_import in get_py_info(target).imports.to_list():
attr_import = provider_import_to_attr_import(provider_import)
if attr_import:
result.append(attr_import)

return result

if target[PyInfo].imports:
if get_py_info(target).imports:
imports.extend(provider_imports_to_attr_imports())

runfiles = target[DefaultInfo].default_runfiles
Expand Down
9 changes: 9 additions & 0 deletions aspect_template/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ filegroup(
"code_generator_info.template.bzl",
"java_info.bzl",
"java_info.template.bzl",
"python_info.bzl",
"python_info.template.bzl"
],
visibility = ["//visibility:public"],
)
Expand All @@ -23,6 +25,13 @@ genrule(
cmd = STRIP_TEMPLATE_SCRIPT,
)

genrule(
name = "generate_py_info_template",
srcs = ["python_info.bzl"],
outs = ["python_info.template.bzl"],
cmd = STRIP_TEMPLATE_SCRIPT,
)

genrule(
name = "generate_code_generator_info_template",
srcs = ["code_generator_info.bzl"],
Expand Down
37 changes: 37 additions & 0 deletions aspect_template/python_info.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# TEMPLATE-INCLUDE-BEGIN
###if( $isPythonEnabled == "true" && $bazel8OrAbove == "true" )
##load("@rules_python//python:defs.bzl", "PyInfo")
###end
# TEMPLATE-INCLUDE-END

def py_info_in_target(target):
# TEMPLATE-IGNORE-BEGIN
return PyInfo in target
# TEMPLATE-IGNORE-END

# TEMPLATE-INCLUDE-BEGIN
## #if( $isPythonEnabled == "true" )
## return PyInfo in target
## #else
## return None
## #end
# TEMPLATE-INCLUDE-END

def get_py_info(target):
# TEMPLATE-IGNORE-BEGIN
if PyInfo in target:
return target[PyInfo]
else:
return None
# TEMPLATE-IGNORE-END

# TEMPLATE-INCLUDE-BEGIN
## #if( $isPythonEnabled == "true" )
## if PyInfo in target:
## return target[PyInfo]
## else:
## return None
## #else
## return None
## #end
# TEMPLATE-INCLUDE-END
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ public class SyncAspectTemplateProvider implements SyncListener {

private final static String TEMPLATE_JAVA = "java_info.template.bzl";
private final static String REALIZED_JAVA = "java_info.bzl";
private final static String TEMPLATE_PYTHON = "python_info.template.bzl";
private final static String REALIZED_PYTHON = "python_info.bzl";
private final static String TEMPLATE_CODE_GENERATOR = "code_generator_info.template.bzl";
private final static String REALIZED_CODE_GENERATOR = "code_generator_info.bzl";

Expand All @@ -54,6 +56,15 @@ public void onSyncStart(Project project, BlazeContext context, SyncMode syncMode
prepareProjectAspect(project);
}

@Override
public void onQuerySyncStart(Project project, BlazeContext context) {
try {
prepareProjectAspect(project);
} catch (SyncFailedException e) {
throw new RuntimeException(e);
}
}

private void prepareProjectAspect(Project project) throws SyncFailedException {
var manager = BlazeProjectDataManager.getInstance(project);

Expand All @@ -77,7 +88,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);
writeLanguageInfos(manager, realizedAspectsPath, templateAspects, project);
writeCodeGeneratorInfo(manager, project, realizedAspectsPath, templateAspects);
}

Expand Down Expand Up @@ -105,26 +116,46 @@ private void writeCodeGeneratorInfo(
}
}

private void writeJavaInfo(
BlazeProjectDataManager manager,
Path realizedAspectsPath,
File templateAspects) throws SyncFailedException {
var realizedFile = realizedAspectsPath.resolve(REALIZED_JAVA);
private void writeLanguageInfos(
BlazeProjectDataManager manager,
Path realizedAspectsPath,
File templateAspects,
Project project) throws SyncFailedException {
var templateLanguageStringMap = getLanguageStringMap(manager);
writeLanguageInfo(manager, realizedAspectsPath, templateAspects, TEMPLATE_JAVA, REALIZED_JAVA, templateLanguageStringMap);
writeLanguageInfo(manager, realizedAspectsPath, templateAspects, TEMPLATE_PYTHON, REALIZED_PYTHON, templateLanguageStringMap);
}

private void writeLanguageInfo(
BlazeProjectDataManager manager,
Path realizedAspectsPath,
File templateAspects,
String templateFileName,
String realizedFileName,
Map<String, String> templateLanguageStringMap) throws SyncFailedException {
var realizedFile = realizedAspectsPath.resolve(realizedFileName);
var templateWriter = new TemplateWriter(templateAspects.toPath());
var templateVariableMap = getJavaStringStringMap(manager);
if (!templateWriter.writeToFile(TEMPLATE_JAVA, realizedFile, templateVariableMap)) {
throw new SyncFailedException("Could not create template for: " + REALIZED_JAVA);
if (!templateWriter.writeToFile(templateFileName, realizedFile, templateLanguageStringMap)) {
throw new SyncFailedException("Could not create template for: " + realizedFileName);
}
}

private static @NotNull Map<String, String> getJavaStringStringMap(BlazeProjectDataManager manager) {
var projectData = Optional.ofNullable(manager.getBlazeProjectData()); // It can be empty on intial sync. Fall back to no lauguage support
private static @NotNull Map<String, String> getLanguageStringMap(BlazeProjectDataManager manager) {
var projectData = Optional.ofNullable(manager.getBlazeProjectData()); // It can be empty on intial sync. Fall back to no language support
var activeLanguages = projectData.map(it -> it.getWorkspaceLanguageSettings().getActiveLanguages()).orElse(ImmutableSet.of());
// TODO: adapt the logic to query sync
boolean isQuerySync = projectData.map(BlazeProjectData::isQuerySync).orElse(false);
var externalWorkspaceData = isQuerySync ? null : projectData.map(BlazeProjectData::getExternalWorkspaceData).orElse(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) || activeLanguages.contains(LanguageClass.GENERIC) ? "true" : "false"
);
var isJavaEnabled = activeLanguages.contains(LanguageClass.JAVA) &&
(isQuerySync || (externalWorkspaceData != null && (!isAtLeastBazel8 || externalWorkspaceData.getByRepoName("rules_java") != null)));
var isPythonEnabled = activeLanguages.contains(LanguageClass.PYTHON) &&
(isQuerySync || (externalWorkspaceData != null && (!isAtLeastBazel8 || externalWorkspaceData.getByRepoName("rules_python") != null)));
return Map.of(
"bazel8OrAbove", isAtLeastBazel8 ? "true" : "false",
"isJavaEnabled", isJavaEnabled ? "true" : "false",
"isPythonEnabled", isPythonEnabled ? "true" : "false"
);
}

private static List<String> ruleNamesForLanguageClass(LanguageClass languageClass, ProjectViewSet viewSet) {
Expand Down

0 comments on commit 3c6e732

Please sign in to comment.