Skip to content

Commit cd9964d

Browse files
bazel-iofmeum
andauthored
[9.0.0] Include allowed attribute values in Stardoc proto (bazelbuild#27678)
The set of allowed values of a `string` or `int` attribute is crucial information for users of a rule or aspect. Closes bazelbuild#25877. PiperOrigin-RevId: 832334774 Change-Id: Ibe005910905bf045de9ef5eb8218cbe12f509724 Commit bazelbuild@58cd0a1 Co-authored-by: Fabian Meumertzheim <fabian@meumertzhe.im>
1 parent 6b7508f commit cd9964d

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

src/main/java/com/google/devtools/build/lib/starlarkdocextract/AttributeInfoExtractor.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,17 @@ static AttributeInfo buildAttributeInfo(ExtractorContext context, Attribute attr
6969
builder.setDefaultValue(UNREPRESENTABLE_VALUE);
7070
}
7171
}
72+
if (attribute.getAllowedValues() instanceof Attribute.AllowedValueSet allowedValueSet) {
73+
for (Object value : allowedValueSet.getAllowedValues()) {
74+
try {
75+
builder.addValues(
76+
StringEncoding.internalToUnicode(
77+
context.labelRenderer().reprWithoutLabelConstructor(value)));
78+
} catch (InvalidStarlarkValueException e) {
79+
builder.addValues(UNREPRESENTABLE_VALUE);
80+
}
81+
}
82+
}
7283
return builder.build();
7384
}
7485

src/main/protobuf/stardoc_output.proto

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,10 @@ message AttributeInfo {
170170

171171
// If true, the attribute is defined in Bazel's native code, not in Starlark.
172172
bool natively_defined = 8;
173+
174+
// If non-empty, the string representations of the allowed values for this
175+
// attribute.
176+
repeated string values = 9;
173177
}
174178

175179
// Representation of a set of providers.

src/test/java/com/google/devtools/build/lib/starlarkdocextract/ModuleInfoExtractorTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -705,7 +705,7 @@ def _my_impl(ctx):
705705
implementation = _my_impl,
706706
attrs = {
707707
"a": attr.string(doc = "My doc", default = "foo"),
708-
"b": attr.string(mandatory = True),
708+
"b": attr.string(mandatory = True, values = ["foo", "bar"]),
709709
"c": attr.label(providers = [MyInfo1, MyInfo2]),
710710
"d": attr.label(providers = [[MyInfo1, MyInfo2], [MyInfo3]]),
711711
"_e": attr.string(doc = "Hidden attribute"),
@@ -729,6 +729,7 @@ def _my_impl(ctx):
729729
.setName("b")
730730
.setType(AttributeType.STRING)
731731
.setMandatory(true)
732+
.addAllValues(ImmutableList.of("\"foo\"", "\"bar\""))
732733
.build(),
733734
AttributeInfo.newBuilder()
734735
.setName("c")

0 commit comments

Comments
 (0)