From fe607fdaf038403b56800adaf7e6b40ac6c8c457 Mon Sep 17 00:00:00 2001 From: Laurenz Date: Sat, 16 Dec 2023 15:19:14 +0100 Subject: [PATCH] Bazel 5/6/7 compatibility --- .bazelrc | 3 +++ .bazelversion | 2 +- .github/workflows/ci.yaml | 5 +++-- tests/BUILD | 2 +- tests/appimage_test_cc_with_sh_test.sh | 4 +++- tests/test.cc | 15 +++++++++++---- tests/test.py | 12 +++++++++--- 7 files changed, 31 insertions(+), 12 deletions(-) diff --git a/.bazelrc b/.bazelrc index 04578a2..80632fa 100644 --- a/.bazelrc +++ b/.bazelrc @@ -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 diff --git a/.bazelversion b/.bazelversion index 19b860c..35907cd 100644 --- a/.bazelversion +++ b/.bazelversion @@ -1 +1 @@ -6.4.0 +7.x diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index ce4a5f3..24171cd 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -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: @@ -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 //... diff --git a/tests/BUILD b/tests/BUILD index 7a6bfe7..56e6364 100644 --- a/tests/BUILD +++ b/tests/BUILD @@ -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", ) diff --git a/tests/appimage_test_cc_with_sh_test.sh b/tests/appimage_test_cc_with_sh_test.sh index 31f54f9..1f67ef6 100755 --- a/tests/appimage_test_cc_with_sh_test.sh +++ b/tests/appimage_test_cc_with_sh_test.sh @@ -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 diff --git a/tests/test.cc b/tests/test.cc index fbd6845..ebe5f81 100644 --- a/tests/test.cc +++ b/tests/test.cc @@ -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; diff --git a/tests/test.py b/tests/test.py index 7da6856..27585b9 100644 --- a/tests/test.py +++ b/tests/test.py @@ -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: