RicoSuter / NSwag

The Swagger/OpenAPI toolchain for .NET, ASP.NET Core and TypeScript.
http://NSwag.org
MIT License
6.61k stars 1.22k forks source link

NSwag.MSBuild runs 2 times during build #2794

Open geidans opened 4 years ago

geidans commented 4 years ago

It appears added target NSwag executes 2 times (in parallel) during build.

I have added the following section to csproj project file:

  <Target Name="NSwag" BeforeTargets="PrepareForBuild" Condition="'$(GenerateCode)'=='True' ">
    <Exec Command="$(NSwagExe_Core31) run client.nswag /variables:Configuration=$(Configuration)" />
  </Target>

The log output:

2>Executing file 'client.nswag' with variables 'Configuration=Debug'...
2>
2>Executing file 'client.nswag' with variables 'Configuration=Debug'...

And it fails here:

2>C:\Program Files\dotnet\sdk\3.1.201\Microsoft.Common.CurrentVersion.targets(2081,5): warning MSB3088: Could not read state file "obj\Debug\netcoreapp3.1\XXXXXXXX.csprojAssemblyReference.cache". The process cannot access the file 'XXXXXXXXXX\obj\Debug\netcoreapp3.1\XXXXXXXXXX.csprojAssemblyReference.cache' because it is being used by another process. [XXXXXXXXXXXXXXXXX.csproj]

When running build in VS 2019 it appear to be invoked 3 times, resulting in intermittent failure:

There is no pattern on when it fails or when it succedes.

jeremyVignelles commented 3 years ago

I'm not sure that PrepareForBuild is called only once in the build process. I dropped the BeforeTargets=.. and usually run dotnet build /t:NSwag as part of my build pipeline, because my spec doesn't change very often.

I have this in a build.ps1:

& dotnet build -t:NSwag "Project.csproj"
if (-not $?)
{
    throw 'Error generating NSwag'
}

$GitStatusOutput = (git status --porcelain) | Out-String
if ( $GitStatusOutput -ne "" )
{
    throw 'There are uncommited changes in your repository, please commit your changes and run this command again'
}

That way, if I forgot to update the NSwag client classes, I am notified that something went wrong, and I need to test locally.

geidans commented 3 years ago

It appeared that problem is due to parallel builds in Visual Studio. Having played with dependencies did not help. I have now disabled parallel build and all seem to be working fine.