bazelbuild / bazelbuild/bazel

BAZEL_TEST should not be set when using `bazel run some_test`

Open
#27,141 0 comments 1 reaction 0 assignees View on GitHub
team-Core type: bug untriaged
Dominant language
Java
Stars
25.8k
Forks
4.6k
Avg merge
2d 20h
Merged PRs (30d)
72

Description

### Description of the bug:

The [documentation](https://bazel.build/reference/test-encyclopedia#initial-conditions) for that variable says:

> `BAZEL_TEST` Signifies test executable is being driven by `bazel test`

However, it is defined by test-setup.sh which is injected for both `bazel test` and `bazel run`: https://github.com/bazelbuild/bazel/blob/e6186027bcc0de2a5059783fa746cd9b88369d65/tools/test/test-setup.sh#L36

This means that `BAZEL_TEST` cannot be used to detect if a test is being run within the special environment used by `bazel test` which seems to make it unfit for purpose. In particular, we want logic like `useColor = isatty() || runningInBazelTest()` because `bazel test` captures stdout and forwards it indirectly to the terminal (when using `--test_output=streamed`) and correctly handles filtering out colors when run like `bazel test foo_test | blah`.

I could see a few solutions here, in personal order of preference:
1) Stop wrapping tests with `test-setup.sh` when using `bazel run`. Most of what it does is undesirable in that case
2) Set the `BAZEL_TEST` variable from bazel itself rather than `test-setup.sh` so it is only set when using `bazel test`.
99) Fix the docs to say "Signifies test executable is being driven by `bazel test` or `bazel run`". This seems like the least desirable option while still being technically a fix. If you choose it it would be nice to have some other blessed way to know if we are actually in `bazel test`

### Which category does this issue belong to?

_No response_

### What's the simplest, easiest way to reproduce this bug? Please provide a minimal example if possible.

Write a `cc_test` that prints the value of the `BAZEL_TEST` environment variable and `bazel run` it. For human inspection you could even just do `system("env | grep BAZEL_TEST");`.

I'm not sure if `test-setup.sh` is used by `sh_test` but if so, that would be even easier.

### Which operating system are you running Bazel on?

_No response_

### What is the output of `bazel info release`?

_No response_

### If `bazel info release` returns `development version` or `(@non-git)`, tell us how you built Bazel.

_No response_

### What's the output of `git remote get-url origin; git rev-parse HEAD` ?

```text

```

### If this is a regression, please try to identify the Bazel commit where the bug was introduced with bazelisk --bisect.

_No response_

### Have you found anything relevant by searching the web?

Discussed a bit in this bazel slack #cc [thread](https://bazelbuild.slack.com/archives/CGA9QFQ8H/p1759138501534379)

### Any other information, logs, or outputs that you want to share?

_No response_

Contributor guide

Open the contributing guide

Research direction

Start with tools/test/test-setup.sh and the test-encyclopedia initial-conditions documentation, then reproduce the reported environment-variable behavior with a minimal cc_test and bazel run. Compare it with bazel test, and consider the listed alternatives before verifying that the chosen behavior and documentation consistently distinguish the two commands.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp, shell
Domain
build-system, testing-qa
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.