-
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
Add common env attribute for *_binary and *_test #7364
Comments
Also one interesting case we ran into where we did not just need a hardcoded env var, but one with an absolut path to the location of some generated files, so now we are wrapping that target and setting But either way this would be a nice addition. |
I don't have any fundamental counter-arguments, except that it's Yet Another Feature... I would expect that since you control both the test rule and the code it runs, it's just as easy to just do @aehlig , mind giving a second opinion? |
A little more context on why this feature would be valuable for us: |
Erm, sorry about the delay. If we added this feature, we would have to worry about maintaining it forever... I think if there were more people who wanted it, I would be more amenable to it, but as it is, I don't want to add a single-use feature to Bazel. I'll take a look at the code review, but I'm not very enthusiastic and would much prefer not to merge it. |
@lberki is there anything that would make you more enthusiastic about this change? I originally tried to add the feature into |
More use cases and more people to whom this would be useful. |
I have a use case need for this. I have a |
@lberki is this still something you'd be opposed to merging? |
I would like to have this feature for Google to remove some internal hacks in test execution, and I have a patch to implement it. |
@ulfjack I can not find the commit here on github. Is the plan to get this patch merged into open source also? |
It wasn't merged yet - Google doesn't have a separate code base for Bazel. :-) There are some open questions that I haven't addressed yet. I was considering changing the name of the attribute to |
We find value in being able to set env variables via attribute rather than inside tests. For example, one use case we have is the ability to conditionally set env variables for a subset of our repo. Meaning we have a |
@ulfjack Any update on when this will be merged? |
I left Google at the end of January, and I've been exceptionally busy with my new company (www.engflow.com). Not sure when I will have time to look into this again. |
This would be extremely useful for setting the ASAN environment variables when executing tests. |
Running into another case where this is needed. The Go package github.com/mattn/go-oci8 requires @lberki Are the use cases and enthusiasm so far enough? According to @ulfjack, it seems Google would also benefit from this too by reducing some internal hacks. |
@lberki Friendly ping. I am generally willing to work on this feature request, but wouldn't want contributions to get stuck in review due to this being labeled P4. In fact, isn't this essentially a P2 issue already due to it being a blocker for e.g. a starlarkified |
@fmeum I think the turnaround time for reviewing pull requests is much better than responding to issues. I had two pull requests getting merged in reasonable time. |
@oquenchil As we're making progress on starlarkify cc rules, we probably should bump the priority of this issue? |
I have fared well with this strategy so far, so why not try it again. This PR adds |
Is adding I don't see a problem, giving rule writers an option to use The title of the bug says "Add common env attribute for *_binary and *_test". I oppose adding any new attributes like this to standard rules that Bazel implements, because they would hurt hermeticity. Adding it to TestEnvironment and providing extendable standard rules in Starlark, does allow anybody to create custom rules like *_test with it, even if this is a bad idea. |
A missing part is making run + binary work. I'm running into this as part of Starlarkifying the Python rules, where some targets set env for a binary. Given that env is already public, without this feature, an incompatible change is inevitable. It sounds like this problem isn't limited to just the Python rules, either. I dug through the Bazel code yesterday trying to figure out if there's any way a Starlark rule, or special builtin Starlark rule with a Java helper, could make this work without any modifications to (core) Bazel. I don't think there is; there just isn't any point where Starlark code could try to inject or set something somewhere to cause the necessary, lower-level, logic to run. The three basic options appear to be:
(From what I can tell, (2) and (3) would probably require changes within RuleConfiguredTargetBuilder.java#build and/or RunfilesSupport.java#computeActionEnvironmentModify; those seem to be the common code paths that a Starlark rule will go through where the envvars are computed. The logic to do so is already there, it's just only run for test rules) IMHO, (1) seems most preferable. A provider allows a rule to control, filter, and transform the env/env_inherits values before passing them along. There may be a bit of extra boilerplate to e.g. expand makevars, but I don't think that's a big deal. It's easy to imagine that a rule might want to reject or warn about certain envvars, or silently ignore them, or silently tweak them. If Bazel team is still hesitant about this, we can restrict creation of it to builtin rules and revisit making it more generally available later. Always present attributes that are "magical" and implemented directly by Bazel have the advantage of guaranteed consistent behavior. But that's also their major drawback: no control. Having to modify core Bazel (and hence wait for a bazel release) just to pass along some envvar also seems annoying (but to be fair, I wouldn't expect this logic to change much). |
What I had in mind aligns with @rickeylev's option (1) and also what @jbms proposed: Introduce the equivalent of |
Adds an inherited_environment field to testing.TestEnvironment to allow Starlark rules to implement the equivalent of the native rules' `env_inherit` attribute. Work towards bazelbuild#7364. To fully resolve that issue, it remains to handle executable non-test rules. RELNOTES: Starlark test rules can use the new inherited_environment parameter of testing.TestEnvironment to specify environment variables whose values should be inherited from the shell environment.
This sounds reasonable. Are there use cases where a rule would return both TestEnvironment and RunEnvironment, or we should just call it Environment (aliasing and deprecating TestEnvironment) thus covering both use cases? Is there a use case for a library? Should there be an error if this is used in a lib? |
I don't see a reason for a rule to return both (in fact, this duplication could become very confusing and error-prone). It would make sense to just make a renamed Do you have a preference for the Starlark name of the new provider? Does it require namespacing or could it just be called e.g. Also, would you agree with the new provider's key being visible to Starlark? This would allow wrapper rules around binaries and tests to forward the provider (which would be very useful for transition wrappers like the one in #14673).
Transitively collecting general-purpose environment variables doesn't seem like a good use case to me and would probably be prone to conflicts, so I would say this should be an error. |
Adds an inherited_environment field to testing.TestEnvironment to allow Starlark rules to implement the equivalent of the native rules' `env_inherit` attribute. Work towards bazelbuild#7364. To fully resolve that issue, it remains to handle executable non-test rules. RELNOTES: Starlark test rules can use the new inherited_environment parameter of testing.TestEnvironment to specify environment variables whose values should be inherited from the shell environment.
Adds an inherited_environment field to testing.TestEnvironment to allow Starlark rules to implement the equivalent of the native rules' `env_inherit` attribute. Work towards #7364. To fully resolve that issue, it remains to handle executable non-test rules. RELNOTES: Starlark test rules can use the new inherited_environment parameter of testing.TestEnvironment to specify environment variables whose values should be inherited from the shell environment. Closes #14849. PiperOrigin-RevId: 439277689
Adds an inherited_environment field to testing.TestEnvironment to allow Starlark rules to implement the equivalent of the native rules' `env_inherit` attribute. Work towards bazelbuild#7364. To fully resolve that issue, it remains to handle executable non-test rules. RELNOTES: Starlark test rules can use the new inherited_environment parameter of testing.TestEnvironment to specify environment variables whose values should be inherited from the shell environment. Closes bazelbuild#14849. PiperOrigin-RevId: 439277689
|
Adds an inherited_environment field to testing.TestEnvironment to allow Starlark rules to implement the equivalent of the native rules' `env_inherit` attribute. Work towards bazelbuild#7364. To fully resolve that issue, it remains to handle executable non-test rules. RELNOTES: Starlark test rules can use the new inherited_environment parameter of testing.TestEnvironment to specify environment variables whose values should be inherited from the shell environment. Closes bazelbuild#14849. PiperOrigin-RevId: 439277689
Adds an inherited_environment field to testing.TestEnvironment to allow Starlark rules to implement the equivalent of the native rules' `env_inherit` attribute. Work towards bazelbuild#7364. To fully resolve that issue, it remains to handle executable non-test rules. RELNOTES: Starlark test rules can use the new inherited_environment parameter of testing.TestEnvironment to specify environment variables whose values should be inherited from the shell environment. Closes bazelbuild#14849. PiperOrigin-RevId: 439277689 Cherry-pick contains parts of: Delete non-interning, non-singleton @AutoCodec. PiperOrigin-RevId: 411683398
Adds an inherited_environment field to testing.TestEnvironment to allow Starlark rules to implement the equivalent of the native rules' `env_inherit` attribute. Work towards bazelbuild#7364. To fully resolve that issue, it remains to handle executable non-test rules. RELNOTES: Starlark test rules can use the new inherited_environment parameter of testing.TestEnvironment to specify environment variables whose values should be inherited from the shell environment. Closes bazelbuild#14849. PiperOrigin-RevId: 439277689 Cherry-pick contains parts of: Delete non-interning, non-singleton @AutoCodec. PiperOrigin-RevId: 411683398
Adds an inherited_environment field to testing.TestEnvironment to allow Starlark rules to implement the equivalent of the native rules' `env_inherit` attribute. Work towards bazelbuild#7364. To fully resolve that issue, it remains to handle executable non-test rules. RELNOTES: Starlark test rules can use the new inherited_environment parameter of testing.TestEnvironment to specify environment variables whose values should be inherited from the shell environment. Closes bazelbuild#14849. PiperOrigin-RevId: 439277689 Cherry-pick contains parts of: Delete non-interning, non-singleton @AutoCodec. PiperOrigin-RevId: 411683398 Cherry-pick makes the following additional changes: - Replace use of ImmutableMap.buildKeepingLast with .copyOf
Adds an inherited_environment field to testing.TestEnvironment to allow Starlark rules to implement the equivalent of the native rules' `env_inherit` attribute. Work towards bazelbuild#7364. To fully resolve that issue, it remains to handle executable non-test rules. RELNOTES: Starlark test rules can use the new inherited_environment parameter of testing.TestEnvironment to specify environment variables whose values should be inherited from the shell environment. Closes bazelbuild#14849. PiperOrigin-RevId: 439277689 Cherry-pick contains parts of: Delete non-interning, non-singleton @AutoCodec. PiperOrigin-RevId: 411683398 Cherry-pick makes the following additional changes: - Replace use of ImmutableMap.buildKeepingLast with .copyOf
Adds an inherited_environment field to testing.TestEnvironment to allow Starlark rules to implement the equivalent of the native rules' `env_inherit` attribute. Work towards #7364. To fully resolve that issue, it remains to handle executable non-test rules. RELNOTES: Starlark test rules can use the new inherited_environment parameter of testing.TestEnvironment to specify environment variables whose values should be inherited from the shell environment. Closes #14849. PiperOrigin-RevId: 439277689 Cherry-pick contains parts of: Delete non-interning, non-singleton @AutoCodec. PiperOrigin-RevId: 411683398 Cherry-pick makes the following additional changes: - Replace use of ImmutableMap.buildKeepingLast with .copyOf Co-authored-by: Chenchu Kolli <ckolli@google.com>
The new RunEnvironmentInfo provider allows any executable or test Starlark rule to specify the environment for when it is executed, either as part of a test action or via the run command. Refactors testing.TestEnvironment to construct a RunEnvironmentInfo and adds a warning (but not an error) if the provider constructed in this way is returned from a non-executable non-test rule. If a RunEnvironmentInfo is constructed directly via the Starlark constructor, this warning becomes an error. Fixes bazelbuild#7364 Fixes bazelbuild#15224 Fixes bazelbuild#15225 Closes bazelbuild#15232. PiperOrigin-RevId: 448185352
* Specify fixedEnv/inheritedEnv interaction in ActionEnvironment Previously, ActionEnvironment did not publicly document how fixed and inherited environment variables interact, but still cautioned users to keep the two sets disjoint without enforcing this. As a result, neither could users rely on the interaction nor could ActionEnvironment benefit from the additional freedom of not specifying the behavior. With this commit, ActionEnvironment explicitly specifies that the values of environment variable inherited from the client environment take precedence over fixed values and codifies this behavior in a test. This has been the effective behavior all along and has the advantage that users can provide overrideable defaults for environment variables. Closes #15170. PiperOrigin-RevId: 439315634 * Intern trivial ActionEnvironment and EnvironmentVariables instances When an ActionEnvironment is constructed out of an existing one, the ActionEnvironment and EnvironmentVariables instances, which are immutable, can be reused if no variables are added. Also renames addVariables and addFixedVariables to better reflect that they are operating on an immutable type. Closes #15171. PiperOrigin-RevId: 440312159 * Let Starlark executable rules specify their environment The new RunEnvironmentInfo provider allows any executable or test Starlark rule to specify the environment for when it is executed, either as part of a test action or via the run command. Refactors testing.TestEnvironment to construct a RunEnvironmentInfo and adds a warning (but not an error) if the provider constructed in this way is returned from a non-executable non-test rule. If a RunEnvironmentInfo is constructed directly via the Starlark constructor, this warning becomes an error. Fixes #7364 Fixes #15224 Fixes #15225 Closes #15232. PiperOrigin-RevId: 448185352 * Fix strict deps violation ``` src/main/java/com/google/devtools/build/lib/analysis/starlark/StarlarkRuleClassFunctions.java:990: error: [strict] Using type com.google.devtools.build.lib.cmdline.LabelValidator from an indirect dependency (TOOL_INFO: "//src/main/java/com/google/devtools/build/lib/cmdline:LabelValidator"). See command below ** LabelValidator.parseAbsoluteLabel(labelString); ^ ```
Description of the problem / feature request:
In the same way as we can define in code that bazel passes on cli args on
bazel run
andbazel test
it would be great to be able to define env vars in the same way.Feature requests: what underlying problem are you trying to solve with this feature?
Some applications/libraries/tests/test frameworks prefer environment variables over cli args and being able to define that on a per target level in code would save the need to use external tools or
test_env
in a lot of cases. We also have cases where we just need env variables in ci for certain tests (that use docker) and this could also accommodate for that. Also with make var substitution or stamping we could even pass in non static env vars this way.This could also help solving part of the issue described in #6111
Also I noticed on our team, developers where quite surprised when they saw one can set
args
in code but notenv
. So it seems some developers even have some expectations around this behaviour.One could of course work around this by wrapping the binary into a
sh_binary
or similar and that is probably a reasonable solution, but the same would be true forargs
so parity on this would be ideal.Have you found anything relevant by searching the web?
Not really, maybe #955 is somewhat related.
The text was updated successfully, but these errors were encountered: