From 5e293ad97f9779e8ff82cee143e6a6c40daef703 Mon Sep 17 00:00:00 2001 From: Jon Ross-Perkins Date: Wed, 13 Nov 2024 10:16:49 -0800 Subject: [PATCH] Fix bazel-bin invocations of run_tool (#4521) SCRIPT_LOCATION contains `bazel-out`, so this drops the portions shared between the script and tool locations before removing the suffix. --- toolchain/install/run_tool.bzl | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/toolchain/install/run_tool.bzl b/toolchain/install/run_tool.bzl index eaf2e51a75e3..e9410b51e079 100644 --- a/toolchain/install/run_tool.bzl +++ b/toolchain/install/run_tool.bzl @@ -13,16 +13,26 @@ import sys _SCRIPT_LOCATION = "{0}" _TOOL_LOCATION = "{1}" +# Drop shared parent portions so that we're working with a minimum suffix. +# This is so that both `bazel-out` and manual `bazel-bin` invocations work. +script_parts = _SCRIPT_LOCATION.split('/') +tool_parts = _TOOL_LOCATION.split('/') +while script_parts and tool_parts and script_parts[0] == tool_parts[0]: + del script_parts[0] + del tool_parts[0] +script_suffix = '/' + '/'.join(script_parts) +tool_suffix = '/' + '/'.join(tool_parts) + # Make sure we have the expected structure. -if not __file__.endswith(_SCRIPT_LOCATION): +if not __file__.endswith(script_suffix): exit( "Unable to figure out path:\\n" f" __file__: {{__file__}}\\n" - f" script: {{_SCRIPT_LOCATION}}\\n" + f" script: {{script_suffix}}\\n" ) # Run the tool using the absolute path, forwarding arguments. -tool_path = __file__.removesuffix(_SCRIPT_LOCATION) + _TOOL_LOCATION +tool_path = __file__.removesuffix(script_suffix) + tool_suffix os.execv(tool_path, [tool_path] + sys.argv[1:]) """