Skip to content

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Oct 8, 2025

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:

[TestInitialize]
public void TestSetup(TestContext tc)
{
    SomeCode();

    // Some comments;
}

The fixer would remove the parameter but also remove the comment, producing:

[TestInitialize]
public void TestSetup()
{
    SomeCode();
}

Root Cause

The FixtureMethodFixer helper class was using SyntaxGenerator.MethodDeclaration() to create a completely new method from scratch. This approach extracted only the executable statements from the original method using GetStatements(), 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:

  1. Starts with the original MethodDeclarationSyntax
  2. Applies only the necessary changes using:
    • .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 needed
  3. Preserves everything else, including comments, whitespace, and formatting

This follows Roslyn best practices for syntax transformations and ensures minimal, targeted changes.

Changes

  • Modified: src/Analyzers/MSTest.Analyzers.CodeFixes/Helpers/FixtureMethodFixer.cs

    • Rewrote FixSignatureAsync() to preserve method body and trivia
    • Updated helper methods to return proper Roslyn syntax types instead of generic SyntaxNode
    • Added defensive type check for MethodDeclarationSyntax
  • Added Tests:

    • TestInitializeShouldBeValidAnalyzerTests.cs - Verify comments preserved for MSTest0008
    • TestCleanupShouldBeValidAnalyzerTests.cs - Verify comments preserved for MSTest0009
    • ClassInitializeShouldBeValidAnalyzerTests.cs - Verify comments preserved for MSTest0006

Impact

This fix improves all six MSTest fixture method analyzers that use FixtureMethodFixer:

  • MSTest0001 (AssemblyInitialize)
  • MSTest0002 (AssemblyCleanup)
  • MSTest0006 (ClassInitialize)
  • MSTest0007 (ClassCleanup)
  • MSTest0008 (TestInitialize) ← Original reported issue
  • MSTest0009 (TestCleanup)

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
    • Triggering command: /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

This section details on the original issue you should resolve

<issue_title>MSTest0008 fixer seems to be suggesting to remove the comments</issue_title>
<issue_description>Let's take the following example. The code fixer suggests to remove the comments as well.

[TestInitialize]
public void TestSetup(TestContext tc)
{
    SomeCode();

    // Some comments;
}

Image</issue_description>

Comments on the Issue (you are @copilot in this section)

Fixes #6697

✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI and others added 2 commits October 8, 2025 11:59
Co-authored-by: Evangelink <11340282+Evangelink@users.noreply.github.com>
…nitialize fixers

Co-authored-by: Evangelink <11340282+Evangelink@users.noreply.github.com>
@Copilot Copilot AI changed the title [WIP] Fix MSTest0008 fixer to retain comments Fix MSTest fixture method fixers removing comments from method bodies Oct 8, 2025
@Copilot Copilot AI requested a review from Evangelink October 8, 2025 12:08
Copilot finished work on behalf of Evangelink October 8, 2025 12:08

using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove using.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MSTest0008 fixer seems to be suggesting to remove the comments

2 participants