fix: actually load assemblies in InfrastructureGenerator#4550
fix: actually load assemblies in InfrastructureGenerator#4550
Conversation
SummaryFixes hooks in library assemblies by directly loading assemblies in the ModuleInitializer instead of queueing them to an unprocessed queue. Critical IssuesNone found ✅ AnalysisThe fix correctly addresses the root cause identified in #4541: Problem: Hooks defined in referenced library assemblies weren't executing because:
Solution: Changed to directly load assemblies: _ = global::System.Reflection.Assembly.Load("{assemblyName}");This triggers ModuleInitializers immediately, allowing hooks to register. TUnit Rules Compliance:
Testing:
Verdict✅ APPROVE - Clean fix for a critical bug affecting library-based test organization. |
## Problem Hooks in referenced library assemblies were not executing because the InfrastructureGenerator queued assembly loaders but never invoked them. ## Root Cause The InfrastructureGenerator called `SourceRegistrar.RegisterAssembly()` which added loaders to `Sources.AssemblyLoaders` queue, but this queue was never processed. Assemblies were never loaded, so their ModuleInitializers (which register hooks) never executed. ## Solution Changed the generated code to directly load assemblies via `Assembly.Load()` in the ModuleInitializer, instead of queuing loaders that are never invoked. This ensures: 1. Referenced assemblies are loaded early during module initialization 2. Their ModuleInitializers execute and register hooks globally 3. Hooks from library assemblies work correctly ## Testing - Added AbstractTestSessionHookTests to verify hooks in abstract base classes work - Verified hooks from TUnit.TestProject.Library execute correctly Fixes #4541 Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2d92e50 to
dedb5ee
Compare
SummaryFixes hooks in library assemblies by directly loading assemblies in ModuleInitializer instead of queueing them to an unprocessed queue. Critical IssuesSilent Exception Swallowing - The try-catch block at InfrastructureGenerator.cs:265-275 silently swallows all exceptions with only a comment. This could hide assembly name typos, missing dependencies, version conflicts, or security issues. Impact: Users won't know why hooks aren't executing if assembly loading fails. Recommendation: Either fail fast with AggregateException or only catch specific expected exceptions like FileNotFoundException or BadImageFormatException. Previous Review StatusThe previous review approved the fix, correctly identifying that it solves the root cause. This concern about exception handling is an additional consideration not covered in that review. VerdictREQUEST CHANGES - The fix is correct, but silent exception swallowing could mask real issues. |
Problem
Hooks (
[Before(TestSession)],[After(TestSession)], etc.) in referenced library assemblies were not executing, even though the source generator correctly generated hook registration code with ModuleInitializers.Reported in #4541
Root Cause
The
InfrastructureGeneratorhas a critical bug on line 264:This queues a loader function to
Sources.AssemblyLoaders, but this queue is never processed anywhere in the codebase. The assemblies are never actually loaded, so their ModuleInitializers (which register hooks) never execute.The Flow
[ModuleInitializer]InfrastructureGeneratordetects referenced assemblies with TUnit.CoreRegisterAssembly()but they're never invokedSolution
Changed the generated code to directly load assemblies in the ModuleInitializer:
Now assemblies are loaded immediately during module initialization, triggering their ModuleInitializers which register hooks globally.
Testing
AbstractTestSessionHookTests.csto verify hooks in abstract base classes workTUnit.TestProject.Libraryexecute correctlyImpact
This fixes a long-standing bug where hooks in library projects referenced by test projects would silently fail to execute. Users organizing their test infrastructure into separate libraries can now use hooks as expected.
Closes #4541
🤖 Generated with Claude Code