Skip to content

TUXU0001 code fixer fails with 'Changes must be within bounds of SourceText' on multi-targeting projects #4484

@thomhurst

Description

@thomhurst

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

  1. 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>
  1. Add test files with xUnit attributes
  2. 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:

  1. Ensure text changes are produced consistently regardless of target framework
  2. Add special handling for multi-targeting projects
  3. Document the limitation and workaround in the migration guide

Tests to Add

Please add tests for:

  1. Multi-targeting projects (net472;net8.0)
  2. Projects with conditional compilation symbols
  3. Projects with framework-specific code in #if directives

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions