Skip to content

Commit

Permalink
Bazel 5/6/7 compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
lalten committed Dec 16, 2023
1 parent d078814 commit fe607fd
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 12 deletions.
3 changes: 3 additions & 0 deletions .bazelrc
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
build --color=yes
build --cxxopt=-std=c++20
build --incompatible_strict_action_env
build --keep_going
common --noenable_bzlmod # This line is automatically removed in CI for Bazel 5
test --announce_rc
test --keep_going
test --spawn_strategy=sandboxed
test --test_env=NO_CLEANUP=1
test --test_env=PYTEST_ADDOPTS="--color=yes --capture=tee-sys --tb=short --showlocals -ra --verbose --verbose"
test --test_env=USE_BAZEL_VERSION # Pass this on to tests to enable adapting to bazel-version specific behavior
test --test_output=errors
test --test_verbose_timeout_warnings
2 changes: 1 addition & 1 deletion .bazelversion
Original file line number Diff line number Diff line change
@@ -1 +1 @@
6.4.0
7.x
5 changes: 3 additions & 2 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
strategy:
fail-fast: false
matrix:
bazel-version: ["5.x", "6.x", "latest"]
bazel-version: ["5.x", "6.x", "7.x"]
os: ["ubuntu-latest", "macos-latest"]

steps:
Expand All @@ -25,7 +25,8 @@ jobs:
path: "~/.cache/bazelisk\n~/.cache/bazel\n"
key: bazel-${{ matrix.bazel-version }}-cache-${{ github.run_id }}
restore-keys: bazel-${{ matrix.bazel-version }}-cache-
- run: sed -i.bak '/remove if Bazel version < 6/d' tests/test.cc && rm tests/test.cc.bak

- run: sed -i.bak '/enable_bzlmod/d' .bazelrc && rm .bazelrc.bak
if: ${{ startsWith(matrix.bazel-version, '5') }}

- run: bazel test //...
Expand Down
2 changes: 1 addition & 1 deletion tests/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ py_binary(
":external_bin.appimage",
":symlink_and_emptyfile",
],
env = {"MY_BINARY_ENV": "not propagated by rules_python :("},
env = {"MY_BINARY_ENV": "propagated only in Bazel 7+"},
main = "test.py",
)

Expand Down
4 changes: 3 additions & 1 deletion tests/appimage_test_cc_with_sh_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@

set -eux

exec env --ignore-environment "tests/appimage_test_cc" --appimage-extract-and-run
exec env --ignore-environment \
USE_BAZEL_VERSION="${USE_BAZEL_VERSION:-latest}" \
"tests/appimage_test_cc" --appimage-extract-and-run
15 changes: 11 additions & 4 deletions tests/test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,25 @@ int main(int argc, char** argv, char** envp) {
// When running inside the appimage, we want the env to not be lost.
bool have_binary_env = false;
bool have_appimage_env = false;
std::string bazel_version{"unknown"};
for (char** env = envp; *env != 0; env++) {
char* thisEnv = *env;
std::string thisEnv{*env};
std::cout << thisEnv << std::endl;
if (std::string(thisEnv) == "MY_BINARY_ENV=not lost") {
if (thisEnv == "MY_BINARY_ENV=not lost") {
have_binary_env = true;
} else if (std::string(thisEnv) == "MY_APPIMAGE_ENV=overwritten") {
} else if (thisEnv == "MY_APPIMAGE_ENV=overwritten") {
have_appimage_env = true;
} else if (thisEnv.starts_with("USE_BAZEL_VERSION=")) {
bazel_version = thisEnv.substr(18);
}
}
if (!have_binary_env) {
std::cerr << "MY_BINARY_ENV not found or has wrong value" << std::endl;
return EXIT_FAILURE; // remove if Bazel version < 6
if (bazel_version.starts_with("5")) {
// This is only expected to work in Bazel 6+
} else {
return EXIT_FAILURE;
}
}
if (!have_appimage_env) {
std::cerr << "MY_APPIMAGE_ENV not found or has wrong value" << std::endl;
Expand Down
12 changes: 9 additions & 3 deletions tests/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,15 @@ def test_runfiles_symlinks() -> None:

def test_binary_env() -> None:
"""Test that env attr on the binary target is handled."""
# Unfortunately rules_python does not seem to set the RunEnvironmentInfo provider.
# See https://github.com/bazelbuild/rules_python/issues/901
assert "MY_BINARY_ENV" not in os.environ
bazel_version = os.getenv("USE_BAZEL_VERSION", "latest")
if bazel_version.startswith(("latest", "7.")):
# Bazel >= 7.0.0 does set the RunEnvironmentInfo provider 🎉.
assert os.getenv("MY_BINARY_ENV") == "propagated only in Bazel 7+"
else:
# Otherwise it seems rules_python does not set the RunEnvironmentInfo provider.
# See https://github.com/bazelbuild/rules_python/issues/901
val = os.getenv("MY_BINARY_ENV")
assert val is None, f"Expected env to be missing, but got {val}"


def greeter() -> None:
Expand Down

0 comments on commit fe607fd

Please sign in to comment.