Skip to content

Comments with deeper indentation than surrounding fields are not preserved on save #747

@mallardduck

Description

@mallardduck

Describe the bug
This is a follow up bug to: #713

While the previous bug and PR resolved issues with Parsing un-even indentation on comments, those indentations are not preserved. This is less than desirable if the goal is simply to edit a single field and preserve the rest of the files formatting.

The use-case here is modifying values.yaml files of helm charts that are human maintained. So we should not reformat the file and simply desire to use AST to select a field and update the value.

To Reproduce

Adding the following to TestComment cases will produce a failing test:

		{
			name: "commented values.yaml with uneven indentation",
			yaml: `
topLevelNode:
  attribute: true
  # comment about the next attribute
  # someOptInFeature: true
  allGoodSoFar: true
    # until this breaks it
  despair: true
`,
		},

Expected behavior
When yaml with un-even comments are parsed and modified, the un-even comments should maintain existing indent level (unless that was otherwise modified by the user.

So input of:

  allGoodSoFar: true
    # until this breaks it
  despair: true

Seeking to set despair to false should output:

  allGoodSoFar: true
    # until this breaks it
  despair: false

And not:

  allGoodSoFar: true
  # until this breaks it
  despair: false

Screenshots
N/A

Version Variables

  • Go version: 1.24
  • go-yaml's Version: 1.18.0

Additional context
Add any other context about the problem here.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions