question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

.NET 8: Enable build and run of project with arbitrary TFM

See original GitHub issue

I write a bunch of samples that I want to enable for developers to learn the product. TFMs are a limiting artifact when you want to enable a project to run on a newer runtime even though it targets an older one. I feel badly when I change the TFM solely to get to the app to run on a newer runtime.

App I am using: https://github.com/richlander/testapps/tree/master/versioninfo

In past, I used the TargetFrameworks approach, like the following:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFrameworks>netcoreapp3.0;net5.0</TargetFrameworks>
  </PropertyGroup>

</Project>

This is a pain, because dotnet build does the following:

PS D:\git\testapps\versioninfo> dotnet run
Unable to run your project
Your project targets multiple frameworks. Specify which framework to run using '--framework'.

That’s unacceptable as a default experience for samples, and only to enable a more advanced scenario.

Next approach is to configure the project for the min-support TFM and then mutate via the CLI:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp3.0</TargetFramework>
  </PropertyGroup>

</Project>
PS D:\git\testapps\versioninfo> dotnet run
**.NET Core info**
Version: 3.1.2
FrameworkDescription: .NET Core 3.1.2
CoreCLR Build: 3.1.2-servicing.20066.2
CoreCLR Hash: fd22fd600a2526a7c58536da0e80325eb710d7f1
CoreFX Build: 3.1.2
CoreFX Hash: e946cebe43a510e8c6476bbc8185d1445df33a1a

**Environment info**
OSVersion: Microsoft Windows NT 6.2.9200.0
OSDescription: Microsoft Windows 10.0.19624
OSArchitecture: X64
ProcessorCount: 8

PS D:\git\testapps\versioninfo> dotnet build /p:TargetFramework=net5.0
Microsoft (R) Build Engine version 16.7.0-preview-20220-01+80e487bff for .NET Core
Copyright (C) Microsoft Corporation. All rights reserved.

  Determining projects to restore...
  Restored D:\git\testapps\versioninfo\versioninfo.csproj (in 61 ms).
  You are using a preview version of .NET. See: https://aka.ms/dotnet-core-preview
  versioninfo -> D:\git\testapps\versioninfo\bin\Debug\net5.0\versioninfo.dll

Build succeeded.
    0 Warning(s)
    0 Error(s)

Time Elapsed 00:00:00.83
PS D:\git\testapps\versioninfo> .\bin\Debug\net5.0\versioninfo.exe
**.NET Core info**
Version: 5.0.0
FrameworkDescription: .NET 5.0.0-preview.4.20227.9
CoreCLR Build: 5.0.0-preview.4.20227.9
CoreCLR Hash: 6d1f7e01d3429054ec3dcb7c75b3450b9fe1429e
CoreFX Build: 5.0.0-preview.4.20227.9
CoreFX Hash: 6d1f7e01d3429054ec3dcb7c75b3450b9fe1429e

**Environment info**
OSVersion: Microsoft Windows NT 10.0.19624.0
OSDescription: Microsoft Windows 10.0.19624
OSArchitecture: X64
ProcessorCount: 8

PS D:\git\testapps\versioninfo> dotnet run /p:TargetFramework=net5.0
/p:TargetFramework=net5.0
**.NET Core info**
Version: 3.1.2
FrameworkDescription: .NET Core 3.1.2
CoreCLR Build: 3.1.2-servicing.20066.2
CoreCLR Hash: fd22fd600a2526a7c58536da0e80325eb710d7f1
CoreFX Build: 3.1.2
CoreFX Hash: e946cebe43a510e8c6476bbc8185d1445df33a1a

**Environment info**
OSVersion: Microsoft Windows NT 6.2.9200.0
OSDescription: Microsoft Windows 10.0.19624
OSArchitecture: X64
ProcessorCount: 8

PS D:\git\testapps\versioninfo> dotnet run /p:TargetFramework=net5.0 -- arguments
/p:TargetFramework=net5.0
arguments
**.NET Core info**
Version: 3.1.2
FrameworkDescription: .NET Core 3.1.2
CoreCLR Build: 3.1.2-servicing.20066.2
CoreCLR Hash: fd22fd600a2526a7c58536da0e80325eb710d7f1
CoreFX Build: 3.1.2
CoreFX Hash: e946cebe43a510e8c6476bbc8185d1445df33a1a

