-
-
Notifications
You must be signed in to change notification settings - Fork 645
feat: add --debugger flag #3478
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+198
−11
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
b451db7
refactor: remove semantics
rickeylev f7c6913
feat: add flag to specifying custom debugger
rickeylev c21b861
Update CHANGELOG.md
rickeylev e457445
add howto doc
rickeylev c88eb36
Merge branch 'feat.debugger.flag' of https://github.com/rickeylev/rul…
rickeylev a861c54
Merge branch 'main' of https://github.com/bazel-contrib/rules_python …
rickeylev 4011210
Merge branch 'refactor.cleanup.semantics' into feat.debugger.flag
rickeylev 66dc8c6
fix doc failure
rickeylev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| :::{default-domain} bzl | ||
| ::: | ||
|
|
||
| # How to integrate a debugger | ||
|
|
||
| This guide explains how to use the {obj}`--debugger` flag to integrate a debugger | ||
| with your Python applications built with `rules_python`. | ||
|
|
||
| ## Basic Usage | ||
|
|
||
| The {obj}`--debugger` flag allows you to inject an extra dependency into `py_test` | ||
| and `py_binary` targets so that they have a custom debugger available at | ||
| runtime. The flag is roughly equivalent to manually adding it to `deps` of | ||
| the target under test. | ||
|
|
||
| To use the debugger, you typically provide the `--debugger` flag to your `bazel run` command. | ||
|
|
||
| Example command line: | ||
|
|
||
| ```bash | ||
| bazel run --@rules_python//python/config_settings:debugger=@pypi//pudb \ | ||
| //path/to:my_python_binary | ||
| ``` | ||
|
|
||
| This will launch the Python program with the `@pypi//pudb` dependency added. | ||
|
|
||
| The exact behavior (e.g., waiting for attachment, breaking at the first line) | ||
| depends on the specific debugger and its configuration. | ||
|
|
||
| :::{note} | ||
| The specified target must be in the requirements.txt file used with | ||
| `pip.parse()` to make it available to Bazel. | ||
| ::: | ||
|
|
||
| ## Python `PYTHONBREAKPOINT` Environment Variable | ||
|
|
||
| For more fine-grained control over debugging, especially for programmatic breakpoints, | ||
| you can leverage the Python built-in `breakpoint()` function and the | ||
| `PYTHONBREAKPOINT` environment variable. | ||
|
|
||
| The `breakpoint()` built-in function, available since Python 3.7, | ||
| can be called anywhere in your code to invoke a debugger. The `PYTHONBREAKPOINT` | ||
| environment variable can be set to specify which debugger to use. | ||
|
|
||
| For example, to use `pdb` (the Python Debugger) when `breakpoint()` is called: | ||
|
|
||
| ```bash | ||
| PYTHONBREAKPOINT=pudb.set_trace bazel run \ | ||
| --@rules_python//python/config_settings:debugger=@pypi//pudb \ | ||
| //path/to:my_python_binary | ||
| ``` | ||
|
|
||
| For more details on `PYTHONBREAKPOINT`, refer to the [Python documentation](https://docs.python.org/3/library/functions.html#breakpoint). | ||
|
|
||
| ## Setting a default debugger | ||
|
|
||
| By adding settings to your user or project `.bazelrc` files, you can have | ||
| these settings automatically added to your bazel invocations. e.g. | ||
|
|
||
| ``` | ||
| common --@rules_python//python/config_settings:debugger=@pypi//pudb | ||
| common --test_env=PYTHONBREAKPOINT=pudb.set_trace | ||
| ``` | ||
|
|
||
| Note that `--test_env` isn't strictly necessary. The `py_test` and `py_binary` | ||
| rules will respect the `PYTHONBREAKPOINT` environment variable in your shell. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| """Flag to tell if exec or target mode is active.""" | ||
|
|
||
| load(":py_internal.bzl", "py_internal") | ||
|
|
||
| def _bazel_config_mode_impl(ctx): | ||
| return [config_common.FeatureFlagInfo( | ||
| value = "exec" if py_internal.is_tool_configuration(ctx) else "target", | ||
| )] | ||
|
|
||
| bazel_config_mode = rule( | ||
| implementation = _bazel_config_mode_impl, | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see that the
debugpycan be used either as a CLI viapython -m debugpywhich could work well with ourmain_moduleor viaimport debugpythat would automatically work with this as well. To be truly useful, it would be nice to give at least one example how to setup the debugger. That can be done in a separate PR though.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What does
python -m debugpydo? Just start a debugpy server?debugpy is particularly interesting because its a client/server design, so it can can actually Just Work with
bazel test(where stdin is gone).