How-To: Debug Dapr applications with Visual Studio Code

When developing Dapr applications, you typically use the Dapr CLI to start your daprized service similar to this:

One approach to attaching the debugger to your service is to first run daprd with the correct arguments from the command line and then launch your code and attach the debugger. While this is a perfectly acceptable solution, it does require a few extra steps and some instruction to developers who might want to clone your repo and hit the “play” button to begin debugging.

If your application is a collection of microservices, each with a Dapr sidecar, it will be useful to debug them together in Visual Studio Code. This page will use the to showcase how to configure VSCode to debug multiple Dapr application using VSCode debugging.

Prerequisites

The file contains for a VS Code debug run. This file defines what will launch and how it is configured when the user begins debugging. Configurations are available for each programming language in the Visual Studio Code marketplace.

Scaffold debugging configuration

The Dapr VSCode extension offers built-in scaffolding to generate launch.json and tasks.json for you.

  1. {
  2. "version": "0.2.0",
  3. "configurations": [
  4. {
  5. "type": "pwa-node",
  6. "request": "launch",
  7. "name": "Nodeapp with Dapr",
  8. "<node_internals>/**"
  9. ],
  10. "program": "${workspaceFolder}/node/app.js",
  11. "preLaunchTask": "daprd-debug-node",
  12. "postDebugTask": "daprd-down-node"
  13. },
  14. {
  15. "type": "python",
  16. "request": "launch",
  17. "name": "Pythonapp with Dapr",
  18. "program": "${workspaceFolder}/python/app.py",
  19. "console": "integratedTerminal",
  20. "preLaunchTask": "daprd-debug-python",
  21. "postDebugTask": "daprd-down-python"
  22. }
  23. ]
  24. }

If you’re using ports other than the default ports baked into the code, set the DAPR_HTTP_PORT and DAPR_GRPC_PORT environment variables in the launch.json debug configuration. Match with the httpPort and grpcPort in the daprd tasks.json. For example, launch.json:

tasks.json:

  1. {
  2. "httpPort": 3502,
  3. "grpcPort": 50002
  4. }

Each configuration requires a request, type and name. These parameters help VSCode identify the task configurations in the .vscode/tasks.json files.

  • type defines the language used. Depending on the language, it might require an extension found in the marketplace, such as the Python Extension.
  • name is a unique name for the configuration. This is used for compound configurations when calling multiple configurations in your project.
  • ${workspaceFolder} is a VS Code variable reference. This is the path to the workspace opened in VS Code.
  • The preLaunchTask and postDebugTask parameters refer to the program configurations run before and after launching the application. See step 2 on how to configure these.

For more information on VSCode debugging parameters see .

Step 2: Configure tasks.json

For each defined in .vscode/launch.json , a corresponding task definition must exist in .vscode/tasks.json.

For the quickstart, each service needs a task to launch a Dapr sidecar with the daprd type, and a task to stop the sidecar with daprd-down. The parameters appId, httpPort, metricsPort, label and type are required. Additional optional parameters are available, see the reference table here.

For this example the compound configuration is:

  1. {
  2. "version": "2.0.0",
  3. "configurations": [...],
  4. "compounds": [
  5. {
  6. "name": "Node/Python Dapr",
  7. "configurations": ["Nodeapp with Dapr","Pythonapp with Dapr"]
  8. }
  9. ]
  10. }

Step 4: Launch your debugging session

You can now run the applications in debug mode by finding the compound command name you have defined in the previous step in the VS Code debugger:

You are now debugging multiple applications with Dapr!

Below are the supported parameters for VS Code tasks. These parameters are equivalent to daprd arguments as detailed in this reference: