BenchmarkDotNet icon indicating copy to clipboard operation
BenchmarkDotNet copied to clipboard

Generated benchmarks ignore --framework flag when running out of process

Open fanasere opened this issue 2 years ago • 10 comments
trafficstars

hello I am hitting an error where when I run our benchmarks out of process with target net7.0 (like this: dotnet run -c release --framework net7.0 -r win-x64), Bechnmarkdotnet generates a csproj named "BenchmarkDotNet.Autogenerated" that contains the following configuration: <TargetFramework>net7.0</TargetFramework> but because this project is part of a solution where Directory.Build.Props is used with configuration: <TargetFrameworks>netstandard2.0;net7.0</TargetFrameworks> the configuration TargetFrameworks takes precedence over TargetFramework and i get error: error NU1201: Project is not compatible with netstandard2.0.

is there a way to configure benchmarkdotnet to put TargetFrameworks in the generated project instead of TargetFramework? or any other way to get around this problem thank you for your help

fanasere avatar Jul 16 '23 13:07 fanasere

It seems that we should do that, and actually pass the -f argument in the build script.

timcassell avatar Jul 17 '23 21:07 timcassell

that would greatly help us thank you

fanasere avatar Jul 18 '23 08:07 fanasere

It seems that we should do that, and actually pass the -f argument in the build script.

I don't remember why exactly, but we have avoided that in the past (I know it sounds silly, but this code is now very old ;) ).

We should rather get Directory.Build.Props settings ignored, as they often cause issues (like enforcing treating warning as errors and getting errors when building the generated code). We have tried that, but it does not always work:

https://github.com/dotnet/BenchmarkDotNet/blob/fd2639ff7b9e8353bb48c0eb6668c8bf32d19792/src/BenchmarkDotNet/Templates/CsProj.txt#L4-L5

adamsitnik avatar Jul 18 '23 11:07 adamsitnik

Yes I noticed these configurations but in my case they are not helping, what does make it work is changing the configuration TargetFramework to TargetFrameworks at line 7. as TargetFrameworks is a configuration that takes precedence over TargetFramework, and when comes from Directory.Build.Props it overrides the TargetFramework configuration in the generated csproj and causes this problem. is there a downside to changing the configuration to TargetFrameworks?

fanasere avatar Jul 18 '23 12:07 fanasere

I don't remember why exactly, but we have avoided that in the past (I know it sounds silly, but this code is now very old ;) ).

Well, if we're going to be copying the entire original xproj in #1403, we will need to handle that case anyway. And hopefully that will also allow us to remove hacks like <ImportDirectoryBuildProps>false</ImportDirectoryBuildProps>.

timcassell avatar Jul 18 '23 18:07 timcassell

The ImportDirectoryBuildProps is being ignored.. :\

How do I fix that?

ShaharPrishMSFT avatar Nov 16 '23 14:11 ShaharPrishMSFT

How do I fix that?

@ViktorHofer @eerhardt @ericstj BDN generates a .csproj file where we try to ignore global prop files (hat often alter some MSBuild properites) by using ImportDirectoryBuildProps tag. Is there something better that we can do to get them always ignored?

https://github.com/dotnet/BenchmarkDotNet/blob/fc7afeddcff7a52ccee165ac99ba216e8eb138ab/src/BenchmarkDotNet/Templates/CsProj.txt#L1-L6

adamsitnik avatar Nov 16 '23 15:11 adamsitnik

I could not make those properties affect the build - it would always look at my .props file

What I ended up doing is have an empty Directory.Build.props file in my benchmark project (under a folder), and I copy it to the output folder.

This seems to solve the issue.

ShaharPrishMSFT avatar Nov 16 '23 15:11 ShaharPrishMSFT

@ShaharPrishMSFT thanks for sharing the workaround!

adamsitnik avatar Nov 16 '23 15:11 adamsitnik

We usually do this the following way: https://github.com/dotnet/arcade/blob/main/src/Microsoft.DotNet.Arcade.Sdk/tools/Directory.Build.props

So, just create a Directory.Build.props file next to the project file with that content. Setting ImportDirectoryBuildProps to false in the project body is too late.

ViktorHofer avatar Nov 16 '23 15:11 ViktorHofer