Skip to content

Commit 9fe1be6

Browse files
author
Pavel Minaev
authored
Merge pull request #44 from int19h/fix_2081
Fix microsoft/ptvsd#2081
2 parents 189c326 + 1cbd589 commit 9fe1be6

3 files changed

Lines changed: 49 additions & 16 deletions

File tree

src/debugpy/adapter/clients.py

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -248,16 +248,6 @@ def launch_request(self, request):
248248
if self.session.id != 1 or len(servers.connections()):
249249
raise request.cant_handle('"attach" expected')
250250

251-
sudo = request("sudo", json.default("Sudo" in self.session.debug_options))
252-
if sudo:
253-
if sys.platform == "win32":
254-
raise request.cant_handle('"sudo":true is not supported on Windows.')
255-
else:
256-
if "Sudo" in self.session.debug_options:
257-
raise request.isnt_valid(
258-
'"sudo":false and "debugOptions":["Sudo"] are mutually exclusive'
259-
)
260-
261251
# Launcher doesn't use the command line at all, but we pass the arguments so
262252
# that they show up in the terminal if we're using "runInTerminal".
263253
if "program" in request:
@@ -286,7 +276,7 @@ def launch_request(self, request):
286276
console_title = request("consoleTitle", json.default("Python Debug Console"))
287277

288278
launchers.spawn_debuggee(
289-
self.session, request, sudo, args, console, console_title
279+
self.session, request, args, console, console_title
290280
)
291281

292282
@_start_message_handler

src/debugpy/adapter/launchers.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,8 @@ def terminate_debuggee(self):
6565
pass
6666

6767

68-
def spawn_debuggee(session, start_request, sudo, args, console, console_title):
69-
cmdline = ["sudo"] if sudo else []
70-
cmdline += [sys.executable, os.path.dirname(launcher.__file__)]
71-
cmdline += args
68+
def spawn_debuggee(session, start_request, args, console, console_title):
69+
cmdline = [sys.executable, os.path.dirname(launcher.__file__)] + args
7270
env = {}
7371

7472
arguments = dict(start_request.arguments)

tests/debugpy/test_run.py

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@
77
import os
88
import pytest
99
import re
10+
import sys
1011
import time
1112

1213
import debugpy
1314
from debugpy.common import log, messaging
14-
from tests import debug, test_data
15+
from tests import debug, test_data, timeline
1516
from tests.debug import runners, targets
1617
from tests.patterns import some
1718

@@ -90,6 +91,50 @@ def code_to_debug():
9091
assert "ok" in session.output("stdout")
9192

9293

94+
@pytest.mark.skipif(sys.platform == "win32", reason="sudo not available on Windows")
95+
@pytest.mark.parametrize("run", runners.all_launch)
96+
def test_sudo(pyfile, tmpdir, run, target):
97+
# Since the test can't rely on sudo being allowed for the user, create a dummy
98+
# sudo script that doesn't actually elevate, but sets an environment variable
99+
# that can be checked in the debuggee.
100+
sudo = tmpdir / "sudo"
101+
sudo.write(
102+
"""#!/bin/sh
103+
exec env DEBUGPY_SUDO=1 "$@"
104+
"""
105+
)
106+
os.chmod(sudo.strpath, 0o777)
107+
108+
@pyfile
109+
def code_to_debug():
110+
import os
111+
112+
import debuggee
113+
from debuggee import backchannel
114+
115+
debuggee.setup()
116+
backchannel.send(os.getenv("DEBUGPY_SUDO", "0"))
117+
118+
with debug.Session() as session:
119+
session.config["sudo"] = True
120+
session.config.env["PATH"] = tmpdir.strpath + ":" + os.environ["PATH"]
121+
122+
backchannel = session.open_backchannel()
123+
with run(session, target(code_to_debug)):
124+
pass
125+
126+
# The "runInTerminal" request sent by the adapter to spawn the launcher,
127+
# if any, shouldn't be using sudo.
128+
assert all(
129+
"sudo" not in req.arguments["args"]
130+
for req in session.all_occurrences_of(timeline.Request("runInTerminal"))
131+
)
132+
133+
# The launcher, however, should use our dummy sudo to spawn the debuggee,
134+
# and the debuggee should report the environment variable accordingly.
135+
assert backchannel.receive() == "1"
136+
137+
93138
@pytest.mark.parametrize(
94139
# Can't test "internalConsole", because we don't have debuggee stdin to press the key.
95140
"run",

0 commit comments

Comments
 (0)