Add NETSDK1235 warning for non-AnyCPU PlatformTarget in modern .NET
PlatformTarget is a legacy .NET Framework setting that has no effect in modern .NET. Users explicitly setting it to non-AnyCPU values (x64, x86, arm, arm64) should be guided to remove this customization.
Changes
- New warning NETSDK1235: Warns when
PlatformTargetis explicitly set to a non-AnyCPU value in .NETCoreApp projects - Target
_CheckForNonAnyCPUPlatformTarget: Only fires when user explicitly sets the property (not when SDK infers it fromRuntimeIdentifier) - Unit tests: Validates warning appears for explicit non-AnyCPU values and does not appear for AnyCPU
Example
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<PlatformTarget>x64</PlatformTarget> <!-- NETSDK1235 warning -->
</PropertyGroup>
</Project>
Output:
warning NETSDK1235: Setting the PlatformTarget property to 'x64' is not recommended
for modern .NET applications. The PlatformTarget property is a legacy setting from
.NET Framework and has no effect in modern .NET. Consider removing PlatformTarget
or setting it to 'AnyCPU'.
No warning is issued when:
PlatformTargetisAnyCPUPlatformTargetis not set or inferred fromRuntimeIdentifier- Target framework is .NET Framework
Original prompt
This section details on the original issue you should resolve
<issue_title>Flag usage of non-AnyCPU values for PlatformTarget in modern .NET applications</issue_title> <issue_description>### Is your feature request related to a problem? Please describe.
The
PlatformTargetfield (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 beAnyCPU. 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.</issue_description>
Comments on the Issue (you are @copilot in this section)
@jkotas 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</comment_new> <comment_new>
@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.</comment_new>@baronfel
- Fixes dotnet/sdk#42556
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.
There is currently a requirement when using CsWin32 to specify a PlatformTarget to generate certain methods, I believe due to x64 vs x86 differences