Skip to content

Bazel 8 compatibility #2349

@simeonwarren

Description

@simeonwarren

Describe the bug
These rules do not work with bazel 8 (8.4.2)

Stacktrace:

USE_BAZEL_VERSION=8.4.2 bazelisk query @all_bindist_toolchains//...
Starting local Bazel server (8.4.2) and connecting to it...
ERROR: Traceback (most recent call last):
	File "/home/simeonwarren/.cache/bazel/_bazel_simeonwarren/64dff1f7bd7cb061c854077d6cce85d2/external/rules_haskell+/haskell/ghc_bindist.bzl", line 5, column 55, in <toplevel>
		load("@bazel_tools//tools/cpp:lib_cc_configure.bzl", "get_cpu_value")
Error: file '@bazel_tools//tools/cpp:lib_cc_configure.bzl' does not contain symbol 'get_cpu_value'

To Reproduce

Reproduction script with both bazel 7 and bazel 8
#!/usr/bin/env sh

set -eu

dir="$(mktemp -d)"
echo "Using temporary directory: ${dir}"
trap "rm -r '${dir}'" EXIT
cd "${dir}"

echo "Setup BUILD.bazel"
touch BUILD.bazel
echo "Setup the patch"
cat - >patch.patch <<'EOF'
diff --git a/haskell/cabal.bzl b/haskell/cabal.bzl
index d9f92908..c6b87e7c 100644
--- a/haskell/cabal.bzl
+++ b/haskell/cabal.bzl
@@ -4,8 +4,8 @@ load("@bazel_skylib//lib:dicts.bzl", "dicts")
 load("@bazel_skylib//lib:paths.bzl", "paths")
 load("@bazel_skylib//lib:sets.bzl", "sets")
 load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe", "read_netrc", "use_netrc")
-load("@bazel_tools//tools/cpp:lib_cc_configure.bzl", "get_cpu_value")
 load("@rules_cc//cc:find_cc_toolchain.bzl", "find_cc_toolchain", "use_cc_toolchain")
+load("//tools:get_cpu_value.bzl", "get_cpu_value")
 load(":cc.bzl", "cc_interop_info", "ghc_cc_program_args")
 load(":haddock.bzl", "generate_unified_haddock_info")
 load(":private/actions/info.bzl", "library_info_output_groups")
diff --git a/haskell/ghc_bindist.bzl b/haskell/ghc_bindist.bzl
index 6c2732de..0474806b 100644
--- a/haskell/ghc_bindist.bzl
+++ b/haskell/ghc_bindist.bzl
@@ -2,10 +2,10 @@

 load("@bazel_skylib//lib:paths.bzl", "paths")
 load("@bazel_tools//tools/build_defs/repo:utils.bzl", "patch")
-load("@bazel_tools//tools/cpp:lib_cc_configure.bzl", "get_cpu_value")
 load("@rules_cc//cc:find_cc_toolchain.bzl", "CC_TOOLCHAIN_TYPE")
 load("@rules_sh//sh:posix.bzl", "sh_posix_configure")
 load("//haskell:ghc.bzl", "DEFAULT_GHC_VERSION")
