diff --git a/Directory.Packages.props b/Directory.Packages.props index c6d17db5..156a0864 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -10,7 +10,7 @@ Consumers on net48 or net8+ built with VS 2022 17.14+ or .NET 10+ SDK are fully supported. --> 4.14.0 1.65.51 - 1.0.0-prerelease.33 + 1.0.0-prerelease.36 diff --git a/Justfile b/Justfile index 977113cf..c063079b 100644 --- a/Justfile +++ b/Justfile @@ -3,9 +3,8 @@ set quiet root_folder := "./src/" solution_file := root_folder + "Telemetry.SourceGenerator.slnx" test_solution := solution_file -build_configuration := "Release" +build_configuration := "Debug" -pipeline_version := "0.2.1" pipeline_feed := "https://api.nuget.org/v3/index.json" pipeline_tool := ".tools/purview-build/purview-build" @@ -23,7 +22,7 @@ default: [private] ensure-pipeline-tool: if [ ! -x "{{ pipeline_tool }}" ]; then \ - dotnet tool install Purview.Build --tool-path .tools/purview-build --add-source "{{ pipeline_feed }}" --version "{{ pipeline_version }}"; \ + dotnet tool install Purview.Build --tool-path .tools/purview-build --add-source "{{ pipeline_feed }}"; \ fi # Run the PR pipeline (restore, build, lint, tests) diff --git a/package.json b/package.json index 67176199..f2613fd4 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "purview-telemetry-sourcegenerator", - "version": "5.0.0-prerelease.4", + "version": "5.0.0-prerelease.5", "description": "Generates [`ActivitySource`](https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.activitysource), [`ILogger`](https://learn.microsoft.com/en-us/dotnet/api/microsoft.extensions.logging.ilogger), and [`Metrics`](https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.metrics) based on interface methods.", "readme": "README.md", "repository": { diff --git a/src/src/SourceGenerator/Emitters/DependencyInjectionClassEmitter.cs b/src/src/SourceGenerator/Emitters/DependencyInjectionClassEmitter.cs index e0e8b41d..2946f501 100644 --- a/src/src/SourceGenerator/Emitters/DependencyInjectionClassEmitter.cs +++ b/src/src/SourceGenerator/Emitters/DependencyInjectionClassEmitter.cs @@ -33,8 +33,12 @@ GenerationContext generationContext } var classNameToGenerate = attribute.DependencyInjectionClassName; + var includeGeneratedAttributes = false; if (string.IsNullOrWhiteSpace(classNameToGenerate)) + { classNameToGenerate = implementationClassName + "DIExtension"; + includeGeneratedAttributes = true; + } var classAccessibility = attribute.DependencyInjectionClassIsPublic ? TypeDeclarationAccessibility.Public @@ -62,7 +66,11 @@ GenerationContext generationContext new(classNameToGenerate!, classAccessibility) { IsStatic = true, - IncludeGeneratedAttributes = false, + // When the class name is auto-generated, include the GeneratedCodeAttribute to indicate that it is generated code. + // When the class name is explicitly specified, we don't know if it's pre-existing, and generated by someone else + // so if we apply the generated code attributes it can cause duplicate attribute errors if the user has already applied them to the class + // as AllowMultiple = false on the GeneratedCodeAttribute and others...! + IncludeGeneratedAttributes = includeGeneratedAttributes, Attributes = [EmitterHelpers.EditorBrowsableAttribute()], } ) @@ -102,15 +110,16 @@ CancellationToken token generationContext.Debug($"Emitting DI method for {interfaceName}."); - writer.XmlSummary( - $"Registers the generated {XmlSee("global::" + interfaceType.RenderFullName)} implementation with the service collection." - ); - writer.XmlParam("services", "The service collection to register the telemetry implementation with."); - writer.XmlReturn("The service collection, for chaining."); + writer + .XmlSummary( + $"Registers the generated {XmlSee(interfaceType.RenderFullName)} implementation with the service collection." + ) + .XmlParam("services", "The service collection to register the telemetry implementation with.") + .XmlReturn("The service collection, for chaining."); using ( writer.MethodScope( - new MethodDeclarationOptions( + new( "Add" + methodName, TypeLibrary.DependencyInjection.IServiceCollection, TypeDeclarationAccessibility.Public @@ -119,10 +128,7 @@ CancellationToken token IsStatic = true, Parameters = [ - new ParameterDeclarationOptions("services", TypeLibrary.DependencyInjection.IServiceCollection) - { - IsThis = true, - }, + new("services", TypeLibrary.DependencyInjection.IServiceCollection) { IsThis = true }, ], IncludeGeneratedAttributes = false, } diff --git a/src/src/SourceGenerator/Emitters/GeneratedTypesEmitter.cs b/src/src/SourceGenerator/Emitters/GeneratedTypesEmitter.cs index ee35661d..e27eaae2 100644 --- a/src/src/SourceGenerator/Emitters/GeneratedTypesEmitter.cs +++ b/src/src/SourceGenerator/Emitters/GeneratedTypesEmitter.cs @@ -178,7 +178,7 @@ public static void EmitAll(IncrementalGeneratorPostInitializationContext context emitter.Emitter(writer, emitter.Type); - context.AddSource($"{emitter.Type.MetadataFullName}.g.cs", writer); + context.AddSource($"{emitter.Type.Name}.g.cs", writer); } } diff --git a/src/tests/SourceGenerator.IntegrationTests/Generators/TelemetrySourceGeneratorTests.cs b/src/tests/SourceGenerator.IntegrationTests/Generators/TelemetrySourceGeneratorTests.cs index 2d133484..410be0a3 100644 --- a/src/tests/SourceGenerator.IntegrationTests/Generators/TelemetrySourceGeneratorTests.cs +++ b/src/tests/SourceGenerator.IntegrationTests/Generators/TelemetrySourceGeneratorTests.cs @@ -56,7 +56,7 @@ namespace Testing; var generationResult = await GenerateAsync(empty, cancellationToken: cancellationToken); // Assert - var autoCounterAttribute = generationResult.GetSource("Purview.Telemetry.AutoCounterAttribute.g.cs"); + var autoCounterAttribute = generationResult.GetSource("AutoCounterAttribute.g.cs"); await Assert .That(autoCounterAttribute) .ContainsGeneratedCode("#pragma warning disable CS8625") @@ -67,7 +67,7 @@ await Assert .IsFalse() .Because("missing-documentation warnings must be resolved with XML summaries, not pragmas"); - var targetsEnum = generationResult.GetSource("Purview.Telemetry.Targets.g.cs"); + var targetsEnum = generationResult.GetSource("Targets.g.cs"); await Assert .That(targetsEnum) .ContainsGeneratedCode("/// Excludes logging targets.")