Skip to content

Commit

Permalink
feat: add ability to pass user flags to python interpreter (#442)
Browse files Browse the repository at this point in the history
This change is meant to demonstrate the proposed update for the
following feature request:
#436

---

### Changes are visible to end-users: yes

Users can influence behaviour of interpreter by passing additional flags
via interpreter_args attribute

### Test plan

- Manual testing;

Modified py_binary with new attribute

```
py_binary(
    name = "foo",
    srcs = [...],
    deps = [...],
    interpreter_options = ["-m", "debugpy", "--listen", "12345", "--wait-for-client"],
)
```

Ran bazel target

`bazel run //path/to/rules_py/py_binary/target:foo`

From VSCode side:

- installed Python Debugger extension
- updated launch.json with

```
{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python Debugger: Attach",
            "type": "debugpy",
            "request": "attach",
            "connect": {
              "host": "localhost",
              "port": 12345
            }
          }
    ]
}
```

Used Run&Debug tab in VSCode to set breakpoints, connect to program and
debug.

I also tried to do proposed here
(#436) way with select
and `-c dbg`, it works, but less minimalistic to show proposed change
  • Loading branch information
muravev-vasilii authored Dec 10, 2024
1 parent 743e9a8 commit e4f87b0
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 5 deletions.
5 changes: 3 additions & 2 deletions docs/py_binary.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions docs/py_test.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion py/private/py_binary.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def _py_binary_rule_impl(ctx):
output = executable_launcher,
substitutions = {
"{{BASH_RLOCATION_FN}}": BASH_RLOCATION_FUNCTION,
"{{INTERPRETER_FLAGS}}": " ".join(py_toolchain.flags),
"{{INTERPRETER_FLAGS}}": " ".join(py_toolchain.flags + ctx.attr.interpreter_options),
"{{VENV_TOOL}}": to_rlocation_path(ctx, venv_toolchain.bin),
"{{ARG_COLLISION_STRATEGY}}": ctx.attr.package_collisions,
"{{ARG_PYTHON}}": to_rlocation_path(ctx, py_toolchain.python) if py_toolchain.runfiles_interpreter else py_toolchain.python.path,
Expand Down Expand Up @@ -154,6 +154,10 @@ A collision can occour when multiple packages providing the same file are instal
default = "error",
values = ["error", "warning", "ignore"],
),
"interpreter_options": attr.string_list(
doc = "Additional options to pass to the Python interpreter in addition to -B and -I passed by rules_py",
default = [],
),
"_run_tmpl": attr.label(
allow_single_file = True,
default = "//py/private:run.tmpl.sh",
Expand Down

0 comments on commit e4f87b0

Please sign in to comment.