Skip to content

Commit 73dc9f3

Browse files
java-team-github-botGuice Team
authored andcommitted
Add a workaround to unbreak the gen_maven_artifact invocation.
PiperOrigin-RevId: 573252178
1 parent 6e9176d commit 73dc9f3

File tree

2 files changed

+27
-8
lines changed

2 files changed

+27
-8
lines changed

core/src/com/google/inject/BUILD

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
# Copyright 2011 Google Inc. All rights reserved.
2-
# Author: sameb@google.com (Sam Berlin)
3-
41
load("@rules_java//java:defs.bzl", "java_library")
5-
load("//:mvn.bzl", "gen_maven_artifact")
62
load(
73
"//:build_defs.bzl",
84
"JAVAC_OPTS",
95
"POM_VERSION",
106
)
7+
load("//:mvn.bzl", "gen_maven_artifact")
8+
9+
# Copyright 2011 Google Inc. All rights reserved.
10+
# Author: sameb@google.com (Sam Berlin)
1111

1212
package(
1313
default_visibility = ["//:src"],

mvn.bzl

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414

1515
"""starlark macros to generate maven files."""
1616

17-
load("@google_bazel_common//tools/javadoc:javadoc.bzl", "javadoc_library")
1817
load("@google_bazel_common//tools/jarjar:jarjar.bzl", "jarjar_library")
18+
load("@google_bazel_common//tools/javadoc:javadoc.bzl", "javadoc_library")
1919
load("@google_bazel_common//tools/maven:pom_file.bzl", "pom_file")
2020

2121
ExportInfo = provider(
@@ -44,8 +44,8 @@ def _validate_target_libs_rule_impl(ctx):
4444
artifact.
4545
"""
4646
target = ctx.attr.target
47-
expected = [lib.label for lib in target[ExportInfo].exports.to_list()]
48-
actual = [lib.label for lib in ctx.attr.actual_target_libs]
47+
expected = [lib.label for lib in target[ExportInfo].exports.to_list() if str(lib.label) not in ctx.attr.optional_target_libs]
48+
actual = [lib.label for lib in ctx.attr.actual_target_libs if lib.label not in ctx.attr.optional_target_libs]
4949
missing = sorted(['"{}"'.format(x) for x in expected if x not in actual])
5050
extra = sorted(['"{}"'.format(x) for x in actual if x not in expected])
5151
if missing or extra:
@@ -60,6 +60,10 @@ _validate_target_libs_rule = rule(
6060
attrs = {
6161
"target": attr.label(aspects = [_collect_exports_aspect]),
6262
"actual_target_libs": attr.label_list(),
63+
64+
# This is a string_list instead of label_list to allow exluding
65+
# transitive exports for which the caller lacks visibility.
66+
"optional_target_libs": attr.string_list(),
6367
},
6468
)
6569

@@ -71,6 +75,7 @@ def gen_maven_artifact(
7175
javadoc_srcs,
7276
packaging = "jar",
7377
artifact_target_libs = [],
78+
optional_artifact_target_libs = [],
7479
is_extension = False):
7580
"""Generates files required for a maven artifact.
7681
@@ -81,6 +86,10 @@ def gen_maven_artifact(
8186
artifact_target: The target containing the actual maven target.
8287
artifact_target_libs: The list of dependencies that should be packaged together with artifact_target,
8388
corresponding to the list of targets exported by artifact_target.
89+
optional_artifact_target_libs: The list of labels that are allowed to be
90+
omitted from artifact_target_libs. Other than these exceptions, all
91+
transitive exports of artifact_target must be included in
92+
artifact_target_libs.
8493
javadoc_srcs: Source files used to generate the Javadoc maven artifact.
8594
packaging: The packaging used for the artifact, default is "jar".
8695
is_extension: Whether the maven artifact is a Guice extension or not.
@@ -91,17 +100,24 @@ def gen_maven_artifact(
91100
name = name + "_validate_target_libs",
92101
target = artifact_target,
93102
actual_target_libs = artifact_target_libs,
103+
optional_target_libs = optional_artifact_target_libs,
94104
)
95105

96106
group_id = "com.google.inject"
97107
if is_extension:
98108
group_id = "com.google.inject.extensions"
99109

100-
# TODO: get artifact_target_libs from bazel and remove the need to pass this in explictly.
110+
# Ideally we would get artifact_target_libs from bazel and remove the need
111+
# to pass this in explictly. However, this doesn't seem possible in Starlark.
112+
# A more significant refactoring of how we instantiate the rules here, or
113+
# extensions to the rule implementation(s), may be necessary.
101114
artifact_targets = [artifact_target] + artifact_target_libs
102115

103116
pom_file(
104117
name = "pom",
118+
# pom_file already scans the transitive deps and exports
119+
# so passing artifact_targets instead of artifact_target seems to be
120+
# redundant.
105121
targets = artifact_targets,
106122
preferred_group_ids = [
107123
"com.google.inject",
@@ -132,6 +148,9 @@ def gen_maven_artifact(
132148
name = artifact_id + "-javadoc",
133149
srcs = javadoc_srcs,
134150
testonly = 1,
151+
# javadoc_library already collects the transitive deps
152+
# so passing artifact_targets instead of artifact_target appears to
153+
# be redundant
135154
deps = artifact_targets,
136155
)
137156

0 commit comments

Comments
 (0)