From 022cf5991bfd6d23b027f7811533c822574cdc95 Mon Sep 17 00:00:00 2001 From: kwy404 Date: Fri, 25 Sep 2026 20:11:40 -0300 Subject: [PATCH] Skip the value of an overridden DEBUGPY_EXTRA_ARGV switch When a switch in DEBUGPY_EXTRA_ARGV was already given on the command line, parse_args_helper() skips the switch but not its value. The value is then parsed as the next argument, and a bare value is taken as a positional file target. For example, `--connect 8888 -m spam` with DEBUGPY_EXTRA_ARGV set to `--connect 5678` ends up with target_kind "file" and target "5678" instead of the "spam" module. Consume the value too when skipping a switch that takes one. --- src/debugpy/server/cli.py | 3 +++ tests/debugpy/server/test_cli.py | 13 +++++++++++++ 2 files changed, 16 insertions(+) diff --git a/src/debugpy/server/cli.py b/src/debugpy/server/cli.py index c7590fc3e..67b922a19 100644 --- a/src/debugpy/server/cli.py +++ b/src/debugpy/server/cli.py @@ -297,6 +297,9 @@ def parse_args_helper( raise ValueError("duplicate switch from environment: " + switch) # if we're parsing from the environment, and we've already seen the switch on the command line, skip it, since command line takes precedence elif isFromEnvironment and switch in seenFromCommandLine: + # also skip the switch's value, if any, so it isn't parsed as a switch or target + if switch and placeholder is not None: + next(iterator, None) continue # otherwise, the switch is new, so add it to the appropriate set else: diff --git a/tests/debugpy/server/test_cli.py b/tests/debugpy/server/test_cli.py index b60b2eb16..fa400ec7a 100644 --- a/tests/debugpy/server/test_cli.py +++ b/tests/debugpy/server/test_cli.py @@ -235,6 +235,19 @@ def test_override_environment_switch(cli): assert options["address"] == ("127.0.0.1", 8888) assert options["target"] == "spam.py" + +# Test that the value of an overridden environment switch is skipped along with it +def test_override_environment_switch_with_module_target(cli): + args = ["--connect", "8888", "-m", "spam"] + + with mock.patch.dict(os.environ, {"DEBUGPY_EXTRA_ARGV": "--connect 5678"}): + _, options = cli(args) + + assert options["address"] == ("127.0.0.1", 8888) + assert options["target_kind"] == "module" + assert options["target"] == "spam" + + # Test that script args (passed to target) are preserved def test_script_args(cli): args = ["--listen", "8888", "spam.py", "arg1", "arg2"]