**Environment info**
OSVersion: Microsoft Windows NT 6.2.9200.0
OSDescription: Microsoft Windows 10.0.19624
OSArchitecture: X64
ProcessorCount: 8

I added the following code to the app to print arguments.

            foreach(var a in args)
            {
                Console.WriteLine(a);
            }

The approach with /p:TargetFramework seems to be generally working, but is failing for the run case. The docs suggest that what I’m doing should be working.

Can my preferred pattern be enabled? This: dotnet run /p:TargetFramework=net5.0 --. That’d be awesome.

Not what I want, but I did discover that the following pattern works:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp3.0</TargetFramework>
    <TargetFrameworks>netcoreapp3.0;net5.0</TargetFrameworks>
  </PropertyGroup>

</Project>
PS D:\git\testapps\versioninfo> dotnet run
**.NET Core info**
Version: 3.1.2
FrameworkDescription: .NET Core 3.1.2
CoreCLR Build: 3.1.2-servicing.20066.2
CoreCLR Hash: fd22fd600a2526a7c58536da0e80325eb710d7f1
CoreFX Build: 3.1.2
CoreFX Hash: e946cebe43a510e8c6476bbc8185d1445df33a1a

**Environment info**
OSVersion: Microsoft Windows NT 6.2.9200.0
OSDescription: Microsoft Windows 10.0.19624
OSArchitecture: X64
ProcessorCount: 8

PS D:\git\testapps\versioninfo> dotnet run -f net5.0
**.NET Core info**
Version: 5.0.0
FrameworkDescription: .NET 5.0.0-preview.4.20227.9
CoreCLR Build: 5.0.0-preview.4.20227.9
CoreCLR Hash: 6d1f7e01d3429054ec3dcb7c75b3450b9fe1429e
CoreFX Build: 5.0.0-preview.4.20227.9
CoreFX Hash: 6d1f7e01d3429054ec3dcb7c75b3450b9fe1429e

**Environment info**
OSVersion: Microsoft Windows NT 10.0.19624.0
OSDescription: Microsoft Windows 10.0.19624
OSArchitecture: X64
ProcessorCount: 8

For clarity, I don’t like this approach for two reasons:

  • It is an abomination of the project format.
  • It requires including all the TFMs I want to support. This is similar to how RIDs were in 1.0. Super painful.

On the one hand, the existing dotnet build support for my use case is satisfactory (super happy it works), but on the other, it seems the scenario is broken for dotnet run where run and build should have parity.

So, I think this should be fixed. Thanks!

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:9 (9 by maintainers)

github_iconTop GitHub Comments

3reactions
jkotascommented, May 12, 2020

<TargetFramework>netcoreapp$(NETCoreAppMaximumVersion)</TargetFramework> works as “latest” since .NET Core 3.0.

It would be nice if one can say <TargetFramework>latest</TargetFramework> to get the latest. Or even better - default to latest if TargetFramework is not specified.

0reactions
richlandercommented, Jun 25, 2022
Read more comments on GitHub >

github_iconTop Results From Across the Web

8.0.1xx Milestone
NET Core projects, that is shared between Visual Studio and CLI - 8.0.1xx Milestone ... NET 8: Enable build and run of project...
Read more >
dotnet build command - .NET CLI
The dotnet build command builds the project and its dependencies into a set of binaries. The binaries include the project's code in Intermediate ......
Read more >
Create a custom task for code generation - MSBuild
In this tutorial, you'll create a custom task in MSBuild in C# that handles code generation, and then you'll use the task in...
Read more >
dotnet core::Unable to run your project. Ensure you have a ...
A runnable project should target a runnable TFM (for instance, netcoreapp2.0) and have OutputType 'Exe'. The current OutputType is 'Exe'. FYI, ...
Read more >
Deep-dive into .NET Core primitives, part 3: runtimeconfig. ...
Create a new project ( dotnet new console -n MyApp ) · Create a file named “runtimeconfig.template.json” into the project directory (next to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found