Skip to content

Use Embedded API for generator marker attribute#51

Merged
Dreamescaper merged 3 commits intomainfrom
copilot/embed-api-call-attribute-generation
Mar 20, 2026
Merged

Use Embedded API for generator marker attribute#51
Dreamescaper merged 3 commits intomainfrom
copilot/embed-api-call-attribute-generation

Conversation

Copy link
Contributor

Copilot AI commented Mar 20, 2026

The [Conditional("CODE_ANALYSIS")] workaround on GenerateServiceRegistrationsAttribute caused warnings for consumers since the conditional symbol is never defined at runtime. The AddEmbeddedAttributeDefinition() 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 method AddEmbeddedAttributeDefinition() on IncrementalGeneratorPostInitializationContext that emits the canonical Microsoft.CodeAnalysis.EmbeddedAttribute source — 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 #if guards needed.
  • DependencyInjectionGenerator.cs: call AddEmbeddedAttributeDefinition() in RegisterPostInitializationOutput to inject the Microsoft.CodeAnalysis.EmbeddedAttribute definition into the consumer's compilation
  • GenerateAttributeInfo.cs: replace [Conditional("CODE_ANALYSIS")] + using System.Diagnostics with [Embedded] + using Microsoft.CodeAnalysis
context.RegisterPostInitializationOutput(context =>
{
    context.AddEmbeddedAttributeDefinition();  // polyfill, or built-in on Roslyn >= 4.14
    context.AddSource("ServiceScanAttributes.Generated.cs", SourceText.From(GenerateAttributeInfo.Source, Encoding.UTF8));
});
// Before
[Conditional("CODE_ANALYSIS")]
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
internal class GenerateServiceRegistrationsAttribute : Attribute { ... }

// After
[Embedded]
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
internal class GenerateServiceRegistrationsAttribute : Attribute { ... }

[Embedded] marks the attribute as assembly-internal — present at compile time for the generator, invisible to downstream consumers, no conditional-attribute warnings.

  • Tests: updated tree indices ([1][2]) to account for the additional EmbeddedAttribute generated file.
Original prompt

This section details on the original issue you should resolve

<issue_title>Embed api call and update attribute generation</issue_title>
<issue_description>@Dreamescaper Are you wllling to add the Embed api call into your attribute generation? Its a one line add..

https://andrewlock.net/exploring-dotnet-10-preview-features-4-solving-the-source-generator-marker-attribute-problem-in-dotnet-10/

This would make this source generator require dotnet 9 or later...This would eliminate the warnings and such that are currently generated in my situation.</issue_description>

Comments on the Issue (you are @copilot in this section)

@Dreamescaper I'll have to investigate backward compatibility question first, but generally yes

✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

…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 AI changed the title [WIP] Add embed API call to attribute generation Use Embedded API for generator marker attribute Mar 20, 2026
Copilot AI requested a review from Dreamescaper March 20, 2026 12:16
@Dreamescaper
Copy link
Owner

@copilot
How does 4.14.0 bump affects backward-compatibility? Which compiler versions are not supported anymore?

Copy link
Contributor Author

Copilot AI commented Mar 20, 2026

@copilot
How does 4.14.0 bump affects backward-compatibility? Which compiler versions are not supported anymore?

The bump from 4.8.0 → 4.14.0 maps directly to Visual Studio / compiler host versions:

Roslyn version Visual Studio Approx. release
4.8.0 (previous) VS 2022 17.8 Nov 2023
4.14.0 (new) VS 2022 17.14 Jun 2025

Because source generators run inside the compiler process (the compiler provides the Roslyn runtime — the PrivateAssets="all" DLL is not shipped with the package), the consumer's compiler must have AddEmbeddedAttributeDefinition() available. If their SDK ships Roslyn < 4.14.0 (i.e. VS 2022 < 17.14 or a .NET SDK older than what was released around mid-2025), the generator will fail to load (CS8032) when it tries to call the missing method.

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 [Conditional("CODE_ANALYSIS")] approach is the safe fallback and no version bump is needed. I can revert the change if you decide the compatibility cost is too high.

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:

  • learn.microsoft.com
    • Triggering command: /home/REDACTED/work/_temp/ghcca-node/node/bin/node /home/REDACTED/work/_temp/ghcca-node/node/bin/node --enable-source-maps /home/REDACTED/work/_temp/copilot-developer-action-main/dist/index.js (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

@Dreamescaper
Copy link
Owner

@copilot
We need to preserve backward compatibility for now.
AddEmbeddedAttributeDefinition simply adds an attribute, nothing too fancy. I suggest to implement it as polyfill extension method, without Roslyn version bump.

Here's original AddEmbeddedAttributeDefinition implementation in Roslyn:
https://github.com/dotnet/roslyn/pull/76583/changes

…Roslyn version to 4.8.0

Co-authored-by: Dreamescaper <17177729+Dreamescaper@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Mar 20, 2026

@copilot
We need to preserve backward compatibility for now.
AddEmbeddedAttributeDefinition simply adds an attribute, nothing too fancy. I suggest to implement it as polyfill ext...

Implemented in d84376b. Microsoft.CodeAnalysis.CSharp is back to 4.8.0 in both projects. A new polyfill extension method on IncrementalGeneratorPostInitializationContext in Extensions/IncrementalGeneratorPostInitializationContextExtensions.cs emits the same Microsoft.CodeAnalysis.EmbeddedAttribute source as the built-in API:

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 #if guard needed.

@Dreamescaper Dreamescaper marked this pull request as ready for review March 20, 2026 12:50
@Dreamescaper Dreamescaper merged commit 970e164 into main Mar 20, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Embed api call and update attribute generation

2 participants