Skip to content

Commit 45061b3

Browse files
KrishnaM256rules_java Copybara
authored andcommitted
Fix Java coverage support on Windows by adding coverage_main_class to launch_info (#311)
### Summary This PR enables Java code coverage to work properly on Windows by adding the `coverage_main_class` to the `launch_info` metadata in `bazel_java_binary.bzl`. Without this addition, Bazel fails to properly instrument and run Java binaries with coverage enabled on Windows platforms. ### Changes - Added `coverage_main_class` to `launch_info` in `bazel_java_binary.bzl` to explicitly define the main class used during coverage runs. ### Motivation - Before this fix, attempts to run Java coverage tests on Windows would not produce proper coverage output. - This is a step toward resolving [bazelbuild/bazel#18839](bazelbuild/bazel#18839). Closes #311 COPYBARA_INTEGRATE_REVIEW=#311 from KrishnaM256:master 78942a2 PiperOrigin-RevId: 792108185 Change-Id: Ia29301538eaa3f7ca1195230abb32536dbe52e4f
1 parent c0462f0 commit 45061b3

1 file changed

Lines changed: 4 additions & 2 deletions

File tree

java/bazel/rules/bazel_java_binary.bzl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ def _create_stub(ctx, java_attrs, launcher, executable, jvm_flags, main_class, c
232232
jvm_flags_for_launcher = []
233233
for flag in jvm_flags:
234234
jvm_flags_for_launcher.extend(ctx.tokenize(flag))
235-
return _create_windows_exe_launcher(ctx, java_executable, classpath, main_class, jvm_flags_for_launcher, runfiles_enabled, executable)
235+
return _create_windows_exe_launcher(ctx, java_executable, classpath, main_class, jvm_flags_for_launcher, runfiles_enabled, coverage_enabled, executable, coverage_main_class)
236236

237237
if runfiles_enabled:
238238
prefix = "" if helper.is_absolute_target_platform_path(ctx, java_executable) else "${JAVA_RUNFILES}/"
@@ -277,13 +277,15 @@ def _format_classpath_entry(runfiles_enabled, workspace_prefix, file):
277277

278278
return "$(rlocation " + paths.normalize(workspace_prefix + file.short_path) + ")"
279279

280-
def _create_windows_exe_launcher(ctx, java_executable, classpath, main_class, jvm_flags_for_launcher, runfiles_enabled, executable):
280+
def _create_windows_exe_launcher(ctx, java_executable, classpath, main_class, jvm_flags_for_launcher, runfiles_enabled, coverage_enabled, executable, coverage_main_class):
281281
launch_info = ctx.actions.args().use_param_file("%s", use_always = True).set_param_file_format("multiline")
282282
launch_info.add("binary_type=Java")
283283
launch_info.add(ctx.workspace_name, format = "workspace_name=%s")
284284
launch_info.add("1" if runfiles_enabled else "0", format = "symlink_runfiles_enabled=%s")
285285
launch_info.add(java_executable, format = "java_bin_path=%s")
286286
launch_info.add(main_class, format = "java_start_class=%s")
287+
if coverage_enabled:
288+
launch_info.add(coverage_main_class, format = "jacoco_main_class=%s")
287289
launch_info.add_joined(classpath, map_each = _short_path, join_with = ";", format_joined = "classpath=%s", omit_if_empty = False)
288290
launch_info.add_joined(jvm_flags_for_launcher, join_with = "\t", format_joined = "jvm_flags=%s", omit_if_empty = False)
289291
launch_info.add(semantics.find_java_runtime_toolchain(ctx).java_home_runfiles_path, format = "jar_bin_path=%s/bin/jar.exe")

0 commit comments

Comments
 (0)