Fix MSTest fixture method fixers removing comments from method bodies #6700
+223
−34
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.
Issue
The MSTest fixture method code fixers (MSTest0001, MSTest0002, MSTest0006, MSTest0007, MSTest0008, MSTest0009) were incorrectly removing comments from method bodies when applying fixes. This was most noticeable with MSTest0008 (TestInitialize) as reported in the issue.
For example, when fixing a TestInitialize method signature:
The fixer would remove the parameter but also remove the comment, producing:
Root Cause
The
FixtureMethodFixer
helper class was usingSyntaxGenerator.MethodDeclaration()
to create a completely new method from scratch. This approach extracted only the executable statements from the original method usingGetStatements()
, which discarded all trivia including comments, whitespace, and formatting.Solution
Modified
FixtureMethodFixer.cs
to use Roslyn's.WithXXX()
pattern for incremental syntax transformations. Instead of creating a new method, the fixer now:MethodDeclarationSyntax
.WithParameterList()
- Update/remove parameters.WithReturnType()
- Fix return type (void/Task/ValueTask).WithModifiers()
- Fix accessibility and static modifier.WithTypeParameterList(null)
- Remove generic type parameters.WithBody()
- Only filter out return statements when neededThis follows Roslyn best practices for syntax transformations and ensures minimal, targeted changes.
Changes
Modified:
src/Analyzers/MSTest.Analyzers.CodeFixes/Helpers/FixtureMethodFixer.cs
FixSignatureAsync()
to preserve method body and triviaSyntaxNode
MethodDeclarationSyntax
Added Tests:
TestInitializeShouldBeValidAnalyzerTests.cs
- Verify comments preserved for MSTest0008TestCleanupShouldBeValidAnalyzerTests.cs
- Verify comments preserved for MSTest0009ClassInitializeShouldBeValidAnalyzerTests.cs
- Verify comments preserved for MSTest0006Impact
This fix improves all six MSTest fixture method analyzers that use
FixtureMethodFixer
:Fixes #[issue_number]
Warning
Firewall rules blocked me from connecting to one or more addresses (expand for details)
I tried to connect to the following addresses, but was blocked by firewall rules:
u6ovsblobprodcus377.vsblob.vsassets.io
/home/REDACTED/work/testfx/testfx/.dotnet/dotnet msbuild /m /nologo /clp:Summary /v:minimal /nr:true /warnaserror /p:TreatWarningsAsErrors=true /p:ContinuousIntegrationBuild=false /home/REDACTED/work/testfx/testfx/artifacts/toolset/restore.proj /t:__WriteToolsetLocation /clp:ErrorsOnly;NoSummary /p:__ToolsetLocationOutputFile=/home/REDACTED/work/testfx/testfx/artifacts/toolset/11.0.0-beta.25507.4.txt
(dns block)If you need me to access, download, or install something from one of these locations, you can either:
Original prompt
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.