From e9316f015e68ee81de763600a204157c83425ec4 Mon Sep 17 00:00:00 2001 From: Richard Levasseur Date: Mon, 6 Feb 2023 10:56:29 -0800 Subject: [PATCH] python: Don't require `python` binary for Bazel tests. This fixes the failures on MacOS after the recent upgrade to CI. When `//src/test/py/baze:runfiles_test` sets `--incompatible_use_python_toolchains=false`, it eventuallys tries to search `PATH` for `python`. This is because it falls back to the `--python_path` flag, which has a computed default of "python". Since new Mac versions no longer provide Python 2, this doesn't work. To fix, enable Python toolchains. Comments indicate they were only disabled because Python 3 wasn't available on the Mac CI machines at the time. For pywrapper_test: this was failing because it copies system utilities (e.g `/usr/bin/which`) to another location and tries to run them. For newer MacOS versions, this results in a failure due AMFI (a security mechanism, see https://theevilbit.github.io/posts/amfi_launch_constraints/) To fix, instead of copying the binary, write a wrapper that re-execs the original binary. Work towards #16526, #8169 PiperOrigin-RevId: 507526814 Change-Id: Ifaacc30cb155af30af606254eb7ffcd9304478f6 --- src/test/py/bazel/runfiles_test.py | 4 ---- tools/python/pywrapper_test.py | 7 ++++++- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/test/py/bazel/runfiles_test.py b/src/test/py/bazel/runfiles_test.py index f5bb9b2ff66adb..633548d2580a2f 100644 --- a/src/test/py/bazel/runfiles_test.py +++ b/src/test/py/bazel/runfiles_test.py @@ -47,13 +47,9 @@ def _AssertRunfilesLibraryInBazelToolsRepo(self, family, lang_name): self.AssertExitCode(exit_code, 0, stderr) bazel_bin = stdout[0] - # TODO(brandjon): (Issue #8169) Make this test compatible with Python - # toolchains. Blocked on the fact that there's no PY3 environment on our Mac - # workers (bazelbuild/continuous-integration#578). exit_code, _, stderr = self.RunBazel([ "build", "--verbose_failures", - "--incompatible_use_python_toolchains=false", "//foo:runfiles-" + family ]) self.AssertExitCode(exit_code, 0, stderr) diff --git a/tools/python/pywrapper_test.py b/tools/python/pywrapper_test.py index 14fd0d70e41c62..f5eacd54c6ce61 100644 --- a/tools/python/pywrapper_test.py +++ b/tools/python/pywrapper_test.py @@ -86,7 +86,12 @@ def setup_tool(self, cmd): path = which(cmd) self.assertIsNotNone( path, msg="Could not locate '%s' command on PATH" % cmd) - self.CopyFile(path, os.path.join("dir", cmd), executable=True) + # On recent MacOs versions, copying the coreutils tools elsewhere doesn't + # work -- they simply fail with "Killed: 9". To workaround that, just + # re-exec the actual binary. + self.ScratchFile("dir/" + cmd, + ["#!/bin/sh", 'exec {} "$@"'.format(cmd)], + executable=True) def locate_runfile(self, runfile_path): resolved_path = self.Rlocation(runfile_path)