After adding an Azure Function Project to Solution some tasks.json file settings have been removed

Viewed 638

I have a small issue and I'm not sure if I added an Azure function project to my workspace/solution correctly. I have a solution, in it I have 4 projects, an API (DotNet Core), client (Angular), Core (class with models and interfaces), Infrastructure (EF, DB, services).

I wanted to add an Azure function project to this solution/workspace and I did so by creating a folder called 'Azure' as seen in green, then I clicked on my Azure Extention, then clicked 'Add New Project' and walked through the steps. It created it successfully and all the files are in the folder. I can run the function and receive messages in it! Good ok!

enter image description here

Here are my Azure function extension options

enter image description here

PROBLEM - Now when I go to debug the website application (Angular/.Net Core) and I start up the Angular server and then debug/launch, I should have the compiler build me a new API.DLL, but it doesn't! And I see a error message pop up showing this

enter image description here

I can see my tasks.json file has been modified.

Here is tasks.json before I added the Azure function. You can see build label for the API.csproj info

{
  "version": "2.0.0",
  "tasks": [{
      "label": "build",
      "command": "dotnet",
      "type": "process",
      "args": [
        "build",
        "${workspaceFolder}/API/API.csproj",
        "/property:GenerateFullPaths=true",
        "/consoleloggerparameters:NoSummary"
      ],
      "problemMatcher": "$msCompile"
    },
    {
      "label": "publish",
      "command": "dotnet",
      "type": "process",
      "args": [
        "publish",
        "${workspaceFolder}/API/API.csproj",
        "/property:GenerateFullPaths=true",
        "/consoleloggerparameters:NoSummary"
      ],
      "problemMatcher": "$msCompile"
    },
    {
      "label": "watch",
      "command": "dotnet",
      "type": "process",
      "args": [
        "watch",
        "run",
        "${workspaceFolder}/API/API.csproj",
        "/property:GenerateFullPaths=true",
        "/consoleloggerparameters:NoSummary"
      ],
      "problemMatcher": "$msCompile"
    }
  ]
}

And here is tasks.json after I add the function project, where the build label for the DotNet Core API has been removed! Now only Azure Function data is in it.

{
  "version": "2.0.0",
  "tasks": [{
      "label": "watch",
      "command": "dotnet",
      "type": "process",
      "args": [
        "watch",
        "run",
        "${workspaceFolder}/API/API.csproj",
        "/property:GenerateFullPaths=true",
        "/consoleloggerparameters:NoSummary"
      ],
      "problemMatcher": "$msCompile"
    },
    {
      "label": "clean",
      "command": "dotnet",
      "args": [
        "clean",
        "/property:GenerateFullPaths=true",
        "/consoleloggerparameters:NoSummary"
      ],
      "type": "process",
      "problemMatcher": "$msCompile",
      "options": {
        "cwd": "${workspaceFolder}/Azure/Functions"
      }
    },
    {
      "label": "build",
      "command": "dotnet",
      "args": [
        "build",
        "/property:GenerateFullPaths=true",
        "/consoleloggerparameters:NoSummary"
      ],
      "type": "process",
      "dependsOn": "clean",
      "group": {
        "kind": "build",
        "isDefault": true
      },
      "problemMatcher": "$msCompile",
      "options": {
        "cwd": "${workspaceFolder}/Azure/Functions"
      }
    },
    {
      "label": "clean release",
      "command": "dotnet",
      "args": [
        "clean",
        "--configuration",
        "Release",
        "/property:GenerateFullPaths=true",
        "/consoleloggerparameters:NoSummary"
      ],
      "type": "process",
      "problemMatcher": "$msCompile",
      "options": {
        "cwd": "${workspaceFolder}/Azure/Functions"
      }
    },
    {
      "label": "publish",
      "command": "dotnet",
      "args": [
        "publish",
        "--configuration",
        "Release",
        "/property:GenerateFullPaths=true",
        "/consoleloggerparameters:NoSummary"
      ],
      "type": "process",
      "dependsOn": "clean release",
      "problemMatcher": "$msCompile",
      "options": {
        "cwd": "${workspaceFolder}/Azure/Functions"
      }
    },
    {
      "type": "func",
      "dependsOn": "build",
      "options": {
        "cwd": "${workspaceFolder}/Azure/Functions/bin/Debug/net5.0"
      },
      "command": "host start",
      "isBackground": true,
      "problemMatcher": "$func-dotnet-watch"
    }
  ]
}

FYI It looks like my tasks.json file is all tweaked up and modified! It looks like the dotnet build propoerties have been overwritten by the Azure function properties. If I copy and paste back in the old tasks.json file, it works fine. Should the addition of this new Azure function project have removed all my properties for the other parts of the app?

QUESTION - It seems like this isn't the best way to add an Azure Function Project to an existing workspace/solution. So what is the prescribed way and is there a tutorial or link somewhere I can follow as everything on the Microsoft site just shows new standalone function projects by themselves.

1 Answers

Some quick digging and I found this in the vscode-azurefunctions repo

private insertNewTasks(existingTasks: ITask[] | undefined, newTasks: ITask[]): ITask[] {
    existingTasks = existingTasks || [];
    // Remove tasks that match the ones we're about to add
    existingTasks = existingTasks.filter(t1 => !newTasks.find(t2 => {
        if (t1.type === t2.type) {
            switch (t1.type) {
                case func:
                    return t1.command === t2.command;
                case 'shell':
                case 'process':
                    return t1.label === t2.label && t1.identifier === t2.identifier;
                default:
                    // Not worth throwing an error for unrecognized task type
                    // Worst case the user has an extra task in their tasks.json
                    return false;
            }
        } else {
            return false;
        }
    }));
    existingTasks.push(...newTasks);
    return existingTasks;
}

By the looks of it, the extension will try to replace existing tasks if they match in type, label and identifier, rather than trying to extend them; which in my opinion would be the sensible thing to do for something as common as process and build.

So you are correct, your tasks.json is being updated by the extension upon creating a new func project, using the wizard offered by the extension. And I honestly think this is a bug, one you probably want to report to them directly at the vscode-azurefunctions repo. Also, the code in question can be found here.

Alternatives to reporting the issue as a bug and waiting for resolution:

With Func Core Tools you can use func new from the folder you want your function in (using a terminal of your liking). It will let you choose a worker runtime (dotnet, js, python etc.), and the name of the project. It will not interfere with your existing tasks.json.

Related