Skip to content

Exception thrown "Failed to produce valid YAML after modification." on adding to list #2481

Description

@slashblog

I have the following YAML:

family:
  - title: Parent
    children:
      - title: kid1
      - title: kid2
  - title: Uncle

on adding another kid like so:

editor.appendToList(['family', 0, 'children'], {'title': 'kid3'});

the following exception is thrown:

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

The full code to reproduce the issue is as follows:

import 'package:yaml_edit/yaml_edit.dart';

void main() {
  final yaml = '''
family:
  - title: Parent
    children:
      - title: kid1
      - title: kid2
  - title: Uncle
''';

  print('--- BEFORE ---');
  print(yaml);

  final editor = YamlEditor(yaml);

  editor.appendToList(['family', 0, 'children'], {'title': 'kid3'});

  print('--- AFTER (buggy yaml_edit) ---');
  print(editor.toString());

  print('--- EXPECTED ---');
  print('''
family:
  - title: Parent
    children:
      - title: kid1
      - title: kid2
      - title: kid3
  - title: Uncle
''');
}

The issue is coming because:

list.nodes.last.span.end.offset gives offset for character just before "- title: Uncle".

The fix is following:

#2482

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions