Skip to content

Commit

Permalink
map_each
Browse files Browse the repository at this point in the history
  • Loading branch information
fmeum committed Feb 1, 2024
1 parent f0b31c4 commit bebd731
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def _android_lint_action(ctx, source_files, source_jars, compilation_info):
args.add_all("--bootclasspath", bootclasspath)
args.add_all("--classpath", classpath)
args.add_all("--lint_rules", compilation_info.plugins.processor_jars)
args.add("--target_label", ctx.label.to_display_form())
args.add_all("--target_label", [ctx.label], map_each = helper.map_to_display_form)

javac_opts = compilation_info.javac_options
if javac_opts:
Expand Down
2 changes: 1 addition & 1 deletion src/main/starlark/builtins_bzl/common/java/java_common.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ def _stamp_jar(actions, jar, java_toolchain, target_label):
args.add(jar)
args.add(output)
args.add("--nostrip_jar")
args.add("--target_label", target_label.to_display_form())
args.add_all("--target_label", [target_label], map_each = helper.map_to_display_form)
actions.run(
mnemonic = "JavaIjar",
inputs = [jar],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ def run_ijar(
args.add(jar)
args.add(output)
if target_label != None:
args.add("--target_label", target_label.to_display_form())
args.add_all("--target_label", [target_label], map_each = helper.map_to_display_form)
if injecting_rule_kind != None:
args.add("--injecting_rule_kind", injecting_rule_kind)

Expand Down
12 changes: 12 additions & 0 deletions src/main/starlark/builtins_bzl/common/java/java_helper.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,17 @@ def _derive_output_file(ctx, base_file, *, name_suffix = "", extension = None, e
new_basename = paths.replace_extension(base_file.basename, name_suffix + "." + extension + extension_suffix)
return ctx.actions.declare_file(new_basename, sibling = base_file)

def _map_to_display_form(label):
"""`map_each` callback that formats a `Label` with `Label.to_display_form()`
Args:
label: (Label) the label of a target.
Returns:
(str) the display form representation of `label`
"""
return label.to_display_form()

helper = struct(
collect_all_targets_as_deps = _collect_all_targets_as_deps,
filter_launcher_for_target = _filter_launcher_for_target,
Expand All @@ -479,4 +490,5 @@ helper = struct(
tokenize_javacopts = _tokenize_javacopts,
detokenize_javacopts = _detokenize_javacopts,
derive_output_file = _derive_output_file,
map_to_display_form = _map_to_display_form,
)
40 changes: 40 additions & 0 deletions src/test/shell/bazel/bazel_java_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -1983,4 +1983,44 @@ EOF
expect_log "buildozer 'add deps @c//:c' //pkg:a"
}

function test_strict_deps_error_external_repo_starlark_action() {
cat << 'EOF' > MODULE.bazel
bazel_dep(
name = "lib_c",
repo_name = "c",
)
local_path_override(
module_name = "lib_c",
path = "lib_c",
)
EOF

mkdir -p pkg
cat << 'EOF' > pkg/BUILD
java_library(name = "a", srcs = ["A.java"], deps = [":b"])
java_library(name = "b", srcs = ["B.java"], deps = ["@c"])
EOF
cat << 'EOF' > pkg/A.java
public class A extends B implements C {}
EOF
cat << 'EOF' > pkg/B.java
public class B implements C {}
EOF

mkdir -p lib_c
cat << 'EOF' > lib_c/MODULE.bazel
module(name = "lib_c")
EOF
cat << 'EOF' > lib_c/BUILD
java_library(name = "c_pregen", srcs = ["C.java"])
java_import(name = "c", jars = ["libc_pregen.jar"], visibility = ["//visibility:public"])
EOF
cat << 'EOF' > lib_c/C.java
public interface C {}
EOF

bazel build //pkg:a >& $TEST_log && fail "build should fail"
expect_log "buildozer 'add deps @c//:c' //pkg:a"
}

run_suite "Java integration tests"

0 comments on commit bebd731

Please sign in to comment.