Skip to content

Commit 7553926

Browse files
[XSG] disable lineinfo by default on Release (#32294)
helps mitigate #32049
1 parent 8966b71 commit 7553926

File tree

4 files changed

+47
-7
lines changed

4 files changed

+47
-7
lines changed

src/Controls/src/SourceGen/ProjectItem.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@ public bool EnableLineInfo
1919
return true;
2020
if (Options.IsDisabled("build_metadata.additionalfiles.LineInfo"))
2121
return false;
22-
return !Options.IsDisabled("build_property.MauiXamlLineInfo");
22+
if (Options.IsEnabled("build_property.MauiXamlLineInfo"))
23+
return true;
24+
if (Options.IsDisabled("build_property.MauiXamlLineInfo"))
25+
return false;
26+
return Configuration.Equals("Debug", StringComparison.OrdinalIgnoreCase);
2327
}
2428
}
2529

src/Controls/tests/SourceGen.UnitTests/InitializeComponent/LineInfoTests.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,35 @@ public void DiagnosticShowsLocationInInputXamlFile()
3333
var expectedFilePath = Path.Combine(Environment.CurrentDirectory, "Test.xaml");
3434
Assert.True(generatedCode.Contains(@$"#line 9 ""{expectedFilePath}""", StringComparison.Ordinal));
3535
}
36+
37+
[Fact]
38+
public void LineInfoDisabledDefault()
39+
{
40+
var xaml =
41+
"""
42+
<?xml version="1.0" encoding="UTF-8"?>
43+
<ContentPage
44+
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
45+
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
46+
x:Class="Test.TestPage">
47+
<ContentPage.Resources>
48+
<ResourceDictionary>
49+
<ResourceDictionary.MergedDictionaries>
50+
<ResourceDictionary Source="Resources/Styles\Colors.xaml" />
51+
</ResourceDictionary.MergedDictionaries>
52+
</ResourceDictionary>
53+
</ContentPage.Resources>
54+
</ContentPage>
55+
""";
56+
57+
var (result, _) = RunGenerator(xaml, string.Empty, lineinfo: "default");
58+
59+
var generatedCode = result.GeneratedTrees.Single(tree => Path.GetFileName(tree.FilePath) == "Test.xaml.xsg.cs").ToString();
60+
var expectedFilePath = Path.Combine(Environment.CurrentDirectory, "Test.xaml");
61+
#if RELEASE
62+
Assert.False(generatedCode.Contains(@$"#line 9 ""{expectedFilePath}""", StringComparison.Ordinal));
63+
#else
64+
Assert.True(generatedCode.Contains(@$"#line 9 ""{expectedFilePath}""", StringComparison.Ordinal));
65+
#endif
66+
}
3667
}

src/Controls/tests/SourceGen.UnitTests/InitializeComponent/SourceGenXamlInitializeComponentTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ namespace Microsoft.Maui.Controls.SourceGen.UnitTests;
1111

1212
public class SourceGenXamlInitializeComponentTestBase : SourceGenTestsBase
1313
{
14-
protected record AdditionalXamlFile(string Path, string Content, string? RelativePath = null, string? TargetPath = null, string? ManifestResourceName = null, string? TargetFramework = null, string? NoWarn = null, bool TreeOrder = false, bool Dry = false)
15-
: AdditionalFile(Text: ToAdditionalText(Path, Content), Kind: "Xaml", RelativePath: RelativePath ?? Path, TargetPath: TargetPath, ManifestResourceName: ManifestResourceName, TargetFramework: TargetFramework, NoWarn: NoWarn);
14+
protected record AdditionalXamlFile(string Path, string Content, string? RelativePath = null, string? TargetPath = null, string? ManifestResourceName = null, string? TargetFramework = null, string? NoWarn = null, string Lineinfo = "enable")
15+
: AdditionalFile(Text: ToAdditionalText(Path, Content), Kind: "Xaml", RelativePath: RelativePath ?? Path, TargetPath: TargetPath, ManifestResourceName: ManifestResourceName, TargetFramework: TargetFramework, NoWarn: NoWarn, LineInfo: Lineinfo);
1616

17-
protected (GeneratorDriverRunResult result, string? text) RunGenerator(string xaml, string code, string noWarn = "", string targetFramework = "", string? path = null, bool treeOrder = false, bool dry = false)
17+
protected (GeneratorDriverRunResult result, string? text) RunGenerator(string xaml, string code, string noWarn = "", string targetFramework = "", string? path = null, string lineinfo = "enable")
1818
{
1919
var compilation = CreateMauiCompilation();
2020
compilation = compilation.AddSyntaxTrees(CSharpSyntaxTree.ParseText(code));
2121
var workingDirectory = Environment.CurrentDirectory;
22-
var xamlFile = new AdditionalXamlFile(Path.Combine(workingDirectory, path ?? "Test.xaml"), xaml, RelativePath: path ?? "Test.xaml", TargetFramework: targetFramework, NoWarn: noWarn, ManifestResourceName: $"{compilation.AssemblyName}.Test.xaml", TreeOrder: treeOrder, Dry: dry);
22+
var xamlFile = new AdditionalXamlFile(Path.Combine(workingDirectory, path ?? "Test.xaml"), xaml, RelativePath: path ?? "Test.xaml", TargetFramework: targetFramework, NoWarn: noWarn, ManifestResourceName: $"{compilation.AssemblyName}.Test.xaml", Lineinfo: lineinfo);
2323
var result = RunGenerator<CodeBehindGenerator>(compilation, xamlFile);
2424
var generated = result.Results.SingleOrDefault().GeneratedSources.SingleOrDefault(gs => gs.HintName.EndsWith(".xsg.cs")).SourceText?.ToString();
2525

src/Controls/tests/SourceGen.UnitTests/SourceGeneratorDriver.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public static class SourceGeneratorDriver
1818
{
1919
private static MetadataReference[]? MauiReferences;
2020

21-
public record AdditionalFile(AdditionalText Text, string Kind, string RelativePath, string? TargetPath, string? ManifestResourceName, string? TargetFramework, string? NoWarn);
21+
public record AdditionalFile(AdditionalText Text, string Kind, string RelativePath, string? TargetPath, string? ManifestResourceName, string? TargetFramework, string? NoWarn, string LineInfo="enable");
2222

2323
public static GeneratorDriverRunResult RunGenerator<T>(Compilation compilation, params AdditionalFile[] additionalFiles)
2424
where T : IIncrementalGenerator, new()
@@ -154,8 +154,13 @@ public override bool TryGetValue(string key, [NotNullWhen(true)] out string? val
154154
"build_metadata.additionalfiles.RelativePath" => _additionalFile.RelativePath,
155155
"build_metadata.additionalfiles.Inflator" => "SourceGen",
156156
"build_property.targetFramework" => _additionalFile.TargetFramework,
157+
#if RELEASE
158+
"build_property.Configuration" => "Release",
159+
#else
160+
"build_property.Configuration" => "Debug",
161+
#endif
157162
"build_property.EnableMauiXamlDiagnostics" => "true",
158-
"build_property.MauiXamlLineInfo" => "enable",
163+
"build_property.MauiXamlLineInfo" => _additionalFile.LineInfo != "default" ? _additionalFile.LineInfo : null,
159164
"build_property.MauiXamlNoWarn" => _additionalFile.NoWarn,
160165

161166
_ => null

0 commit comments

Comments
 (0)