+load("//tools:get_cpu_value.bzl", "get_cpu_value")
 load(":private/bazel_platforms.bzl", "bazel_platforms")
 load(
     ":private/pkgdb_to_bzl.bzl",
@@ -628,10 +628,7 @@ def _configure_python3_toolchain_impl(repository_ctx):
     else:
         stub_shebang = ""
     repository_ctx.file("BUILD.bazel", executable = False, content = """
-load(
-    "@bazel_tools//tools/python:toolchain.bzl",
-    "py_runtime_pair",
-)
+load("@rules_python//python:py_runtime_pair.bzl", "py_runtime_pair")
 py_runtime(
     name = "python3_runtime",
     interpreter_path = "{python3}",
diff --git a/tools/get_cpu_value.bzl b/tools/get_cpu_value.bzl
new file mode 100644
index 00000000..296bf4b5
--- /dev/null
+++ b/tools/get_cpu_value.bzl
@@ -0,0 +1,40 @@
+# Bazel 7 fix
+# Links:
+# - https://github.com/tweag/rules_sh/blob/4524d11e15bea61c0f6841cbc3ae0bc9c09835d8/sh/private/get_cpu_value.bzl#L4
+# - https://github.com/tweag/rules_sh/blob/4524d11e15bea61c0f6841cbc3ae0bc9c09835d8/sh/private/get_cpu_value.bzl#L4
+def get_cpu_value(repository_ctx):
+    """Compute the cpu_value based on the OS name. Doesn't %-escape the result!
+
+    Args:
+      repository_ctx: The repository context.
+    Returns:
+      One of (darwin, freebsd, x64_windows, ppc, s390x, arm, aarch64, k8, piii)
+    """
+    os_name = repository_ctx.os.name
+    arch = repository_ctx.os.arch
+    if os_name.startswith("mac os"):
+        # Check if we are on x86_64 or arm64 and return the corresponding cpu value.
+        return "darwin_" + ("arm64" if arch == "aarch64" else "x86_64")
+    if os_name.find("freebsd") != -1:
+        return "freebsd"
+    if os_name.find("openbsd") != -1:
+        return "openbsd"
+    if os_name.find("windows") != -1:
+        if arch == "aarch64":
+            return "arm64_windows"
+        else:
+            return "x64_windows"
+
+    if arch in ["power", "ppc64le", "ppc", "ppc64"]:
+        return "ppc"
+    if arch in ["s390x"]:
+        return "s390x"
+    if arch in ["mips64"]:
+        return "mips64"
+    if arch in ["riscv64"]:
+        return "riscv64"
+    if arch in ["arm", "armv7l"]:
+        return "arm"
+    if arch in ["aarch64"]:
+        return "aarch64"
+    return "k8" if arch in ["amd64", "x86_64", "x64"] else "piii"
diff --git a/tools/os_info.bzl b/tools/os_info.bzl
index 5fd021e7..cfdf82a1 100644
--- a/tools/os_info.bzl
+++ b/tools/os_info.bzl
@@ -1,4 +1,4 @@
-load("@bazel_tools//tools/cpp:lib_cc_configure.bzl", "get_cpu_value")
+load(":get_cpu_value.bzl", "get_cpu_value")

 _os_info_bzl_template = """
 cpu_value = "{CPU_VALUE}"
EOF

echo "Setup unmodified MODULE.bazel"
cat - >MODULE.bazel <<'EOF'
bazel_dep(name = "rules_haskell")
bazel_dep(name = "rules_sh", version = "0.5.0")
archive_override(
    module_name = "rules_haskell",
    integrity = "sha256-Sa3Rl+2VNXeURuPkwh+XBWEgZYM3rFYauGb90A2lMHg=",
    strip_prefix = "rules_haskell-7bb6b0790b0c44603173196e1b4d1cd6e5f28078",
    urls = ["https://github.com/tweag/rules_haskell/archive/7bb6b0790b0c44603173196e1b4d1cd6e5f28078.zip"],
)
haskell_toolchains = use_extension(
    "@rules_haskell//extensions:haskell_toolchains.bzl",
    "haskell_toolchains",
)
use_repo(
    haskell_toolchains,
    "all_bindist_toolchains",
)
EOF

echo "Run bazel 7 cmd"
if USE_BAZEL_VERSION=7.6.2 bazelisk query @all_bindist_toolchains//...; then
    echo "Bazel 7 works"
else
    echo "Bazel 7 failed?"
    exit 1
fi
echo "Run bazel 8 cmd"
if USE_BAZEL_VERSION=8.4.2 bazelisk query @all_bindist_toolchains//...; then
    echo "Bazel 8 should have failed without the patch"
    exit 1
else
    echo "Bazel 8 failed without the patch"
fi

echo "Setup MODULE.bazel with the patch"
cat - >MODULE.bazel <<'EOF'
bazel_dep(name = "rules_haskell")
bazel_dep(name = "rules_sh", version = "0.5.0")
archive_override(
    module_name = "rules_haskell",
    integrity = "sha256-Sa3Rl+2VNXeURuPkwh+XBWEgZYM3rFYauGb90A2lMHg=",
    patch_strip = 1,
    patches = ["patch.patch"],
    strip_prefix = "rules_haskell-7bb6b0790b0c44603173196e1b4d1cd6e5f28078",
    urls = ["https://github.com/tweag/rules_haskell/archive/7bb6b0790b0c44603173196e1b4d1cd6e5f28078.zip"],
)
haskell_toolchains = use_extension(
    "@rules_haskell//extensions:haskell_toolchains.bzl",
    "haskell_toolchains",
)
use_repo(
    haskell_toolchains,
    "all_bindist_toolchains",
)
EOF
echo "Run bazel 7 cmd"
if USE_BAZEL_VERSION=7.6.2 bazelisk query @all_bindist_toolchains//...; then
    echo "Bazel 7 works"
else
    echo "Bazel 7 failed?"
    exit 1
fi
echo "Setup .bazelrc"
cat - >.bazelrc <<'EOF'
common --noincompatible_disallow_ctx_resolve_tools
EOF
echo "Run bazel 8 cmd"
if USE_BAZEL_VERSION=8.4.2 bazelisk query @all_bindist_toolchains//...; then
    echo "Bazel 8 works with the patch"
else
    echo "Bazel 8 failed?"
    exit 1
fi

Expected behavior
Rules should work with bazel 8

Environment

  • OS name + version: Fedora Linux 41 (Server Edition)
  • Bazel version: 8.4.2 and 7.6.2
  • Version of the rules: 7bb6b07

Additional context

I've made a PR to fix this, can you take a look?

#2348

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions