bazelbuild / bazel-skylib

Common useful functions and rules for Bazel
https://bazel.build/
Apache License 2.0
394 stars 180 forks source link

Analysis tests cannot see TestEnvironment added environment variables in actions #335

Open rickeylev opened 2 years ago

rickeylev commented 2 years ago

Test rules can return testing.TestEnvironment to include custom environment variables for when the test is run. This works, however, they are not visible in the action object that an analysis test sees.

(incomplete) repro:

def foo_test_impl(ctx):
  return [testing.TestEnvironment({"FOO": "BAR"})]

def verify_foo_test(ctx):
  env = analysistest.begin(ctx)
  actions = analysistest.target_actions(env)
  for a in actions:
    print(a.env)
  return analysistest.end(env)

Expected: The "Testing ..." action should have FOO=BAR as one of its environment variable.

Actual: The FOO=BAR env var is not shown in the action's env values.

fmeum commented 2 years ago

@rickeylev I briefly looked into this, but already failed to get the actual test action in Starlark. All I'm seeing is:

[action 'Writing script test/undertest', action 'Creating source manifest for //test:undertest', action 'Creating runfiles tree bazel-out/k8-fastbuild/bin/test/undertest.runfiles', runfiles for //test:undertest]

If I were able to get the Testing ... action, I think I know how to fix this in Bazel and write a test for it. Could you give me some pointers?

tetromino commented 2 years ago

Confirmed. But I think this is a bug in Bazel, not in Skylib.

Note that in Bazel 6, testing.TestEnvironment is deprecated in favor of RunEnvironmentInfo, but the environment set by RunEnvironmentInfo is also not visible in analysis tests.