-
Notifications
You must be signed in to change notification settings - Fork 4.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use build settings (or equivalent) for arbitrary string attributes #14859
Comments
Sounds like the request is for there to be a string-valued equivalent to |
Yeah, sort of. The difference being that it isn't monomorphic; it's not "forwarding" the value. So that leads to some consideration of how it's actually consumed.
I'm not really sure what the "slippery slope" leads down to. Configurable builds? What would really be great is a global "environment variable"-type API. tag = env.get("DOCKER_TAG", "")
docker_image(
name = "example",
tag = tag,
) Bazel's currently configurability seems almost purposefully difficult. Something like this would be useful in an infinite number of scenarios, without deliberate cooperation of rule authors and users. |
What about using varref? I’m not super familiar with it but from the documentation it seems that they can be used in rules that are subject to “Make” variable substitution. |
|
|
This feature would be very useful for us. For example, assume we are developing an iOS app framework. We can provide an
However, we don't know the bundle id of the app users wants to build beforehand. If we can pass the value of a string flag to a string attribute:
then users can easily provide the bundle id like this: bazel build //app:app. --//app:bundle_id=com.xxx.yy |
@pauldraper as far as I understand your requirement, it looks like Usage is something along the lines of doing Keep in mind though that the variable gets resolved either in the analysis stage or in the execution stage (I don't remember which), meaning that you can't e.g process its value in a macro for example. For some reason this flag is barely documented though, at least in the context of this use case. It seems significantly more documented in the context of Anyway, hope this helps. |
I created bazelbuild/bazel-skylib#440 to make the string- and int-valued build settings defined there usable wherever |
It seems this still requires cooperation from rule authors to do the substitution. |
I don't see why it's a hard or bad idea to have a variable available even as early as the loading phase. Currently, my best workaround is to write a All the ad-hoc customizability I could want. tools/bazel #!/usr/bin/env bash
tmp="$(mktemp)"
(echo -n 'VARS = '; jq -n '$ENV') > "$tmp"
if diff -q "$tmp" vars.bzl > /dev/null; then
rm "$tmp"
else
mv "$tmp" vars.bzl
fi
exec "$BAZEL_REAL" "$@" BUILD.bazel load("@//:vars.bzl", VARS)
example_rule(
name = "example",
example = VARS.get("EXAMPLE"),
) Then EXAMPLE=example bazel build //:example |
I did a review pass over bazelbuild/bazel-skylib#440, which brings build settings up to par with But yes, these approaches still require rule cooperation. And they don't work at all for certain native rule attributes. A completely generic solution would take some Bazel refactoring. I'm not sure offhand was the original reason was to restrict make variable interpretation to a specifically opted in set of attributes, vs. making it universal. I know there was reluctance to support make variables at all. But I hear you on the use case. |
One more thing: if we have global variables that could theoretically be consumed anywhere (no consumes declare that they need it), it's hard to figure out what needs to be invalidated when a variable changes between builds. So it makes it harder to tune fast incremental builds. |
The loading/analysis phases get recalculated. But in my experience analysis cache is already dumped fairly indiscriminately when flags change. Doesn't seem to change the status quo much. |
Thank you for contributing to the Bazel repository! This issue has been marked as stale since it has not had any activity in the last 1+ years. It will be closed in the next 90 days unless any other activity occurs. If you think this issue is still relevant and should stay open, please post any comment here and the issue will no longer be marked as stale. |
Surprisingly, there is no good way to set string attributes to an arbitrary value from the command line.
select
can configure virtually any rule attribute. However, the value must come from an enumerated list.label_flag
can use any value specified on the command line, but it only works for label attributes.build_setting
can use any value specified on the command line, but it requires the rule author to accept a label and interpret the providers.For example, suppose I have:
I want to set the tag on the command line, but I have to convince rule authors to never use
attr.string()
.(FYI, for the specific case of docker_image, rules_docker does in fact support "make expansions" of the tag attribute, using the the undocumented ctx.version_file API in conjunction with workspace status. But you could imagine many cases where users may want to have a "dynamic" string attribute that the rule authors didn't image.)
The text was updated successfully, but these errors were encountered: