Skip to content

Incorrect formatting behaviour when using SyntaxFactory.Block #63013

Open

Description

@bjornhellander

Version Used: 4.3.0-2.final (same behaviour with 4.3.0-3, but not with 4.3.0-1)

After updating the c# 11 test project in StyleCop.Analyzers to use a new Roslyn version, three tests (all called TestCodeFixProviderWithAlternateIndentationAsync) failed due to different formatting behaviour when adding braces around code in if statements (using SyntaxFactory.Block).

See DotNetAnalyzers/StyleCopAnalyzers#3535 for updated code.
The call to SyntaxFactory.Block is made here: https://github.com/bjornhellander/StyleCopAnalyzers/blob/feature/sa1206-required/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/LayoutRules/SA1503CodeFixProvider.cs#L72
And here we set indentation size to 1 in the failing test cases: https://github.com/bjornhellander/StyleCopAnalyzers/blob/feature/sa1206-required/StyleCop.Analyzers/StyleCop.Analyzers.Test/Verifiers/StyleCopCodeFixVerifier%602.cs#L120

The three tests fail in slightly different ways, but the fixed code seems to have indentation size 4, instead of the specified and expected 1:

using System.Diagnostics;

public class Foo
{
 public void Bar(int i)
 {
  if (i == 0)
-  {
-   Debug.Assert(true);
-  }
- }
+        {
+            Debug.Assert(true);
+        }
+    }
}

Activity

  1. ghost added
    untriaged
    on Jul 28, 2022
  2. added
    IDE-CodeStyle
    IDE-Formatter
    and removed
    untriaged
    on Jul 28, 2022
  3. added this to the Backlog milestone on Jul 28, 2022
  4. bjornhellander commented on Nov 27, 2022

    @bjornhellander
    Author

    Any chance on getting some attention to this?

    Since the release of Visual Studio 17.4 (Roslyn 4.4.0) this now happens IRL (as in "not just in tests") when applying the code fix for SA1503 in StyleCop.Analyzers. See DotNetAnalyzers/StyleCopAnalyzers#3581.

  5. bjornhellander commented on Nov 27, 2022

    @bjornhellander
    Author

    I added https://github.com/bjornhellander/BlockFormattingProblem as a simpler repro, in case it helps. It contains one test case which are run using two different versions of Roslyn. The test passes with the older version and fails with the newer.

  6. CyrusNajmabadi commented on Nov 27, 2022

    @CyrusNajmabadi
    Contributor

    @tmat sounds like maybe the same issue as the option serialization bug?

  7. modified the milestones: Backlog, 17.4 on Nov 28, 2022
  8. sharwell commented on Nov 28, 2022

    @sharwell
    Contributor

    @tmat @jasonmalinowski This is blocking the use of the 4.4.0 Roslyn packages on NuGet. I believe this was serviced; we will need a new set of 4.4.1 packages uploaded to NuGet before the fix can be rolled out.

  9. jasonmalinowski commented on Nov 28, 2022

    @jasonmalinowski
    Member

    @sharwell There was a servicing fix for some VS for Windows related issues here, but those changes would only impact the reading of the settings in VS for Windows. Furthermore:

    4.3.0-2.final (same behaviour with 4.3.0-3, but not with 4.3.0-1)

    @tmat's change would have regressed in the 4.4 line, not in 4.3. So this very much seems to be an unrelated issue.

  10. modified the milestones: 17.4, 17.6 P3 on Jan 17, 2023
  11. modified the milestones: 17.6 P3, Backlog on Nov 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Area-IDEIDE-CodeStyleIDE-Formatter

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions