Quantcast
Channel: Andrew Lock | .NET Escapades
Viewing all articles
Browse latest Browse all 743

Fixing build warning NETSSDK1138 when building projects with end-of-life .NET frameworks

$
0
0

In this short post I describe how to get rid of the following build warning:

C:\Program Files\dotnet\sdk\5.0.100\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.EolTargetFrameworks.targets(28,5):
warning NETSDK1138: The target framework 'netcoreapp2.0' is out of support and will not receive security updates in the future. 
Please refer to https://aka.ms/dotnet-core-support for more information about the support policy.

This post shows how to remove the warning either by adding a property to your project file or by passing a property to the build commands.

tl;dr; Set the MSBuild property CheckEolTargetFramework to false, either by setting it in the project's .csproj file, or by passing /p:CheckEolTargetFramework=false when running commands with the .NET CLI

When do you see the warning?

This warning appears when you're building a project which uses a .NET Core version which was no longer supported when the .NET SDK version used to build the project was released.

For example, I was building a .NET Core 3.0 project (which went out of support March 3rd 2020, over a year ago), with the .NET 5 SDK, and saw the following warning:

C:\Program Files\dotnet\sdk\5.0.100\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.EolTargetFrameworks.targets(28,5):
warning NETSDK1138: The target framework 'netcoreapp3.0' is out of support and will not receive security updates in the future. 
Please refer to https://aka.ms/dotnet-core-support for more information about the support policy.

This warning is shown any time you restore or build the project, which can mean multiple warnings cluttering up your build logs.

Does the warning matter?

Well, it depends! If you're creating an app, then as the warning says, you should strongly consider updating to a .NET Core version that has support and is receiving patches!

If, on the other hand, you're a library author that still needs to support these end-of-life (EOL) projects, then the constant warnings can be an annoyance at best, and could break your build at worst.

If you're running your build with /warnaserror, so that you treat warnings as errors, then this warning from the SDK will break your build:

dotnet build /warnaserror 
Microsoft (R) Build Engine version 16.8.3+39993bd9d for .NET
Copyright (C) Microsoft Corporation. All rights reserved.

  Determining projects to restore...
C:\Program Files\dotnet\sdk\5.0.104\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.EolTargetFrameworks.targets(28,5): error NETSDK1138: The target framework 'netcoreapp3.0' is out of support and will not receive security updates in the future. Please refer to https://aka.ms/dotnet-core-support for more information about the support policy. [C:\repos\checkeol\checkeol.csproj]
C:\Program Files\dotnet\sdk\5.0.104\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.EolTargetFrameworks.targets(28,5): error NETSDK1138: The target framework 'netcoreapp3.0' is out of support and will not receive security updates in the future. Please refer to https://aka.ms/dotnet-core-support for more information about the support policy. [C:\repos\checkeol\checkeol.csproj]
  All projects are up-to-date for restore.

Build FAILED.

So if you know that you have to support the end of life project, how can you make this warning go away?

Setting <CheckEolTargetFramework> to remove the warning

The first option to remove the warning is to set the MSBuild property CheckEolTargetFramework. You can do this from your project file by adding a <PropertyGroup> setting that property, as shown below:

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

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp3.0</TargetFramework>
    <!-- Set the CheckEolTargetFramework property to false to fix the warning -->
    <CheckEolTargetFramework>false</CheckEolTargetFramework>
  </PropertyGroup>

</Project>

Now when you restore or build, you won't get the warning about end of life frameworks:

dotnet build
Microsoft (R) Build Engine version 16.8.3+39993bd9d for .NET
Copyright (C) Microsoft Corporation. All rights reserved.

  Determining projects to restore...
  All projects are up-to-date for restore.
  checkeol -> C:\repos\checkeol\bin\Debug\netcoreapp3.0\checkeol.dll

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

Time Elapsed 00:00:01.64

This approach sets an MSBuild property, so if you don't want to modify your project files, you can also set it at runtime, as I show in the next section

Passing CheckEolTargetFramework from the command line

The .NET CLI calls into the MSBuild build system to run NuGet restore and builds, so you can pass MSBuild properties via the command line. To set an MSBuild property when calling a dotnet command, use the /p:PROPERTY=VALUE syntax. So, to set CheckEolTargetFramework=false, use

