Respect isolated mode when patching sys.path - #2050
Respect isolated mode when patching sys.path#2050karandhaodiyal28-hash wants to merge 2 commits into
Conversation
|
Azure Pipelines: There may be pipelines that require an authorized user to comment /azp run to run. |
|
@microsoft-github-policy-service agree |
|
🔒 Automated review in progress — @rchiodo is auto-reviewing this PR. |
| # be in place before trying to use find_spec below to resolve submodules. | ||
| sys.path.insert(0, str("")) | ||
| # Add current directory to path, like Python itself does for -m, unless | ||
| # it's suppressed by isolated mode. This must be in place before trying |
There was a problem hiding this comment.
Warning · Non-blocking recommendation
CPython suppresses the sys.path[0] prepend whenever safe_path is set, not only under isolated. -P / PYTHONSAFEPATH=1 set safe_path without setting isolated, so python -P -m debugpy ... still injects the current directory. Please broaden the gate to account for sys.flags.safe_path (with a compatibility fallback for Python <3.11), or explicitly document why this fix intentionally covers only -I.
[verified]
rchiodo
left a comment
There was a problem hiding this comment.
Approved via Review Center.
|
Thanks @rchiodo, that's a great catch — you're right that I've broadened the gate to cover both. All three sites ( def _skip_sys_path_prepend():
# `sys.flags.safe_path` was added in Python 3.11; fall back to False on older versions.
return sys.flags.isolated or getattr(sys.flags, "safe_path", False)The |
| ) | ||
|
|
||
| sys_path_0 = output.decode("utf-8").strip().splitlines()[-1] | ||
| assert sys_path_0 != repr(not_expected) |
There was a problem hiding this comment.
Warning · Non-blocking recommendation
Harden this subprocess-based regression test: add a timeout and preserve child stderr so a hung or failing debuggee produces a timely, diagnosable failure. The positional last-line parsing and negative-only assertion could also mask unrelated output or an incorrect replacement path; use an explicit marker and assert the expected path behavior. [verified]
rchiodo
left a comment
There was a problem hiding this comment.
Approved via Review Center.
Fixes #1916
Python's isolated mode (
-I) must not prepend the script directory (for a file target) or the current directory (for-m/-c) tosys.path— that's the whole point of the flag, and it exists to prevent local modules from shadowing stdlib/third-party imports. debugpy re-implements Python's startupsys.pathhandling insrc/debugpy/server/cli.py, but did so unconditionally, so a debuggee launched under-Istill got the directory injected:The comments in those three functions already say "like Python itself does for -m/-c" — but Python itself skips the insert under
-I. This guards all three sites (run_file,run_module,run_code) withsys.flags.isolated, matching CPython semantics exactly.sys.flags.isolatedhas existed since 3.4, and non-isolated runs are completely unaffected.The attach-to-PID injection path also inserts a directory, but it deletes it again right after importing its helper, so it's left as-is.
Testing
Added
test_isolated_mode_sys_path(parametrized over file /-m/-c) totests/debugpy/test_cli_args.py, asserting the target directory is not prepended tosys.path[0]when the debuggee runs under-I. The three new cases fail without this change and pass with it. Verified locally on Windows / Python 3.12;test_run.pyshows no new failures introduced (identical pass/fail set with and without the change).