dotnet / BenchmarkDotNet

Powerful .NET library for benchmarking
https://benchmarkdotnet.org
MIT License
10.24k stars 952 forks source link

How to fail the run if NuGet restored with warnings? #2581

Open mstefarov opened 1 month ago

mstefarov commented 1 month ago

I ran into several cases where I accidentally benchmarked the wrong version of a NuGet package because NuGet restore warnings (e.g. NU1102 "Unable to find package 'Foo' with version (>= X.Y.Z)" or NU1603 "An approximate best match of X.Y.Z was resolved.") were not reported by BenchmarkDotNet when I use CsProjClassicNetToolchain or CsProjCoreToolchain, and these warnings did not fail the build/run.

I've been looking for a way to treat these warnings as errors, but have not found a way yet.

Is there another way, short of forking CsProj toolchains?

IMO treating NuGet restore problems as run failures seems like a resonable default behavior too.

timcassell commented 1 month ago

I'm surprised dotnet restore ignores that property. Probably a simple solution would be to copy the WarningsAsErrors property from the csproj. https://github.com/dotnet/BenchmarkDotNet/blob/6a7244d76082f098a19785e4e3b0e0f269fed004/src/BenchmarkDotNet/Toolchains/CsProj/CsProjGenerator.cs#L26-L39

Alternatively, adding <WarningsAsErrors>NU1102;NU1603</WarningsAsErrors> to the csproj template could also work.

I don't have an opinion as to which would be the better option.