Skip to content

Commit 5975f13

Browse files
Don't check for native protobuf flags if the proto fragment doesn't exist. (#25803)
PiperOrigin-RevId: 867872524 Co-authored-by: Protobuf Team Bot <protobuf-github-bot@google.com>
1 parent e06c026 commit 5975f13

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

bazel/flags/flags.bzl

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ _FLAGS = {
5050
}
5151

5252
def get_flag_value(ctx, flag_name):
53-
"""Returns the value of the given flag in Starlark if it's set, otherwise reads the Java flag value.
53+
"""Returns the value of the given flag in Starlark if it's set, otherwise reads the Java flag value, if the proto fragment exists.
5454
5555
Args:
5656
ctx: The rule context.
@@ -64,10 +64,19 @@ def get_flag_value(ctx, flag_name):
6464
if flag_name not in _FLAGS:
6565
return getattr(ctx.attr, "_" + flag_name)
6666

67+
starlark_flag = getattr(ctx.attr, "_" + flag_name)
68+
6769
# Label flags don't have a BuildSettingInfo, just get the value.
68-
if "toolchain" in flag_name and getattr(ctx.attr, "_" + flag_name).label != _FLAGS[flag_name].default:
69-
return getattr(ctx.attr, "_" + flag_name)
70-
elif getattr(ctx.attr, "_" + flag_name)[BuildSettingInfo].value != _FLAGS[flag_name].default:
71-
return getattr(ctx.attr, "_" + flag_name)[BuildSettingInfo].value
70+
if "toolchain" in flag_name:
71+
starlark_flag_is_set = starlark_flag.label != _FLAGS[flag_name].default
72+
starlark_value = starlark_flag
73+
else:
74+
starlark_flag_is_set = starlark_flag[BuildSettingInfo].value != _FLAGS[flag_name].default
75+
starlark_value = starlark_flag[BuildSettingInfo].value
76+
77+
# Starlark flags take precedence over native flags.
78+
# Also of course, use the Starlark value if the proto fragment no longer exists.
79+
if starlark_flag_is_set or not hasattr(ctx.fragments, "proto"):
80+
return starlark_value
7281
else:
7382
return _FLAGS[flag_name].native(ctx)

0 commit comments

Comments
 (0)