Skip to content
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

Extend the pex tool to support debuggers #2141

Merged
merged 3 commits into from Nov 2, 2021
Merged

Extend the pex tool to support debuggers #2141

merged 3 commits into from Nov 2, 2021

Conversation

tiagovtristao
Copy link
Contributor

This PR mainly extends the pex tool to support debuggers. Some refactoring, including files being moved around, was done for better clarity.

port = CONFIG.DEBUG.PORT
if not port:
# A CLI debug session needs access to the source files for improved UX.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is actually also required for IDEs connecting to the debugger, since it needs access to generated artefacts and toolchain sources that don't appear on the original code.

@@ -175,8 +175,8 @@ func TestEnvironment(state *BuildState, target *BuildTarget, testDir string) Bui
if target.HasLabel("cc") {
env = append(env, "GCNO_DIR="+path.Join(RepoRoot, GenDir, target.Label.PackageName))
}
if state.DebugTests {
env = append(env, "DEBUG=true")
if state.DebugFailingTests {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was renamed to avoid any confusion with the more general case of debugging via plz debug

TestResultsFile cli.Filepath `long:"test_results_file" default:"plz-out/log/test_results.xml" description:"File to write combined test results to."`
SurefireDir cli.Filepath `long:"surefire_dir" default:"plz-out/surefire-reports" description:"Directory to copy XML test results to."`
ShowOutput bool `short:"s" long:"show_output" description:"Always show output of tests, even on success."`
Debug bool `short:"d" long:"debug" description:"Allows starting an interactive debugger on test failure. Does not work with all test types (currently only python/pytest, C and C++). Implies -c dbg unless otherwise set."`
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Support for debugging failed C/C++ tests seems to have disappeared since the original commit that introduced it

Comment on lines 12 to +23
if __name__ == '__main__':
if 'PEX_PROFILE_FILENAME' in os.environ:
# If PEX_INTERPRETER is set, then it starts an interactive console.
if os.environ.get('PEX_INTERPRETER', '0') != '0':
import code
resutl = code.interact()
# If PEX_PROFILE_FILENAME is set, then it collects profile information into the filename.
elif os.environ.get('PEX_PROFILE_FILENAME'):
with profile(os.environ['PEX_PROFILE_FILENAME'])():
result = run()
else:
result = run()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consolidate different execution paths (i.e. based on external factors such as env vars) here.

# `DEBUG_PORT` is read by the pex generated for debuggers that support server mode.
cmd = f"{bin}" if not port else f"DEBUG_PORT={port} {bin}"

cmd = f"{cmd} {flags}"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: would probably add this to above rather than creating another string (since it is fairly simple)

Copy link
Member

@Tatskaari Tatskaari left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me! Some of this will need to be migrated over to the plugin though i.e. the build def and pex tool changes.

@@ -158,7 +158,7 @@ var opts struct {
CoverageXMLReport cli.Filepath `long:"coverage_xml_report" default:"plz-out/log/coverage.xml" description:"XML File to write combined coverage results to."`
Incremental bool `short:"i" long:"incremental" description:"Calculates summary statistics for incremental coverage, i.e. stats for just the lines currently modified."`
ShowOutput bool `short:"s" long:"show_output" description:"Always show output of tests, even on success."`
Debug bool `short:"d" long:"debug" description:"Allows starting an interactive debugger on test failure. Does not work with all test types (currently only python/pytest, C and C++). Implies -c dbg unless otherwise set."`
DebugFailingTest bool `short:"d" long:"debug" description:"Allows starting an interactive debugger on test failure. Does not work with all test types (currently only python/pytest). Implies -c dbg unless otherwise set."`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh this is cool!

@@ -103,6 +103,9 @@ def python_binary(name:str, main:str, srcs:list=[], resources:list=[], out:str=N
by the dependent python_library rules, and this rule simply builds the
metadata for it and concatenates them all together.

This target can be debugged via:
plz debug [--debugger=[gdb|debugpy]] [--port] //:target
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean pdb?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch

debug_cmd, debugger = _debug_cmd("./$OUT")
if debug_cmd:
if debugger == "debugpy":
cmd = cmd.replace(' --zip_safe', '')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

qq: why is this needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

debugpy was freaking out during some file access of some sort IIRC

@@ -205,6 +215,9 @@ def python_test(name:str, srcs:list, data:list|dict=[], resources:list=[], deps:
on which is set for the test runner, which can be configured either via the python_test_runner
package property or python.testrunner in the config.

This target can be debugged via:
plz debug [--debugger=[gdb|debugpy]] [--port] //:target
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here

@tiagovtristao tiagovtristao merged commit 4176a6c into thought-machine:master Nov 2, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants