microsoft / WindowsAppSDK

The Windows App SDK empowers all Windows desktop apps with modern Windows UI, APIs, and platform features, including back-compat support, shipped via NuGet.
https://docs.microsoft.com/windows/apps/windows-app-sdk/
MIT License
3.72k stars 308 forks source link

Add build errors if nuget package added to projects targeting unsupported .NET TFMs #2762

Open AdamBraden opened 1 year ago

AdamBraden commented 1 year ago

.NET developers can set TFM to net6.0 and net6.0-windows. If they then add a nuget reference to the WinAppSDK, the build will succeed, but the winappsdk will not be referenced, leading to developer confusion.

A way around this is to add a ResolveAssemblyReferences target that raises an error for netcoreapp as a fallback.

<Project xmlns="[http://schemas.microsoft.com/developer/msbuild/2003">](http://schemas.microsoft.com/developer/msbuild/2003%22%3E)

<Target Name="WindowsAppSDKTFMCheck" BeforeTargets="ResolveAssemblyReferences;Build;Rebuild">
<Error Condition="'$(OutputType)'!='Library'"
        Text = "The 'Microsoft.WindowsAppSDK' nuget package cannot be used to target '$(TargetFramework)'. Target 'net6.0-windows10.0.18362.0' or later instead." />
</Target>

</Project>

Thanks to @dotmorten for pointing this technique out. Approach is similar to this PR: https://github.com/dotnet/reactive/pull/1442/files

dotMorten commented 1 year ago

Additional note: \build\Microsoft.WindowsAppSDK.props and \build\Microsoft.WindowsAppSDK.targets (as well as buildTransitive\*) needs to be moved to subfolders net6.0-windows10.0.xxxxx and native, so that the app sdk is only getting included into the project types that actually support the windows app sdk. (the rest of the files can be in a shared folder and just referenced with a relative path, which might also help avoiding all the duplication in build and buildTransitive folders). Instead of putting it in netcoreapp folder, you might want to put the build error in netstandard1.0 so other projects like .NET Framework and .net standard libraries will also get the build error.