Skip to content

Fix MergeYaml inserting a new entry after surrounding blank lines#8232

Open
bmuschko wants to merge 1 commit into
mainfrom
bmuschko/reproduce-issue-274-test
Open

Fix MergeYaml inserting a new entry after surrounding blank lines#8232
bmuschko wants to merge 1 commit into
mainfrom
bmuschko/reproduce-issue-274-test

Conversation

@bmuschko

@bmuschko bmuschko commented Jul 10, 2026

Copy link
Copy Markdown
Contributor

What's changed?

When MergeYaml merges a new entry into a mapping that is surrounded by blank lines, the recipe mistakes that plain trailing whitespace for an inline comment and moves it onto the newly inserted entry. The new entry is therefore pushed past the blank lines instead of landing directly next to its sibling.

Blank lines after the last sibling — the trailing whitespace lives on the document end prefix:

# Before
service_config:
  name: 'hello'



# Actual (wrong)
service_config:
  name: 'hello'



  enabled: true

# Expected
service_config:
  name: 'hello'
  enabled: true


Blank lines before the entry, inserting Before it — the blank lines live on the entry's prefix:

# Before
service_config:



  name: 'hello'

# Actual (wrong) — blank lines pushed between the two entries
service_config:
  enabled: true



  name: 'hello'

# Expected — blank lines stay at the top, new entry directly before `name`
service_config:



  enabled: true
  name: 'hello'

The fix

Two targeted changes in MergeYamlVisitor.mergeMapping:

  1. Document end prefix — only treat it as a comment to copy onto the inserted entry when it actually contains a #; plain trailing blank lines now stay at the document end. (Mirrors the guard already present on the sibling-mapping branch.)
  2. Before insertion — when inserting before an entry whose prefix carries leading blank lines but no comment, keep those blank lines at the top of the block and place the new entry directly before the existing sibling.

Testing

Two parameterized tests in MergeYamlTest exercise every insertMode/insertProperty placement (null, Last, After, Before) for both the blank-lines-after and blank-lines-before scenarios. Existing Before/comment tests (e.g. insertBeforeElementWithCommentsWithNesting) confirm full-line comments above the target still stay attached to it, and the whole rewrite-yaml module is green.

When `MergeYaml` merged a new entry into a mapping whose surrounding
blank lines are stored on the document end prefix or on a sibling
entry's prefix, the recipe treated that plain trailing whitespace as an
inline comment and moved it onto the newly inserted entry. As a result
the new entry was pushed past the blank lines instead of landing
directly next to its sibling.

Two fixes:
- Only treat a document end prefix as a comment when it actually
  contains one, so plain trailing blank lines stay at the document end.
- When inserting `Before` an entry whose prefix carries leading blank
  lines (but no comment), keep those blank lines at the top of the block
  and place the new entry directly before the existing sibling.

Fixes moderneinc/support-morganstanley#274
@github-project-automation github-project-automation Bot moved this to In Progress in OpenRewrite Jul 10, 2026
@bmuschko bmuschko added the bug Something isn't working label Jul 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

Status: In Progress

Development

Successfully merging this pull request may close these issues.

1 participant