From f34754839b8948ba10c5cd3cec052ad9590fb18d Mon Sep 17 00:00:00 2001 From: Jerome Laban Date: Wed, 2 Dec 2020 12:06:30 -0500 Subject: [PATCH] feat: Add a property to launch the debugger for XAML generation --- .../troubleshooting-source-generation.md | 31 +++++++++++++++++++ .../XamlGenerator/XamlCodeGeneration.cs | 16 +++++----- 2 files changed, 40 insertions(+), 7 deletions(-) diff --git a/doc/articles/uno-development/troubleshooting-source-generation.md b/doc/articles/uno-development/troubleshooting-source-generation.md index f7b4b3e9319f..18fe2334286e 100644 --- a/doc/articles/uno-development/troubleshooting-source-generation.md +++ b/doc/articles/uno-development/troubleshooting-source-generation.md @@ -1,5 +1,36 @@ # Troubleshooting Source Generation +Source Generation in Uno is done in one of two ways: +- Using [C# 9.0 source generators](https://devblogs.microsoft.com/dotnet/introducing-c-source-generators/) +- Using [Uno.SourceGeneration](https://github.com/unoplatform/uno.sourcegeneration) generators + +C# 9.0 generators are enabled automatically when using .NET 5 and up projects, otherwise Uno.SourceGeneration generators are used. + +It is possible to enable C# 9.0 generators for non .NET 5+ projects by adding this: +```xml + + 9.0 + true + +``` +Conversely, Uno.SourceGeneration generators can be used by setting this: +```xml + + false + +``` + +## Debugging the XAML Source Generator +It is possible to step into the XAML generator by adding the following property: +```xml + + True + +``` +Setting this property will popup a Debugger window allowing the selection of a visual studio instance, giving the ability to set breakpoints and trace the generator. + +## Troubleshooting Uno.SourceGeneration based generation + When building, if you're having build error messages that looks like one of those: - `the targets [Microsoft.Build.Execution.TargetResult] failed to execute.` diff --git a/src/SourceGenerators/Uno.UI.SourceGenerators/XamlGenerator/XamlCodeGeneration.cs b/src/SourceGenerators/Uno.UI.SourceGenerators/XamlGenerator/XamlCodeGeneration.cs index 2f305e8643e8..c5d0a08a53d6 100644 --- a/src/SourceGenerators/Uno.UI.SourceGenerators/XamlGenerator/XamlCodeGeneration.cs +++ b/src/SourceGenerators/Uno.UI.SourceGenerators/XamlGenerator/XamlCodeGeneration.cs @@ -12,6 +12,7 @@ using Uno.Logging; using Uno.UI.SourceGenerators.Telemetry; using Uno.UI.Xaml; +using System.Drawing; #if NETFRAMEWORK using Microsoft.Build.Execution; @@ -87,13 +88,14 @@ internal partial class XamlCodeGeneration public XamlCodeGeneration(GeneratorExecutionContext context) { // To easily debug XAML code generation: - // 1. Uncomment the line below - // 2. Build Uno.UI.SourceGenerators and override local files (following instructions here: doc/articles/uno-development/debugging-uno-ui.md#debugging-unoui) - // 3. Build project containing your XAML. When prompted to attach a Visual Studio instance: - // - if it's in an external solution, attach the VS instance running Uno.UI - // - if you're debugging XAML generation inside the Uno solution, opt to create a new VS instance - // - // Debugger.Launch(); + // Add True to your project + + if (!Helpers.DesignTimeHelper.IsDesignTime(context) + && (context.GetMSBuildPropertyValue("UnoUISourceGeneratorDebuggerBreak")?.Equals("True", StringComparison.OrdinalIgnoreCase) ?? false)) + { + Debugger.Launch(); + } + _generatorContext = context; InitTelemetry(context);