diff --git a/CHANGELOG.md b/CHANGELOG.md index 3d666f8e5d..72e2c924f0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ - Sentry now decompresses Request bodies in ASP.NET Core when RequestDecompression middleware is enabled ([#4315](https://github.com/getsentry/sentry-dotnet/pull/4315)) - Custom ISentryEventProcessors are now run for native iOS events ([#4318](https://github.com/getsentry/sentry-dotnet/pull/4318)) - Crontab validation when capturing checkins ([#4314](https://github.com/getsentry/sentry-dotnet/pull/4314)) +- Fixed an issue with the way Sentry detects build settings. This was causing Sentry to produce code that could fail at runtime in AOT compiled applications. ([#4333](https://github.com/getsentry/sentry-dotnet/pull/4333)) - Native AOT: link to static `lzma` on Linux/MUSL ([#4326](https://github.com/getsentry/sentry-dotnet/pull/4326)) - AppDomain.CurrentDomain.ProcessExit hook is now removed on shutdown ([#4323](https://github.com/getsentry/sentry-dotnet/pull/4323)) diff --git a/samples/Sentry.Samples.Android/Sentry.Samples.Android.csproj b/samples/Sentry.Samples.Android/Sentry.Samples.Android.csproj index 61fcb9bc31..fdce591643 100644 --- a/samples/Sentry.Samples.Android/Sentry.Samples.Android.csproj +++ b/samples/Sentry.Samples.Android/Sentry.Samples.Android.csproj @@ -43,6 +43,4 @@ full - - diff --git a/samples/Sentry.Samples.Ios/Sentry.Samples.Ios.csproj b/samples/Sentry.Samples.Ios/Sentry.Samples.Ios.csproj index 25f4b66c46..c3eaafab7c 100644 --- a/samples/Sentry.Samples.Ios/Sentry.Samples.Ios.csproj +++ b/samples/Sentry.Samples.Ios/Sentry.Samples.Ios.csproj @@ -49,6 +49,4 @@ ReferenceOutputAssembly="false"/> - - diff --git a/samples/Sentry.Samples.MacCatalyst/Sentry.Samples.MacCatalyst.csproj b/samples/Sentry.Samples.MacCatalyst/Sentry.Samples.MacCatalyst.csproj index e48c894e34..50b58f7aed 100644 --- a/samples/Sentry.Samples.MacCatalyst/Sentry.Samples.MacCatalyst.csproj +++ b/samples/Sentry.Samples.MacCatalyst/Sentry.Samples.MacCatalyst.csproj @@ -49,6 +49,4 @@ ReferenceOutputAssembly="false"/> - - diff --git a/samples/Sentry.Samples.Maui/Sentry.Samples.Maui.csproj b/samples/Sentry.Samples.Maui/Sentry.Samples.Maui.csproj index f924e8f7c0..9f87848b21 100644 --- a/samples/Sentry.Samples.Maui/Sentry.Samples.Maui.csproj +++ b/samples/Sentry.Samples.Maui/Sentry.Samples.Maui.csproj @@ -115,6 +115,4 @@ - - diff --git a/src/Sentry.SourceGenerators/BuildPropertySourceGenerator.cs b/src/Sentry.SourceGenerators/BuildPropertySourceGenerator.cs index 7110b4f3a7..2bd300cad0 100644 --- a/src/Sentry.SourceGenerators/BuildPropertySourceGenerator.cs +++ b/src/Sentry.SourceGenerators/BuildPropertySourceGenerator.cs @@ -4,12 +4,11 @@ namespace Sentry.SourceGenerators; - /// /// Generates the necessary msbuild variables /// [Generator(LanguageNames.CSharp)] -public class BuildPropertySourceGenerator : ISourceGenerator +public sealed class BuildPropertySourceGenerator : ISourceGenerator { /// /// Initialize the source gen @@ -23,6 +22,8 @@ public void Initialize(GeneratorInitializationContext context) /// public void Execute(GeneratorExecutionContext context) { + const string tabString = " "; + var opts = context.AnalyzerConfigOptions.GlobalOptions; var properties = opts.Keys.Where(x => x.StartsWith("build_property.")).ToList(); if (properties.Count == 0) @@ -37,12 +38,7 @@ public void Execute(GeneratorExecutionContext context) } // we only want to generate code where host setup takes place - if (!opts.TryGetValue("build_property.outputtype", out var outputType)) - { - return; - } - - if (!outputType.Equals("exe", StringComparison.InvariantCultureIgnoreCase)) + if (!context.Compilation.Options.OutputKind.IsExe()) { return; } @@ -50,23 +46,26 @@ public void Execute(GeneratorExecutionContext context) var sb = new StringBuilder(); sb .Append( -""" +$$""" // -// Code generated by Sentry Source Generators -// Changes may cause incorrect behavior and will be lost if the code is -// regenerated. +// This code was generated by Sentry.SourceGenerators. +// Changes to this file may cause incorrect behavior and will be lost if the code is regenerated. // -#if NET8_0_OR_GREATER -namespace Sentry; +#if NET5_0_OR_GREATER + +#nullable enable -[global::System.Runtime.CompilerServices.CompilerGenerated] -public static class BuildVariableInitializer +namespace Sentry.Generated { - [global::System.Runtime.CompilerServices.ModuleInitializer] - public static void Initialize() + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("{{GeneratedCodeText.Tool}}", "{{GeneratedCodeText.Version}}")] + internal static class BuildPropertyInitializer { - global::Sentry.CompilerServices.BuildProperties.Initialize(new global::System.Collections.Generic.Dictionary { + [global::System.Runtime.CompilerServices.ModuleInitializerAttribute] + internal static void Initialize() + { + global::Sentry.CompilerServices.BuildProperties.Initialize(new global::System.Collections.Generic.Dictionary(global::System.StringComparer.OrdinalIgnoreCase) + { """ ); @@ -78,21 +77,21 @@ public static void Initialize() var pn = EscapeString(property.Replace("build_property.", "")); var ev = EscapeString(value); sb - .Append("\t\t\t{") + .Append($"{tabString}{tabString}{tabString}{tabString}{{") .Append($"\"{pn}\", \"{ev}\"") .AppendLine("},"); } } sb - .AppendLine("\t\t});") // close dictionary - .AppendLine("\t}") + .AppendLine($"{tabString}{tabString}{tabString}}});") // close dictionary + .AppendLine($"{tabString}{tabString}}}") + .AppendLine($"{tabString}}}") .AppendLine("}") .AppendLine("#endif"); - context.AddSource("__BuildProperties.g.cs", sb.ToString()); + context.AddSource("Sentry.Generated.BuildPropertyInitializer.g.cs", sb.ToString()); } - private static string EscapeString(string value) => value.Replace("\\", "\\\\"); } diff --git a/src/Sentry.SourceGenerators/GeneratedCodeText.cs b/src/Sentry.SourceGenerators/GeneratedCodeText.cs new file mode 100644 index 0000000000..a0144b1e15 --- /dev/null +++ b/src/Sentry.SourceGenerators/GeneratedCodeText.cs @@ -0,0 +1,7 @@ +namespace Sentry.SourceGenerators; + +internal static class GeneratedCodeText +{ + public static string? Tool { get; } = typeof(BuildPropertySourceGenerator).Assembly.GetName().Name; + public static string? Version { get; } = typeof(BuildPropertySourceGenerator).Assembly.GetName().Version?.ToString(); +} diff --git a/src/Sentry.SourceGenerators/OutputKindExtensions.cs b/src/Sentry.SourceGenerators/OutputKindExtensions.cs new file mode 100644 index 0000000000..a96d233107 --- /dev/null +++ b/src/Sentry.SourceGenerators/OutputKindExtensions.cs @@ -0,0 +1,20 @@ +using Microsoft.CodeAnalysis; + +namespace Sentry.SourceGenerators; + +internal static class OutputKindExtensions +{ + internal static bool IsExe(this OutputKind outputKind) + { + return outputKind switch + { + OutputKind.ConsoleApplication => true, + OutputKind.WindowsApplication => true, + OutputKind.DynamicallyLinkedLibrary => false, + OutputKind.NetModule => false, + OutputKind.WindowsRuntimeMetadata => false, + OutputKind.WindowsRuntimeApplication => true, + _ => false, + }; + } +} diff --git a/src/Sentry/Internal/AotHelper.cs b/src/Sentry/Internal/AotHelper.cs index a10449e6fe..fe5156ab8f 100644 --- a/src/Sentry/Internal/AotHelper.cs +++ b/src/Sentry/Internal/AotHelper.cs @@ -1,4 +1,6 @@ +using System; using Sentry.CompilerServices; +using Sentry.Extensibility; namespace Sentry.Internal; @@ -13,21 +15,31 @@ static AotHelper() IsTrimmed = CheckIsTrimmed(); } - [UnconditionalSuppressMessage("Trimming", "IL2026: RequiresUnreferencedCode", Justification = AvoidAtRuntime)] - private static bool CheckIsTrimmed() + internal static bool CheckIsTrimmed(IDiagnosticLogger? logger = null) { - if (TryGetBoolean("publishtrimmed", out var trimmed)) + if (TryGetBoolean("_IsPublishing", out var isPublishing) && isPublishing) { - return trimmed; - } + logger?.LogDebug("Detected _IsPublishing"); + if (TryGetBoolean("PublishSelfContained", out var selfContained) && selfContained) + { + logger?.LogDebug("Detected PublishSelfContained"); + if (TryGetBoolean("PublishTrimmed", out var trimmed)) + { + logger?.LogDebug("Detected PublishTrimmed"); + return trimmed; + } + } - if (TryGetBoolean("publishaot", out var aot)) - { - return aot; + if (TryGetBoolean("PublishAot", out var aot)) + { + logger?.LogDebug($"Detected PublishAot: {aot}"); + return aot; + } } // fallback check + logger?.LogDebug("Stacktrace fallback"); var stackTrace = new StackTrace(false); return stackTrace.GetFrame(0)?.GetMethod() is null; } @@ -35,9 +47,9 @@ private static bool CheckIsTrimmed() private static bool TryGetBoolean(string key, out bool value) { value = false; - if (BuildProperties.Values?.TryGetValue(key, out var aotValue) ?? false) + if (BuildProperties.Values?.TryGetValue(key, out string? stringValue) ?? false) { - if (bool.TryParse(aotValue, out var result)) + if (bool.TryParse(stringValue, out var result)) { value = result; return true; diff --git a/src/Sentry/Sentry.csproj b/src/Sentry/Sentry.csproj index c34b7a1e1e..1436d24b57 100644 --- a/src/Sentry/Sentry.csproj +++ b/src/Sentry/Sentry.csproj @@ -188,8 +188,6 @@ - - @@ -198,4 +196,5 @@ PackagePath="analyzers/dotnet/cs" Visible="false" /> + diff --git a/src/Sentry/SentrySdk.cs b/src/Sentry/SentrySdk.cs index 15bcf2b43b..6b487517a1 100644 --- a/src/Sentry/SentrySdk.cs +++ b/src/Sentry/SentrySdk.cs @@ -54,6 +54,7 @@ internal static IHub InitHub(SentryOptions options) #pragma warning disable CS0162 // Unreachable code detected #pragma warning disable 0162 // Unreachable code on old .NET frameworks + AotHelper.CheckIsTrimmed(options.DiagnosticLogger); options.LogDebug(AotHelper.IsTrimmed ? "This looks like a Native AOT application build." : "This doesn't look like a Native AOT application build." diff --git a/src/Sentry/buildTransitive/Sentry.SourceGenerators.targets b/src/Sentry/buildTransitive/Sentry.SourceGenerators.targets deleted file mode 100644 index 292d49e34b..0000000000 --- a/src/Sentry/buildTransitive/Sentry.SourceGenerators.targets +++ /dev/null @@ -1,9 +0,0 @@ - - - - - - - - - diff --git a/src/Sentry/buildTransitive/Sentry.targets b/src/Sentry/buildTransitive/Sentry.targets index c2f12b2763..f280eb4f1f 100644 --- a/src/Sentry/buildTransitive/Sentry.targets +++ b/src/Sentry/buildTransitive/Sentry.targets @@ -5,10 +5,13 @@ + + - + + diff --git a/test/Sentry.SourceGenerators.Tests/BuildPropertySourceGeneratorTests.RunResult_BadStrings.verified.txt b/test/Sentry.SourceGenerators.Tests/BuildPropertySourceGeneratorTests.RunResult_BadStrings.verified.txt index 94298c45c3..de008ecfea 100644 --- a/test/Sentry.SourceGenerators.Tests/BuildPropertySourceGeneratorTests.RunResult_BadStrings.verified.txt +++ b/test/Sentry.SourceGenerators.Tests/BuildPropertySourceGeneratorTests.RunResult_BadStrings.verified.txt @@ -3,9 +3,9 @@ GeneratedSources: [ { SyntaxTree: { - FilePath: Sentry.SourceGenerators/Sentry.SourceGenerators.BuildPropertySourceGenerator/__BuildProperties.g.cs, + FilePath: Sentry.SourceGenerators/Sentry.SourceGenerators.BuildPropertySourceGenerator/Sentry.Generated.BuildPropertyInitializer.g.cs, Encoding: utf-8, - Length: 639, + Length: 835, HasCompilationUnitRoot: true, Options: { LanguageVersion: CSharp12, @@ -18,29 +18,32 @@ Encoding: utf-8, Source: // -// Code generated by Sentry Source Generators -// Changes may cause incorrect behavior and will be lost if the code is -// regenerated. +// This code was generated by Sentry.SourceGenerators. +// Changes to this file may cause incorrect behavior and will be lost if the code is regenerated. // -#if NET8_0_OR_GREATER -namespace Sentry; +#if NET5_0_OR_GREATER -[global::System.Runtime.CompilerServices.CompilerGenerated] -public static class BuildVariableInitializer +#nullable enable + +namespace Sentry.Generated { - [global::System.Runtime.CompilerServices.ModuleInitializer] - public static void Initialize() + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Sentry.SourceGenerators", "Version")] + internal static class BuildPropertyInitializer { - global::Sentry.CompilerServices.BuildProperties.Initialize(new global::System.Collections.Generic.Dictionary { - {"My\\Key", "test\\test"}, - {"OutputType", "exe"}, - }); - } + [global::System.Runtime.CompilerServices.ModuleInitializerAttribute] + internal static void Initialize() + { + global::Sentry.CompilerServices.BuildProperties.Initialize(new global::System.Collections.Generic.Dictionary(global::System.StringComparer.OrdinalIgnoreCase) + { + {"My\\Key", "test\\test"}, + }); + } + } } #endif , - Length: 639, + Length: 835, ChecksumAlgorithm: Sha1, CanBeEmbedded: true, Container: {}, @@ -58,310 +61,351 @@ public static class BuildVariableInitializer { LineNumber: 1, Start: 20, - End: 65, - EndIncludingLineBreak: 66, + End: 74, + EndIncludingLineBreak: 75, Span: { Start: 20, - Length: 45 + Length: 54 }, SpanIncludingLineBreak: { Start: 20, - Length: 46 + Length: 55 } }, { LineNumber: 2, - Start: 66, - End: 137, - EndIncludingLineBreak: 138, + Start: 75, + End: 172, + EndIncludingLineBreak: 173, Span: { - Start: 66, - Length: 71 + Start: 75, + Length: 97 }, SpanIncludingLineBreak: { - Start: 66, - Length: 72 + Start: 75, + Length: 98 } }, { LineNumber: 3, - Start: 138, - End: 153, - EndIncludingLineBreak: 154, + Start: 173, + End: 193, + EndIncludingLineBreak: 194, Span: { - Start: 138, - Length: 15 + Start: 173, + Length: 20 }, SpanIncludingLineBreak: { - Start: 138, - Length: 16 + Start: 173, + Length: 21 } }, { LineNumber: 4, - Start: 154, - End: 174, - EndIncludingLineBreak: 175, + Start: 194, + End: 194, + EndIncludingLineBreak: 195, Span: { - Start: 154, - Length: 20 + Start: 194 }, SpanIncludingLineBreak: { - Start: 154, - Length: 21 + Start: 194, + Length: 1 } }, { LineNumber: 5, - Start: 175, - End: 175, - EndIncludingLineBreak: 176, + Start: 195, + End: 216, + EndIncludingLineBreak: 217, Span: { - Start: 175 + Start: 195, + Length: 21 }, SpanIncludingLineBreak: { - Start: 175, - Length: 1 + Start: 195, + Length: 22 } }, { LineNumber: 6, - Start: 176, - End: 197, - EndIncludingLineBreak: 198, + Start: 217, + End: 217, + EndIncludingLineBreak: 218, Span: { - Start: 176, - Length: 21 + Start: 217 }, SpanIncludingLineBreak: { - Start: 176, - Length: 22 + Start: 217, + Length: 1 } }, { LineNumber: 7, - Start: 198, - End: 215, - EndIncludingLineBreak: 216, + Start: 218, + End: 234, + EndIncludingLineBreak: 235, Span: { - Start: 198, - Length: 17 + Start: 218, + Length: 16 }, SpanIncludingLineBreak: { - Start: 198, - Length: 18 + Start: 218, + Length: 17 } }, { LineNumber: 8, - Start: 216, - End: 216, - EndIncludingLineBreak: 217, + Start: 235, + End: 235, + EndIncludingLineBreak: 236, Span: { - Start: 216 + Start: 235 }, SpanIncludingLineBreak: { - Start: 216, + Start: 235, Length: 1 } }, { LineNumber: 9, - Start: 217, - End: 276, - EndIncludingLineBreak: 277, + Start: 236, + End: 262, + EndIncludingLineBreak: 263, Span: { - Start: 217, - Length: 59 + Start: 236, + Length: 26 }, SpanIncludingLineBreak: { - Start: 217, - Length: 60 + Start: 236, + Length: 27 } }, { LineNumber: 10, - Start: 277, - End: 321, - EndIncludingLineBreak: 322, + Start: 263, + End: 264, + EndIncludingLineBreak: 265, Span: { - Start: 277, - Length: 44 + Start: 263, + Length: 1 }, SpanIncludingLineBreak: { - Start: 277, - Length: 45 + Start: 263, + Length: 2 } }, { LineNumber: 11, - Start: 322, - End: 323, - EndIncludingLineBreak: 324, + Start: 265, + End: 364, + EndIncludingLineBreak: 365, Span: { - Start: 322, - Length: 1 + Start: 265, + Length: 99 }, SpanIncludingLineBreak: { - Start: 322, - Length: 2 + Start: 265, + Length: 100 } }, { LineNumber: 12, - Start: 324, - End: 387, - EndIncludingLineBreak: 388, + Start: 365, + End: 415, + EndIncludingLineBreak: 416, Span: { - Start: 324, - Length: 63 + Start: 365, + Length: 50 }, SpanIncludingLineBreak: { - Start: 324, - Length: 64 + Start: 365, + Length: 51 } }, { LineNumber: 13, - Start: 388, - End: 423, - EndIncludingLineBreak: 424, + Start: 416, + End: 421, + EndIncludingLineBreak: 422, Span: { - Start: 388, - Length: 35 + Start: 416, + Length: 5 }, SpanIncludingLineBreak: { - Start: 388, - Length: 36 + Start: 416, + Length: 6 } }, { LineNumber: 14, - Start: 424, - End: 429, - EndIncludingLineBreak: 430, + Start: 422, + End: 498, + EndIncludingLineBreak: 499, Span: { - Start: 424, - Length: 5 + Start: 422, + Length: 76 }, SpanIncludingLineBreak: { - Start: 424, - Length: 6 + Start: 422, + Length: 77 } }, { LineNumber: 15, - Start: 430, - End: 564, - EndIncludingLineBreak: 565, + Start: 499, + End: 540, + EndIncludingLineBreak: 541, Span: { - Start: 430, - Length: 134 + Start: 499, + Length: 41 }, SpanIncludingLineBreak: { - Start: 430, - Length: 135 + Start: 499, + Length: 42 } }, { LineNumber: 16, - Start: 565, - End: 594, - EndIncludingLineBreak: 595, + Start: 541, + End: 550, + EndIncludingLineBreak: 551, Span: { - Start: 565, - Length: 29 + Start: 541, + Length: 9 }, SpanIncludingLineBreak: { - Start: 565, - Length: 30 + Start: 541, + Length: 10 } }, { LineNumber: 17, - Start: 595, - End: 620, - EndIncludingLineBreak: 621, + Start: 551, + End: 736, + EndIncludingLineBreak: 737, Span: { - Start: 595, - Length: 25 + Start: 551, + Length: 185 }, SpanIncludingLineBreak: { - Start: 595, - Length: 26 + Start: 551, + Length: 186 } }, { LineNumber: 18, - Start: 621, - End: 626, - EndIncludingLineBreak: 627, + Start: 737, + End: 750, + EndIncludingLineBreak: 751, Span: { - Start: 621, - Length: 5 + Start: 737, + Length: 13 }, SpanIncludingLineBreak: { - Start: 621, - Length: 6 + Start: 737, + Length: 14 } }, { LineNumber: 19, - Start: 627, - End: 629, - EndIncludingLineBreak: 630, + Start: 751, + End: 793, + EndIncludingLineBreak: 794, Span: { - Start: 627, - Length: 2 + Start: 751, + Length: 42 }, SpanIncludingLineBreak: { - Start: 627, - Length: 3 + Start: 751, + Length: 43 } }, { LineNumber: 20, - Start: 630, - End: 631, - EndIncludingLineBreak: 632, + Start: 794, + End: 809, + EndIncludingLineBreak: 810, Span: { - Start: 630, + Start: 794, + Length: 15 + }, + SpanIncludingLineBreak: { + Start: 794, + Length: 16 + } + }, + { + LineNumber: 21, + Start: 810, + End: 819, + EndIncludingLineBreak: 820, + Span: { + Start: 810, + Length: 9 + }, + SpanIncludingLineBreak: { + Start: 810, + Length: 10 + } + }, + { + LineNumber: 22, + Start: 820, + End: 825, + EndIncludingLineBreak: 826, + Span: { + Start: 820, + Length: 5 + }, + SpanIncludingLineBreak: { + Start: 820, + Length: 6 + } + }, + { + LineNumber: 23, + Start: 826, + End: 827, + EndIncludingLineBreak: 828, + Span: { + Start: 826, Length: 1 }, SpanIncludingLineBreak: { - Start: 630, + Start: 826, Length: 2 } }, { - LineNumber: 21, - Start: 632, - End: 638, - EndIncludingLineBreak: 639, + LineNumber: 24, + Start: 828, + End: 834, + EndIncludingLineBreak: 835, Span: { - Start: 632, + Start: 828, Length: 6 }, SpanIncludingLineBreak: { - Start: 632, + Start: 828, Length: 7 } }, { - LineNumber: 22, - Start: 639, - End: 639, - EndIncludingLineBreak: 639, + LineNumber: 25, + Start: 835, + End: 835, + EndIncludingLineBreak: 835, Span: { - Start: 639 + Start: 835 }, SpanIncludingLineBreak: { - Start: 639 + Start: 835 } } ] }, - HintName: __BuildProperties.g.cs + HintName: Sentry.Generated.BuildPropertyInitializer.g.cs } ], Diagnostics: null diff --git a/test/Sentry.SourceGenerators.Tests/BuildPropertySourceGeneratorTests.RunResult_Publish_AotTrue.verified.txt b/test/Sentry.SourceGenerators.Tests/BuildPropertySourceGeneratorTests.RunResult_Publish_AotTrue.verified.txt index 37b44b9ecd..1fc38316d4 100644 --- a/test/Sentry.SourceGenerators.Tests/BuildPropertySourceGeneratorTests.RunResult_Publish_AotTrue.verified.txt +++ b/test/Sentry.SourceGenerators.Tests/BuildPropertySourceGeneratorTests.RunResult_Publish_AotTrue.verified.txt @@ -3,9 +3,9 @@ GeneratedSources: [ { SyntaxTree: { - FilePath: Sentry.SourceGenerators/Sentry.SourceGenerators.BuildPropertySourceGenerator/__BuildProperties.g.cs, + FilePath: Sentry.SourceGenerators/Sentry.SourceGenerators.BuildPropertySourceGenerator/Sentry.Generated.BuildPropertyInitializer.g.cs, Encoding: utf-8, - Length: 636, + Length: 832, HasCompilationUnitRoot: true, Options: { LanguageVersion: CSharp12, @@ -18,29 +18,32 @@ Encoding: utf-8, Source: // -// Code generated by Sentry Source Generators -// Changes may cause incorrect behavior and will be lost if the code is -// regenerated. +// This code was generated by Sentry.SourceGenerators. +// Changes to this file may cause incorrect behavior and will be lost if the code is regenerated. // -#if NET8_0_OR_GREATER -namespace Sentry; +#if NET5_0_OR_GREATER -[global::System.Runtime.CompilerServices.CompilerGenerated] -public static class BuildVariableInitializer +#nullable enable + +namespace Sentry.Generated { - [global::System.Runtime.CompilerServices.ModuleInitializer] - public static void Initialize() + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Sentry.SourceGenerators", "Version")] + internal static class BuildPropertyInitializer { - global::Sentry.CompilerServices.BuildProperties.Initialize(new global::System.Collections.Generic.Dictionary { - {"PublishAot", "true"}, - {"OutputType", "exe"}, - }); - } + [global::System.Runtime.CompilerServices.ModuleInitializerAttribute] + internal static void Initialize() + { + global::Sentry.CompilerServices.BuildProperties.Initialize(new global::System.Collections.Generic.Dictionary(global::System.StringComparer.OrdinalIgnoreCase) + { + {"PublishAot", "true"}, + }); + } + } } #endif , - Length: 636, + Length: 832, ChecksumAlgorithm: Sha1, CanBeEmbedded: true, Container: {}, @@ -58,310 +61,351 @@ public static class BuildVariableInitializer { LineNumber: 1, Start: 20, - End: 65, - EndIncludingLineBreak: 66, + End: 74, + EndIncludingLineBreak: 75, Span: { Start: 20, - Length: 45 + Length: 54 }, SpanIncludingLineBreak: { Start: 20, - Length: 46 + Length: 55 } }, { LineNumber: 2, - Start: 66, - End: 137, - EndIncludingLineBreak: 138, + Start: 75, + End: 172, + EndIncludingLineBreak: 173, Span: { - Start: 66, - Length: 71 + Start: 75, + Length: 97 }, SpanIncludingLineBreak: { - Start: 66, - Length: 72 + Start: 75, + Length: 98 } }, { LineNumber: 3, - Start: 138, - End: 153, - EndIncludingLineBreak: 154, + Start: 173, + End: 193, + EndIncludingLineBreak: 194, Span: { - Start: 138, - Length: 15 + Start: 173, + Length: 20 }, SpanIncludingLineBreak: { - Start: 138, - Length: 16 + Start: 173, + Length: 21 } }, { LineNumber: 4, - Start: 154, - End: 174, - EndIncludingLineBreak: 175, + Start: 194, + End: 194, + EndIncludingLineBreak: 195, Span: { - Start: 154, - Length: 20 + Start: 194 }, SpanIncludingLineBreak: { - Start: 154, - Length: 21 + Start: 194, + Length: 1 } }, { LineNumber: 5, - Start: 175, - End: 175, - EndIncludingLineBreak: 176, + Start: 195, + End: 216, + EndIncludingLineBreak: 217, Span: { - Start: 175 + Start: 195, + Length: 21 }, SpanIncludingLineBreak: { - Start: 175, - Length: 1 + Start: 195, + Length: 22 } }, { LineNumber: 6, - Start: 176, - End: 197, - EndIncludingLineBreak: 198, + Start: 217, + End: 217, + EndIncludingLineBreak: 218, Span: { - Start: 176, - Length: 21 + Start: 217 }, SpanIncludingLineBreak: { - Start: 176, - Length: 22 + Start: 217, + Length: 1 } }, { LineNumber: 7, - Start: 198, - End: 215, - EndIncludingLineBreak: 216, + Start: 218, + End: 234, + EndIncludingLineBreak: 235, Span: { - Start: 198, - Length: 17 + Start: 218, + Length: 16 }, SpanIncludingLineBreak: { - Start: 198, - Length: 18 + Start: 218, + Length: 17 } }, { LineNumber: 8, - Start: 216, - End: 216, - EndIncludingLineBreak: 217, + Start: 235, + End: 235, + EndIncludingLineBreak: 236, Span: { - Start: 216 + Start: 235 }, SpanIncludingLineBreak: { - Start: 216, + Start: 235, Length: 1 } }, { LineNumber: 9, - Start: 217, - End: 276, - EndIncludingLineBreak: 277, + Start: 236, + End: 262, + EndIncludingLineBreak: 263, Span: { - Start: 217, - Length: 59 + Start: 236, + Length: 26 }, SpanIncludingLineBreak: { - Start: 217, - Length: 60 + Start: 236, + Length: 27 } }, { LineNumber: 10, - Start: 277, - End: 321, - EndIncludingLineBreak: 322, + Start: 263, + End: 264, + EndIncludingLineBreak: 265, Span: { - Start: 277, - Length: 44 + Start: 263, + Length: 1 }, SpanIncludingLineBreak: { - Start: 277, - Length: 45 + Start: 263, + Length: 2 } }, { LineNumber: 11, - Start: 322, - End: 323, - EndIncludingLineBreak: 324, + Start: 265, + End: 364, + EndIncludingLineBreak: 365, Span: { - Start: 322, - Length: 1 + Start: 265, + Length: 99 }, SpanIncludingLineBreak: { - Start: 322, - Length: 2 + Start: 265, + Length: 100 } }, { LineNumber: 12, - Start: 324, - End: 387, - EndIncludingLineBreak: 388, + Start: 365, + End: 415, + EndIncludingLineBreak: 416, Span: { - Start: 324, - Length: 63 + Start: 365, + Length: 50 }, SpanIncludingLineBreak: { - Start: 324, - Length: 64 + Start: 365, + Length: 51 } }, { LineNumber: 13, - Start: 388, - End: 423, - EndIncludingLineBreak: 424, + Start: 416, + End: 421, + EndIncludingLineBreak: 422, Span: { - Start: 388, - Length: 35 + Start: 416, + Length: 5 }, SpanIncludingLineBreak: { - Start: 388, - Length: 36 + Start: 416, + Length: 6 } }, { LineNumber: 14, - Start: 424, - End: 429, - EndIncludingLineBreak: 430, + Start: 422, + End: 498, + EndIncludingLineBreak: 499, Span: { - Start: 424, - Length: 5 + Start: 422, + Length: 76 }, SpanIncludingLineBreak: { - Start: 424, - Length: 6 + Start: 422, + Length: 77 } }, { LineNumber: 15, - Start: 430, - End: 564, - EndIncludingLineBreak: 565, + Start: 499, + End: 540, + EndIncludingLineBreak: 541, Span: { - Start: 430, - Length: 134 + Start: 499, + Length: 41 }, SpanIncludingLineBreak: { - Start: 430, - Length: 135 + Start: 499, + Length: 42 } }, { LineNumber: 16, - Start: 565, - End: 591, - EndIncludingLineBreak: 592, + Start: 541, + End: 550, + EndIncludingLineBreak: 551, Span: { - Start: 565, - Length: 26 + Start: 541, + Length: 9 }, SpanIncludingLineBreak: { - Start: 565, - Length: 27 + Start: 541, + Length: 10 } }, { LineNumber: 17, - Start: 592, - End: 617, - EndIncludingLineBreak: 618, + Start: 551, + End: 736, + EndIncludingLineBreak: 737, Span: { - Start: 592, - Length: 25 + Start: 551, + Length: 185 }, SpanIncludingLineBreak: { - Start: 592, - Length: 26 + Start: 551, + Length: 186 } }, { LineNumber: 18, - Start: 618, - End: 623, - EndIncludingLineBreak: 624, + Start: 737, + End: 750, + EndIncludingLineBreak: 751, Span: { - Start: 618, - Length: 5 + Start: 737, + Length: 13 }, SpanIncludingLineBreak: { - Start: 618, - Length: 6 + Start: 737, + Length: 14 } }, { LineNumber: 19, - Start: 624, - End: 626, - EndIncludingLineBreak: 627, + Start: 751, + End: 790, + EndIncludingLineBreak: 791, Span: { - Start: 624, - Length: 2 + Start: 751, + Length: 39 }, SpanIncludingLineBreak: { - Start: 624, - Length: 3 + Start: 751, + Length: 40 } }, { LineNumber: 20, - Start: 627, - End: 628, - EndIncludingLineBreak: 629, + Start: 791, + End: 806, + EndIncludingLineBreak: 807, + Span: { + Start: 791, + Length: 15 + }, + SpanIncludingLineBreak: { + Start: 791, + Length: 16 + } + }, + { + LineNumber: 21, + Start: 807, + End: 816, + EndIncludingLineBreak: 817, Span: { - Start: 627, + Start: 807, + Length: 9 + }, + SpanIncludingLineBreak: { + Start: 807, + Length: 10 + } + }, + { + LineNumber: 22, + Start: 817, + End: 822, + EndIncludingLineBreak: 823, + Span: { + Start: 817, + Length: 5 + }, + SpanIncludingLineBreak: { + Start: 817, + Length: 6 + } + }, + { + LineNumber: 23, + Start: 823, + End: 824, + EndIncludingLineBreak: 825, + Span: { + Start: 823, Length: 1 }, SpanIncludingLineBreak: { - Start: 627, + Start: 823, Length: 2 } }, { - LineNumber: 21, - Start: 629, - End: 635, - EndIncludingLineBreak: 636, + LineNumber: 24, + Start: 825, + End: 831, + EndIncludingLineBreak: 832, Span: { - Start: 629, + Start: 825, Length: 6 }, SpanIncludingLineBreak: { - Start: 629, + Start: 825, Length: 7 } }, { - LineNumber: 22, - Start: 636, - End: 636, - EndIncludingLineBreak: 636, + LineNumber: 25, + Start: 832, + End: 832, + EndIncludingLineBreak: 832, Span: { - Start: 636 + Start: 832 }, SpanIncludingLineBreak: { - Start: 636 + Start: 832 } } ] }, - HintName: __BuildProperties.g.cs + HintName: Sentry.Generated.BuildPropertyInitializer.g.cs } ], Diagnostics: null diff --git a/test/Sentry.SourceGenerators.Tests/BuildPropertySourceGeneratorTests.RunResult_Success.verified.txt b/test/Sentry.SourceGenerators.Tests/BuildPropertySourceGeneratorTests.RunResult_Success.verified.txt index ff9d84c52d..d94a9ca576 100644 --- a/test/Sentry.SourceGenerators.Tests/BuildPropertySourceGeneratorTests.RunResult_Success.verified.txt +++ b/test/Sentry.SourceGenerators.Tests/BuildPropertySourceGeneratorTests.RunResult_Success.verified.txt @@ -3,9 +3,9 @@ GeneratedSources: [ { SyntaxTree: { - FilePath: Sentry.SourceGenerators/Sentry.SourceGenerators.BuildPropertySourceGenerator/__BuildProperties.g.cs, + FilePath: Sentry.SourceGenerators/Sentry.SourceGenerators.BuildPropertySourceGenerator/Sentry.Generated.BuildPropertyInitializer.g.cs, Encoding: utf-8, - Length: 637, + Length: 833, HasCompilationUnitRoot: true, Options: { LanguageVersion: CSharp12, @@ -18,29 +18,32 @@ Encoding: utf-8, Source: // -// Code generated by Sentry Source Generators -// Changes may cause incorrect behavior and will be lost if the code is -// regenerated. +// This code was generated by Sentry.SourceGenerators. +// Changes to this file may cause incorrect behavior and will be lost if the code is regenerated. // -#if NET8_0_OR_GREATER -namespace Sentry; +#if NET5_0_OR_GREATER -[global::System.Runtime.CompilerServices.CompilerGenerated] -public static class BuildVariableInitializer +#nullable enable + +namespace Sentry.Generated { - [global::System.Runtime.CompilerServices.ModuleInitializer] - public static void Initialize() + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Sentry.SourceGenerators", "Version")] + internal static class BuildPropertyInitializer { - global::Sentry.CompilerServices.BuildProperties.Initialize(new global::System.Collections.Generic.Dictionary { - {"PublishAot", "false"}, - {"OutputType", "exe"}, - }); - } + [global::System.Runtime.CompilerServices.ModuleInitializerAttribute] + internal static void Initialize() + { + global::Sentry.CompilerServices.BuildProperties.Initialize(new global::System.Collections.Generic.Dictionary(global::System.StringComparer.OrdinalIgnoreCase) + { + {"PublishAot", "false"}, + }); + } + } } #endif , - Length: 637, + Length: 833, ChecksumAlgorithm: Sha1, CanBeEmbedded: true, Container: {}, @@ -58,310 +61,351 @@ public static class BuildVariableInitializer { LineNumber: 1, Start: 20, - End: 65, - EndIncludingLineBreak: 66, + End: 74, + EndIncludingLineBreak: 75, Span: { Start: 20, - Length: 45 + Length: 54 }, SpanIncludingLineBreak: { Start: 20, - Length: 46 + Length: 55 } }, { LineNumber: 2, - Start: 66, - End: 137, - EndIncludingLineBreak: 138, + Start: 75, + End: 172, + EndIncludingLineBreak: 173, Span: { - Start: 66, - Length: 71 + Start: 75, + Length: 97 }, SpanIncludingLineBreak: { - Start: 66, - Length: 72 + Start: 75, + Length: 98 } }, { LineNumber: 3, - Start: 138, - End: 153, - EndIncludingLineBreak: 154, + Start: 173, + End: 193, + EndIncludingLineBreak: 194, Span: { - Start: 138, - Length: 15 + Start: 173, + Length: 20 }, SpanIncludingLineBreak: { - Start: 138, - Length: 16 + Start: 173, + Length: 21 } }, { LineNumber: 4, - Start: 154, - End: 174, - EndIncludingLineBreak: 175, + Start: 194, + End: 194, + EndIncludingLineBreak: 195, Span: { - Start: 154, - Length: 20 + Start: 194 }, SpanIncludingLineBreak: { - Start: 154, - Length: 21 + Start: 194, + Length: 1 } }, { LineNumber: 5, - Start: 175, - End: 175, - EndIncludingLineBreak: 176, + Start: 195, + End: 216, + EndIncludingLineBreak: 217, Span: { - Start: 175 + Start: 195, + Length: 21 }, SpanIncludingLineBreak: { - Start: 175, - Length: 1 + Start: 195, + Length: 22 } }, { LineNumber: 6, - Start: 176, - End: 197, - EndIncludingLineBreak: 198, + Start: 217, + End: 217, + EndIncludingLineBreak: 218, Span: { - Start: 176, - Length: 21 + Start: 217 }, SpanIncludingLineBreak: { - Start: 176, - Length: 22 + Start: 217, + Length: 1 } }, { LineNumber: 7, - Start: 198, - End: 215, - EndIncludingLineBreak: 216, + Start: 218, + End: 234, + EndIncludingLineBreak: 235, Span: { - Start: 198, - Length: 17 + Start: 218, + Length: 16 }, SpanIncludingLineBreak: { - Start: 198, - Length: 18 + Start: 218, + Length: 17 } }, { LineNumber: 8, - Start: 216, - End: 216, - EndIncludingLineBreak: 217, + Start: 235, + End: 235, + EndIncludingLineBreak: 236, Span: { - Start: 216 + Start: 235 }, SpanIncludingLineBreak: { - Start: 216, + Start: 235, Length: 1 } }, { LineNumber: 9, - Start: 217, - End: 276, - EndIncludingLineBreak: 277, + Start: 236, + End: 262, + EndIncludingLineBreak: 263, Span: { - Start: 217, - Length: 59 + Start: 236, + Length: 26 }, SpanIncludingLineBreak: { - Start: 217, - Length: 60 + Start: 236, + Length: 27 } }, { LineNumber: 10, - Start: 277, - End: 321, - EndIncludingLineBreak: 322, + Start: 263, + End: 264, + EndIncludingLineBreak: 265, Span: { - Start: 277, - Length: 44 + Start: 263, + Length: 1 }, SpanIncludingLineBreak: { - Start: 277, - Length: 45 + Start: 263, + Length: 2 } }, { LineNumber: 11, - Start: 322, - End: 323, - EndIncludingLineBreak: 324, + Start: 265, + End: 364, + EndIncludingLineBreak: 365, Span: { - Start: 322, - Length: 1 + Start: 265, + Length: 99 }, SpanIncludingLineBreak: { - Start: 322, - Length: 2 + Start: 265, + Length: 100 } }, { LineNumber: 12, - Start: 324, - End: 387, - EndIncludingLineBreak: 388, + Start: 365, + End: 415, + EndIncludingLineBreak: 416, Span: { - Start: 324, - Length: 63 + Start: 365, + Length: 50 }, SpanIncludingLineBreak: { - Start: 324, - Length: 64 + Start: 365, + Length: 51 } }, { LineNumber: 13, - Start: 388, - End: 423, - EndIncludingLineBreak: 424, + Start: 416, + End: 421, + EndIncludingLineBreak: 422, Span: { - Start: 388, - Length: 35 + Start: 416, + Length: 5 }, SpanIncludingLineBreak: { - Start: 388, - Length: 36 + Start: 416, + Length: 6 } }, { LineNumber: 14, - Start: 424, - End: 429, - EndIncludingLineBreak: 430, + Start: 422, + End: 498, + EndIncludingLineBreak: 499, Span: { - Start: 424, - Length: 5 + Start: 422, + Length: 76 }, SpanIncludingLineBreak: { - Start: 424, - Length: 6 + Start: 422, + Length: 77 } }, { LineNumber: 15, - Start: 430, - End: 564, - EndIncludingLineBreak: 565, + Start: 499, + End: 540, + EndIncludingLineBreak: 541, Span: { - Start: 430, - Length: 134 + Start: 499, + Length: 41 }, SpanIncludingLineBreak: { - Start: 430, - Length: 135 + Start: 499, + Length: 42 } }, { LineNumber: 16, - Start: 565, - End: 592, - EndIncludingLineBreak: 593, + Start: 541, + End: 550, + EndIncludingLineBreak: 551, Span: { - Start: 565, - Length: 27 + Start: 541, + Length: 9 }, SpanIncludingLineBreak: { - Start: 565, - Length: 28 + Start: 541, + Length: 10 } }, { LineNumber: 17, - Start: 593, - End: 618, - EndIncludingLineBreak: 619, + Start: 551, + End: 736, + EndIncludingLineBreak: 737, Span: { - Start: 593, - Length: 25 + Start: 551, + Length: 185 }, SpanIncludingLineBreak: { - Start: 593, - Length: 26 + Start: 551, + Length: 186 } }, { LineNumber: 18, - Start: 619, - End: 624, - EndIncludingLineBreak: 625, + Start: 737, + End: 750, + EndIncludingLineBreak: 751, Span: { - Start: 619, - Length: 5 + Start: 737, + Length: 13 }, SpanIncludingLineBreak: { - Start: 619, - Length: 6 + Start: 737, + Length: 14 } }, { LineNumber: 19, - Start: 625, - End: 627, - EndIncludingLineBreak: 628, + Start: 751, + End: 791, + EndIncludingLineBreak: 792, Span: { - Start: 625, - Length: 2 + Start: 751, + Length: 40 }, SpanIncludingLineBreak: { - Start: 625, - Length: 3 + Start: 751, + Length: 41 } }, { LineNumber: 20, - Start: 628, - End: 629, - EndIncludingLineBreak: 630, + Start: 792, + End: 807, + EndIncludingLineBreak: 808, + Span: { + Start: 792, + Length: 15 + }, + SpanIncludingLineBreak: { + Start: 792, + Length: 16 + } + }, + { + LineNumber: 21, + Start: 808, + End: 817, + EndIncludingLineBreak: 818, Span: { - Start: 628, + Start: 808, + Length: 9 + }, + SpanIncludingLineBreak: { + Start: 808, + Length: 10 + } + }, + { + LineNumber: 22, + Start: 818, + End: 823, + EndIncludingLineBreak: 824, + Span: { + Start: 818, + Length: 5 + }, + SpanIncludingLineBreak: { + Start: 818, + Length: 6 + } + }, + { + LineNumber: 23, + Start: 824, + End: 825, + EndIncludingLineBreak: 826, + Span: { + Start: 824, Length: 1 }, SpanIncludingLineBreak: { - Start: 628, + Start: 824, Length: 2 } }, { - LineNumber: 21, - Start: 630, - End: 636, - EndIncludingLineBreak: 637, + LineNumber: 24, + Start: 826, + End: 832, + EndIncludingLineBreak: 833, Span: { - Start: 630, + Start: 826, Length: 6 }, SpanIncludingLineBreak: { - Start: 630, + Start: 826, Length: 7 } }, { - LineNumber: 22, - Start: 637, - End: 637, - EndIncludingLineBreak: 637, + LineNumber: 25, + Start: 833, + End: 833, + EndIncludingLineBreak: 833, Span: { - Start: 637 + Start: 833 }, SpanIncludingLineBreak: { - Start: 637 + Start: 833 } } ] }, - HintName: __BuildProperties.g.cs + HintName: Sentry.Generated.BuildPropertyInitializer.g.cs } ], Diagnostics: null diff --git a/test/Sentry.SourceGenerators.Tests/BuildPropertySourceGeneratorTests.cs b/test/Sentry.SourceGenerators.Tests/BuildPropertySourceGeneratorTests.cs index b952748dd5..89775ffc5c 100644 --- a/test/Sentry.SourceGenerators.Tests/BuildPropertySourceGeneratorTests.cs +++ b/test/Sentry.SourceGenerators.Tests/BuildPropertySourceGeneratorTests.cs @@ -5,51 +5,80 @@ namespace Sentry.SourceGenerators.Tests; - public class BuildPropertySourceGeneratorTests { + private static readonly string s_hintName = "Sentry.Generated.BuildPropertyInitializer.g.cs"; + [SkippableFact] public Task RunResult_Success() { Skip.If(RuntimeInformation.IsOSPlatform(OSPlatform.Windows)); - var driver = BuildDriver(typeof(Program).Assembly, ("PublishAot", "false"), ("OutputType", "exe")); + var driver = BuildDriver(OutputKind.ConsoleApplication, typeof(Program).Assembly, ("PublishAot", "false")); var result = driver.GetRunResult().Results.FirstOrDefault(); result.Exception.Should().BeNull(); result.GeneratedSources.Length.Should().Be(1); - result.GeneratedSources.First().HintName.Should().Be("__BuildProperties.g.cs"); + result.GeneratedSources.First().HintName.Should().Be(s_hintName); return Verify(result); } - [SkippableFact] public Task RunResult_BadStrings() { Skip.If(RuntimeInformation.IsOSPlatform(OSPlatform.Windows)); // we're hijacking PublishAot to make life easy - var driver = BuildDriver(typeof(Program).Assembly, ("My\\Key", "test\\test"), ("OutputType", "exe")); + var driver = BuildDriver(OutputKind.ConsoleApplication, typeof(Program).Assembly, ("My\\Key", "test\\test")); var result = driver.GetRunResult().Results.FirstOrDefault(); result.Exception.Should().BeNull(); result.GeneratedSources.Length.Should().Be(1); - result.GeneratedSources.First().HintName.Should().Be("__BuildProperties.g.cs"); + result.GeneratedSources.First().HintName.Should().Be(s_hintName); return Verify(result); } - [SkippableFact] public Task RunResult_Publish_AotTrue() { Skip.If(RuntimeInformation.IsOSPlatform(OSPlatform.Windows)); - var driver = BuildDriver(typeof(Program).Assembly, ("PublishAot", "true"), ("OutputType", "exe")); + var driver = BuildDriver(OutputKind.ConsoleApplication, typeof(Program).Assembly, ("PublishAot", "true")); var result = driver.GetRunResult().Results.FirstOrDefault(); result.Exception.Should().BeNull(); result.GeneratedSources.Length.Should().Be(1); - result.GeneratedSources.First().HintName.Should().Be("__BuildProperties.g.cs"); + result.GeneratedSources.First().HintName.Should().Be(s_hintName); return Verify(result); } + [SkippableFact] + public void RunResult_NoProperties_NoGeneratedSources() + { + Skip.If(RuntimeInformation.IsOSPlatform(OSPlatform.Windows)); + + var driver = BuildDriver(OutputKind.ConsoleApplication, typeof(Program).Assembly); + var result = driver.GetRunResult().Results.FirstOrDefault(); + result.Exception.Should().BeNull(); + + result.GeneratedSources.Should().BeEmpty(); + } + + [SkippableTheory] + [InlineData(OutputKind.ConsoleApplication, true)] + [InlineData(OutputKind.WindowsApplication, true)] + [InlineData(OutputKind.WindowsRuntimeApplication, true)] + [InlineData(OutputKind.DynamicallyLinkedLibrary, false)] + [InlineData(OutputKind.NetModule, false)] + [InlineData(OutputKind.WindowsRuntimeMetadata, false)] + public void RunResult_OutputType_Values(OutputKind outputKind, bool sourceGenExpected) + { + Skip.If(RuntimeInformation.IsOSPlatform(OSPlatform.Windows)); + + var driver = BuildDriver(outputKind, typeof(Program).Assembly, ("PublishTrimmed", "true")); + var result = driver.GetRunResult().Results.FirstOrDefault(); + result.Exception.Should().BeNull(); + + var generated = result.GeneratedSources.Any(x => x.HintName.Equals(s_hintName)); + generated.Should().Be(sourceGenExpected); + } [SkippableTheory] [InlineData("no", true)] @@ -59,21 +88,20 @@ public void RunResult_SentryDisableSourceGenerator_Values(string value, bool sou { Skip.If(RuntimeInformation.IsOSPlatform(OSPlatform.Windows)); - var driver = BuildDriver(typeof(Program).Assembly, ("SentryDisableSourceGenerator", value), ("OutputType", "exe")); + var driver = BuildDriver(OutputKind.ConsoleApplication, typeof(Program).Assembly, ("SentryDisableSourceGenerator", value)); var result = driver.GetRunResult().Results.FirstOrDefault(); result.Exception.Should().BeNull(); - var generated = result.GeneratedSources.Any(x => x.HintName.Equals("__BuildProperties.g.cs")); + var generated = result.GeneratedSources.Any(x => x.HintName.Equals(s_hintName)); generated.Should().Be(sourceGenExpected); } - [SkippableFact] public Task RunResult_Expect_None() { Skip.If(RuntimeInformation.IsOSPlatform(OSPlatform.Windows)); - var driver = BuildDriver(typeof(Program).Assembly, ("PublishAot", "false")); + var driver = BuildDriver(OutputKind.DynamicallyLinkedLibrary, typeof(Program).Assembly, ("PublishAot", "false")); var result = driver.GetRunResult().Results.FirstOrDefault(); result.Exception.Should().BeNull(); result.GeneratedSources.Length.Should().Be(0); @@ -81,11 +109,10 @@ public Task RunResult_Expect_None() return Verify(result); } - - private static GeneratorDriver BuildDriver(Assembly metadataAssembly, params IEnumerable<(string Key, string Value)> buildProperties) + private static GeneratorDriver BuildDriver(OutputKind outputKind, Assembly metadataAssembly, params IEnumerable<(string Key, string Value)> buildProperties) { var metadataReference = MetadataReference.CreateFromFile(metadataAssembly.Location); - var options = new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary); + var options = new CSharpCompilationOptions(outputKind); var compilation = CSharpCompilation.Create("TestAssembly", [], [metadataReference], options); var generator = new BuildPropertySourceGenerator(); diff --git a/test/Sentry.SourceGenerators.Tests/VerifySettingsInitializer.cs b/test/Sentry.SourceGenerators.Tests/VerifySettingsInitializer.cs new file mode 100644 index 0000000000..8142f145c6 --- /dev/null +++ b/test/Sentry.SourceGenerators.Tests/VerifySettingsInitializer.cs @@ -0,0 +1,20 @@ +namespace Sentry.SourceGenerators.Tests; + +internal static class VerifySettingsInitializer +{ + private static readonly AssemblyName s_assemblyName = typeof(BuildPropertySourceGenerator).Assembly.GetName(); + + [ModuleInitializer] + internal static void Initialize() + { + VerifierSettings.AddScrubber(VersionScrubber); + } + + private static void VersionScrubber(StringBuilder text) + { + if (s_assemblyName.Version is not null) + { + text.Replace(s_assemblyName.Version.ToString(), nameof(Version)); + } + } +}