Skip to content

Conversation

@mbland
Copy link
Collaborator

@mbland mbland commented Sep 14, 2025

Description

Implements the "values are subject to $(location) and "Make variable" substitution" contract for the common env attribute for binary and test rules. Fixes #1770.

Motivation

Makes env attribute semantics for Scala binary and test targets conformant with those documented in the Bazel docs:

Most of the new functions could live in a separate repository at some point.

As for why we're using the "deprecated" ctx.expand_make_variables:

The "deprecated" comment in the ctx.expand_make_variables docstring has existed from the beginning of the file's existence (2018-05-11):

Implements the "values are subject to `$(location)` and "Make variable"
substitution" contract for the common `env` attribute for binary and
test rules. Fixes bazel-contrib#1770.

---

Makes `env` attribute semantics for Scala binary and test targets
conformant with those documented in the Bazel docs:

- https://bazel.build/reference/be/common-definitions#common-attributes-binaries
- https://bazel.build/reference/be/common-definitions#common-attributes-tests
- https://bazel.build/reference/be/make-variables#use
- https://bazel.build/rules/lib/providers/RunEnvironmentInfo

Most of the new functions could live in a separate repository at some
point.

As for why we're using the "deprecated" `ctx.expand_make_variables`:

- bazelbuild/bazel#5859
- bazelbuild/bazel-skylib#486

The "deprecated" comment in the `ctx.expand_make_variables` docstring
has existed from the beginning of the file's existence (2018-05-11):

- bazelbuild/bazel@abbb900
gemini-code-assist[bot]

This comment was marked as spam.

gemini-code-assist[bot]

This comment was marked as spam.

@bazel-contrib bazel-contrib deleted a comment from gemini-code-assist bot Sep 14, 2025
@bazel-contrib bazel-contrib deleted a comment from gemini-code-assist bot Sep 14, 2025
Copy link
Collaborator

@WojciechMazur WojciechMazur left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Updates `run_environment_info` to take a list of `attr.label_list`
values directly instead of attribute names. This is less likely to
silently fail, since a misspelled `ctx.attr` member will break the
build.
@mbland
Copy link
Collaborator Author

mbland commented Sep 14, 2025

@WojciechMazur Thanks for the quick turnaround. I've actually just pushed three small new changes; I'll merge once CI passes:

  • I updated the run_environment_info API to take a list of attr.label_list instead of a list of string. That should prevent silent breakages if a user misspells an attribute name.

  • I added a new shell test, test_scala_test_env_attribute_with_env_inherit_and_test_env, to clarify the expected interations between env, env_inherit, and --test_env. Basically, precedence is env, --test_env, env_inherit, except on macOS, where env_inherit overrides env. I'll file a Bazel bug eventually.

  • I added .gemini/config.yaml to disable Gemini Code Assist for this repo.

@WojciechMazur @simuons After this lands, I'll start a new version bump pull request. Once that lands, what do you think about pushing v7.2.0? (Or should it be v7.1.2, if we consider implementing this behavior a bugfix rather than a new feature?)

This test helps clarify and codify the existing relationship between the
`env` attribute, the `env_inherit` attribute, and the `--test_env` flag.
As it turns out, if Bazel is actually `bazel.exe`, then Bash _will not_
export its environment variables to the Bazel process on Windows. This
means that `env_inherit` functionality will have no effect, breaking
test_scala_test_env_attribute_with_env_inherit_and_test_env. In this
case, we _must_ export them as Command Prompt variables.

Setting `bazel_bin` in `setup_suite` and using it in
`test_scala_binary_env_attribute_expansion` made that test case more
portable as well.
Apparently our Windows build in continuous integration actually much
prefers using `bazel` to `bazel.exe`:

- https://buildkite.com/bazel/rules-scala-scala/builds/5785#019949dc-4cbb-4224-9d10-b7e0aacb5503/69-156

```txt
/c/b/bk-windows-dmg8/bazel/rules-scala-scala/test/shell/test_env_attribute_expansion.sh: line 26: /c/bazel/bazel
/c/bazel/bazel.exe: No such file or directory
/c/b/bk-windows-dmg8/bazel/rules-scala-scala/test/shell/test_env_attribute_expansion.sh: line 29: /c/bazel/bazel
/c/bazel/bazel.exe: No such file or directory
/c/b/bk-windows-dmg8/bazel/rules-scala-scala/tmp/test_env_attribute_expansion/expected.txt 2025-09-14 20:19:57.299728200 +0000
/c/b/bk-windows-dmg8/bazel/rules-scala-scala/tmp/test_env_attribute_expansion/actual.txt 2025-09-14 20:19:57.298429500 +0000
@@ -1,7 +0,0 @@
-LOCATION: West of House
-DATA_PATH: test/data/foo.txt
-DEP_PATH: test/HelloLib.jar
-SRC_PATH: test/EnvAttributeBinary.scala
-BINDIR: bazel-out/
-FROM_TOOLCHAIN_VAR: bazquux
-ESCAPED: $(rootpath //test/data:foo.txt) $(BINDIR) $UNKNOWN
```
@mbland
Copy link
Collaborator Author

mbland commented Sep 14, 2025

  • Basically, precedence is env, --test_env, env_inherit, except on macOS, where env_inherit overrides env. I'll file a Bazel bug eventually.

Wow, I was wrong about this being a Bazel bug on macOS. I ended up going on a bit of a journey to fix test_env_attribute_expansion.sh on Windows, but it's all working now. Will merge once CI finishes.

As for why I was wrong:

  • For starters, I had one of the environment variables still set to the empty string in my macOS environment, which is why I thought there was a problem. But then the macOS CI build broke, prompting me to find the stale environment variable in my shell as the culprit.

  • As it turns out, env_inherit will override env, not the other way around.

  • On Windows, when Bazel is in the path as bazel.exe, then MSYS2 Bash will not pass its environment variables to the Bazel process, breaking the test cases that validate env_inherit behavior. As you can see, I had to add code to test_env_attribute_expansion.sh to find the Bazel executable, then wrap bazel.exe in a .bat file to get the correct behavior in test_scala_test_env_attribute_with_env_inherit_and_test_env.

I have no idea why the Buildkite Windows environment uses bazel, and my Windows VM uses bazel.exe. I studied bazelbuild/continuous-integration a bit, especially buildkite/bazelci.py, and can't yet really see how it does it. But that test is finally working everywhere, so I'm happy for now.

@mbland mbland merged commit 4395bf4 into bazel-contrib:master Sep 14, 2025
1 check passed
@mbland mbland deleted the implement-env branch September 14, 2025 21:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Conform to *_test $(location) and "Make variable" substitution behavior from native *_test rules

2 participants