Bugfix: What happens when @Optional() NestJS services are promoted t...

Documentation for Sessions

🐛 Bugfix: What happens when @Optional() NestJS services are promoted t...

Date: 2026-03-27
Time: 18:54:55 UTC
Session: gspace1-276.13
Agent: backend
Confidence: 95%
Status: ✅ Documented


📋 Decision Context

Question:

What happens when @Optional() NestJS services are promoted to required injection without verifying module imports?

Task: Wire 8: Guarantee @Optional() services


🎯 Decision Made

Chosen:

Always verify the provider's source module is imported by the consumer's module before removing @Optional(). AwarenessModule was missing from EngineModule — the app would crash at bootstrap.

Options Considered:

  1. Trust that modules are already imported (failed)
  2. Add runtime onModuleInit check (insufficient — crashes before init)
  3. Verify module import chain before removing @Optional() (chosen)

💭 Reasoning

Root cause: @Optional() services silently resolve to null when their module isn't imported. The code's null guards (ternary checks, ?. operators) were load-bearing — they weren't defensive coding, they were compensating for a missing module import. TypeScript compilation does NOT catch NestJS DI scope errors — those only manifest at runtime. The plan assumed AwarenessModule was imported by EngineModule; it wasn't. Code review caught this before runtime. Early warning: any @Optional() → required promotion should trigger a grep for the service's module in the consumer module's imports array.


📁 Files Involved


🔗 Related


Auto-generated by MCP Smart Consolidation