Fix MatrixDataSource failing with ClassConstructor#4838
Merged
Conversation
When a test class uses [ClassConstructor<T>] for DI and has [MatrixDataSource] tests, the matrix expansion fails with IndexOutOfRangeException because the early instance creation bypasses ClassConstructorHelper and calls InstanceFactory directly, which tries to index into an empty args array. Fixes both engine and source generator: - Engine: CreateInstanceForMethodDataSources now checks ClassConstructor first - Engine: Consolidated duplicated instance creation in BuildTestsFromMetadataAsync - Source gen: InstanceFactoryGenerator.HasClassConstructorAttribute now checks assembly-level [assembly: ClassConstructor<T>] in addition to class attributes - Source gen: All inline InstanceFactory paths in TestMetadataGenerator now guard against ClassConstructor, emitting the throw-stub instead of constructor code Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Contributor
Code reviewNo issues found. Checked for bugs and CLAUDE.md compliance. This is a well-structured fix that properly addresses the dual-mode compatibility requirements. The changes correctly implement ClassConstructor detection in both the source generator (with throw-stubs) and the engine (with proper fallback logic), ensuring that |
This was referenced Feb 17, 2026
This was referenced Feb 18, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
IndexOutOfRangeExceptionwhen a test class uses[ClassConstructor<T>]for DI and has[MatrixDataSource]tests ([BUG] Test Classes With Injected Dependencies Cause Matrix Expansion To Fail #4836)ClassConstructorHelperbefore falling back toInstanceFactory[assembly: ClassConstructor<T>]and guards all inlineInstanceFactorycode paths with the ClassConstructor throw-stubTest plan
TUnit.Enginebuilds successfully (0 errors, 0 warnings)TUnit.Core.SourceGeneratorbuilds successfully (0 errors, 0 warnings).received.txtchanges)[ClassConstructor<T>]+[MatrixDataSource]test method🤖 Generated with Claude Code