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
- Rely on publish AoT in the csproj Related to https://github.com/dotnet/sdk/issues/37228 + https://github.com/dotnet/runtime/issues/94406 — committed to hwoodiwiss/ArgumentativeFilters by hwoodiwiss 6 months ago
@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 onEnableTrimAnalyzers
which will cause the error on projects targeting justnetstandard2.0
(for a good reason). This behavior makes littles sense for property likePublishTrimmed
, but MSBuild currently does it for everything./cc @sbomer
I think the current build-error message is a little confusing:
When the solution involves removing
PublishTrimmed
from command-line and instead specifying it in app-csproj-file, and also making sureIsTrimmable
is conditional.I’ll check with MSBuild folks again. Last time we asked, there were two possible fixes:
PublishTrimmed
from influencing restore whenIsPublishable=false
(default for library projects).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 passPublishTrimmed
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.