Skip to content

C#: Fix AutoFormat not expanding empty constructor bodies with initializers#7191

Merged
knutwannheden merged 1 commit intomainfrom
maybeautoformat-produces-no-indentation-in-local-rewritetest-non-rpc
Mar 28, 2026
Merged

C#: Fix AutoFormat not expanding empty constructor bodies with initializers#7191
knutwannheden merged 1 commit intomainfrom
maybeautoformat-produces-no-indentation-in-local-rewritetest-non-rpc

Conversation

@knutwannheden
Copy link
Copy Markdown
Contributor

@knutwannheden knutwannheden commented Mar 28, 2026

Motivation

MaybeAutoFormat in C# recipes produces incorrect output in two scenarios:

  1. Empty constructor bodies with initializers — Roslyn's Formatter.Format does not expand Constructor() : base(){} to Allman-style multi-line braces (dotnet/roslyn#82974)
  2. SDK consumed via NuGet PackageReference — When the OpenRewrite SDK is consumed with PrivateAssets="all", AdhocWorkspace cannot discover Microsoft.CodeAnalysis.CSharp.Workspaces via Assembly.Load (the dependency entries are absent from .deps.json), making the entire Roslyn formatter a silent no-op

Summary

  • Work around the Roslyn bug in MinimumViableSpacingVisitor by inserting newlines into empty constructor body blocks that have a constructor initializer and Space.Empty prefix/end
  • Fix AdhocWorkspace assembly discovery by explicitly providing already-loaded Roslyn assemblies to MefHostServices.Create, bypassing the Assembly.Load path
  • Clarified MVS class documentation

Test plan

  • New test AutoFormatEmptyConstructorBodyWithInitializer verifies Allman-style braces for constructors with initializers
  • All 1788 existing tests pass
  • Verify with recipes-csharp consuming the SDK as a NuGet package

…lizers

Roslyn's Formatter.Format does not expand empty constructor bodies to
Allman style when a constructor initializer (`: base(...)`) is present
(dotnet/roslyn#82974). This means
MaybeAutoFormat produces `Foo() : base() { }` instead of multi-line
braces.

Work around this in MinimumViableSpacingVisitor by inserting newlines
into empty method body blocks that have a constructor initializer and
Space.Empty prefix/end, so the printed source has multi-line braces
that Roslyn can then indent correctly.
@github-project-automation github-project-automation bot moved this to In Progress in OpenRewrite Mar 28, 2026
@knutwannheden knutwannheden changed the title C#: Fix AutoFormat not expanding empty constructor bodies with initializers C#: Fix AutoFormat not expanding empty constructor bodies with initializers Mar 28, 2026
@knutwannheden knutwannheden merged commit 46e69ce into main Mar 28, 2026
1 check passed
@knutwannheden knutwannheden deleted the maybeautoformat-produces-no-indentation-in-local-rewritetest-non-rpc branch March 28, 2026 20:39
@github-project-automation github-project-automation bot moved this from In Progress to Done in OpenRewrite Mar 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

1 participant