C#: Preserve MinimumViableSpacing fixes when Roslyn formatting is a no-op#7182
Merged
knutwannheden merged 1 commit intomainfrom Mar 27, 2026
Conversation
…o-op When a recipe adds a modifier to a VariableDeclarations (e.g., MakeFieldReadOnly adding `readonly`), the type expression often has an empty prefix because the indentation lives on the VarDecl prefix. MinimumViableSpacingVisitor correctly adds a space between the modifier and the type, but RoslynFormatter discarded this fix when Roslyn formatting made no additional changes — the short-circuit `source == formattedSource` returned the original CU without MVS changes. Now, when Roslyn is a no-op but MVS made changes, we reconcile the MVS changes within the target subtrees instead of discarding them.
MinimumViableSpacing fixes when Roslyn formatting is a no-op
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Motivation
The
MakeFieldReadOnlyrecipe inrecipes-csharpproducesreadonlyList<T>instead ofreadonly List<T>when adding thereadonlymodifier to fields. Thereadonlykeyword is missing a trailing space before the type name.The root cause is in
RoslynFormatter: whenMinimumViableSpacingVisitor(MVS) correctly adds a space between the modifier and the type expression, but Roslyn formatting makes no additional changes, the formatter short-circuits atsource == formattedSourceand returns the original CU — discarding the MVS fix.The flow was:
mvsCu(correct:readonly List<int>)mvsCu→ correct outputsource == formattedSource→ returnsoriginalCu(no space!) →readonlyList<int>Summary
Format()(single subtree) andFormatSpans()(multi-target batch)MaybeAutoFormat)Test plan
AutoFormatAfterAddingReadonlyModifierToGenericField— addsreadonlytoList<int>field via visitor +MaybeAutoFormat, verifiesreadonly List<int>outputAutoFormatAfterAddingReadonlyModifierToSimpleField— same forintfieldAddedModifierToFieldWithGenericType,AddedModifierToFieldWithSimpleType)