bazel icon indicating copy to clipboard operation
bazel copied to clipboard

`env` attribute of `*_test` rules should be able to overwrite variables like `PATH`

Open martis42 opened this issue 4 years ago • 8 comments

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.

martis42 avatar Dec 13 '21 21:12 martis42

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 avatar Dec 25 '21 04:12 janakdr

@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.

martis42 avatar Dec 27 '21 09:12 martis42

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.

janakdr avatar Dec 28 '21 16:12 janakdr

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.

github-actions[bot] avatar Feb 07 '24 01:02 github-actions[bot]

The issue is still relevant and unresolved

martis42 avatar Feb 07 '24 20:02 martis42

+1 to this request.

mgottscho avatar May 09 '24 20:05 mgottscho

@comius I think this should be higher than a P4.

brentleyjones avatar May 16 '24 20:05 brentleyjones

I submitted https://github.com/bazelbuild/bazel/pull/22420 to fix this

keith avatar May 16 '24 21:05 keith

Can confirm this works now as described in my request.

martis42 avatar Jul 13 '24 14:07 martis42