microsoft / debugpy

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

Can't debug if project resides in folder whose name contains special characters #1288

Open sba923 opened 1 year ago

sba923 commented 1 year ago

Due to some SharePoint configuration weirdness, my project resides on a OneDrive-for-Business sync'ed folder whose path is:

C:\Users\...\...\... – Projects - Documents\...\test\tools

where the character before Projects is U+2013.

When attempting to debug a Python script in this context, I get:

image

int19h commented 1 year ago

Note that there is an error from Powershell first before debugger even gets a chance to run. This seems to indicate that Windows is not happy about this path in general. I wonder if the folder is lazily synced? That's the default behavior for OneDrive, if I remember correctly... perhaps the auto-sync logic simply doesn't kick in for this, and so the file isn't actually physically there?

Another possibility that I can think of is that the terminal is not running in UTF-8 mode, and thus the character gets mapped lossily, breaking the path in the command line when it is submitted. However, this really shouldn't be affecting Powershell...

sba923 commented 1 year ago

Windows and PowerShell have no problems with this path, and the VScode terminal does happen to be configured for UTF-8:

image

I'm routinely debugging PowerShell code in VScode off that very same folder.

The PowerShell error is caused by the execution of the command line built by the Python extension, where the character is replaced by a space.

gramster commented 1 year ago

@karthiknadig, is this an issue with debugger invocation in PVSC?

karthiknadig commented 1 year ago

The command that is sent to the terminal with the launcher is generated by debugpy it gets sent as runInTerminal request back to the VS Code. The DAP client in VS Code handles writing the command to the terminal. If the issue is with how the command is getting altered when it is sent to the terminal then this is like an issue with VS Code terminal support.

PVSC does not handle the runInTerminal request, it is handled by VS Code DAP client.

int19h commented 1 year ago

@sba923 Could you share the logs from running with "logToFile": true? If there's too much private information there, the thing I'm most interested in is the actual JSON values for "cwd" in various requests - this is what ends up sent by debugpy to VSCode for the latter to build the actual command line for the terminal.

sba923 commented 1 year ago

Does this help?

ms-python.python-2023.17.12551009.zip

int19h commented 1 year ago

Yes, thank you!

@karthiknadig This appears to be a VSCode issue. What the adapter does:

D+00000.078: /handling #2 request "launch" from Client[1]/
             Client[1] <-- {
                 "seq": 4,
                 "type": "request",
                 "command": "runInTerminal",
                 "arguments": {
                     "kind": "integrated",
                     "title": "Python Debug Console",
                     "args": [
                         "C:\\Users\\<REDACTED>\\AppData\\Local\\Programs\\Python\\Python310\\python.exe",
                         "c:\\Users\\<REDACTED>\\.vscode\\extensions\\ms-python.python-2023.17.12551009\\pythonFiles\\lib\\python\\debugpy\\adapter/../..\\debugpy\\launcher",
                         "57820",
                         "--",
                         "C:\\Users\\<REDACTED>\\<REDACTED> Group\\<REDACTED> \u2013 Projects - Documents\\<REDACTED> Payment\\test\\tools\\helloworld.py"
                     ],
                     "env": {
                         "DEBUGPY_LOG_DIR": "c:\\Users\\<REDACTED>\\.vscode\\extensions\\ms-python.python-2023.17.12551009"
                     },
                     "cwd": "c:\\Users\\<REDACTED>\\<REDACTED> Group\\<REDACTED> \u2013 Projects - Documents\\<REDACTED> Payment\\test\\tools"
                 }
             }

So U+2013 is still there in "cwd" and "args" as it should be. What VSCode does:

279 Client <-- Adapter:
{
    "seq": 4,
    "type": "request",
    "command": "runInTerminal",
    "arguments": {
        "kind": "integrated",
        "title": "Python Debug Console",
        "args": [
            "C:\\Users\\<REDACTED>\\AppData\\Local\\Programs\\Python\\Python310\\python.exe",
            "c:\\Users\\<REDACTED>\\.vscode\\extensions\\ms-python.python-2023.17.12551009\\pythonFiles\\lib\\python\\debugpy\\adapter/../..\\debugpy\\launcher",
            "57820",
            "--",
            "C:\\Users\\<REDACTED>\\<REDACTED> Group\\<REDACTED> – Projects - Documents\\<REDACTED> Payment\\test\\tools\\helloworld.py"
        ],
        "env": {
            "DEBUGPY_LOG_DIR": "c:\\Users\\<REDACTED>\\.vscode\\extensions\\ms-python.python-2023.17.12551009"
        },
        "cwd": "c:\\Users\\<REDACTED>\\<REDACTED> Group\\<REDACTED> – Projects - Documents\\<REDACTED> Payment\\test\\tools"
    }
}

This is harder to see because it's not escaped here, but the "–" before Projects is indeed U+2013. But, as seen on the screenshots, it gets stripped right after, when VSCode generates the command line in response to this request (or possibly when it gets "typed" into the terminal?). Our launcher conveniently logs its own sys.argv very early on where this is also visible:

I+00000.078: sys.argv before parsing: ['c:\\Users\\<REDACTED>\\.vscode\\extensions\\ms-python.python-2023.17.12551009\\pythonFiles\\lib\\python\\debugpy\\adapter/../..\\debugpy\\launcher', '57820', '--', 'C:\\Users\\<REDACTED>\\<REDACTED> Group\\<REDACTED>  Projects - Documents\\<REDACTED> Payment\\test\\tools\\helloworld.py']

Could you transfer the bug accordingly?

sba923 commented 1 year ago

Just curious: how come this doesn't affect other extensions e.g., PowerShell?

int19h commented 1 year ago

I would expect it to affect Node.js if using "console":"integratedTerminal". I'm not familiar with the Powershell extension, but my guess would be that they don't use "runInTerminal" to implement Start Debugging.