dotnet / sdk

Core functionality needed to create .NET Core projects, that is shared between Visual Studio and CLI
https://dot.net/core
MIT License
2.65k stars 1.06k forks source link

NETCOREAPP2_0_OR_GREATER not defined when net5-windows + UseWindowsForms #29718

Open CreateAndInject opened 1 year ago

CreateAndInject commented 1 year ago

Compiler doesn't compile Test1 when targeting net5-windows (>=.NETCore3)

#if NETCOREAPP2_0_OR_GREATER

class Test1
{

}
#endif

#if NETCOREAPP3_0_OR_GREATER
class Test2
{

}
#endif

namespace ConsoleApp_NETCore
{
    class Program
    {
        static void Main(string[] args)
        {
            Log("test");
        }

        static void Log(string msg)
        {
#if NETCOREAPP3_0_OR_GREATER
            System.Windows.Forms.MessageBox.Show(msg);
#else
            System.Console.WriteLine(msg);
#endif
        }
    }
}
<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
      <UseWindowsForms>true</UseWindowsForms>
      <!--<UseWindowsForms Condition="'$(TargetFramework)' != 'netcoreapp2.1'">true</UseWindowsForms>-->
      <TargetFrameworks>netcoreapp2.1;netcoreapp3.0-windows;net5-windows;net6-windows</TargetFrameworks>
      <LangVersion>preview</LangVersion>
  </PropertyGroup>

</Project>

I know .NETCore2.1 doesn't support WinForms, so I already add #if to prevent .NETCore2.1 compile WinForms codes

dotnet-issue-labeler[bot] commented 1 year ago

I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.

CyrusNajmabadi commented 1 year ago

What error do you get?

CreateAndInject commented 1 year ago

class Test1 is not a part of net5-windows output, but it should be

CreateAndInject commented 1 year ago

@CyrusNajmabadi Can you reproduce this bug?

Youssef1313 commented 1 year ago

I don't think this is a Roslyn issue. I looked at how dotnet/sdk generates symbols for implicit DefineConstants, and the root cause for this issue is that .NET Core 2.0 isn't in SupportedNETCoreAppTargetFramework when building against netcoreapp3.0-windows:

image

I can't track down this further, but I think investigation should start at dotnet/sdk, and they can move the issue appropriately after investigation.

Youssef1313 commented 1 year ago

I could track this down. The bug is caused by this:

https://github.com/dotnet/wpf/blob/bed334c8b18400e79ef7b0287965e9f642932155/packaging/Microsoft.NET.Sdk.WindowsDesktop/targets/Microsoft.NET.Sdk.WindowsDesktop.props#L130-L167

dotnet/wpf removes few TFMs from SupportedNETCoreAppTargetFramework (including .NET Core 2.0), which then affects <DefineConstants>

This was done intentionally, but the fact that this affects DefineConstants seems like an unintended consequence.

This might require work on both dotnet/sdk and dotnet/wpf.

baronfel commented 7 months ago

@marcpopMSFT @dsplaisted this is another instance of the 'supported TFMs should be different from known TFMs' problem that we were looking at in that other bug last week. We should have a way to treat these two lists distinctly.