From e322a841aad51a7ea73129d86461a32f277ab57d Mon Sep 17 00:00:00 2001 From: Neeraj Date: Fri, 31 Jul 2026 11:25:07 +0530 Subject: [PATCH 1/4] Fix for https://github.com/dart-lang/tools/issues/2481 --- pkgs/yaml_edit/lib/src/list_mutations.dart | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pkgs/yaml_edit/lib/src/list_mutations.dart b/pkgs/yaml_edit/lib/src/list_mutations.dart index 7613da65c8..ab40f43ab4 100644 --- a/pkgs/yaml_edit/lib/src/list_mutations.dart +++ b/pkgs/yaml_edit/lib/src/list_mutations.dart @@ -122,7 +122,8 @@ SourceEdit _appendToBlockList( // exists if (list.isNotEmpty) { final lastValueSpanEnd = list.nodes.last.span.end.offset; - final nextNewLineIndex = yaml.indexOf('\n', lastValueSpanEnd - 1); + final realEnd = yaml.substring(0, lastValueSpanEnd).trimRight().length; + final nextNewLineIndex = yaml.indexOf('\n', realEnd - 1); if (nextNewLineIndex == -1) { formattedValue = getLineEnding(yaml) + formattedValue; } else { From 8372b5c81cb67d5fd7c0393aa85e0ab111c0ddcf Mon Sep 17 00:00:00 2001 From: Neeraj Date: Fri, 31 Jul 2026 16:00:38 +0530 Subject: [PATCH 2/4] Changes to accomodate code review --- pkgs/yaml_edit/lib/src/list_mutations.dart | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/pkgs/yaml_edit/lib/src/list_mutations.dart b/pkgs/yaml_edit/lib/src/list_mutations.dart index ab40f43ab4..ee1d404797 100644 --- a/pkgs/yaml_edit/lib/src/list_mutations.dart +++ b/pkgs/yaml_edit/lib/src/list_mutations.dart @@ -122,8 +122,15 @@ SourceEdit _appendToBlockList( // exists if (list.isNotEmpty) { final lastValueSpanEnd = list.nodes.last.span.end.offset; - final realEnd = yaml.substring(0, lastValueSpanEnd).trimRight().length; - final nextNewLineIndex = yaml.indexOf('\n', realEnd - 1); + var realEnd = lastValueSpanEnd; + while (realEnd > 0) { + if (yaml[realEnd - 1].trim().isEmpty) { + realEnd--; + } else { + break; + } + } + final nextNewLineIndex = yaml.indexOf('\n', realEnd > 0 ? realEnd - 1 : 0); if (nextNewLineIndex == -1) { formattedValue = getLineEnding(yaml) + formattedValue; } else { From fbd9b61556dcf560968632c0f06c3c5da550c8c5 Mon Sep 17 00:00:00 2001 From: Neeraj Date: Thu, 6 Aug 2026 21:39:26 +0530 Subject: [PATCH 3/4] Changes as per comments --- pkgs/yaml_edit/CHANGELOG.md | 5 ++++ pkgs/yaml_edit/lib/src/list_mutations.dart | 12 ++------- pkgs/yaml_edit/test/append_test.dart | 29 ++++++++++++++++++++++ 3 files changed, 36 insertions(+), 10 deletions(-) diff --git a/pkgs/yaml_edit/CHANGELOG.md b/pkgs/yaml_edit/CHANGELOG.md index 37c0088baa..41c94d8ac9 100644 --- a/pkgs/yaml_edit/CHANGELOG.md +++ b/pkgs/yaml_edit/CHANGELOG.md @@ -1,3 +1,8 @@ +## 2.2.5 + +- Exception thrown "Failed to produce valid YAML after modification." on adding to list + ([#2481](https://github.com/dart-lang/tools/issues/2481)) + ## 2.2.4 - Removes comments associated with a node when `remove` is called. diff --git a/pkgs/yaml_edit/lib/src/list_mutations.dart b/pkgs/yaml_edit/lib/src/list_mutations.dart index ee1d404797..888406591b 100644 --- a/pkgs/yaml_edit/lib/src/list_mutations.dart +++ b/pkgs/yaml_edit/lib/src/list_mutations.dart @@ -121,16 +121,8 @@ SourceEdit _appendToBlockList( // Adjusts offset to after the trailing newline of the last entry, if it // exists if (list.isNotEmpty) { - final lastValueSpanEnd = list.nodes.last.span.end.offset; - var realEnd = lastValueSpanEnd; - while (realEnd > 0) { - if (yaml[realEnd - 1].trim().isEmpty) { - realEnd--; - } else { - break; - } - } - final nextNewLineIndex = yaml.indexOf('\n', realEnd > 0 ? realEnd - 1 : 0); + final lastValueSpanEnd = getContentSensitiveEnd(list.nodes.last); + final nextNewLineIndex = yaml.indexOf('\n', lastValueSpanEnd); if (nextNewLineIndex == -1) { formattedValue = getLineEnding(yaml) + formattedValue; } else { diff --git a/pkgs/yaml_edit/test/append_test.dart b/pkgs/yaml_edit/test/append_test.dart index cb705edaca..d18d07bd2b 100644 --- a/pkgs/yaml_edit/test/append_test.dart +++ b/pkgs/yaml_edit/test/append_test.dart @@ -218,6 +218,35 @@ b: ''')); }); + test('block append nested to list', () { + final yamlEditor = YamlEditor(''' +family: + - title: Parent + children: + - title: kid1 + - title: kid2 + - title: Uncle +'''); + + expect( + () => yamlEditor + .appendToList(['family', 0, 'children'], {'title': 'kid3'}), + returnsNormally); + expectYamlBuilderValue(yamlEditor, { + 'family': [ + { + 'title': 'Parent', + 'children': [ + {'title': 'kid1'}, + {'title': 'kid2'}, + {'title': 'kid3'}, + ], + }, + {'title': 'Uncle'}, + ] + }); + }); + test('block append nested and with comments', () { final yamlEditor = YamlEditor(''' a: From c3bd2c283a8dc437c89d8951dcd761a82f4a5987 Mon Sep 17 00:00:00 2001 From: Neeraj Jakhar <31380679+slashblog@users.noreply.github.com> Date: Wed, 19 Aug 2026 06:04:00 +0530 Subject: [PATCH 4/4] Update CHANGELOG.md --- pkgs/yaml_edit/CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkgs/yaml_edit/CHANGELOG.md b/pkgs/yaml_edit/CHANGELOG.md index 2032bed1a5..6c815bbd21 100644 --- a/pkgs/yaml_edit/CHANGELOG.md +++ b/pkgs/yaml_edit/CHANGELOG.md @@ -1,5 +1,5 @@ ## 2.2.5-wip -- Fix bug where appending to a nested block list before an outdented sibling threw an exception ([#2481](https://github.com/dart-lang/tools/issues/2481)). +- Fix a bug where appending to a nested block list before an outdented sibling failed. ([#2481](https://github.com/dart-lang/tools/issues/2481)). ## 2.2.4