Cyberwiz
Cyberwiz

Reputation: 11426

How to debug Chalice route handlers in VSCode

Using the following launch.json, I'm able to hit breakpoints in the module level code, but not in route-handlers.

launch configuration (the last three lines may not be needed, but are just some things I've tried)

{
    "name": "Chalice: Local",
    "type": "python",
    "request": "launch",
    "program": "${workspaceFolder}/.venv/bin/chalice",
    "args": [
        "local",
        "--no-autoreload",
    ],
    "console": "integratedTerminal",
    "justMyCode": false,
    "subProcess": true,
    "gevent": true
}

app.py:

...
app = Chalice(app_name="something") # breakpoint here does hit
...

@app.route("/{cluster}", methods=["POST"])
def deploy_pods(cluster):
    print(cluster) # breakpoint here doesnt hit (but I can see the print happening)

Upvotes: 0

Views: 210

Answers (3)

Cyberwiz
Cyberwiz

Reputation: 11426

Here's what I ended up doing, with the inspiration of various responses here:

{
    "version": "0.2.0",
    "configurations": [
      {
        "name": "Debug Chalice App",
        "type": "python",
        "request": "attach",
        "connect": {
          "host": "localhost",
          "port": 5678
        },
        "justMyCode": true
      },
      {
        "name": "Run Chalice Locally",
        "type": "python",
        "request": "launch",
        "program": "${workspaceFolder}/.venv/bin/chalice",
        "args": [
          "local",
          "--no-autoreload"
        ],
        "env": {
          "DEBUG": "1"
        },
        "console": "integratedTerminal"
      }
    ]
}

and at the top of my app.py:

if os.getenv("DEBUG"):
    import debugpy

    debugpy.listen(("0.0.0.0", 5678))
    print("Waiting for debugger to attach...")
    debugpy.wait_for_client()

I still need to start the process and the attach manually, so it isn't as smooth as I'd like it, but it works!

Upvotes: 0

negstek
negstek

Reputation: 691

my conf works for my project, are you in the right folder ? You should go in the folder where is your app.py, look my cwd var:

{
    "name": "Chalice: public-api",
    "type": "debugpy",
    "request": "launch",
    "program": "${env:CHAT3D_BACKEND_VENV_PATH}/bin/chalice",
    "args": [
        "local",
        "--no-autoreload"
    ],
    "cwd": "${workspaceFolder}/public-api"
}

Upvotes: 0

Jeanneli
Jeanneli

Reputation: 267

this issue could be the reason why there is no invocation. I tried it with your code. In the deploy_pods, as you description, can't hit the breakpoint. I tried invoking deploy_pods outside of the method and passing in the arguments, and the breakpoint was hit. enter image description here

Upvotes: 0

Related Questions