Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Build error NETSDK1124 with NET8 RC2 when multiple TargetFrameworks #94406

Closed
snakefoot opened this issue Oct 28, 2023 · 16 comments
Closed

Build error NETSDK1124 with NET8 RC2 when multiple TargetFrameworks #94406

snakefoot opened this issue Oct 28, 2023 · 16 comments
Labels
area-Tools-ILLink .NET linker development as well as trimming analyzers

Comments

@snakefoot
Copy link

snakefoot commented Oct 28, 2023

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

@ghost ghost added the untriaged New issue has not been triaged by the area owner label Oct 28, 2023
@ghost
Copy link

ghost commented Oct 28, 2023

Tagging subscribers to this area: @dotnet/area-infrastructure-libraries
See info in area-owners.md if you want to be subscribed.

Issue Details

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)

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 works fine with NET6 + NET7

Known Workarounds

Uninstall NET8 RC2 SDK

Configuration

No response

Other information

No response

Author: snakefoot
Assignees: -
Labels:

area-Infrastructure-libraries, untriaged

Milestone: -

@ViktorHofer ViktorHofer transferred this issue from dotnet/runtime Oct 28, 2023
@snakefoot
Copy link
Author

snakefoot commented Oct 29, 2023

@ViktorHofer Curious why this is moved to dotnet/sdk, since #91535 + #91542 was handled in dotnet/runtime ? Or was it just a fix to dotnet/runtime to work-around the issue in dotnet/sdk ?

@snakefoot
Copy link
Author

snakefoot commented Oct 29, 2023

Was able to publish without build-errors by adding -p:TargetFrameworks=net8.0 (And ensure all libraries supported net8.0):

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

@snakefoot
Copy link
Author

snakefoot commented Oct 29, 2023

Hrm adding -p:TargetFrameworks=net8.0 apparently disables --self-contained, and final optimizing is not performed.

@snakefoot snakefoot reopened this Oct 29, 2023
@ViktorHofer ViktorHofer transferred this issue from dotnet/sdk Nov 6, 2023
@ViktorHofer ViktorHofer added area-Tools-ILLink .NET linker development as well as trimming analyzers and removed area-Infrastructure-libraries labels Nov 6, 2023
@ghost
Copy link

ghost commented Nov 6, 2023

Tagging subscribers to this area: @dotnet/area-infrastructure-libraries
See info in area-owners.md if you want to be subscribed.

Issue Details

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

Author: snakefoot
Assignees: -
Labels:

area-Infrastructure-libraries, untriaged

Milestone: -

@ViktorHofer
Copy link
Member

Good point. I just moved the issue back. cc @agocke

@ChaosEngine
Copy link

ChaosEngine commented Nov 7, 2023

I am seeing similiar error on
dotnet publish --no-restore -c Release --self-contained -r linux-x64 -p:PublishTrimmed=true Project
when including <TargetFramework>netstandard2.0</TargetFramework> library as dependency of main
<TargetFramework>net8.0</TargetFramework build

@ViktorHofer
Copy link
Member

cc @vitek-karas

@vitek-karas
Copy link
Member

@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

@snakefoot
Copy link
Author

snakefoot commented Nov 8, 2023

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.

@ChaosEngine
Copy link

ChaosEngine commented Nov 9, 2023

@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

My problem is also resolved for two Docker builds (alpine and debian based) with trimming enabled - now only in top-most *.csproj I am having <PublishTrimmed>true</PublishTrimmed>

@vitek-karas
Copy link
Member

Our guidance is to put the PublishTrimmed (or PublishAot) properties into the project file. Not only it doesn't have this particular problem but it will also:

  • enable analyzers for your app's project so that you get trim/AOT related warnings during build
  • modifies some settings such that the app will behave the same in debug builds as well as when published trimmed (trimming by default disables some runtime functionality which is not compatible with trimming for example)
  • this means that you should gain confidence in the behavior of the app after publish, and you can test with the same runtime behavior it will have after trimming.

@sbomer - can we raise this again with the SDK/MSBuild people, to see if we can make progress on it? Originally it was just a quirk we found internally, but this indicates people are running into it.

@sbomer
Copy link
Member

sbomer commented Nov 10, 2023

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.

@snakefoot
Copy link
Author

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.

@vitek-karas
Copy link
Member

The problem is that the error was never meant to be triggered by PublishTrimmed, it's there if you add IsTrimmable onto a netstandard2.0 project. But the flow of the various properties causes it to show up anyway. I guess we could detect that PublishTrimmed is set and show a different error possibly.

@agocke
Copy link
Member

agocke commented Apr 22, 2024

Another way to easily handle this in the project file is to use a condition:

  <PropertyGroup Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net7.0'))">
    <PublishTrimmed>true</PublishTrimmed>
  </PropertyGroup>

@agocke agocke closed this as completed Apr 22, 2024
@dotnet-policy-service dotnet-policy-service bot removed the untriaged New issue has not been triaged by the area owner label Apr 22, 2024
@github-actions github-actions bot locked and limited conversation to collaborators May 23, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-Tools-ILLink .NET linker development as well as trimming analyzers
Projects
Archived in project
Development

No branches or pull requests

6 participants