Skip to content

Fix double registration in MSTest.AotReflection.SourceGeneration PoC and make it packable (non-shipping)#9338

Merged
Evangelink merged 2 commits into
microsoft:mainfrom
Evangelink:evangelink/fix-aot-poc-double-registration
Jun 22, 2026
Merged

Fix double registration in MSTest.AotReflection.SourceGeneration PoC and make it packable (non-shipping)#9338
Evangelink merged 2 commits into
microsoft:mainfrom
Evangelink:evangelink/fix-aot-poc-double-registration

Conversation

@Evangelink

Copy link
Copy Markdown
Member

What

Two fixes to the experimental MSTest.AotReflection.SourceGeneration PoC:

  1. Fix double test registration. The PoC .csproj <Compile Include>-ed MSTest.SourceGeneration's ReflectionMetadataGenerator.cs — which is itself decorated with [Generator]. As a result, the PoC analyzer assembly ran two source generators, and each emitted a [ModuleInitializer] that called ReflectionMetadataHook.Register(...) for the same assembly:

    • MSTestSourceGeneratedReflectionMetadata (shipping generator, 3-arg Register)
    • MSTestAotSourceGeneratedReflectionMetadata (PoC generator, 5-arg Register)

    CompositeSourceGeneratedReflectionDataProvider appends both providers with no de-duplication, so every test class was discovered twice. A trivial 2-test project reported 4 passing tests.

    The PoC's own RuntimeRegistrationEmitter already emits a complete and strictly richer registration (5-arg overload carrying materialized type/assembly attributes) plus [DynamicDependency] rooting, so the shared shipping generator was redundant. The three shared Compile includes (ReflectionMetadataGenerator.cs, ReflectionMetadataEmitter.cs, TestAssemblyMetadata.cs) — none of which were referenced by any PoC source — are removed. Only one [ModuleInitializer] is emitted now.

  2. Make the project packable as a non-shipping package (IsPackable=true, IsShipping=false) so it can be consumed locally for experimentation. The generator dll is packed under analyzers/dotnet/cs. The package lands in artifacts/packages/<config>/NonShipping and is not published.

Verification

  • MSTest.AotReflection.SourceGeneration.UnitTests: 66/66 pass.
  • Stood up a minimal consuming project that references the generator as an analyzer:
    • Before: a 2-test project reported total: 4 (each test discovered twice).
    • After: reports total: 2, and only the PoC's generated files are emitted (the duplicate Sample.MSTestReflectionMetadata.g.cs from the shipping generator is gone).
  • dotnet pack produces MSTest.AotReflection.SourceGeneration.<ver>.nupkg in NonShipping, with the analyzer dll at analyzers/dotnet/cs/.

Notes

This remains a PoC tracked by #1837. The reflection-free registry it emits is still not consumed by the adapter (runtime discovery continues to go through ReflectionMetadataHook + [DynamicDependency]), which is out of scope for this change.

…and make it packable (non-shipping)

The PoC analyzer assembly compiled in MSTest.SourceGeneration's ReflectionMetadataGenerator (which is itself [Generator]), so referencing the PoC ran two source generators. Each emitted a [ModuleInitializer] calling ReflectionMetadataHook.Register for the same assembly, registering it twice with the composite provider and doubling discovered test counts (a 2-test project reported 4).

The PoC's own RuntimeRegistrationEmitter already emits a complete, richer (5-arg, attribute-carrying) registration plus DynamicDependency rooting, so the shared shipping generator was redundant. Remove the three shared Compile includes (generator, emitter, model) that nothing in the PoC referenced; only one ModuleInitializer is emitted now and a sample reports the correct test count.

Also make the project packable as a non-shipping package (IsPackable=true, IsShipping=false) so it can be consumed locally for experimentation; the generator dll is packed under analyzers/dotnet/cs.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings June 22, 2026 12:00

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Note

Copilot couldn't run its full agentic review because no GitHub Actions runner was available. Make sure your repository has a runner available to run Copilot's review, or add a copilot-setup-steps.yml file specifying one with the runs-on attribute. See the docs for more details.

This PR fixes duplicate MSTest registration in the experimental MSTest.AotReflection.SourceGeneration generator by removing shared “shipping” generator sources, and makes the PoC packable as a non-shipping NuGet analyzer for local experimentation.

Changes:

  • Remove shared MSTest.SourceGeneration generator/emitters/models Compile Includes to prevent double [ModuleInitializer] registration.
  • Enable packing (IsPackable=true, IsShipping=false) and add a pack rule to place the analyzer DLL under analyzers/dotnet/cs.
  • Update package description metadata to a dedicated PackageDescription.
Show a summary per file
File Description
src/Analyzers/MSTest.AotReflection.SourceGeneration/MSTest.AotReflection.SourceGeneration.csproj Removes shared generator sources to prevent double registration; enables non-shipping packing and adds analyzer packing entry.

Copilot's findings

  • Files reviewed: 1/1 changed files
  • Comments generated: 3

@Evangelink Evangelink added the state/needs-review Awaiting review from the team. label Jun 22, 2026
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@Evangelink
Evangelink enabled auto-merge (squash) June 22, 2026 12:32
@Evangelink
Evangelink disabled auto-merge June 22, 2026 12:36
@Evangelink
Evangelink merged commit c6839b9 into microsoft:main Jun 22, 2026
51 of 58 checks passed
@Evangelink
Evangelink deleted the evangelink/fix-aot-poc-double-registration branch June 22, 2026 12:36
Evangelink added a commit that referenced this pull request Jun 22, 2026
…ounts

After the build progresses past the restore/compile fixes, verify-nupkgs.ps1
fails because MSTest.AotReflection.SourceGeneration (made packable as a
non-shipping PoC in #9338) is a 4.3.0 package not present in the expected
file-count table, so it is validated with an empty expected value. Add it with
its actual content count (6: [Content_Types].xml, Icon.png, .nuspec, the
analyzer dll, .psmdcp, .rels).

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

state/needs-review Awaiting review from the team.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants