Skip to content
This repository was archived by the owner on May 17, 2024. It is now read-only.

Commit 93294d4

Browse files
authored
Merge pull request #377 from twsouthwick/ImportWindowsDesktopTargets
Add ImportWindowsDesktopTargets=true for desktop .NET Framework apps
2 parents 3118d1e + eaed0db commit 93294d4

6 files changed

Lines changed: 22 additions & 1 deletion

File tree

src/MSBuild.Abstractions/MSBuildConversionWorkspace.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public ImmutableDictionary<string, ImmutableDictionary<string, string>> Determin
100100
private void RemoveTargetsNotLoadableByNETSDKMSBuild(string path)
101101
{
102102
var projectFile = File.ReadAllText(path);
103-
if (projectFile is { Length:>0 })
103+
if (projectFile is { Length: > 0 })
104104
{
105105
var replacement =
106106
projectFile
@@ -178,6 +178,11 @@ private bool TryCreateSdkBaselineProject(string projectFilePath, IProject projec
178178
{
179179
MSBuildHelpers.AddUseWinForms(propGroup);
180180
}
181+
182+
if (MSBuildHelpers.HasWPFOrWinForms(propGroup) && tfm.ContainsIgnoreCase(MSBuildFacts.Net5))
183+
{
184+
MSBuildHelpers.AddImportWindowsDesktopTargets(propGroup);
185+
}
181186
}
182187

183188
// Create a new collection because a project with this name has already been loaded into the global collection.

src/MSBuild.Abstractions/MSBuildHelpers.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,12 @@ public static ProjectItemElement GetPackagesConfigItem(ProjectItemGroupElement p
281281
/// </summary>
282282
public static void AddUseWPF(ProjectPropertyGroupElement propGroup) => propGroup.AddProperty(DesktopFacts.UseWPFPropertyName, "true");
283283

284+
/// <summary>
285+
/// Adds the ImportWindowsDesktopTargets=true property to ensure builds targeting .NET Framework will succeed.
286+
/// </summary>
287+
/// <param name="propGroup"></param>
288+
public static void AddImportWindowsDesktopTargets(ProjectPropertyGroupElement propGroup) => propGroup.AddProperty(DesktopFacts.ImportWindowsDesktopTargetsName, "true");
289+
284290
/// <summary>
285291
/// Finds the property group with the TFM specified, which is normally the top-level property group.
286292
/// </summary>

src/MSBuild.Conversion.Facts/DesktopFacts.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ public static class DesktopFacts
5454
public const string WinSDKAttribute = "Microsoft.NET.Sdk.WindowsDesktop";
5555
public const string UseWPFPropertyName = "UseWPF";
5656
public const string UseWinFormsPropertyName = "UseWindowsForms";
57+
public const string ImportWindowsDesktopTargetsName = "ImportWindowsDesktopTargets";
5758
public const string DesignerSuffix = ".Designer.cs";
5859
public const string XamlFileExtension = ".xaml";
5960
public const string SettingsDesignerFileName = "Settings.Designer.cs";

src/MSBuild.Conversion.Project/ProjectRootElementExtensionsForConversion.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,13 @@ public static IProjectRootElement AddDesktopProperties(this IProjectRootElement
409409
MSBuildHelpers.AddUseWPF(propGroup);
410410
}
411411

412+
if (!baselineProject.GlobalProperties.Contains(DesktopFacts.ImportWindowsDesktopTargetsName, StringComparer.OrdinalIgnoreCase)
413+
&& !projectRootElement.Sdk.Equals(DesktopFacts.WinSDKAttribute)
414+
&& (MSBuildHelpers.IsWPF(projectRootElement) || MSBuildHelpers.IsWinForms(projectRootElement)))
415+
{
416+
MSBuildHelpers.AddImportWindowsDesktopTargets(propGroup);
417+
}
418+
412419
return projectRootElement;
413420
}
414421

tests/TestData/SmokeTests.WinformsNet5Baseline/SmokeTests.WinformsNet5Baseline.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
<OutputType>WinExe</OutputType>
66
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
77
<UseWindowsForms>true</UseWindowsForms>
8+
<ImportWindowsDesktopTargets>true</ImportWindowsDesktopTargets>
89
</PropertyGroup>
910

1011
<ItemGroup>

tests/TestData/SmokeTests.WpfNet5Baseline/SmokeTests.WpfNet5Baseline.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
<OutputType>WinExe</OutputType>
66
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
77
<UseWPF>true</UseWPF>
8+
<ImportWindowsDesktopTargets>true</ImportWindowsDesktopTargets>
89
</PropertyGroup>
910

1011
<ItemGroup>

0 commit comments

Comments
 (0)