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

3

11

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?

Idle answered 16/4, 2021 at 5:21 Comment(0)
E
4

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}"

Evert answered 16/4, 2021 at 5:38 Comment(2)
Or simply remove the .vscode-folder, restart visual code and allow it to re-create those files as they should look like. Those files easily get messed up if you move files around etc.Repugn
They also get easily messed up by just using the Azure Functions extension in VS Code. From experience, it generates broken configurations more often than it creates working ones. I concluded it's not worth it and uninstalled it, creating the configurations manually according to documentation is much faster and more reliable.Plotinus
I
6

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 !

Idle answered 16/4, 2021 at 7:30 Comment(1)
You are welcome! Glad that you were able to solve it. BREvert
E
4

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}"

Evert answered 16/4, 2021 at 5:38 Comment(2)
Or simply remove the .vscode-folder, restart visual code and allow it to re-create those files as they should look like. Those files easily get messed up if you move files around etc.Repugn
They also get easily messed up by just using the Azure Functions extension in VS Code. From experience, it generates broken configurations more often than it creates working ones. I concluded it's not worth it and uninstalled it, creating the configurations manually according to documentation is much faster and more reliable.Plotinus
C
0

The previous solutions did not work for me. I had to add the following line to the host start task in tasks.json:

    "isBackground": true

This way the debugger does not wait for the host start task to finish.

Costanza answered 20/9, 2023 at 9:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.