Skip to content

Commit

Permalink
Fix runtime classpath for bazel java_test
Browse files Browse the repository at this point in the history
The native rule used three slightly different runtime classpaths in different cases. This fixes the case for adding `$test_support` to the classpath for the one version check action conditioned on the value of `--explicit_java_test_deps`

Fixes #17985

PiperOrigin-RevId: 523359353
Change-Id: I57b6b7b6af95708a62e47c3395de0292c7e15cbc
  • Loading branch information
hvadehra authored and copybara-github committed Apr 11, 2023
1 parent b4ef50c commit 036991e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
10 changes: 7 additions & 3 deletions src/main/starlark/builtins_bzl/bazel/java/bazel_java_binary.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ load(":common/paths.bzl", "paths")
JavaInfo = _builtins.toplevel.JavaInfo

def _bazel_java_binary_impl(ctx):
return _bazel_base_binary_impl(ctx, is_test_rule_class = False)

def _bazel_java_test_impl(ctx):
return _bazel_base_binary_impl(ctx, is_test_rule_class = True) + helper.test_providers(ctx)

def _bazel_base_binary_impl(ctx, is_test_rule_class):
deps = _collect_all_targets_as_deps(ctx, classpath_type = "compile_only")
runtime_deps = _collect_all_targets_as_deps(ctx)

Expand Down Expand Up @@ -51,6 +57,7 @@ def _bazel_java_binary_impl(ctx):
executable,
feature_config,
strip_as_default,
is_test_rule_class = is_test_rule_class,
)

if ctx.attr.use_testrunner:
Expand Down Expand Up @@ -85,9 +92,6 @@ def _bazel_java_binary_impl(ctx):

return providers.values()

def _bazel_java_test_impl(ctx):
return _bazel_java_binary_impl(ctx) + helper.test_providers(ctx)

def _collect_all_targets_as_deps(ctx, classpath_type = "all"):
deps = helper.collect_all_targets_as_deps(ctx, classpath_type = classpath_type)

Expand Down
15 changes: 12 additions & 3 deletions src/main/starlark/builtins_bzl/common/java/java_binary.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

""" Implementation of java_binary for bazel """

load(":common/java/java_common.bzl", "BASIC_JAVA_LIBRARY_IMPLICIT_ATTRS", "basic_java_library")
load(":common/java/java_common.bzl", "BASIC_JAVA_LIBRARY_IMPLICIT_ATTRS", "basic_java_library", "collect_deps")
load(":common/java/java_util.bzl", "create_single_jar")
load(":common/java/java_helper.bzl", helper = "util")
load(":common/java/java_semantics.bzl", "semantics")
Expand Down Expand Up @@ -131,7 +131,16 @@ def basic_java_binary(
add_opens = ctx.attr.add_opens,
)
java_info = target["JavaInfo"]
runtime_classpath = java_info.compilation_info.runtime_classpath
runtime_classpath = depset(
order = "preorder",
transitive = [
java_info.transitive_runtime_jars
for java_info in (
collect_deps(ctx.attr.runtime_deps + deps) +
([coverage_config.runner] if coverage_config and coverage_config.runner else [])
)
],
)
if extension_registry_provider:
runtime_classpath = depset(order = "preorder", direct = [extension_registry_provider.class_jar], transitive = [runtime_classpath])
java_info = java_common.merge(
Expand Down Expand Up @@ -193,7 +202,7 @@ def basic_java_binary(
transitive = [output_groups["_source_jars"]],
)

one_version_output = _create_one_version_check(ctx, java_info.transitive_runtime_jars) if (
one_version_output = _create_one_version_check(ctx, java_attrs.runtime_classpath) if (
ctx.fragments.java.one_version_enforcement_on_java_tests or not is_test_rule_class
) else None
validation_outputs = [one_version_output] if one_version_output else []
Expand Down

0 comments on commit 036991e

Please sign in to comment.