dotnet restore /p:CheckEolTargetFramework=false

or

dotnet build /p:CheckEolTargetFramework=false

This sets the property to false, ensuring that all projects built with the dotnet build command ignore EOL target frameworks.

This approach means that MSBuild doesn't generate the warning for EOL frameworks but there's another option—generate the warning, but ignore it.

Ignore the specific warning

MSBuild allows you to ignore specific warnings when you build using the /nowarn flag. We can use that mechanism to ignore our EOL warnings in MSBuild by using /nowarn:netsdk1138

dotnet restore /nowarn:netsdk1138

This approach gives the same end functionality as the previous approaches, but it works slightly differently. In the previous approaches, we don't check for EOL .NET Core versions. With the /nowarn approach, the check is still made, you just won't see the warning generated. You can see this in action if you run the above command, with detailed logging enabled:

> dotnet restore /nowarn:netsdk1138 -v:detailed

...
>Target "_CheckForUnsupportedNETCoreVersion" in file "C:\Program Files\dotnet\sdk\5.0.104\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.TargetFrameworkInference.targets" from project "C:\repos\checkeol\checkeol.csproj" (target "CollectPackageReferences" depends on it):
       Task "NETSdkError" skipped, due to false condition; ('$(_TargetFrameworkVersionWithoutV)' > '$(NETCoreAppMaximumVersion)') was evaluated as ('3.0' > '5.0').
   1:5>Done building target "_CheckForUnsupportedNETCoreVersion" in project "checkeol.csproj".
   1:5>Target "_CheckForEolTargetFrameworks" in file "C:\Program Files\dotnet\sdk\5.0.104\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.EolTargetFrameworks.targets" from project "C:\repos\checkeol\checkeol.csproj" (target "CollectPackageReferences" depends on it):
       Using "NETSdkWarning" task from assembly "C:\Program Files\dotnet\sdk\5.0.104\Sdks\Microsoft.NET.Sdk\targets\..\tools\net5.0/Microsoft.NET.Build.Tasks.dll".
       Task "NETSdkWarning"
       C:\Program Files\dotnet\sdk\5.0.104\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.EolTargetFrameworks.targets(28,5): message NETSDK1138: The target framework 'netcoreapp3.0' is out of support and will not receive security updates in the future. Please refer to https://aka.ms/dotnet-core-support for more information about the support policy. [C:\repos\checkeol\checkeol.csproj]
       Done executing task "NETSdkWarning".
   1:5>Done building target "_CheckForEolTargetFrameworks" in project "checkeol.csproj".
...

The logs show that the _CheckForUnsupportedNETCoreVersion target was executed as part of the build, and the warning was emitted, but we just don't see it.

In contrast, if you run dotnet restore /p:CheckEolTargetFramework=false -v:detailed you'll see that the _CheckForUnsupportedNETCoreVersion target is skipped entirely:

> dotnet restore /p:CheckEolTargetFramework=false -v:detailed

1>Done building target "_CheckForUnsupportedNETCoreVersion" in project "checkeol.csproj".
       Target "_CheckForEolTargetFrameworks" skipped, due to false condition; ('@(_EolNetCoreTargetFrameworkVersions->AnyHaveMetadataValue('Identity', '$(_TargetFrameworkVersionWithoutV)'))' and '$(TargetFrameworkIdentifier)' == '.NETCoreApp' and '$(CheckEolTargetFramework)' == 'true') was evaluated as ('true' and '.NETCoreApp' == '.NETCoreApp' and 'false' == 'true').

Based on that, it makes most sense to set the CheckEolTargetFramework property to skip the task entirely.

Summary

In this post, I showed how to stop your .NET Core application generating warnings on build and restore when you're targeting a .NET Core framework that is no longer supported. You can do this by setting the <CheckEolTargetFramework> property to false in your project's .csproj file or by passing /p:CheckEolTargetFramework=false when running commands with the dotnet CLI or msbuild. Alternatively, you can ignore the warning generated by using /nowarn:netsdk1138 in your CLI commands.


Viewing all articles
Browse latest Browse all 743

Trending Articles