Skip to content

Fail fast when a mixed-lifetime IEnumerable<T> singleton mirror is missing (#381)#382

Merged
jeremydmiller merged 1 commit into
mainfrom
fix-381-enumerable-singleton-mirror-failfast
May 27, 2026
Merged

Fail fast when a mixed-lifetime IEnumerable<T> singleton mirror is missing (#381)#382
jeremydmiller merged 1 commit into
mainfrom
fix-381-enumerable-singleton-mirror-failfast

Conversation

@jeremydmiller
Copy link
Copy Markdown
Member

Summary

Fixes #381 — the mixed-lifetime IEnumerable<T> codegen routes each singleton element through a keyed "mirror" registration created by AddJasperFxEnumerableSingletonSupport(). If a consumer never calls that method (or registers a mixed-lifetime family after it runs), the mirror doesn't exist and the singleton element was injected as null with no error — the enumerable silently contained a null that surfaced far away as an NRE inside a handler (the footgun behind #380).

This implements option 2 from the issue: a small guard in the generated enumerable code.

What changed

  • CreateArrayFrame.GenerateCode now emits a null guard before the new T[]{…} array literal for any singleton element sourced from a keyed mirror. When the mirror is absent, the generated code throws an actionable InvalidOperationException instead of yielding a null element:

    Keyed mirror jasperfx-enumerable-singleton-0 for the singleton element of IEnumerable<T> was not found. Call AddJasperFxEnumerableSingletonSupport() once during DI setup, before BuildServiceProvider().

  • EnumerableSingletons gains a KeyPrefix constant, an IsMirrorKey() detector, and a MissingMirrorMessage() builder.

The guard is robust regardless of how the constructor parameter was populated (MS DI's keyed resolution, or JasperFx's QuickBuild optional GetKeyedService path that produced the silent null).

Tests

Added to inline_enumerable_with_mixed_lifetimes:

  • missing_mirror_emits_a_failfast_guard_in_the_generated_code — asserts the guard text
  • missing_mirror_throws_actionable_error_instead_of_injecting_null — exercises the silent-null path (optional keyed resolution returns null) and asserts the actionable throw
  • registered_mirror_does_not_trip_the_guard — no-regression case with support registered

Full CodegenTests (359) and CoreTests (439) pass on net9.0 and net10.0.

Version

Bumps JasperFxVersion 2.1.3 → 2.1.4.

🤖 Generated with Claude Code

…ssing (#381)

The mixed-lifetime IEnumerable<T> codegen routes each singleton element through
a keyed "mirror" registration created by AddJasperFxEnumerableSingletonSupport().
If a consumer never calls that method (or registers a mixed-lifetime family after
it runs), the mirror does not exist and the singleton element was injected as
null with no error — the enumerable silently contained a null that surfaced far
away as an NRE (the footgun behind #380).

CreateArrayFrame now emits a null guard before constructing the element array for
any singleton element sourced from a keyed mirror. When the mirror is absent the
generated code throws an actionable InvalidOperationException naming the missing
key and pointing at AddJasperFxEnumerableSingletonSupport(), instead of yielding
a null element.

- EnumerableSingletons: KeyPrefix constant, IsMirrorKey() detector, and
  MissingMirrorMessage() builder.
- CreateArrayFrame.GenerateCode: emit the guard for mirror-sourced singletons.
- Tests cover the generated guard text, the runtime throw under optional keyed
  resolution (the silent-null path), and a no-regression case with support
  registered.

Bumps JasperFxVersion 2.1.3 -> 2.1.4.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@jeremydmiller jeremydmiller merged commit a5e3f0d into main May 27, 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.

Fail-fast when a mixed-lifetime IEnumerable<T> singleton mirror is missing (silent null today)

1 participant