Use Embedded API for generator marker attribute#51
Conversation
…bute
- Update Microsoft.CodeAnalysis.CSharp from 4.8.0 to 4.14.0
- Call AddEmbeddedAttributeDefinition() in RegisterPostInitializationOutput
- Replace [Conditional("CODE_ANALYSIS")] with [Embedded] on the generated attribute
- Update tests to account for the additional EmbeddedAttribute generated tree
Co-authored-by: Dreamescaper <17177729+Dreamescaper@users.noreply.github.com>
|
@copilot |
The bump from 4.8.0 → 4.14.0 maps directly to Visual Studio / compiler host versions:
Because source generators run inside the compiler process (the compiler provides the Roslyn runtime — the Impact summary: users on VS 2022 17.8–17.13 or equivalent SDK versions (late 2023 – early 2025) would need to update their tooling to use this package. If you'd prefer to keep support for older toolchains, the existing Warning Firewall rules blocked me from connecting to one or more addresses (expand for details)I tried to connect to the following addresses, but was blocked by firewall rules:
If you need me to access, download, or install something from one of these locations, you can either:
|
|
@copilot Here's original AddEmbeddedAttributeDefinition implementation in Roslyn: |
…Roslyn version to 4.8.0 Co-authored-by: Dreamescaper <17177729+Dreamescaper@users.noreply.github.com>
Implemented in d84376b. public static void AddEmbeddedAttributeDefinition(this IncrementalGeneratorPostInitializationContext context)
{
context.AddSource("Microsoft.CodeAnalysis.EmbeddedAttribute", SourceText.From(EmbeddedAttributeSource, Encoding.UTF8));
}On Roslyn ≥ 4.14, the compiler-provided instance method takes precedence over the extension method automatically, so there's no |
The
[Conditional("CODE_ANALYSIS")]workaround onGenerateServiceRegistrationsAttributecaused warnings for consumers since the conditional symbol is never defined at runtime. TheAddEmbeddedAttributeDefinition()API solves this cleanly via compiler-embedded types. To preserve backward compatibility with Roslyn 4.8.0+ (VS 2022 17.8+), the API is implemented as a polyfill extension method rather than requiring a Roslyn version bump.Changes
Extensions/IncrementalGeneratorPostInitializationContextExtensions.cs(new): polyfill extension methodAddEmbeddedAttributeDefinition()onIncrementalGeneratorPostInitializationContextthat emits the canonicalMicrosoft.CodeAnalysis.EmbeddedAttributesource — identical to what Roslyn 4.14's built-in API does. On Roslyn ≥ 4.14, the compiler-provided instance method automatically shadows this extension with no#ifguards needed.DependencyInjectionGenerator.cs: callAddEmbeddedAttributeDefinition()inRegisterPostInitializationOutputto inject theMicrosoft.CodeAnalysis.EmbeddedAttributedefinition into the consumer's compilationGenerateAttributeInfo.cs: replace[Conditional("CODE_ANALYSIS")]+using System.Diagnosticswith[Embedded]+using Microsoft.CodeAnalysis[Embedded]marks the attribute as assembly-internal — present at compile time for the generator, invisible to downstream consumers, no conditional-attribute warnings.[1]→[2]) to account for the additionalEmbeddedAttributegenerated file.Original prompt
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.