From 827d77b6838e74096f1731ff95cdb8050e5caf5f Mon Sep 17 00:00:00 2001 From: Andrew Lindesay Date: Sun, 9 Mar 2025 10:29:57 +1300 Subject: [PATCH 1/6] feat: env-var for additional interpreter args in bootstrap There is no means to be able to provide additional interpreter arguments at launch time. This will make it difficult to run a `rules_python` build product from an IDE. This commit introduces a new env-var `RULES_PYTHON_ADDITIONAL_INTERPRETER_ARGS` which can be provided to the bootstrap stage 1 bash script that will invoke the Python interpreter with additional arguments. Later integration of this feature into any supporting IDE should be easier. --- CHANGELOG.md | 1 + python/private/stage1_bootstrap_template.sh | 6 ++++++ 2 files changed, 7 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index c5bf986216..4517f52ffd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -94,6 +94,7 @@ 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 {#v0-0-0-removed} ### Removed diff --git a/python/private/stage1_bootstrap_template.sh b/python/private/stage1_bootstrap_template.sh index 19ff763094..6e8ae13386 100644 --- a/python/private/stage1_bootstrap_template.sh +++ b/python/private/stage1_bootstrap_template.sh @@ -197,6 +197,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 @@ -215,6 +216,11 @@ 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[@]}") +fi + export RUNFILES_DIR command=( From 9aa5eb6e985bc9872382d959844d3617d94f8d15 Mon Sep 17 00:00:00 2001 From: Andrew Lindesay Date: Mon, 10 Mar 2025 14:05:07 +1300 Subject: [PATCH 2/6] feat: env-var for additional interpreter args in bootstrap - add unset env-var --- python/private/stage1_bootstrap_template.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/python/private/stage1_bootstrap_template.sh b/python/private/stage1_bootstrap_template.sh index 6e8ae13386..4f0dcda8f7 100644 --- a/python/private/stage1_bootstrap_template.sh +++ b/python/private/stage1_bootstrap_template.sh @@ -219,6 +219,7 @@ 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 From 14c2bfc29522083f2163520a29487a417c842434 Mon Sep 17 00:00:00 2001 From: Andrew Lindesay Date: Mon, 10 Mar 2025 18:29:27 +1300 Subject: [PATCH 3/6] feat: env-var for additional interpreter args in bootstrap stage 1 -- add docs --- docs/environment-variables.md | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/docs/environment-variables.md b/docs/environment-variables.md index d50070af55..eb890470ce 100644 --- a/docs/environment-variables.md +++ b/docs/environment-variables.md @@ -1,5 +1,27 @@ # 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 +``` + +::: + :::{envvar} RULES_PYTHON_BOOTSTRAP_VERBOSE When `1`, debug information about bootstrapping of a program is printed to From d7ecc3bd230e7ce820a24b159eca946685d4b234 Mon Sep 17 00:00:00 2001 From: Andrew Lindesay Date: Thu, 13 Mar 2025 10:03:50 +1300 Subject: [PATCH 4/6] feat: env-var for additional interpreter args in bootstrap stage 1 -- fix docs --- docs/environment-variables.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/docs/environment-variables.md b/docs/environment-variables.md index eb890470ce..7d49422a0b 100644 --- a/docs/environment-variables.md +++ b/docs/environment-variables.md @@ -1,6 +1,6 @@ # Environment Variables -:::{envvar} RULES_PYTHON_ADDITIONAL_INTERPRETER_ARGS +::::{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 @@ -20,7 +20,9 @@ in the command executed being; python /path/to/debugger.py --port 12345 --file /path/to/file.py ``` -::: +:::{versionadded} VERSION_NEXT_FEATURE + +:::: :::{envvar} RULES_PYTHON_BOOTSTRAP_VERBOSE From 60268467ed9ce4918822f38fc94fcce7814e4293 Mon Sep 17 00:00:00 2001 From: Richard Levasseur Date: Sun, 16 Mar 2025 20:13:16 -0700 Subject: [PATCH 5/6] add docs to cross ref interpreter_args and env var --- docs/environment-variables.md | 4 ++++ python/private/py_executable.bzl | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/docs/environment-variables.md b/docs/environment-variables.md index 7d49422a0b..c7c0181d18 100644 --- a/docs/environment-variables.md +++ b/docs/environment-variables.md @@ -20,6 +20,10 @@ 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 :::: 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 ::: """, From df484a1d02150e7b49fa05993a54265118bb952d Mon Sep 17 00:00:00 2001 From: Richard Levasseur Date: Sun, 16 Mar 2025 20:18:28 -0700 Subject: [PATCH 6/6] xref changelog --- CHANGELOG.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d96862dedd..15fb211ce8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -94,7 +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 +* (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.