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

Move generic debug adapter settings to dedicated subsystem (Cherry-pick of #15928) #15977

Merged
merged 1 commit into from
Jun 28, 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
4 changes: 3 additions & 1 deletion src/python/pants/backend/python/goals/pytest_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
TestResult,
TestSubsystem,
)
from pants.core.subsystems.debug_adapter import DebugAdapterSubsystem
from pants.core.util_rules.config_files import ConfigFiles, ConfigFilesRequest
from pants.core.util_rules.source_files import SourceFiles, SourceFilesRequest
from pants.engine.addresses import Address
Expand Down Expand Up @@ -405,6 +406,7 @@ async def debug_python_test(field_set: PythonTestFieldSet) -> TestDebugRequest:
async def debugpy_python_test(
field_set: PythonTestFieldSet,
debugpy: DebugPy,
debug_adapter: DebugAdapterSubsystem,
pytest: PyTest,
) -> TestDebugAdapterRequest:
debugpy_pex = await Get(Pex, PexRequest, debugpy.to_pex_request())
Expand All @@ -426,7 +428,7 @@ async def debugpy_python_test(
main=debugpy.main,
prepend_argv=(
"--listen",
f"{debugpy.host}:{debugpy.port}",
f"{debug_adapter.host}:{debug_adapter.port}",
"--wait-for-client",
# @TODO: Techincally we should use `pytest.main`, however `debugpy` doesn't support
# launching an entry_point.
Expand Down
10 changes: 0 additions & 10 deletions src/python/pants/backend/python/subsystems/debugpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
from pants.core.goals.generate_lockfiles import GenerateToolLockfileSentinel
from pants.engine.rules import collect_rules, rule
from pants.engine.unions import UnionRule
from pants.option.option_types import IntOption, StrOption
from pants.util.docutil import git_url


Expand All @@ -30,15 +29,6 @@ class DebugPy(PythonToolBase):
default_lockfile_path = "src/python/pants/backend/python/subsystems/debugpy.lock"
default_lockfile_url = git_url(default_lockfile_path)

host = StrOption(
"--host", default="127.0.0.1", help="The hostname to use when launching the debugpy server."
)
port = IntOption(
"--port",
default=5678, # The canonical port
help="The port to use when launching the debugpy server.",
)


class DebugPyLockfileSentinel(GenerateToolLockfileSentinel):
resolve_name = DebugPy.options_scope
Expand Down
14 changes: 12 additions & 2 deletions src/python/pants/core/goals/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from typing import Any, ClassVar, TypeVar, cast

from pants.core.goals.package import BuiltPackage, PackageFieldSet
from pants.core.subsystems.debug_adapter import DebugAdapterSubsystem
from pants.core.util_rules.distdir import DistDir
from pants.engine.addresses import Address, UnparsedAddressInputs
from pants.engine.collection import Collection
Expand Down Expand Up @@ -413,6 +414,7 @@ class Test(Goal):
@rule_helper
async def _run_debug_tests(
test_subsystem: TestSubsystem,
debug_adapter: DebugAdapterSubsystem,
) -> Test:
targets_to_valid_field_sets = await Get(
TargetRootsToFieldSets,
Expand All @@ -435,7 +437,14 @@ async def _run_debug_tests(
exit_code = 0
for debug_request in debug_requests:
if test_subsystem.debug_adapter:
logger.info("Launching debug adapter, which will wait for a client connection...")
logger.info(
softwrap(
f"""
Launching debug adapter at '{debug_adapter.host}:{debug_adapter.port}',
which will wait for a client connection...
"""
)
)

debug_result = await Effect(
InteractiveProcessResult, InteractiveProcess, debug_request.process
Expand All @@ -449,13 +458,14 @@ async def _run_debug_tests(
async def run_tests(
console: Console,
test_subsystem: TestSubsystem,
debug_adapter: DebugAdapterSubsystem,
workspace: Workspace,
union_membership: UnionMembership,
distdir: DistDir,
run_id: RunId,
) -> Test:
if test_subsystem.debug or test_subsystem.debug_adapter:
return await _run_debug_tests(test_subsystem)
return await _run_debug_tests(test_subsystem, debug_adapter)

shard, num_shards = parse_shard_spec(test_subsystem.shard, "the [test].shard option")
targets_to_valid_field_sets = await Get(
Expand Down
9 changes: 8 additions & 1 deletion src/python/pants/core/goals/test_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
build_runtime_package_dependencies,
run_tests,
)
from pants.core.subsystems.debug_adapter import DebugAdapterSubsystem
from pants.core.util_rules.distdir import DistDir
from pants.engine.addresses import Address
from pants.engine.console import Console
Expand All @@ -55,7 +56,7 @@
TargetRootsToFieldSetsRequest,
)
from pants.engine.unions import UnionMembership
from pants.testutil.option_util import create_goal_subsystem
from pants.testutil.option_util import create_goal_subsystem, create_subsystem
from pants.testutil.rule_runner import (
MockEffect,
MockGet,
Expand Down Expand Up @@ -167,6 +168,11 @@ def run_test_rule(
extra_env_vars=[],
shard="",
)
debug_adapter_subsystem = create_subsystem(
DebugAdapterSubsystem,
host="127.0.0.1",
port="5678",
)
workspace = Workspace(rule_runner.scheduler, _enforce_effects=False)
union_membership = UnionMembership(
{
Expand Down Expand Up @@ -207,6 +213,7 @@ def mock_coverage_report_generation(
rule_args=[
console,
test_subsystem,
debug_adapter_subsystem,
workspace,
union_membership,
DistDir(relpath=Path("dist")),
Expand Down
28 changes: 28 additions & 0 deletions src/python/pants/core/subsystems/debug_adapter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Copyright 2022 Pants project contributors (see CONTRIBUTORS.md).
# Licensed under the Apache License, Version 2.0 (see LICENSE).

from __future__ import annotations

from pants.option.option_types import IntOption, StrOption
from pants.option.subsystem import Subsystem
from pants.util.strutil import softwrap


class DebugAdapterSubsystem(Subsystem):
options_scope = "debug-adapter"
help = softwrap(
"""
Options used to configure and launch a Debug Adapter server.

See https://microsoft.github.io/debug-adapter-protocol/ for more information.
"""
)

host = StrOption(
"--host", default="127.0.0.1", help="The hostname to use when launching the server."
)
port = IntOption(
"--port",
default=5678,
help="The port to use when launching the server.",
)