-
-
Notifications
You must be signed in to change notification settings - Fork 110
Description
Description
When running the TUXU0001 code fixer on a project that targets multiple frameworks (e.g., net472;net8.0), the code fixer crashes with a System.ArgumentException: Changes must be within bounds of SourceText error.
Steps to Reproduce
- Create a test project with multiple target frameworks:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net472;net8.0</TargetFrameworks>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="xunit" Version="2.9.3" />
<PackageReference Include="TUnit" Version="1.11.*" />
</ItemGroup>
</Project>- Add test files with xUnit attributes
- Run the code fixer:
dotnet format analyzers --severity warn --diagnostics TUXU0001
Expected Behavior
The code fixer should handle multi-targeting projects and apply changes correctly to source files.
Actual Behavior
The code fixer crashes with the following exception:
Unhandled exception: System.AggregateException: One or more errors occurred. (Changes must be within bounds of SourceText (Parameter 'changes'))
---> System.ArgumentException: Changes must be within bounds of SourceText (Parameter 'changes')
at Microsoft.CodeAnalysis.Text.SourceText.WithChanges(IEnumerable`1 changes)
at Microsoft.CodeAnalysis.LinkedFileMergeConflictCommentAdditionService.GetCommentChangesForDocument(IEnumerable`1 partitionedChanges, String projectName, SourceText oldDocumentText)
at Microsoft.CodeAnalysis.LinkedFileMergeConflictCommentAdditionService.CreateEdits(SourceText originalSourceText, ArrayBuilder`1 unmergedChanges)
at Microsoft.CodeAnalysis.LinkedFileDiffMergingSession.MergeLinkedDocumentGroupAsync(ArrayBuilder`1 newDocumentsAndHashes, CancellationToken cancellationToken)
at Microsoft.CodeAnalysis.LinkedFileDiffMergingSession.MergeDiffsAsync(CancellationToken cancellationToken)
at Microsoft.CodeAnalysis.Solution.WithMergedLinkedFileChangesAsync(Solution oldSolution, Nullable`1 solutionChanges, CancellationToken cancellationToken)
at Microsoft.CodeAnalysis.Workspace.TryApplyChanges(Solution newSolution, IProgress`1 progressTracker)
at Microsoft.CodeAnalysis.MSBuild.MSBuildWorkspace.TryApplyChanges(Solution newSolution, IProgress`1 progressTracker)
at Microsoft.CodeAnalysis.MSBuild.MSBuildWorkspace.TryApplyChanges(Solution newSolution)
at Microsoft.CodeAnalysis.Tools.CodeFormatter.FormatWorkspaceAsync(...)
Workaround
Temporarily change the project to single-target framework before running the code fixer:
<TargetFramework>net8.0</TargetFramework>
<!-- <TargetFrameworks>net472;net8.0</TargetFrameworks> -->Root Cause Analysis
The issue appears to be related to how the code fixer handles "linked files" - files that are shared across multiple target frameworks. When the code fixer tries to apply changes, it attempts to merge changes across the different framework compilations, but the change offsets don't align properly.
This may be related to how the TUnit code fixer produces text changes that aren't accounting for the fact that the same file may have different offsets when compiled under different frameworks.
Environment
- TUnit Version: 1.11.64
- .NET SDK: 10.0.102
- OS: Windows
Suggested Fix
The code fixer should either:
- Ensure text changes are produced consistently regardless of target framework
- Add special handling for multi-targeting projects
- Document the limitation and workaround in the migration guide
Tests to Add
Please add tests for:
- Multi-targeting projects (
net472;net8.0) - Projects with conditional compilation symbols
- Projects with framework-specific code in
#ifdirectives