Skip to content

Commit

Permalink
Add options for specifying a minimum workload version requirement (#2…
Browse files Browse the repository at this point in the history
…5993)

* Add a MSBuild for bad installs

If we detect that you have installed .NET MAUI into a TFM which is not
found in the package, then we will fail the build.

The error code is MA003 and there is a property to skip this warning as
well:

Errors will look like:
- error MA003: This version of .NET MAUI requires at least a
  TargetFramework of 'net9.0-windows10.0.19041.0', which is greater
  than the current TargetFramework 'net8.0-windows10.0.19041.0'.
- error MA003: This version of .NET MAUI requires at least a
  TargetFramework of 'net9.0-ios', which is greater than the current
  TargetFramework 'net8.0-ios'.
- error MA003: This version of .NET MAUI requires at least a
  TargetFramework of 'net9.0-maccatalyst17.0', which is greater than
  the current TargetFramework 'net8.0-maccatalyst17.0'.

* Workload versions are better

* $(MauiWorkloadVersion) may not be defined

This happens mostly in the maui repo, but also may happen if the
build is not using workloads. This is not really possible outside
of the maui repo source build.

* props

* Add a test
  • Loading branch information
mattleibow authored Dec 6, 2024
1 parent d3bb77c commit c97464a
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 7 deletions.
11 changes: 11 additions & 0 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,15 @@
<_MauiPreviousDotNetVersionNoDot Condition="'$(_MauiPreviousDotNetVersionNoDot)' == ''">$(_MauiPreviousDotNetVersionMajor)$(_MauiPreviousDotNetVersionMinor)</_MauiPreviousDotNetVersionNoDot>
<_MauiPreviousDotNetTfm Condition="'$(_MauiPreviousDotNetTfm)' == ''">net$(_MauiPreviousDotNetVersion)</_MauiPreviousDotNetTfm>

<!-- The minimum TFM that the packaged will install into -->
<_MauiMinimumSupportedDotNetTfm>$(_MauiDotNetVersion)</_MauiMinimumSupportedDotNetTfm>
<_MauiMinimumSupportedDotNetTfm Condition="'$(IncludePreviousTfms)' == 'true'">$(_MauiPreviousDotNetVersion)</_MauiMinimumSupportedDotNetTfm>

<!-- The minimum supported version of the workload that the packages will install into -->
<MicrosoftMauiMinimumSupportedWorkloadVersion>$(_MauiDotNetVersion).0</MicrosoftMauiMinimumSupportedWorkloadVersion>
<!-- If the previous TFMs are included, then we want to use the latest previous workload for safety -->
<MicrosoftMauiMinimumSupportedWorkloadVersion Condition="'$(IncludePreviousTfms)' == 'true'">$(_MauiPreviousDotNetVersion).0</MicrosoftMauiMinimumSupportedWorkloadVersion>

<_MauiTargetPlatformIdentifier>$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)'))</_MauiTargetPlatformIdentifier>
<_MauiNoTargetPlatform>false</_MauiNoTargetPlatform>
<_MauiNoTargetPlatform Condition="'$(_MauiTargetPlatformIdentifier)' == ''">True</_MauiNoTargetPlatform>
Expand Down Expand Up @@ -158,6 +167,8 @@
<WindowsPreviousTargetFrameworkVersion>10.0.19041.0</WindowsPreviousTargetFrameworkVersion>
<Windows2PreviousTargetFrameworkVersion>10.0.20348.0</Windows2PreviousTargetFrameworkVersion>
<TizenPreviousTargetFrameworkVersion>7.0</TizenPreviousTargetFrameworkVersion>
<!-- Minimums -->
<MinimumWindowsTargetFrameworkVersion>10.0.17763.0</MinimumWindowsTargetFrameworkVersion>
</PropertyGroup>

<Import Condition="Exists('Directory.Build.Override.props')" Project="Directory.Build.Override.props" />
Expand Down
15 changes: 10 additions & 5 deletions src/Core/src/Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,18 @@
</ItemGroup>

<ItemGroup>
<None Include="nuget\**" Exclude="nuget\**\*.in.*" PackagePath="" Pack="true" />
<None Remove="nuget\**" />
<None Include="nuget\**" Exclude="nuget\**\*.in.*;nuget\**\net-windows\*" PackagePath="" Pack="true" />
<None Include="nuget\buildTransitive\net-windows\**" PackagePath="buildTransitive\net$(_MauiMinimumSupportedDotNetTfm)-windows$(MinimumWindowsTargetFrameworkVersion)" Pack="true" />
</ItemGroup>

