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.71k stars 1.06k forks source link

Flag usage of non-AnyCPU values for PlatformTarget in modern .NET applications #42556

Open baronfel opened 2 months ago

baronfel commented 2 months ago

Is your feature request related to a problem? Please describe.

The PlatformTarget field (and therefore Platform in MSbuild) only applies to .NET Framework and doesn't apply to modern .NET applications. For these applications, Platform can (should) always be AnyCPU. We should guide users to removing customizations to this property for projects that do not need it.

From Jan Kotas in dotnet/sdk#42344:

PlatformTarget is left-over from .NET Framework. (PlatformTarget can only represent architectures that .NET Framework targets. It does not represent full range of platforms that .NET Core can target.)

If you are targeting current .NET, building everything as AnyCPU (ie hardcoding PlatformTarget to AnyCPU) all the time should work just fine. It is what we are doing in dotnet/runtime repo. We hardcode PlatformTarget to AnyCPU, even for platform-specific binaries.

jkotas commented 2 months ago

Also, you may want to stop inferring the PlatformTarget in the SDK when targeting modern .NET applications:

https://github.com/dotnet/sdk/blob/88c64736ff7039536fc2546d5026d8b78e840e49/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.RuntimeIdentifierInference.targets#L124-L157

baronfel commented 2 months ago

@jkotas this issue is for an MSBuild BuildCheck for usage in customer projects, so I extracted your comment to a new issue for the SDK to stop setting it in the first place.