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](https://github.com/bazelbuild/bazel/issues/18839). Closes #311 COPYBARA_INTEGRATE_REVIEW=https://github.com/bazelbuild/rules_java/pull/311 from KrishnaM256:master 78942a24d9e34daa2ec9cecd8473dbfbb8c64059 PiperOrigin-RevId: 792108185 Change-Id: Ia29301538eaa3f7ca1195230abb32536dbe52e4f
diff --git a/java/bazel/rules/bazel_java_binary.bzl b/java/bazel/rules/bazel_java_binary.bzl index da54722..1e4411e 100644 --- a/java/bazel/rules/bazel_java_binary.bzl +++ b/java/bazel/rules/bazel_java_binary.bzl
@@ -232,7 +232,7 @@ jvm_flags_for_launcher = [] for flag in jvm_flags: jvm_flags_for_launcher.extend(ctx.tokenize(flag)) - return _create_windows_exe_launcher(ctx, java_executable, classpath, main_class, jvm_flags_for_launcher, runfiles_enabled, executable) + return _create_windows_exe_launcher(ctx, java_executable, classpath, main_class, jvm_flags_for_launcher, runfiles_enabled, coverage_enabled, executable, coverage_main_class) if runfiles_enabled: prefix = "" if helper.is_absolute_target_platform_path(ctx, java_executable) else "${JAVA_RUNFILES}/" @@ -277,13 +277,15 @@ return "$(rlocation " + paths.normalize(workspace_prefix + file.short_path) + ")" -def _create_windows_exe_launcher(ctx, java_executable, classpath, main_class, jvm_flags_for_launcher, runfiles_enabled, executable): +def _create_windows_exe_launcher(ctx, java_executable, classpath, main_class, jvm_flags_for_launcher, runfiles_enabled, coverage_enabled, executable, coverage_main_class): launch_info = ctx.actions.args().use_param_file("%s", use_always = True).set_param_file_format("multiline") launch_info.add("binary_type=Java") launch_info.add(ctx.workspace_name, format = "workspace_name=%s") launch_info.add("1" if runfiles_enabled else "0", format = "symlink_runfiles_enabled=%s") launch_info.add(java_executable, format = "java_bin_path=%s") launch_info.add(main_class, format = "java_start_class=%s") + if coverage_enabled: + launch_info.add(coverage_main_class, format = "jacoco_main_class=%s") launch_info.add_joined(classpath, map_each = _short_path, join_with = ";", format_joined = "classpath=%s", omit_if_empty = False) launch_info.add_joined(jvm_flags_for_launcher, join_with = "\t", format_joined = "jvm_flags=%s", omit_if_empty = False) launch_info.add(semantics.find_java_runtime_toolchain(ctx).java_home_runfiles_path, format = "jar_bin_path=%s/bin/jar.exe")