BenchmarkDotNet icon indicating copy to clipboard operation
BenchmarkDotNet copied to clipboard

Benchmarks referencing TestAdapter fail to build with OS-specific TFMs

Open molesmoke opened this issue 7 months ago • 4 comments

Attempting to use the TestAdapter with a project using an OS-specific TFM such as <TargetFramework>net8.0-windows</TargetFramework> gives an error when trying to run the benchmark through the test explorer:

D:\BenchmarkTest\bin\x64\Release\2cab09cb-454b-49b3-b8fb-d898d2a7f2df\BenchmarkDotNet.Autogenerated.csproj : error NU1201: Project BenchmarkTest is not compatible with net8.0 (.NETCoreApp,Version=v8.0). Project BenchmarkTest supports: net8.0-windows7.0 (.NETCoreApp,Version=v8.0)

molesmoke avatar Jan 08 '24 23:01 molesmoke

I tried to add a custom entry point and config to specify the TFM explicitly, but got the same error:

public static class Program
{
    sealed class CustomConfig : ManualConfig
    {
        public CustomConfig()
        {
            AddJob(Job.ShortRun.WithToolchain(CsProjCoreToolchain.From(
                            new NetCoreAppSettings(
                                targetFrameworkMoniker: "net8.0-windows",
                                runtimeFrameworkVersion: null,
                                name: "net8.0-windows")))
                                .AsDefault());
            AddLogger(ConsoleLogger.Default);
            AddColumn(TargetMethodColumn.Method, StatisticColumn.Iterations, StatisticColumn.Mean, StatisticColumn.Max);
            AddColumnProvider(DefaultColumnProviders.Params);
        }
    }

    static void Main(string[] args)
    {
        CustomConfig config = new();
        _ = BenchmarkRunner.Run(typeof(Program).Assembly, config, args);
    }
}

molesmoke avatar Jan 08 '24 23:01 molesmoke

You can use a Config attribute https://benchmarkdotnet.org/articles/features/vstest.html#setting-a-default-configuration Or you can disable the generated entry point <GenerateProgramFile>false</GenerateProgramFile>

timcassell avatar Jan 08 '24 23:01 timcassell

@timcassell Ah, yup, the Config attribute does work, but I was trying to do it just via a custom entry point to avoid hitting problems from #2500. If I do both the Config attribute and <GenerateProgramFile>false</GenerateProgramFile> then everything is OK - my bad

molesmoke avatar Jan 09 '24 00:01 molesmoke

cc @caaavik-msft

adamsitnik avatar Jan 09 '24 08:01 adamsitnik