From 69ddc625c76645206cbd5d111cc9c876305a50a7 Mon Sep 17 00:00:00 2001 From: Jordan Borean Date: Thu, 18 Sep 2025 09:03:00 +1000 Subject: [PATCH 1/3] Use debugpy from VSCode bundle Ensure that the `debugpy` module bundled by VSCode is included in the debug environment set by `ansible-test shell --dev-debug-on-demand`. This means that `debugpy` does not need to be manually installed in the Python environment running Ansible. --- changelogs/fragments/test-debugpy-path.yml | 4 ++++ test/lib/ansible_test/_internal/debugging.py | 23 ++++++++++++++++++-- 2 files changed, 25 insertions(+), 2 deletions(-) create mode 100644 changelogs/fragments/test-debugpy-path.yml 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..b91dcad260d 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: pathlib.Path @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.absolute()), ) From 51e07729229b1a8a3d7961c33c3df20f84ca9038 Mon Sep 17 00:00:00 2001 From: Jordan Borean Date: Thu, 18 Sep 2025 09:22:03 +1000 Subject: [PATCH 2/3] Fix sanity test --- test/lib/ansible_test/_internal/debugging.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/lib/ansible_test/_internal/debugging.py b/test/lib/ansible_test/_internal/debugging.py index b91dcad260d..eac0bd611d1 100644 --- a/test/lib/ansible_test/_internal/debugging.py +++ b/test/lib/ansible_test/_internal/debugging.py @@ -437,7 +437,7 @@ class DebugpyOptions: port: int adapter_access_token: str | None - debugpy_package_path: pathlib.Path + debugpy_package_path: str @cache From 1f522fc829f1eb4e96adb0974ae2956c2923064d Mon Sep 17 00:00:00 2001 From: Jordan Borean Date: Fri, 19 Sep 2025 07:43:05 +1000 Subject: [PATCH 3/3] Update test/lib/ansible_test/_internal/debugging.py MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: 🇺🇦 Sviatoslav Sydorenko (Святослав Сидоренко) --- test/lib/ansible_test/_internal/debugging.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/lib/ansible_test/_internal/debugging.py b/test/lib/ansible_test/_internal/debugging.py index eac0bd611d1..606c5a7a270 100644 --- a/test/lib/ansible_test/_internal/debugging.py +++ b/test/lib/ansible_test/_internal/debugging.py @@ -469,5 +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.absolute()), + debugpy_package_path=str(pathlib.Path(debugpy.__file__).resolve().parent.parent), )