Skip to content

Commit 4ce6ca9

Browse files
hvadehrarules_java Copybara
authored andcommitted
Validate java_common.compile(strict_deps) in Starlark
Before this, passing an invalid value would crash Bazel PiperOrigin-RevId: 760518664 Change-Id: If482d29e36b40529a9f2c0be523944163408327f
1 parent 94426d1 commit 4ce6ca9

File tree

3 files changed

+90
-1
lines changed

3 files changed

+90
-1
lines changed

java/private/java_common_internal.bzl

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,14 @@ load(":native.bzl", "get_internal_java_common")
2929

3030
# copybara: default multiline visibility
3131

32+
_STRICT_DEPS_VALUES = [
33+
"OFF", # Silently allow referencing transitive dependencies.
34+
"WARN", # Warn about transitive dependencies being used directly.
35+
"ERROR", # Fail the build when transitive dependencies are used directly.
36+
"STRICT", # Transition to strict by default.
37+
"DEFAULT", # When no flag value is specified on the command line.
38+
]
39+
3240
def compile(
3341
ctx,
3442
output,
@@ -117,6 +125,11 @@ def compile(
117125
get_internal_java_common().check_provider_instances([java_toolchain], "java_toolchain", JavaToolchainInfo)
118126
get_internal_java_common().check_provider_instances(plugins, "plugins", JavaPluginInfo)
119127

128+
# normalize and validate strict_deps
129+
strict_deps = (strict_deps or "default").upper()
130+
if strict_deps not in _STRICT_DEPS_VALUES:
131+
fail("Got an invalid value for strict_deps:", strict_deps, "must be one of:", _STRICT_DEPS_VALUES)
132+
120133
plugin_info = merge_plugin_info_without_outputs(plugins + deps)
121134

122135
all_javac_opts = [] # [depset[str]]
@@ -177,7 +190,7 @@ def compile(
177190
has_sources = source_files or source_jars
178191
has_resources = resources or resource_jars
179192

180-
is_strict_mode = strict_deps.upper() != "OFF"
193+
is_strict_mode = strict_deps != "OFF"
181194
classpath_mode = ctx.fragments.java.reduce_java_classpath()
182195

183196
direct_jars = depset()

test/java/common/java_common_tests.bzl

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ load("//test/java/testutil:rules/custom_library_with_custom_output_source_jar.bz
1919
load("//test/java/testutil:rules/custom_library_with_exports.bzl", "custom_library_with_exports")
2020
load("//test/java/testutil:rules/custom_library_with_named_outputs.bzl", "custom_library_with_named_outputs")
2121
load("//test/java/testutil:rules/custom_library_with_sourcepaths.bzl", "custom_library_with_sourcepaths")
22+
load("//test/java/testutil:rules/custom_library_with_strict_deps.bzl", "custom_library_with_strict_deps")
2223
load("//test/java/testutil:rules/custom_library_with_wrong_plugins_type.bzl", "custom_library_with_wrong_plugins_type")
2324

2425
def _test_compile_default_values(name):
@@ -743,6 +744,56 @@ def _test_compile_neverlink_impl(env, target):
743744
"somedep.jar",
744745
]).in_order()
745746

747+
def _test_compile_strict_deps_case_sensitivity(name):
748+
util.helper_target(
749+
custom_library_with_strict_deps,
750+
name = name + "/enabled",
751+
strict_deps = "error",
752+
)
753+
util.helper_target(
754+
custom_library_with_strict_deps,
755+
name = name + "/disabled",
756+
strict_deps = "off",
757+
)
758+
759+
analysis_test(
760+
name = name,
761+
impl = _test_compile_strict_deps_case_sensitivity_impl,
762+
targets = {
763+
"enabled": name + "/enabled",
764+
"disabled": name + "/disabled",
765+
},
766+
)
767+
768+
def _test_compile_strict_deps_case_sensitivity_impl(env, targets):
769+
env.expect.that_target(targets.enabled).action_named("Javac").contains_flag_values(
770+
[("--strict_java_deps", "ERROR")],
771+
)
772+
env.expect.that_target(targets.disabled).action_named("Javac").not_contains_arg(
773+
"--strict_java_deps",
774+
)
775+
776+
def _test_compile_strict_deps_enum(name):
777+
util.helper_target(
778+
custom_library_with_strict_deps,
779+
name = name + "/custom",
780+
strict_deps = "foo",
781+
)
782+
783+
analysis_test(
784+
name = name,
785+
impl = _test_compile_strict_deps_enum_impl,
786+
target = name + "/custom",
787+
expect_failure = True,
788+
# This is a crash in earlier Bazel versions (i.e. native rules)
789+
attr_values = {"tags": ["min_bazel_8"]},
790+
)
791+
792+
def _test_compile_strict_deps_enum_impl(env, target):
793+
env.expect.that_target(target).failures().contains_predicate(
794+
matching.str_matches("invalid value for strict_deps: FOO"),
795+
)
796+
746797
def java_common_tests(name):
747798
test_suite(
748799
name = name,
@@ -770,5 +821,7 @@ def java_common_tests(name):
770821
_test_compile_custom_output_source_jar,
771822
_test_compile_additional_inputs_and_outputs,
772823
_test_compile_neverlink,
824+
_test_compile_strict_deps_case_sensitivity,
825+
_test_compile_strict_deps_enum,
773826
],
774827
)
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
"""Helper rule for testing compilation with default parameter values"""
2+
3+
load("//java/common:java_common.bzl", "java_common")
4+
load("//java/common:java_semantics.bzl", "semantics")
5+
6+
def _custom_library_impl(ctx):
7+
output_jar = ctx.actions.declare_file("lib" + ctx.label.name + ".jar")
8+
compilation_provider = java_common.compile(
9+
ctx,
10+
output = output_jar,
11+
strict_deps = ctx.attr.strict_deps,
12+
java_toolchain = semantics.find_java_toolchain(ctx),
13+
)
14+
return [DefaultInfo(files = depset([output_jar])), compilation_provider]
15+
16+
custom_library_with_strict_deps = rule(
17+
_custom_library_impl,
18+
attrs = {
19+
"strict_deps": attr.string(),
20+
},
21+
toolchains = [semantics.JAVA_TOOLCHAIN_TYPE],
22+
fragments = ["java"],
23+
)

0 commit comments

Comments
 (0)