diff --git a/changelogs/fragments/test-debugpy-path.yml b/changelogs/fragments/test-debugpy-path.yml new file mode 100644 index 00000000000..098d7bfbc9c --- /dev/null +++ b/changelogs/fragments/test-debugpy-path.yml @@ -0,0 +1,4 @@ +bugfixes: + - >- + ansible-test - Ensure the bundled debugpy module from VSCode is available in the ``--dev-debug-on-demand`` + environment. diff --git a/test/lib/ansible_test/_internal/debugging.py b/test/lib/ansible_test/_internal/debugging.py index b3c4a605ec8..606c5a7a270 100644 --- a/test/lib/ansible_test/_internal/debugging.py +++ b/test/lib/ansible_test/_internal/debugging.py @@ -7,6 +7,7 @@ import dataclasses import importlib import json import os +import pathlib import re import sys import typing as t @@ -255,6 +256,11 @@ class DebugpySettings(DebuggerSettings): The `--connect`, `--adapter-access-token`, and `--parent-session-pid` options will be provided by ansible-test. """ + debugpy_package_path: str | None = None + """ + The path to the debugpy package to add to PYTHONPATH. + """ + @classmethod def is_active(cls) -> bool: return detect_debugpy_options() is not None @@ -262,7 +268,7 @@ class DebugpySettings(DebuggerSettings): @classmethod def apply_defaults(cls, settings: t.Self) -> t.Self: if options := detect_debugpy_options(): - settings = dataclasses.replace(settings, port=options.port) + settings = dataclasses.replace(settings, port=options.port, debugpy_package_path=options.debugpy_package_path) settings.connect.update( access_token=options.adapter_access_token, parent_session_pid=os.getpid(), @@ -303,11 +309,22 @@ class DebugpySettings(DebuggerSettings): return cli_args def get_environment_variables(self, profile: DebuggerProfile) -> dict[str, str]: - return dict( + env = dict( PATHS_FROM_ECLIPSE_TO_PYTHON=json.dumps(list(profile.get_source_mapping().items())), PYDEVD_DISABLE_FILE_VALIDATION="1", ) + if self.debugpy_package_path: + python_path = os.environ.get('PYTHONPATH', '') + if python_path: + python_path = f"{self.debugpy_package_path}{os.pathsep}{python_path}" + else: + python_path = self.debugpy_package_path + + env['PYTHONPATH'] = python_path + + return env + def initialize_debugger(args: CommonConfig) -> None: """Initialize the debugger settings before delegation.""" @@ -420,6 +437,7 @@ class DebugpyOptions: port: int adapter_access_token: str | None + debugpy_package_path: str @cache @@ -451,4 +469,5 @@ def detect_debugpy_options() -> DebugpyOptions | None: return DebugpyOptions( port=port, adapter_access_token=opts.adapter_access_token, + debugpy_package_path=str(pathlib.Path(debugpy.__file__).resolve().parent.parent), )