<Target Name="_CopyToBuildTasksDir" AfterTargets="Build">
<ItemGroup>
<_CopyItems Include="nuget\buildTransitive\**" Exclude="nuget\**\*.in.*" />
<_CopyItems Include="$(IntermediateOutputPath)Microsoft.Maui.Core.BundledVersions.targets" />
<_CopyItems Include="nuget\buildTransitive\**" SubFolder="" Exclude="nuget\**\*.in.*;nuget\**\net-*\*" />
<_CopyItems Include="nuget\buildTransitive\net-windows\**" SubFolder="net$(_MauiMinimumSupportedDotNetTfm)-windows$(MinimumWindowsTargetFrameworkVersion)" />
<_CopyItems Include="$(IntermediateOutputPath)Microsoft.Maui.Core.BundledVersions.targets" SubFolder="" />
</ItemGroup>
<Copy SourceFiles="@(_CopyItems)" DestinationFolder="$(_MauiBuildTasksLocation)%(RecursiveDir)" ContinueOnError="true" Retries="0" />
<Copy SourceFiles="@(_CopyItems)" DestinationFolder="$(_MauiBuildTasksLocation)%(SubFolder)%(RecursiveDir)" ContinueOnError="true" Retries="0" />
</Target>

<Import Project="$(MauiSrcDirectory)Workload\Shared\LibraryPacks.targets" />
Expand All @@ -84,10 +87,12 @@
<Target Name="_GenerateBundledVersions"
BeforeTargets="Build;AssignTargetPaths"
DependsOnTargets="SetVersions"
Inputs="$(MSBuildProjectFile);$(MauiRootDirectory)eng\Versions.props;nuget\buildTransitive\Microsoft.Maui.Core.BundledVersions.in.targets"
Inputs="$(MSBuildProjectFile);$(MauiRootDirectory)Directory.Build.props;$(MauiRootDirectory)eng\Versions.props;nuget\buildTransitive\Microsoft.Maui.Core.BundledVersions.in.targets"
Outputs="$(IntermediateOutputPath)Microsoft.Maui.Core.BundledVersions.targets">
<ItemGroup>
<_ValuesToReplace Include="VERSION" PropertyName="PackageReferenceVersion" />
<_ValuesToReplace Include="MINIMUM_SUPPORTED_TFM" PropertyName="_MauiMinimumSupportedDotNetTfm" />
<_ValuesToReplace Include="MINIMUM_SUPPORTED_WORKLOAD" PropertyName="MicrosoftMauiMinimumSupportedWorkloadVersion" />
</ItemGroup>
<Copy
SourceFiles="nuget\buildTransitive\Microsoft.Maui.Core.BundledVersions.in.targets"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,32 @@

<Import Project="$(MSBuildThisFileDirectory)Microsoft.Maui.Core.BundledVersions.targets" />

<!--
Check to make sure that .NET MAUI packages are not installed as .NET Standard
when they are installed into a .NET "Core" project. If this happens, it means
that the user has installed a .NET MAUI package that requires a later TFM than
what the project has.
Some examples:
- Installing a .NET 9 MAUI package into a .NET 8 project
- Installing a .NET 9 MAUI package into a very old .NET 9 installation
To skip this check, set $(SkipCheckForMauiMinimumSupportedWorkloadVersion) to true.
-->
<Target
Name="_CheckForMauiMinimumSupportedWorkloadVersion"
BeforeTargets="_CheckForInvalidConfigurationAndPlatform;_CheckForUnsupportedTargetPlatformIdentifier"
Condition="
'$(SkipCheckForMauiMinimumSupportedWorkloadVersion)' != 'true' and
'$(MauiWorkloadVersion)' != '' and
$([MSBuild]::VersionLessThan('$(MauiWorkloadVersion)', '$(MauiMinimumSupportedWorkloadVersion)'))">

<Error
Code="MA003"
Text="The current .NET MAUI package version '$(MauiCorePackageVersion)' requires the .NET MAUI workload version to be at least '$(MauiMinimumSupportedWorkloadVersion)'. The current .NET MAUI workload version is '$(MauiWorkloadVersion)'." />

