Skip to content

Conversation

@thomhurst
Copy link
Owner

Fixes #4583

Problem

[Before(TestSession)] hooks in library projects weren't executing. Library projects that reference TUnit.Core don't have source generation active, so they don't get generated ModuleInitializers to register their hooks.

Solution

Reference a public type from each loaded library assembly using typeof(), which triggers the assembly's module constructor to run and register hooks.

Changes

  • InfrastructureGenerator.cs:
    • Extract first public type from each assembly at compile time
    • Generate _ = typeof(Fully.Qualified.TypeName); instead of Assembly.Load()
  • AssemblyInfoModel.cs: Store fully qualified type names instead of assembly names

Why This Works

Referencing a type via typeof() causes:

  1. The assembly containing that type to load
  2. The assembly's module constructor to execute
  3. Hooks to be registered automatically

This approach is cleaner than explicitly calling reflection-based hook discovery and avoids making internal APIs public.

Testing

Verified with the reproduction repository from #4583:

  • Before fix: Hook never executed ("BeforeTestSession is not run!")
  • After fix: Hook executes correctly (fails on Docker connection, proving hook runs)

@claude
Copy link
Contributor

claude bot commented Jan 30, 2026

Code review

No issues found. Checked for bugs and CLAUDE.md compliance.

@thomhurst thomhurst force-pushed the fix-4583-library-hooks branch from 782bc46 to b26d4f1 Compare January 30, 2026 14:25
@thomhurst thomhurst force-pushed the fix-4583-library-hooks branch from b26d4f1 to d004089 Compare January 30, 2026 14:46
Reset branch to main and reapply core changes only:
- Changed from Assembly.Load() to typeof() references for library assemblies
- Renamed AssembliesToLoad to TypesToReference in AssemblyInfoModel
- Removed problematic TypeExtensions.cs changes that caused build errors

This approach triggers module constructors without the complexity of
assembly name resolution and avoids generic type parameter qualification issues.
- Prefer non-generic public types to avoid typeof() formatting complexity
- Fallback to generic types with proper open generic syntax (<>, <,>, etc.)
- Revert global.json SDK version to 10.0.102

This prevents compilation errors when the first public type in a library
is generic, which would previously generate invalid typeof(Foo<T>) where
T is not in scope.
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.

[Bug]: The method marked with [Before (TestSession)] still does not run

2 participants