Skip to content

Commit 93993e7

Browse files
author
William Li
authored
Merge pull request #15465 from sfoslund/OptIntoWPFFix
Opt into IncludePackageReferencesDuringMarkupCompilation
2 parents 17357d7 + f03c22d commit 93993e7

File tree

2 files changed

+91
-0
lines changed

2 files changed

+91
-0
lines changed

src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.Sdk.BeforeCommon.targets

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ Copyright (c) .NET Foundation. All rights reserved.
2323
<PropertyGroup>
2424
<_IsExecutable Condition="'$(OutputType)' == 'Exe' or '$(OutputType)'=='WinExe'">true</_IsExecutable>
2525
<OutputType Condition="'$(DisableWinExeOutputInference)' != 'true' and '$(OutputType)' == 'Exe' and ('$(UseWindowsForms)' == 'true' or '$(UseWPF)' == 'true')">WinExe</OutputType>
26+
<IncludePackageReferencesDuringMarkupCompilation Condition=" '$(IncludePackageReferencesDuringMarkupCompilation)' == '' ">true</IncludePackageReferencesDuringMarkupCompilation>
2627
</PropertyGroup>
2728

2829
<PropertyGroup Condition="'$(HasRuntimeOutput)' == ''">

src/Tests/Microsoft.NET.Build.Tests/GivenThatWeWantToBuildAWindowsDesktopProject.cs

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,5 +237,95 @@ public void It_fails_if_target_platform_identifier_and_version_are_invalid()
237237
.And
238238
.NotHaveStdOutContaining("NETSDK1140");
239239
}
240+
241+
[WindowsOnlyFact]
242+
public void It_can_use_source_generators_with_wpf()
243+
{
244+
var sourceGenProject = new TestProject()
245+
{
246+
Name = "SourceGen",
247+
TargetFrameworks = "netstandard2.0"
248+
};
249+
sourceGenProject.AdditionalProperties.Add("LangVersion", "preview");
250+
sourceGenProject.PackageReferences.Add(new TestPackageReference("Microsoft.CodeAnalysis.CSharp", "3.8.0-3.final", privateAssets: "all"));
251+
sourceGenProject.PackageReferences.Add(new TestPackageReference("Microsoft.CodeAnalysis.Analyzers", "3.0.0", privateAssets: "all"));
252+
sourceGenProject.SourceFiles.Add("Program.cs", SourceGenSourceFile);
253+
var sourceGenTestAsset = _testAssetsManager.CreateTestProject(sourceGenProject);
254+
255+
var testDir = sourceGenTestAsset.Path;
256+
var newCommand = new DotnetCommand(Log, "new", "wpf", "-o", "wpfApp", "--no-restore");
257+
newCommand.WorkingDirectory = testDir;
258+
newCommand.Execute()
259+
.Should()
260+
.Pass();
261+
262+
// Reference generated code from a wpf app
263+
var projFile = Path.Combine(testDir, "wpfApp", "wpfApp.csproj");
264+
File.WriteAllText(projFile, $@"<Project Sdk=`Microsoft.NET.Sdk`>
265+
<PropertyGroup>
266+
<OutputType>WinExe</OutputType>
267+
<TargetFramework>net5.0-windows</TargetFramework>
268+
<UseWPF>true</UseWPF>
269+
<LangVersion>preview</LangVersion>
270+
</PropertyGroup>
271+
<ItemGroup>
272+
<ProjectReference Include=`..\{sourceGenProject.Name}\{sourceGenProject.Name}.csproj` OutputItemType=`Analyzer` ReferenceOutputAssembly=`false` />
273+
</ItemGroup>
274+
</Project>".Replace('`', '"'));
275+
File.WriteAllText(Path.Combine(testDir, "wpfApp", "MainWindow.xaml.cs"), $@"using System.Windows;
276+
namespace wpfApp
277+
{{
278+
public partial class MainWindow : Window
279+
{{
280+
public MainWindow()
281+
{{
282+
HelloWorldGenerated.HelloWorld.SayHello();
283+
}}
284+
}}
285+
}}");
286+
287+
var buildCommand = new BuildCommand(Log, Path.Combine(testDir, "wpfApp"));
288+
buildCommand.Execute()
289+
.Should()
290+
.Pass();
291+
}
292+
293+
private static readonly string SourceGenSourceFile = @"using System.Collections.Generic;
294+
using System.Text;
295+
using Microsoft.CodeAnalysis;
296+
using Microsoft.CodeAnalysis.Text;
297+
298+
namespace SourceGeneratorSamples
299+
{
300+
[Generator]
301+
public class HelloWorldGenerator : ISourceGenerator
302+
{
303+
public void Execute(GeneratorExecutionContext context)
304+
{
305+
StringBuilder sourceBuilder = new StringBuilder(@`
306+
using System;
307+
namespace HelloWorldGenerated
308+
{
309+
public static class HelloWorld
310+
{
311+
public static void SayHello()
312+
{
313+
Console.WriteLine(``Hello from generated code!``);
314+
`);
315+
IEnumerable<SyntaxTree> syntaxTrees = context.Compilation.SyntaxTrees;
316+
foreach (SyntaxTree tree in syntaxTrees)
317+
{
318+
sourceBuilder.AppendLine($@`Console.WriteLine(@`` - {tree.FilePath}``);`);
319+
}
320+
sourceBuilder.Append(@`
321+
}
322+
}
323+
}`);
324+
context.AddSource(`helloWorldGenerated`, SourceText.From(sourceBuilder.ToString(), Encoding.UTF8));
325+
}
326+
327+
public void Initialize(GeneratorInitializationContext context) {}
328+
}
329+
}".Replace('`', '"');
240330
}
241331
}

0 commit comments

Comments
 (0)