Skip to content

Fix bug where appending to a nested block list before an outdented sibling failed - #2482

Open
slashblog wants to merge 9 commits into
dart-lang:mainfrom
slashblog:bug-fixes
Open

Fix bug where appending to a nested block list before an outdented sibling failed#2482
slashblog wants to merge 9 commits into
dart-lang:mainfrom
slashblog:bug-fixes

Conversation

@slashblog

Copy link
Copy Markdown
  • This change addresses the following issue I raised earlier. The original issue has more details (including the cause of the issue and steps to reproduce), but briefly adding an item to a list causes the following exception to be thrown: "Failed to produce valid YAML after modification.". This change fixes this issue.

  • I’ve reviewed the contributor guide and applied the relevant portions to this PR.
Contribution guidelines:

Many Dart repos have a weekly cadence for reviewing PRs - please allow for some latency before initial review feedback.

Note: The Dart team is trialing Gemini Code Assist. Don't take its comments as final Dart team feedback. Use the suggestions if they're helpful; otherwise, wait for a human reviewer.

@slashblog
slashblog requested a review from a team as a code owner July 31, 2026 06:05
@google-cla

google-cla Bot commented Jul 31, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request updates the block list appending logic in yaml_edit to trim trailing whitespace when finding the next newline index. The reviewer identified a performance issue where creating a substring on every append operation leads to O(N) time and memory complexity, as well as a potential RangeError crash if the index becomes negative. An optimized O(1) auxiliary space solution was suggested to scan backwards instead.

Comment thread pkgs/yaml_edit/lib/src/list_mutations.dart Outdated
@sigurdm

sigurdm commented Aug 6, 2026

Copy link
Copy Markdown
Contributor

Thanks for investigating and finding the cause of #2481!

Instead of manually backtracking whitespace characters with a while loop, package:yaml_edit already provides an internal helper getContentSensitiveEnd(YamlNode) in lib/src/utils.dart for this exact scenario (see how _addToBlockMap in lib/src/map_mutations.dart uses it).

We can simplify _appendToBlockList in lib/src/list_mutations.dart to:

  if (list.isNotEmpty) {
    final lastValueSpanEnd = getContentSensitiveEnd(list.nodes.last);
    final nextNewLineIndex = yaml.indexOf('\n', lastValueSpanEnd);
    if (nextNewLineIndex == -1) {
      formattedValue = getLineEnding(yaml) + formattedValue;
    } else {
      offset = nextNewLineIndex + 1;
    }
  }

Could you also:

  • Add a regression test for this in test/append_test.dart.
  • Add a note to CHANGELOG.md.

@slashblog

Copy link
Copy Markdown
Author

@sigurdm Done changes as per your comment.

@sigurdm

sigurdm commented Aug 13, 2026

Copy link
Copy Markdown
Contributor

Please update the PR title to say what the change does, rather than repeat the issue number

@slashblog slashblog changed the title Fix for https://github.com/dart-lang/tools/issues/2481 Fix bug where appending to a nested block list before an outdented sibling threw an exception Aug 18, 2026
@slashblog

Copy link
Copy Markdown
Author

@sigurdm I have changed both PR title and Changelog entry. Please do the needful.

Comment thread pkgs/yaml_edit/CHANGELOG.md Outdated
@slashblog slashblog changed the title Fix bug where appending to a nested block list before an outdented sibling threw an exception Fix bug where appending to a nested block list before an outdented sibling failed Aug 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants