Skip to content

Let two generators built on StuntGenerator run on one compilation - #359

Merged
kzu merged 1 commit into
mainfrom
fix/two-generators
Nov 29, 2026
Merged

kzu merged 1 commit into
mainfrom
fix/two-generators

Conversation

@kzu

@kzu kzu commented Nov 29, 2026

Copy link
Copy Markdown
Member

A project that uses Stunt.Of<T>() and a generator that configures a StuntGenerator of its own (Moq's MockGenerator) did not compile with 1.0.0-beta.4:

  • CS0111: both generators emitted Stunts.Generated.StuntRegistrations.Initialize.
  • CS0101 and CS0579: both emitted the ModuleInitializerAttribute polyfill where the framework lacks it.
  • Both shared the registry key of CompiledStuntFactory, so a stunt and a mock of the same type in one assembly would replace each other.
  • [assembly: Stunt<T>] made both generators generate a type.

What changes

The generator attribute tells generators apart.

  • A generator with its own attribute appends a suffix made from the attribute's full name (_Moq_MockGeneratorAttribute) to StuntRegistrations, StuntDefinitions and the __RegisterStunt forwarders. The names of the default generator do not change.
  • It passes that name to new CompiledStuntFactory.Register overloads, and the registry key includes it. A runtime creates its proxies with new CompiledStuntFactory(typeof(MockGeneratorAttribute)); the parameterless constructor creates plain stunts only.
  • The polyfill is a partial class without attributes, so the declarations of several generators merge.
  • [assembly: Stunt<T>] asks for a stunt, so a generator with its own generator attribute no longer reads it. It reads the attribute set with WithRegistrationAttribute, or none.

Breaking for extenders

  • A runtime built on CompiledStuntFactory for a generator with its own generator attribute must pass that attribute to the constructor, or its proxies are not found. Moq's StaticMockFactory needs this change.
  • StuntClosure.DefinitionsTypeName and StuntClosure.DefinitionMethods are instance members now.

Tests

StuntGeneratorTests: RunsTwoGeneratorsOverOneCompilation, ClosesWrappersOfAnotherGeneratorFromReferencedAssemblies and ModuleInitializerPolyfillsOfTwoGeneratorsMerge. The unit test project passes (407 tests).

🤖 Generated with Claude Code

This pull request was created by CodeAlta.

A project that uses Stunt.Of and a generator that configures a
StuntGenerator of its own (Moq's MockGenerator) did not compile: both
emitted Stunts.Generated.StuntRegistrations.Initialize (CS0111) and the
ModuleInitializerAttribute polyfill (CS0101, CS0579), and they shared
the registry key of CompiledStuntFactory, so a stunt and a mock of the
same type replaced each other.

The generator attribute now tells generators apart:

- A generator with its own attribute appends a suffix made from the
  attribute's full name to StuntRegistrations, StuntDefinitions and the
  __RegisterStunt forwarders. The names of the default generator do not
  change. StuntClosure.DefinitionsTypeName and DefinitionMethods become
  instance members, so each closure reads its own definitions.
- It passes that name to new CompiledStuntFactory.Register overloads,
  and the registry key includes it. A runtime creates its proxies with
  new CompiledStuntFactory(typeof(MockGeneratorAttribute)); the
  parameterless constructor creates plain stunts only.
- The polyfill is a partial class without attributes, so the
  declarations of several generators merge.
- [assembly: Stunt<T>] asks for a stunt, so a generator with its own
  generator attribute no longer reads it: it reads the attribute set
  with WithRegistrationAttribute, or none.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Co-authored-by: CodeAlta <noreply@codealta.io>
@kzu kzu added the bug Something isn't working label Nov 29, 2026
@kzu
kzu enabled auto-merge (rebase) November 29, 2026 05:15
@kzu
kzu merged commit fb11b45 into main Nov 29, 2026
6 checks passed
@kzu
kzu deleted the fix/two-generators branch November 29, 2026 05:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant