-
Notifications
You must be signed in to change notification settings - Fork 402
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
rustc: add arbitrary environment variables #314
Conversation
Hi @tommilligan, Thanks for your contribution! Code-wise, this PR looks ok. It is missing the corresponding entry in the doc, but I think it needs to be left away if we move forward with the doc (keeping the thing as undocumented for now). It seems like what you are trying to achieve can be done with |
Sure - for a concrete example, I've removed the diff --git a/test/build_env/BUILD b/test/build_env/BUILD
index 55496c9..5d14626 100644
--- a/test/build_env/BUILD
+++ b/test/build_env/BUILD
@@ -14,7 +14,4 @@ rust_test(
rust_test(
name = "arbitrary_env_test",
srcs = ["tests/arbitrary_env.rs"],
- rustc_env = {
- "USER_DEFINED_KEY": "USER_DEFINED_VALUE",
- },
) I then ran the same test using $ bazel test --action_env=USER_DEFINED_KEY=USER_DEFINED_VALUE //test/build_env:arbitrary_env_test
DEBUG: <snip>
INFO: Build option --action_env has changed, discarding analysis cache.
INFO: Analyzed target //test/build_env:arbitrary_env_test (3 packages loaded, 439 targets configured).
INFO: Found 1 test target...
ERROR: /home/tom/Documents/personal/rules_rust/test/build_env/BUILD:14:1: error executing shell command: '/bin/bash -c CARGO_MANIFEST_DIR=$(pwd)/test/build_env external/rust_linux_x86_64/bin/rustc "$@" --remap-path-prefix="$(pwd)"=__bazel_redacted_pwd test/build_env/tests/arbitrary_env.rs --crate-name...' failed (Exit 1) bash failed: error executing command /bin/bash -c 'CARGO_MANIFEST_DIR=$(pwd)/test/build_env external/rust_linux_x86_64/bin/rustc "$@" --remap-path-prefix="$(pwd)"=__bazel_redacted_pwd' '' test/build_env/tests/arbitrary_env.rs ... (remaining 16 argument(s) skipped)
Use --sandbox_debug to see verbose messages from the sandbox
error: environment variable `USER_DEFINED_KEY` not defined
--> test/build_env/tests/arbitrary_env.rs:3:18
|
3 | let actual = env!("USER_DEFINED_KEY");
| ^^^^^^^^^^^^^^^^^^^^^^^^
error: aborting due to previous error
Target //test/build_env:arbitrary_env_test failed to build
Use --verbose_failures to see the command lines of failed build steps.
INFO: Elapsed time: 0.615s, Critical Path: 0.15s
INFO: 0 processes.
FAILED: Build did NOT complete successfully
//test/build_env:arbitrary_env_test FAILED TO BUILD
FAILED: Build did NOT complete successfully This is due to the fact that in This has been discussed in a couple of places:
A different way to workaround this constraint would be to get the default shell environment (not sure if this is possible), update it with the cargo specific environment variables, then set this as the shell environment. This would mimic the behaviour of If this is possible I'd actually prefer it, but it feels like it could be a breaking change, whereas a |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I prefer this over the alternatives. I let some time for Marco or Alex to chime in.
Taking a page from local_defines in cc_library, perhaps it would be worth indicating if the listed env vars propagate to a dependent rust_library, and if make variable expansion applies or not. A way to do that could be naming it https://docs.bazel.build/versions/master/be/c-cpp.html#cc_library_args Had been dealing with prost-build myself, so this is cool to see! |
@dfreese thanks for the suggestion, if others agree Currently this implementation:
which I'm happy to add to the docstring. |
@tommilligan sounds good. Thank for working on this! @smklein wanted to point this out since you had been looking at cargo raze's handling of build.rs files. |
I've often wanted this myself but usually find better solutions in the end. With prost-build, we don't actually want it to call I think |
@kornholi could you elaborate on why this should be added to
I don't think 1. is relevant, as the environment is only relevant to a specific crate's compilation, and does not propagate up or down the build tree. It's possible that 2. would be useful to inspect the environment when a crate was compiled, but I think in this case the whole of the generated |
You have the right idea, but there's already some code that (incorrectly) assumes |
Looking at it again, the name and behavior of rustc_env is consistent with the rust_flags, so this is good with me. |
I think @kornholi is correct, since we might use the CrateInfo for more than just artifact analysis (e.g. for building compilation database). I would prefer you move it there before we merge or we might forget about it and get bugged by it in the future. |
@damienmg updated. As with |
I think all concerns have been resolved for this PR, so I will go ahead and merge it. Thanks a lot @tommilligan for this! |
The existing rustc_env attribute is useful when the provided env var will not differ between machines, but is not practical when each user of the build script may require a different value, such as passing in a custom path to a tool on their machine. Fixes the issue mentioned in bazelbuild#314 (comment) by starting from the default environment.
The existing rustc_env attribute is useful when the provided env var will not differ between machines, but is not practical when each user of the build script may require a different value, such as passing in a custom path to a tool on their machine. Fixes the issue mentioned in bazelbuild#314 (comment) by starting from the default environment.
The existing rustc_env attribute is useful when the provided env var will not differ between machines, but is not practical when each user of the build script may require a different value, such as passing in a custom path to a tool on their machine. Fixes the issue mentioned in #314 (comment) by starting from the default environment.
Rules that call
rustc
setup environment variables in line withcargo
's behaviour (see the documentation here). However they don't allow passing in additional environment variables that may be required at compile time, such as thePROTOC
variable inprost-build
.This PR adds the
rustc_env
attribute to the common attribute set, allowing it to be set in the following rules:rust_benchmark
rust_binary
rust_library
rust_test
Variables are provided in a string dictionary, and will be merged into the generated environment overriding existing values.