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

Migrate python debugging logic to rules #34

Merged
merged 4 commits into from
Jan 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .plzconfig
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[Please]
Version = >=16.10.1
Version = >=16.17.0

[Parse]
PreloadBuildDefs = test/build_defs/test.build_defs
Expand Down Expand Up @@ -31,6 +31,10 @@ DefaultValue = unittest
ConfigKey = TestRunnerBootstrap
Optional = true

[PluginConfig "debugger"]
ConfigKey = Debugger
DefaultValue = pdb

[PluginConfig "module_dir"]
ConfigKey = ModuleDir
DefaultValue = third_party.python
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ InterpreterOptions = -b -s
DefaultInterpreter = python3
PexTool = @self//tools/please_pex
TestRunner = unittest
Debugger = pdb
ModuleDir = third_party.python
WheelRepo = https://pypi.org/pypi
WheelNameScheme = None
Expand Down
42 changes: 40 additions & 2 deletions build_defs/python.build_defs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,11 @@ def python_binary(name:str, main:str, srcs:list=[], resources:list=[], out:str=N
if site:
cmd += ' -S'

if CONFIG.BUILD_CONFIG == 'dbg':
# Both `pdb` and `debugpy` require the pex to be exploded to get debugging to work.
cmd = (f'{cmd} -d=' + CONFIG.PYTHON.DEBUGGER).replace(' --zip_safe', '')


assert main not in srcs, "main file should not be included in srcs"

lib_rule = python_library(
Expand Down Expand Up @@ -170,13 +175,17 @@ def python_binary(name:str, main:str, srcs:list=[], resources:list=[], out:str=N
cmd = '$TOOL z -i . -s .pex.zip -s .whl --preamble_from="$SRC" --include_other --add_init_py --strict'
if strip:
cmd += ' --strip_py'

debug_cmd = _debug_cmd("./$OUT")

return build_rule(
name=name,
srcs=[pex_rule],
deps=[lib_rule],
outs=[out or (name + '.pex')],
data=data,
cmd=cmd,
debug_cmd=debug_cmd,
needs_transitive_deps=True,
binary=True,
output_is_complete=True,
Expand Down Expand Up @@ -245,6 +254,10 @@ def python_test(name:str, srcs:list, data:list|dict=[], resources:list=[], deps:
if site:
cmd += ' -S'

if CONFIG.BUILD_CONFIG == 'dbg':
# Both `pdb` and `debugpy` require the pex to be exploded to get debugging to work.
cmd = (f'{cmd} -d=' + CONFIG.PYTHON.DEBUGGER).replace(' --zip_safe', '')

# If the config specifies a PYTHON_TEST_RUNNER_BOOTSTRAP target, then this ought to contain
# the test-runner library (e.g. pytest), and its transitive dependencies. In this case, we just
# add the specified target as a dependency.
Expand Down Expand Up @@ -292,9 +305,17 @@ def python_test(name:str, srcs:list, data:list|dict=[], resources:list=[], deps:
deps = [pex_rule, lib_rule]

test_cmd = f'$TEST {flags}'
if worker:
test_cmd = f'$(worker {worker}) && {test_cmd} '
worker_cmd = f'$(worker {worker})' if worker else ""
if worker_cmd:
test_cmd = f'{worker_cmd} && {test_cmd} '
deps += [worker]

debug_cmd = _debug_cmd(
bin = "./$TEST",
flags = flags,
pre_cmd = worker_cmd,
)

# This rule concatenates the .pex with all the other precompiled zip files from dependent rules.
return build_rule(
name=name,
Expand All @@ -307,6 +328,7 @@ def python_test(name:str, srcs:list, data:list|dict=[], resources:list=[], deps:
labels=labels + ['test_results_dir'],
cmd='$TOOL z -i . -s .pex.zip -s .whl --preamble_from="$SRC" --include_other --add_init_py --strict',
test_cmd=test_cmd,
debug_cmd=debug_cmd,
needs_transitive_deps=True,
output_is_complete=True,
binary=True,
Expand Down Expand Up @@ -647,6 +669,22 @@ def _add_licences(name, output):
if not found:
log.warning('No licence found for %s, should add licences = [...] to the rule', name.lstrip('_').split('#')[0])


# `pdb` and `debugpy` are the current debuggers supported by the PEX tool.
def _debug_cmd(bin:str, flags:str='', pre_cmd:str=''):
if CONFIG.BUILD_CONFIG != 'dbg':
return ""

# `PEX_EXPLODE` will explode the PEX before executing to allow clients to access third party sources files.
# `DEBUG_PORT` is read by the PEX generated for debuggers that support server mode.
cmd = f"{bin} {flags}" if not CONFIG.DEBUG_PORT else f"PEX_EXPLODE=true DEBUG_PORT={CONFIG.DEBUG_PORT} {bin} {flags}"

if pre_cmd:
cmd = f"{pre_cmd} && {cmd}"

return cmd


if CONFIG.BAZEL_COMPATIBILITY:
py_library = python_library
py_binary = python_binary
Expand Down
2 changes: 1 addition & 1 deletion third_party/python/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ python_wheel(
python_wheel(
name = "protobuf",
outs = ["google"],
version = "3.18.1",
version = "3.19.3",
deps = [":six"],
)

Expand Down
2 changes: 1 addition & 1 deletion tools/wheel_resolver/test/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ gentest(
python_wheel(
name = "protobuf",
outs = ["google"],
version = "3.18.1",
version = "3.19.3",
deps = [
"//third_party/python:six",
],
Expand Down