`env` attribute of `*_test` rules should be able to overwrite variables like `PATH`
Description of the problem / feature request:
The test rule attribute env should be able to overwrite environment variables, not just inject additional ones.
Feature requests: what underlying problem are you trying to solve with this feature?
I am writing tests which require a custom PATH value.
One could modify the PATH environment variable inside the test implementation, but I consider this a hacky workaround.
--test_env can do the required task, but it influences all tests, whereas I want to influence just some specific tests.
Bugs: what's the simplest, easiest way to reproduce this bug? Please provide a minimal example if possible.
BUILD file
sh_test(
name = "foo",
srcs = ["foo.sh"],
env = {
"PATH": "foo",
"FOO": "bar",
},
)
test implementation
#!/usr/bin/env bash
echo $PATH
echo $FOO
command: bazel test --test_output=all //:foo
The test will not print the expected foo.
What operating system are you running Bazel on?
Ubuntu 20.04
What's the output of bazel info release?
release 4.1.0
Have you found anything relevant by searching the web?
The documentation explicitly states Specifies additional environment ....
It seems the current behavior is intended. Still, I believe being able to overwrite PATH is a valid feature.
No open issues discussing overwriting environment variables could be found.
Looks like this is only an issue with native tests, passing over to server rules team to make a policy decision. Hopefully this problem goes away since Starlark doesn't use this mechanism in any case?
@janakdr Does this mean we will lose this feature completely when the language specific rules are migrated to Starlark? This would surprise me since the env and env_inherit attributes are recent additions from Bazel 4.0.0. I would have hoped the Starlark based rules will gain this feature as well eventually.
I think it's the opposite: in a Starlark action, you can set the
environment to whatever you like via the env parameter of actions.run.
I'll defer to the rules team for a definitive answer, though.
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.
The issue is still relevant and unresolved
+1 to this request.
@comius I think this should be higher than a P4.
I submitted https://github.com/bazelbuild/bazel/pull/22420 to fix this
Can confirm this works now as described in my request.