Skip to content

Conversation

@thomhurst
Copy link
Owner

Summary

Fixes #4583 - [Before(TestSession)] and [After(TestSession)] hooks defined in referenced library projects were not executing.

Problem

The InfrastructureGenerator generates code to load referenced assemblies early (to trigger their module initializers which register hooks). However, the HasPhysicalLocation check was too restrictive:

return correspondingReference is PortableExecutableReference { FilePath: not null and not "" };

This only accepted PortableExecutableReference objects. During IDE design-time builds, project-to-project references are represented as CompilationReference objects. The incremental generator may cache results from these builds, resulting in missing Assembly.Load() calls for library projects.

Solution

  • Renamed HasPhysicalLocation to IsLoadableAtRuntime to better reflect its purpose
  • Simplified the logic to accept any assembly with a corresponding reference in the compilation
  • Any referenced assembly will be available at runtime (either as a compiled DLL or NuGet package)

Test Plan

  • Added LibraryTestSessionHooks class to TUnit.TestProject.Library with [Before(TestSession)] and [After(TestSession)] hooks
  • Added LibraryTestSessionHookTests in TUnit.TestProject to verify the library's hooks execute
  • Verified test passes with dotnet run -- --treenode-filter "/*/*/LibraryTestSessionHookTests/*"

The InfrastructureGenerator was not including project-to-project
references (CompilationReference) when generating Assembly.Load() calls.
During IDE design-time builds, project references are represented as
CompilationReference objects rather than PortableExecutableReference,
causing the library's module initializers (which register hooks) to not
be triggered early enough.

Renamed HasPhysicalLocation to IsLoadableAtRuntime and simplified the
logic to accept any assembly with a corresponding reference, since all
referenced assemblies will be available at runtime.

Fixes #4583
@thomhurst thomhurst merged commit 4dae69d into main Jan 29, 2026
12 of 13 checks passed
@thomhurst thomhurst deleted the fix/library-session-hooks-4583 branch January 29, 2026 15:27
This was referenced Jan 29, 2026
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