robotcodedev / robotcode

RobotFramework support for Visual Studio Code
https://robotcode.io
Apache License 2.0
182 stars 15 forks source link

[QUESTION] How does the extension connect to the python debugger? #151

Closed rcsmith00 closed 1 year ago

rcsmith00 commented 1 year ago

Please describe.

I have a local setup that works fine to debug tests within VS Code. I have another setup on a different machine. Can debug python scripts fine. Can run tests without debug fine. But when I try to run a test with debug, I get a " Cannot connect to python debugger" error.

Could it be a firewall preventing connection?
Is it const DEBUG_ATTACH_DEFAULT_TCP_PORT = 6612;

How can I get more visibility to determine root cause? thanks

Desktop (please complete the following information):

d-biehl commented 1 year ago

If you can debug Python script, it should not be the firewall, but maybe it is... Did you disable the firewall?

Is there any Python program/script running in debug mode in the background?

RobotCode tries to connect the Python debugger on the default port 5678, but looks first if the port is free, if not it tries to find a free port and you should get a message like start debugpy session on port 59453 in the terminal.

The port 6612 is the robotcode debugger port, but since it tries to connect the python debugger, it should be already started and working.

You can create a launch.json and add the following snippet (if not already there):

 {
    "name": "RobotCode: Default",
    "type": "robotcode",
    "request": "launch",
    "purpose": "default",
    "presentation": {
        "hidden": true
    },
    "pythonConfiguration": "RobotCode: Python",
    "attachPython": true,
    "robotCodeArgs": ["-v"],
    "attachPythonPort": 0,
},
{
    "name": "RobotCode: Python",
    "type": "python",
    "request": "attach",
    "presentation": {
        "hidden": true
    },
    "justMyCode": true
}

I added the two settings robotCodeArgs and attachPythonPort in the snippet above. You can play around with them.

The robotCodeArgs setting says, be a bit more verbose, or it will print some more information, which default settings are used and what is happening and so on.

With the setting attachPythonPort you can set the port on which the Python debugger should run. Try a few ports, if you set it to 0 the debugger will look for a port by itself.

There is also a setting debuggerTimeout which specifies how long to wait until a client connects.

Hope this helps a bit. If not, let me know.

rcsmith00 commented 1 year ago

Thanks for the info.

Not a firewall, which I thought was unlikely since just local.

It appears that the python debugger is not being launched at all. How does the robot debugger launch it?

d-biehl commented 1 year ago

The message comes from VSCode as a MessageBox? like this:

image

This is a bit difficult to explain, but to try it in a short way:

When you press "Debug Test", the "robotcode" debugger starts, when the robotcode debugger is initialized, it starts internally a debugpy session with the given port (see previous post), then it sends a command to VSCode that VSCode should connect the debugger for Python to this port.

Not sure if it helps you, but here are the links in the source code, maybe you recognize something?

https://github.com/d-biehl/robotcode/blob/054d2101536a205d87b2d7409609cfe5ac1ba6ff/packages/debugger/src/robotcode/debugger/run.py#L92

https://github.com/d-biehl/robotcode/blob/054d2101536a205d87b2d7409609cfe5ac1ba6ff/vscode-client/debugmanager.ts#L590


Can you post the console/terminal output here, preferably with the launch.config given above.

Which terminal do you use? windows cmd? windows builtin powershell? The new powershell (pwsh) or anything else?

rcsmith00 commented 1 year ago

Hi Daniel, Sorry it took me a while to get back to this. Narrowed it down to Windows powershell configured as the default terminal in VS Code. pwsh was not installed. If switched to command prompt, then it worked. After installing pwsh, that also worked when set as default terminal

d-biehl commented 1 year ago

Sometimes it is necessary to set the correct execution policy for Windows built-in Powershell to allow scripts to run. It should work if you run Set-ExecutionPolicy Unrestricted in a Powershell prompt. I don`t remember why, but I had to do this on some customer machines. I think mainly because the administrators have disabled script execution for normal users. Maybe you can give it a try?

I personally would switch to the new pwsh ;-)

If it works for you, can we close this issue/question?

rcsmith00 commented 1 year ago

thanks for the support!