Skip to content

NUnit Migration: dotnet format crashes on multi-targeted projects #4344

@thomhurst

Description

@thomhurst

Description

When running the TUnit NUnit migration code fixer on projects that target multiple frameworks (e.g., <TargetFrameworks>net8.0;net9.0;net10.0;net462</TargetFrameworks>), the dotnet format analyzers command crashes with an exception related to linked file merging.

Steps to Reproduce

  1. Create a project with NUnit tests that targets multiple frameworks:
    <TargetFrameworks>net8.0;net9.0;net10.0;net462</TargetFrameworks>
  2. Add TUnit packages
  3. Run dotnet format analyzers --severity info --diagnostics TUNU0001

Expected Behavior

The migration should complete successfully for all target frameworks.

Actual Behavior

The command crashes with the following error:

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(...)
   at Microsoft.CodeAnalysis.LinkedFileDiffMergingSession.MergeLinkedDocumentGroupAsync(...)
   ...

The migration process appears to identify files to format and attempts changes, but crashes when trying to merge the changes across multiple target frameworks.

Workaround

Temporarily change the project to a single target framework:

<TargetFramework>net8.0</TargetFramework>
<!-- <TargetFrameworks>net8.0;net9.0;net10.0;net462</TargetFrameworks> -->

Then run the migration, and restore multi-targeting afterward.

Notes

This may be a dotnet format issue rather than a TUnit code fixer issue, but it significantly impacts the migration experience for projects with multiple target frameworks.

The error occurs in Microsoft.CodeAnalysis.LinkedFileMergeConflictCommentAdditionService, suggesting the issue is with how changes are being merged across the same files compiled for different target frameworks.

Possible Solutions

  1. Document the workaround in migration guide
  2. Investigate if the TUnit code fixer can make changes more compatible with linked file scenarios
  3. Report to dotnet/format if this is an upstream issue

Environment

  • TUnit version: 1.9.91
  • .NET version: .NET 8.0 (dotnet format from .NET 10.0.101 SDK)
  • OS: Windows
  • dotnet format version: (from .NET SDK 10.0.101)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions