bazelbuild / bazel

a fast, scalable, multi-language and extensible build system
https://bazel.build
Apache License 2.0
23.21k stars 4.07k forks source link

py3wrapper.sh should fail with an explicit error message if `which` is not present on the PATH #22535

Open sitaktif opened 5 months ago

sitaktif commented 5 months ago

Description of the bug:

Currently, if which is not present on the system, running a py_binary will fail with the following message:

Error occurred while attempting to use the default Python toolchain (@rules_python//python:autodetecting_toolchain).
Neither 'python3' nor 'python' were found on the target platform's PATH, which is:

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/opt/sdp/bin

Please ensure an interpreter is available on this platform (and marked executable), or else register an appropriate Python toolchain as per the documentation for py_runtime_pair (https://github.com/bazelbuild/rules_python/blob/master/docs/python.md#py_runtime_pair).

However the which command invoked by the wrapper script doesn't fail because python is not present on the PATH, but instead because the which utility is not installed on the system.

The wrapper script should check for the return code of the command: which will return 1 if a command is not found, but the shell will have the subprocess return 127 if which wasn't found (from man bash: If a command is not found, the child process created to execute it returns a status of 127.).

sitaktif commented 5 months ago

This is the relevant part of the code https://github.com/bazelbuild/bazel/blob/68009f84eaa5d095d119bc0c3b8bfd52d3c9e40c/tools/python/pywrapper_template.txt#L48-L64

gibfahn commented 5 months ago

I think if you change which to type -P, then that should have the same result, but type is a builtin (https://www.gnu.org/software/bash/manual/html_node/Bash-Builtins.html#index-type).

miklelappo commented 4 months ago

I met the same error when using bazel on archlinux:latest docker which doesn't have which installed by default. While python3 is in path error message is completely unmeaning. It would worth to indeed use builtins (would this work for zsh, fish, ....) or just check if which is installed on a system. I'd opt for the last option