runtime: Build error NETSDK1124 with NET8 RC2 when multiple TargetFrameworks

Description

Trying to publish an application <TargetFramework>net8.0</TargetFramework> with trimming enabled:

dotnet publish -r win-x64 -c release --self-contained -p:PublishTrimmed=true .\examples\NetCore2\HostingExample

The application depends on two libraries:

  • NLog.Extensions.Logging:
    <TargetFrameworks>net461;netstandard1.3;netstandard1.5;netstandard2.0;netstandard2.1;net6.0;net8.0</TargetFrameworks>
    
  • NLog.Extensions.Hosting:
    <TargetFrameworks>netstandard2.0;netstandard2.1;net6.0;net8.0</TargetFrameworks>
    

The build fails with the following error:

C:\Program Files\dotnet\sdk\8.0.100-rc.2.23502.2\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Sdk.FrameworkReferenceRes
olution.targets(90,5): error NETSDK1124: Trimming assemblies requires .NET Core 3.0 or higher. [C:\Users\snakefoot\sour
ce\repos\NLog.Extensions.Logging\src\NLog.Extensions.Hosting\NLog.Extensions.Hosting.csproj::TargetFramework=netstandar
d2.0]
C:\Program Files\dotnet\sdk\8.0.100-rc.2.23502.2\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Sdk.FrameworkReferenceRes
olution.targets(90,5): error NETSDK1124: Trimming assemblies requires .NET Core 3.0 or higher. [C:\Users\snakefoot\sour
ce\repos\NLog.Extensions.Logging\src\NLog.Extensions.Logging\NLog.Extensions.Logging.csproj::TargetFramework=net461]

Reproduction Steps

Publish NET8 application with trimming, that depends on projects with mutiple <TargetFrameworks> (Besides NET8)

Also tried to update library-projects to be very explicit about when to enable trimming, but build with publish still fails (No warnings or errors with standard dotnet build):

    <EnableTrimAnalyzer Condition=" '$(TargetFramework)' == 'net8.0'">true</EnableTrimAnalyzer>
    <IsTrimmable Condition=" '$(TargetFramework)' == 'net8.0'">true</IsTrimmable>

Also tried adding these command-line options -f net8.0 or -p:TargetFramework=net8.0, but the build-error remains.

Expected behavior

Build publishes the application without errors.

Actual behavior

Build fails with error NETSDK1124: Trimming assemblies requires .NET Core 3.0 or higher. for the library projects.

Regression?

Build publish with trimming works fine with NET6 + NET7

Known Workarounds

Uninstall NET8 RC2 SDK or stop using multiple TargetFrameworks in library-projects or depend on nuget-packages instead of the library-projects directly.

Configuration

No response

Other information

No response

About this issue

  • Original URL
  • State: open
  • Created 8 months ago
  • Reactions: 4
  • Comments: 15 (8 by maintainers)

Commits related to this issue

Most upvoted comments

@snakefoot, could you please try adding <PublishTrimmed>true</PublishTrimmed> to your project file for the app and remove it from the command line?

There is a somewhat unexpected MSBuild behavior where all properties passed on the command line are forced onto all projects (even dependencies), and PublishTrimmed will turn on EnableTrimAnalyzers which will cause the error on projects targeting just netstandard2.0 (for a good reason). This behavior makes littles sense for property like PublishTrimmed, but MSBuild currently does it for everything.

/cc @sbomer

I think the current build-error message is a little confusing:

NETSDK1124 | Trimming assemblies requires .NET Core 3.0 or higher.

When the solution involves removing PublishTrimmed from command-line and instead specifying it in app-csproj-file, and also making sure IsTrimmable is conditional.

I’ll check with MSBuild folks again. Last time we asked, there were two possible fixes:

I think both of those solutions are worse than the original problem. It sounds like moving PublishTrimmed to the project file was an acceptable solution so far, so I don’t think we should go out of our way to make it easier to pass PublishTrimmed on the command-line unless it becomes a blocker for people, or we have a better fix on the MSBuild side.

Yes moving <PublishTrimmed>true</PublishTrimmed> into the application csproj-file (and removing from the command-line) resolved the publish error for me.

Combined with adding <IsTrimmable Condition="'$(TargetFramework)' == 'net8.0'">true</IsTrimmable> to the other library-projects.