Repository: google/jax

Issue:
Failure building jaxlib from source on `arm64` with bazel 4.1.0
Please:

- [x] Check for duplicate issues.
- [x] Provide a complete example of how to reproduce the bug, wrapped in triple backticks like this:

```shell
python build/build.py
```

Building `jaxlib` at HEAD on my machine using bazel 4.1.0 fails (macOS 12.0, Macbook Pro 14" M1 Pro 2021). This error seems to come from a bad `xcode-locator` call, possibly because the bazel version that is pulled there (see the traceback below) is configured for the wrong architecture? IIRC, I also do not (yet) have Rosetta installed.

Manually editing the `.bazelversion` file and setting it to `4.2.1` (which enables the use of the latest bazel version provided by `brew`) avoids this issue and builds the wheel successfully (though I have not tested it yet).

- [x] If applicable, include full error messages/tracebacks.

```
➜ python build/build.py

     _   _  __  __
    | | / \ \ \/ /
 _  | |/ _ \ \  /
| |_| / ___ \/  \
 \___/_/   \/_/\_\


b'\x1b[31mERROR: The project you\'re trying to build requires Bazel 4.1.0 (specified in /Users/nicholasjunge/Workspaces/python/jax/.bazelversion), but it wasn\'t found in /opt/homebrew/Cellar/bazel/4.2.1_1/libexec/bin.\x1b[0m\n\nBazel binaries for all official releases can be downloaded from here:\n  https://github.com/bazelbuild/bazel/releases\n\nYou can download the required version directly using this command:\n  (cd "/opt/homebrew/Cellar/bazel/4.2.1_1/libexec/bin" && curl -fLO https://releases.bazel.build/4.1.0/release/bazel-4.1.0-darwin-arm64 && chmod +x bazel-4.1.0-darwin-arm64)\n'
Bazel binary path: ./bazel-4.1.0-darwin-arm64
Bazel version: 4.1.0
Python binary path: /Users/nicholasjunge/Workspaces/python/jax/venv/bin/python
Python version: 3.9
NumPy version: 1.21.4
MKL-DNN enabled: yes
Target CPU: arm64
Target CPU features: release
CUDA enabled: no
TPU enabled: no
ROCm enabled: no

Building XLA and installing it in the jaxlib source tree...
./bazel-4.1.0-darwin-arm64 run --verbose_failures=true --config=mkl_open_source_only :build_wheel -- --output_path=/Users/nicholasjunge/Workspaces/python/jax/dist --cpu=arm64
INFO: Options provided by the client:
  Inherited 'common' options: --isatty=0 --terminal_columns=80
INFO: Reading rc options for 'run' from /Users/nicholasjunge/Workspaces/python/jax/.bazelrc:
  Inherited 'common' options: --experimental_repo_remote_exec
INFO: Reading rc options for 'run' from /Users/nicholasjunge/Workspaces/python/jax/.bazelrc:
  Inherited 'build' options: --apple_platform_type=macos --macos_minimum_os=10.9 --announce_rc --define open_source_build=true --spawn_strategy=standalone --enable_platform_specific_config --define=no_aws_support=true --define=no_gcp_support=true --define=no_hdfs_support=true --define=no_kafka_support=true --define=no_ignite_support=true --define=grpc_no_ares=true -c opt --config=short_logs --copt=-DMLIR_PYTHON_PACKAGE_PREFIX=jaxlib.mlir.
INFO: Reading rc options for 'run' from /Users/nicholasjunge/Workspaces/python/jax/.jax_configure.bazelrc:
  Inherited 'build' options: --strategy=Genrule=standalone --repo_env PYTHON_BIN_PATH=/Users/nicholasjunge/Workspaces/python/jax/venv/bin/python --action_env=PYENV_ROOT --python_path=/Users/nicholasjunge/Workspaces/python/jax/venv/bin/python --distinct_host_configuration=false
INFO: Found applicable config definition build:short_logs in file /Users/nicholasjunge/Workspaces/python/jax/.bazelrc: --output_filter=DONT_MATCH_ANYTHING
INFO: Found applicable config definition build:mkl_open_source_only in file /Users/nicholasjunge/Workspaces/python/jax/.bazelrc: --define=tensorflow_mkldnn_contraction_kernel=1
INFO: Found applicable config definition build:macos in file /Users/nicholasjunge/Workspaces/python/jax/.bazelrc: --config=posix
INFO: Found applicable config definition build:posix in file /Users/nicholasjunge/Workspaces/python/jax/.bazelrc: --copt=-fvisibility=hidden --copt=-Wno-sign-compare --cxxopt=-std=c++14 --host_cxxopt=-std=c++14
Loading:
Loading: 0 packages loaded
WARNING: Download from https://storage.googleapis.com/mirror.tensorflow.org/github.com/llvm/llvm-project/archive/8909dc5ebe8ad39f1743131eb70df402d796acab.tar.gz failed: class com.google.devtools.build.lib.bazel.repository.downloader.UnrecoverableHttpException GET returned 404 Not Found
WARNING: Download from https://storage.googleapis.com/mirror.tensorflow.org/github.com/tensorflow/runtime/archive/fbdd15997f4495011fed44d051152a2a69436e69.tar.gz failed: class com.google.devtools.build.lib.bazel.repository.downloader.UnrecoverableHttpException GET returned 404 Not Found
Analyzing: target //build:build_wheel (0 packages loaded, 0 targets configured)
DEBUG: Rule 'io_bazel_rules_docker' indicated that a canonical reproducible form can be obtained by modifying arguments shallow_since = "1596824487 -0400"
DEBUG: Repository io_bazel_rules_docker instantiated at:
  /Users/nicholasjunge/Workspaces/python/jax/WORKSPACE:37:14: in <toplevel>
  /private/var/tmp/_bazel_nicholasjunge/270a4a78734ae0f3124fa7265b8a65ef/external/org_tensorflow/tensorflow/workspace0.bzl:108:34: in workspace
  /private/var/tmp/_bazel_nicholasjunge/270a4a78734ae0f3124fa7265b8a65ef/external/bazel_toolchains/repositories/repositories.bzl:35:23: in repositories
Repository rule git_repository defined at:
  /private/var/tmp/_bazel_nicholasjunge/270a4a78734ae0f3124fa7265b8a65ef/external/bazel_tools/tools/build_defs/repo/git.bzl:199:33: in <toplevel>
WARNING: Download from https://storage.googleapis.com/mirror.tensorflow.org/github.com/open-source-parsers/jsoncpp/archive/1.9.5.tar.gz failed: class com.google.devtools.build.lib.bazel.repository.downloader.UnrecoverableHttpException GET returned 404 Not Found
INFO: Analyzed target //build:build_wheel (0 packages loaded, 0 targets configured).
INFO: Found 1 target...
[0 / 5] [Prepa] BazelWorkspaceStatusAction stable-status.txt ... (2 actions, 0 running)
ERROR: /private/var/tmp/_bazel_nicholasjunge/270a4a78734ae0f3124fa7265b8a65ef/external/zlib/BUILD.bazel:5:11: Compiling gzclose.c failed: Exec failed due to IOException: com.google.devtools.build.lib.shell.ExecFailedException: java.io.IOException: Cannot run program "/var/tmp/_bazel_nicholasjunge/install/d0b631b3e5a643567f221e133629d965/xcode-locator": error=86, Bad CPU type in executable
Target //build:build_wheel failed to build
INFO: Elapsed time: 0.249s, Critical Path: 0.04s
INFO: 20 processes: 19 internal, 1 local.
FAILED: Build did NOT complete successfully
ERROR: Build failed. Not running target
FAILED: Build did NOT complete successfully
b''
Traceback (most recent call last):
  File "/Users/nicholasjunge/Workspaces/python/jax/build/build.py", line 524, in <module>
    main()
  File "/Users/nicholasjunge/Workspaces/python/jax/build/build.py", line 519, in main
    shell(command)
  File "/Users/nicholasjunge/Workspaces/python/jax/build/build.py", line 53, in shell
    output = subprocess.check_output(cmd)
  File "/opt/homebrew/Cellar/python@3.9/3.9.8/Frameworks/Python.framework/Versions/3.9/lib/python3.9/subprocess.py", line 424, in check_output
    return run(*popenargs, stdout=PIPE, timeout=timeout, check=True,
  File "/opt/homebrew/Cellar/python@3.9/3.9.8/Frameworks/Python.framework/Versions/3.9/lib/python3.9/subprocess.py", line 528, in run
    raise CalledProcessError(retcode, process.args,
subprocess.CalledProcessError: Command '['./bazel-4.1.0-darwin-arm64', 'run', '--verbose_failures=true', '--config=mkl_open_source_only', ':build_wheel', '--', '--output_path=/Users/nicholasjunge/Workspaces/python/jax/dist', '--cpu=arm64']' returned non-zero exit status 36.
```


Discussion:
@hawkinsp since you are building jaxlib for mac today

Resolving patch:
diff --git a/build/build.py b/build/build.py
--- a/build/build.py
+++ b/build/build.py
@@ -90,40 +90,40 @@ def check_numpy_version(python_bin_path):
 
 # Bazel
 
-BAZEL_BASE_URI = "https://github.com/bazelbuild/bazel/releases/download/4.1.0/"
+BAZEL_BASE_URI = "https://github.com/bazelbuild/bazel/releases/download/4.2.1/"
 BazelPackage = collections.namedtuple("BazelPackage",
                                       ["base_uri", "file", "sha256"])
 bazel_packages = {
     ("Linux", "x86_64"):
         BazelPackage(
             base_uri=None,
-            file="bazel-4.1.0-linux-x86_64",
+            file="bazel-4.2.1-linux-x86_64",
             sha256=
-            "0eb2e378d2782e7810753e2162245ad1179c1bb12f848c692b4a595b4edf779b"),
+            "1a4f3a3ce292307bceeb44f459883859c793436d564b95319aacb8af1f20557c"),
     ("Linux", "aarch64"):
         BazelPackage(
             base_uri=None,
-            file="bazel-4.1.0-linux-arm64",
+            file="bazel-4.2.1-linux-arm64",
             sha256=
-            "b3834742166379e52b880319dec4699082cb26fa96cbb783087deedc5fbb5f2b"),
+            "0a849f99d59eab7058212a89b2a0d2b6a17f1ef7ba7fb7a42523a7171bb1c64f"),
     ("Darwin", "x86_64"):
         BazelPackage(
             base_uri=None,
-            file="bazel-4.1.0-darwin-x86_64",
+            file="bazel-4.2.1-darwin-x86_64",
             sha256=
-            "2eecc3abb0ff653ed0bffdb9fbfda7b08548c2868f13da4a995f01528db200a9"),
+            "74d93848f0c9d592e341e48341c53c87e3cb304a54a2a1ee9cff3df422f0b23c"),
     ("Darwin", "arm64"):
         BazelPackage(
             base_uri=None,
-            file="bazel-4.1.0-darwin-arm64",
+            file="bazel-4.2.1-darwin-arm64",
             sha256=
-            "c372d39ab9dac96f7fdfc2dd649e88b05ee4c94ce3d6cf2313438ef0ca6d5ac1"),
+            "f0375d77afe61a7167cd6d9cda1fde0eb29839e0ca8718002da73ea895b31dc0"),
     ("Windows", "AMD64"):
         BazelPackage(
             base_uri=None,
-            file="bazel-4.1.0-windows-x86_64.exe",
+            file="bazel-4.2.1-windows-x86_64.exe",
             sha256=
-            "7b2077af7055b421fe31822f83c3c3c15e36ff39b69560ba2472dde92dd45b46"),
+            "8fa10dfdc8b2f610a8891b68e2a132540e6fac4fea62174cffd9994b9147586e"),
 }
 
 
@@ -185,7 +185,7 @@ def get_bazel_paths(bazel_path_flag):
 
 def get_bazel_path(bazel_path_flag):
   """Returns the path to a Bazel binary, downloading Bazel if not found. Also,
-  checks Bazel's version is at least newer than 4.1.0.
+  checks Bazel's version is at least newer than 4.2.1.
 
   A manual version check is needed only for really old bazel versions.
   Newer bazel releases perform their own version check against .bazelversion
@@ -194,11 +194,11 @@ def get_bazel_path(bazel_path_flag):
   """
   for path in filter(None, get_bazel_paths(bazel_path_flag)):
     version = get_bazel_version(path)
-    if version is not None and version >= (4, 1, 0):
+    if version is not None and version >= (4, 2, 1):
       return path, ".".join(map(str, version))
 
   print("Cannot find or download a suitable version of bazel."
-        "Please install bazel >= 4.1.0.")
+        "Please install bazel >= 4.2.1.")
   sys.exit(-1)
 
 


Test patch: