diff --git a/CHANGELOG.md b/CHANGELOG.md index dc2419360c..15fb211ce8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -94,6 +94,9 @@ Unreleased changes template. * (rules) APIs for creating custom rules based on the core py_binary, py_test, and py_library rules ([#1647](https://github.com/bazelbuild/rules_python/issues/1647)) +* (rules) Added env-var to allow additional interpreter args for stage1 bootstrap. + See {obj}`RULES_PYTHON_ADDITIONAL_INTERPRETER_ARGS` environment variable. + Only applicable for {obj}`--bootstrap_impl=script`. * (rules) Added {obj}`interpreter_args` attribute to `py_binary` and `py_test`, which allows pass arguments to the interpreter before the regular args. diff --git a/docs/environment-variables.md b/docs/environment-variables.md index d50070af55..c7c0181d18 100644 --- a/docs/environment-variables.md +++ b/docs/environment-variables.md @@ -1,5 +1,33 @@ # Environment Variables +::::{envvar} RULES_PYTHON_ADDITIONAL_INTERPRETER_ARGS + +This variable allows for additional arguments to be provided to the Python interpreter +at bootstrap time when the `bash` bootstrap is used. If +`RULES_PYTHON_ADDITIONAL_INTERPRETER_ARGS` were provided as `-Xaaa`, then the command +would be; + +``` +python -Xaaa /path/to/file.py +``` + +This feature is likely to be useful for the integration of debuggers. For example, +it would be possible to configure the `RULES_PYTHON_ADDITIONAL_INTERPRETER_ARGS` to +be set to `/path/to/debugger.py --port 12344 --file` resulting +in the command executed being; + +``` +python /path/to/debugger.py --port 12345 --file /path/to/file.py +``` + +:::{seealso} +The {bzl:obj}`interpreter_args` attribute. +::: + +:::{versionadded} VERSION_NEXT_FEATURE + +:::: + :::{envvar} RULES_PYTHON_BOOTSTRAP_VERBOSE When `1`, debug information about bootstrapping of a program is printed to diff --git a/python/private/py_executable.bzl b/python/private/py_executable.bzl index d1905448a6..bbaed3104e 100644 --- a/python/private/py_executable.bzl +++ b/python/private/py_executable.bzl @@ -98,6 +98,10 @@ CPython, see https://docs.python.org/3/using/cmdline.html. Only supported for {obj}`--bootstrap_impl=script`. Ignored otherwise. ::: +:::{seealso} +The {obj}`RULES_PYTHON_ADDITIONAL_INTERPRETER_ARGS` environment variable +::: + :::{versionadded} VERSION_NEXT_FEATURE ::: """, diff --git a/python/private/stage1_bootstrap_template.sh b/python/private/stage1_bootstrap_template.sh index 523210ad14..bd142cf7c7 100644 --- a/python/private/stage1_bootstrap_template.sh +++ b/python/private/stage1_bootstrap_template.sh @@ -202,6 +202,7 @@ stage2_bootstrap="$RUNFILES_DIR/$STAGE2_BOOTSTRAP" declare -a interpreter_env declare -a interpreter_args +declare -a additional_interpreter_args # Don't prepend a potentially unsafe path to sys.path # See: https://docs.python.org/3.11/using/cmdline.html#envvar-PYTHONSAFEPATH @@ -220,6 +221,12 @@ if [[ "$IS_ZIPFILE" == "1" ]]; then interpreter_args+=("-XRULES_PYTHON_ZIP_DIR=$zip_dir") fi +if [[ -n "${RULES_PYTHON_ADDITIONAL_INTERPRETER_ARGS}" ]]; then + read -a additional_interpreter_args <<< "${RULES_PYTHON_ADDITIONAL_INTERPRETER_ARGS}" + interpreter_args+=("${additional_interpreter_args[@]}") + unset RULES_PYTHON_ADDITIONAL_INTERPRETER_ARGS +fi + export RUNFILES_DIR command=(