Skip to content

[yaml_edit] YamlEditor.appendToList fails on multiline flow list with trailing comma #2532

Closed

Description

@bkonyi

Description

Calling YamlEditor.appendToList on a flow-style list ([...]) that spans multiple lines and contains a trailing comma results in an assertion failure (Failed to produce valid YAML after modification).

The editor inserts , <element> before the closing bracket ], but because the preceding item already has a trailing comma, it produces an invalid double-comma syntax.

Minimal Reproduction

import 'package:yaml_edit/yaml_edit.dart';

void main() {
  const yaml = '''
analyzer:
  exclude:
    [
      foo/**,
      build/**,
    ]
''';

  final editor = YamlEditor(yaml);
  editor.appendToList(['analyzer', 'exclude'], 'android/**');
  print(editor.toString());
}

Actual Result

Throws _YamlAssertionError:

Assertion failed: (package:yaml_edit) Failed to produce valid YAML after modification.

# YAML before edit:
> analyzer:
>   exclude:
>     [
>       foo/**,
>       build/**,
>     ]
> 

# YAML after edit:
> analyzer:
>   exclude:
>     [
>       foo/**,
>       build/**,
>     , android/**]
> 

package:yaml_edit/src/editor.dart: YamlEditor._performEdit
package:yaml_edit/src/editor.dart: YamlEditor.insertIntoList
package:yaml_edit/src/editor.dart: YamlEditor.appendToList

Expected Result

The new element should be appended inside the flow list with valid YAML syntax without throwing an assertion error:

analyzer:
  exclude:
    [
      foo/**,
      build/**,
      android/**,
    ]

Metadata

Metadata

Assignees

No one assigned

    Labels

    package:yaml_edittype-bugIncorrect behavior (everything from a crash to more subtle misbehavior)

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions