How to start Azure function locally and attach debugger to process dynamically via launch.json and tasks.json?

Viewed 2751

Using .NET Core on a Mac with Visual Studio Code, I'm trying to host an Azure Function locally, and attach the debugger to it. Manually, I can get it to work by starting the function with func host start, followed by pressing the debug button in the IDE, which runs the default launch.json file. This let's me pick a process to attach the debugger to because of the line "processId": "${command:pickProcess}".

I would like to automize this flow, triggering it with a simple click on the debug button in VSCode.

launch- and tasks.json

In the above picture I attempt to solve this by running a preLaunchTask before attaching the debugger. This task should host and start the function - which it does so successfully when isolated from the flow. However, when I hit the Debug button with the above configuration, VSCode lets me pick a process immediately before actually having completed the preLaunchTask and so the process func is not yet available.

Is my line of thinking in order? What could I be missing in order for this to work?

2 Answers

You might try to create the local Functions app with the Visual Studio Code Functions extension. When you created the Functions app with the extension, it automatically adds a VS Code launch configuration to your project which at least works on my machine :-).

One difference between my and your configuration is the processId which you might change to this: "processId": "${command:azureFunctions.pickProcess}"

The VSCode Function extension works like a charm, the problem is that the team already started on this solution in Visual Studio Windows and so I have to adapt to that.

I did not notice the azureFunctions part in the launch configuration that comes with the extension though! I edited my launch.json to the following:

{
  "name": "Attach to .NET Functions",
  "type": "coreclr",
  "request": "attach",
  "processId": "${command:azureFunctions.pickProcess}"
}

which seemed to do the trick! Notice that there's no preLaunchTask anymore. However, the host gets started, and the debugger gets attached! Thanks @MartinBrandl !

Related