azure-functionsncrunch

NCrunch can't build project that references Microsoft.Azure.Functions.Worker.Sdk


I have an Azure Functions v3 (dotnet isolated) C# project that references Microsoft.Azure.Functions.Worker.Sdk version 1.0.4. When NCrunch tries to build this project it fails with the message: ..\..\..\..\..\..\program files (x86)\microsoft visual studio\2019\professional\MSBuild\Current\Bin\Microsoft.Common.CurrentVersion.targets (1150, 7): The target "NCrunchPrepareForBuild" does not exist in the project. If I remove the package reference the build succeeds.

Conditionally disabling the reference with <PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.0.4" OutputItemType="Analyzer" Condition="'$(NCrunch)' != '1'" /> doesn't work. Oddly, if I change that to Condition="'$(NCrunch)' == '1'" it does work, which suggests that the $(NCrunch) property has not been set at this point in the build process.

The line referenced by the error message looks like this:

<Target
      Name="PrepareForBuild"
      DependsOnTargets="$(PrepareForBuildDependsOn)">

Solution

  • As suggested by arkiaconsulting and Clement:

    Paths triggered only for the NCrunch build

     <Target Name="CopyAzureFunctionOutput" AfterTargets="_GenerateFunctionsPostBuild" Condition="'$(NCrunch)' == '1'">
        <Exec Command="xcopy /y &quot;$(OutDir)\bin&quot; &quot;$(OutDir)&quot;" />
      </Target>
    

    To disable the targets loading, you can set the ExcludeRestorePackageImports property to true in your project referencing the Azure Function package like this:

    <PropertyGroup Condition="'$(NCrunch)' == '1'">
    <ExcludeRestorePackageImports>true</ExcludeRestorePackageImports>
    </PropertyGroup>
    
    

    That way, the targets are not triggered, the files are not moved into the bin folders and NCrunch works fine.

    You can refer to Azure function:build issue

    If the above suggestions don't work then you can ask the same on NCrunch forum and also open an issue on GitHub: azure-functions-vs-build-sdk