</Target>

<!--
This double check seems excessive, but importing the WinUI.targets when building for windows is not
sufficient. The WASDK targets assume everything is WinUI and thus just passes along the TFM of the app.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
<Project>

<PropertyGroup>
<MauiCorePackageVersion>@VERSION@</MauiCorePackageVersion>
<MauiMinimumSupportedTargetFrameworkVersion>@MINIMUM_SUPPORTED_TFM@</MauiMinimumSupportedTargetFrameworkVersion>
<MauiMinimumSupportedWorkloadVersion>@MINIMUM_SUPPORTED_WORKLOAD@</MauiMinimumSupportedWorkloadVersion>
</PropertyGroup>

<!-- .NET MAUI version for telemetry: this is the installed nuget in use, not the workload version -->
<ItemGroup>
<ProjectCapability Include="Microsoft.Maui.Core.Version.@VERSION@" />
Expand Down
2 changes: 1 addition & 1 deletion src/Maui.InTree.props
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<ImportGroup Condition="Exists('$(_MauiBuildTasksLocation)Microsoft.Maui.Controls.Build.Tasks.dll') and Exists('$(_MauiBuildTasksLocation)Microsoft.Maui.Resizetizer.dll')">

<!-- Microsoft.Maui.Core.props -->
<Import Project="$(_MauiBuildTasksLocation)net6.0-windows10.0.17763.0\Microsoft.Maui.Core.props" Condition="'$(_MauiTargetPlatformIsWindows)' == 'true'" />
<Import Project="$(_MauiBuildTasksLocation)net$(_MauiMinimumSupportedDotNetTfm)-windows$(MinimumWindowsTargetFrameworkVersion)\Microsoft.Maui.Core.props" Condition="'$(_MauiTargetPlatformIsWindows)' == 'true'" />
<Import Project="$(_MauiBuildTasksLocation)Microsoft.Maui.Core.props" Condition="'$(_MauiTargetPlatformIsWindows)' != 'true'" />

<!-- Microsoft.Maui.Controls.Build.Tasks.props -->
Expand Down
2 changes: 1 addition & 1 deletion src/Maui.InTree.targets
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<ImportGroup Condition="Exists('$(_MauiBuildTasksLocation)Microsoft.Maui.Controls.Build.Tasks.dll') and Exists('$(_MauiBuildTasksLocation)Microsoft.Maui.Resizetizer.dll')">

<!-- Microsoft.Maui.Core.targets -->
<Import Project="$(_MauiBuildTasksLocation)net6.0-windows10.0.17763.0\Microsoft.Maui.Core.targets" Condition="'$(_MauiTargetPlatformIsWindows)' == 'true'" />
<Import Project="$(_MauiBuildTasksLocation)net$(_MauiMinimumSupportedDotNetTfm)-windows$(MinimumWindowsTargetFrameworkVersion)\Microsoft.Maui.Core.targets" Condition="'$(_MauiTargetPlatformIsWindows)' == 'true'" />
<Import Project="$(_MauiBuildTasksLocation)Microsoft.Maui.Core.targets" Condition="'$(_MauiTargetPlatformIsWindows)' != 'true'" />

<!-- Microsoft.Maui.Controls.Build.Tasks.targets -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,24 @@ public void Build(string id, string framework, string config, bool shouldPack, s
$"Project {Path.GetFileName(projectFile)} failed to build. Check test output/attachments for errors.");
}

[Test]
[TestCase("maui", DotNetPrevious, "Debug")]
public void InstallPackagesIntoUnsupportedTfmFails(string id, string framework, string config)
{
var projectDir = TestDirectory;
var projectFile = Path.Combine(projectDir, $"{Path.GetFileName(projectDir)}.csproj");

Assert.IsTrue(DotnetInternal.New(id, projectDir, framework),
$"Unable to create template {id}. Check test output for errors.");

FileUtilities.ReplaceInFile(projectFile,
"$(MauiVersion)",
MauiPackageVersion);

Assert.False(DotnetInternal.Build(projectFile, config, properties: BuildProps, msbuildWarningsAsErrors: true),
$"Project {Path.GetFileName(projectFile)} built, but should not have. Check test output/attachments for why.");
}

[Test]
// with spaces
[TestCase("maui", "Project Space", "projectspace")]
Expand Down

0 comments on commit c97464a

Please sign in to comment.