microsoft / debugpy

An implementation of the Debug Adapter Protocol for Python
https://pypi.org/project/debugpy/
Other
1.88k stars 140 forks source link

Unable to debug files in more complicated file paths #641

Closed rzhao271 closed 3 years ago

rzhao271 commented 3 years ago

Environment data

Version : 1.57.0-insider (user setup) Commit : fe1eafc80c07608de52a2f9c99fee103dea77f5e Date : 2021-06-04T12:21:40.077Z Electron : 12.0.9 Chrome : 89.0.4389.128 Node.js : 14.16.0 V8 : 8.9.255.25-electron.0 OS : Windows_NT x64 10.0.19043

Actual behavior

The file test's test/hello.py is not found.

(.venv) codespace@codespaces_19c705:/workspaces/testworkspace3$  /usr/bin/env /workspaces/testworkspace3/.venv/bin/python /home/codespace/.vscode-remote/extensions/ms-python.python-2021.5.842923320/pythonFiles/lib/python/debugpy/launcher 35789 -- "/workspaces/testworkspace3/test\'s test/hello.py" 
Traceback (most recent call last):
  File "/opt/python/3.8.6/lib/python3.8/runpy.py", line 194, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/opt/python/3.8.6/lib/python3.8/runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "/home/codespace/.vscode-remote/extensions/ms-python.python-2021.5.842923320/pythonFiles/lib/python/debugpy/__main__.py", line 45, in <module>
    cli.main()
  File "/home/codespace/.vscode-remote/extensions/ms-python.python-2021.5.842923320/pythonFiles/lib/python/debugpy/../debugpy/server/cli.py", line 444, in main
    run()
  File "/home/codespace/.vscode-remote/extensions/ms-python.python-2021.5.842923320/pythonFiles/lib/python/debugpy/../debugpy/server/cli.py", line 285, in run_file
    runpy.run_path(target_as_str, run_name=compat.force_str("__main__"))
  File "/opt/python/3.8.6/lib/python3.8/runpy.py", line 264, in run_path
    code, fname = _get_code_from_file(run_name, path_name)
  File "/opt/python/3.8.6/lib/python3.8/runpy.py", line 234, in _get_code_from_file
    with io.open_code(decoded_path) as f:
FileNotFoundError: [Errno 2] No such file or directory: "/workspaces/testworkspace3/test\\'s test/hello.py"

Expected behavior

The file test's test/hello.py is found.

Steps to reproduce:

  1. Create a directory test's test.
  2. Add a file hello.py with the line print('test') within that directory
  3. Try to debug hello.py

Also try with the directory test'![](){}|=+-_*&^%$#@!<>,.?~`s test/hello.py

fabioz commented 3 years ago

As a note, I can reproduce the issue on Linux (and it seems to work fine on Windows).

I'll investigate.

fabioz commented 3 years ago

Ok, I've done some investigation and it seems to be an issue in VSCode itself (I'll report there and link the issue shortly)

Notes:

  1. This works properly with internalConsole and with externalTerminal (so, it only fails with integratedTerminal).
  2. The adapter appears to send the runInTerminal to the client properly. i.e.: the logs show:
D+00000.037: /handling #2 request "launch" from Client[1]/
             Client[1] <-- {
                 "seq": 4,
                 "type": "request",
                 "command": "runInTerminal",
                 "arguments": {
                     "kind": "integrated",
                     "title": "Python Debug Console",
                     "args": [
                         "/home/fabio/Desktop/miniconda3/envs/py37_tests/bin/python",
                         "/home/fabio/Desktop/debugpy_workspace/debugpy/src/debugpy/launcher",
                         "36577",
                         "--",
                         "/home/fabio/Desktop/debugpy_workspace/check/test's test/tests's.py"
                     ],
                     "env": {
                         "DEBUGPY_LOG_DIR": "/home/fabio/.vscode/extensions/ms-python.python-2021.5.842923320"
                     },
                     "cwd": "/home/fabio/Desktop/debugpy_workspace"
                 }
             }
  1. When the args are actually received in the launcher, chars appear to be escaped (wrongly). i.e.: the log shows:

I+00000.024: sys.argv before parsing: ['/home/fabio/Desktop/debugpy_workspace/debugpy/src/debugpy/launcher', '36577', '--', "/home/fabio/Desktop/debugpy_workspace/check/test\\'s test/tests\\'s.py"]

  1. The problem doesn't happen if there aren't spaces in the path, so, it seems that the issue is that VSCode is doing quoting once for the \' and then wrapping around with " due to the spaces on the path (which makes the launch fail).

So, this appears to be a bug in how VSCode is handling the runInTerminal (on Linux).

fabioz commented 3 years ago

I'm closing the issue here since the issue appears to be on VSCode, not really on debugpy.