Skip to content

Commit

Permalink
feat: Add a property to launch the debugger for XAML generation
Browse files Browse the repository at this point in the history
  • Loading branch information
jeromelaban committed Dec 2, 2020
1 parent 7398dc9 commit f347548
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 7 deletions.
31 changes: 31 additions & 0 deletions doc/articles/uno-development/troubleshooting-source-generation.md
Original file line number Diff line number Diff line change
@@ -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
<PropertyGroup>
<LangVersion>9.0</LangVersion>
<UnoUIUseRoslynSourceGenerators>true</UnoUIUseRoslynSourceGenerators>
</PropertyGroup>
```
Conversely, Uno.SourceGeneration generators can be used by setting this:
```xml
<PropertyGroup>
<UnoUIUseRoslynSourceGenerators>false</UnoUIUseRoslynSourceGenerators>
</PropertyGroup>
```

## Debugging the XAML Source Generator
It is possible to step into the XAML generator by adding the following property:
```xml
<PropertyGroup>
<UnoUISourceGeneratorDebuggerBreak>True</UnoUISourceGeneratorDebuggerBreak>
</PropertyGroup>
```
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.`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 <UnoUISourceGeneratorDebuggerBreak>True</UnoUISourceGeneratorDebuggerBreak> to your project

if (!Helpers.DesignTimeHelper.IsDesignTime(context)
&& (context.GetMSBuildPropertyValue("UnoUISourceGeneratorDebuggerBreak")?.Equals("True", StringComparison.OrdinalIgnoreCase) ?? false))
{
Debugger.Launch();
}

_generatorContext = context;
InitTelemetry(context);

Expand Down

0 comments on commit f347548

Please sign in to comment.