Skip to content

Conversation

@thomhurst
Copy link
Owner

Summary

Fixes #4602

When user namespaces contain "TUnit" as a segment (e.g., MySolution.TUnit.Tests or Reqnroll.Retry.TUnit), the generated code would fail to compile because C# namespace resolution walks up the namespace hierarchy before checking global scope.

This PR ensures all type references in source-generated code use the global:: prefix:

  • TUnit.Core.Helpers.CastHelper.Cast<...>global::TUnit.Core.Helpers.CastHelper.Cast<...>
  • System.Array.Empty<...>()global::System.Array.Empty<...>()
  • System.Activator.CreateInstance<...>()global::System.Activator.CreateInstance<...>()
  • System.Threading.CancellationToken.Noneglobal::System.Threading.CancellationToken.None
  • System.Collections.Generic.Dictionary<System.Type, ...>global::System.Collections.Generic.Dictionary<global::System.Type, ...>
  • System.Type.EmptyTypesglobal::System.Type.EmptyTypes
  • System.Type[]global::System.Type[]
  • System.Attribute[]global::System.Attribute[]

Files Changed

File Changes
TupleArgumentHelper.cs 12× CastHelper references
InstanceFactoryGenerator.cs 1× CastHelper reference
TestMetadataGenerator.cs 4× CastHelper, 1× CancellationToken.None, 1× Activator.CreateInstance
HookMetadataGenerator.cs 1× System.Array.Empty
StaticPropertyInitializationGenerator.cs 2× System.Array.Empty
CodeGenerationHelpers.cs 4× System.Array.Empty, 1× System.Type.EmptyTypes, 3× Dictionary, 1× Type[], 1× Attribute[]

New Tests

Added ConflictingNamespaceTests.cs with 6 tests that verify generated code compiles correctly when user namespaces conflict with TUnit's namespace:

  • BasicTest_WithConflictingNamespace - Tests with MySolution.TUnit.Core
  • DataDrivenTest_WithConflictingNamespace - Tests with Reqnroll.Retry.TUnit.Core.Helpers
  • HooksTest_WithConflictingNamespace - Tests with MyCompany.TUnit.Core.Hooks
  • MethodDataSource_WithConflictingNamespace - Tests with fake CastHelper in conflicting namespace
  • MatrixTest_WithConflictingNamespace - Tests with conflicting TestMetadata/ParameterMetadata
  • TupleDataSource_WithConflictingNamespace - Tests with fake CastHelper/DataSourceHelpers

Test plan

  • All existing source generator tests pass
  • New ConflictingNamespaceTests verify the fix works with conflicting namespaces
  • Generated code compiles successfully when user namespaces contain "TUnit" as a segment

… in generated code

Fixes #4602

When user namespaces contain "TUnit" as a segment (e.g., MySolution.TUnit.Tests
or Reqnroll.Retry.TUnit), namespace resolution would fail because C# looks up
the namespace hierarchy before checking global scope.

This change ensures all type references in source-generated code use the
global:: prefix:

- TUnit.Core.Helpers.CastHelper.Cast<...>
- System.Array.Empty<...>()
- System.Activator.CreateInstance<...>()
- System.Threading.CancellationToken.None
- System.Collections.Generic.Dictionary<System.Type, ...>
- System.Type.EmptyTypes
- System.Type[]
- System.Attribute[]

Also adds ConflictingNamespaceTests to verify generated code compiles correctly
with conflicting user namespaces.
@thomhurst thomhurst force-pushed the fix/global-prefix-for-source-generator-types branch from fb0dfe4 to 159780f Compare January 29, 2026 17:50
@claude
Copy link
Contributor

claude bot commented Jan 29, 2026

Code review

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

@thomhurst thomhurst merged commit f99bb2d into main Jan 29, 2026
12 of 13 checks passed
@thomhurst thomhurst deleted the fix/global-prefix-for-source-generator-types branch January 29, 2026 19:47
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]: Missing global:: prefix on TUnit.Core type references causes namespace resolution failures.

2 participants