Skip to content

Add enough primitives to support comment preservation  #566

Open

Description

@SamSaffron

Preserving comments in YAML files is a general problem many automation tools have.

The general problem is that you have some YAML file say:

# the answer
- universe: 41

When you decide to programmatically change the file to correct the answer by loading and saving YAML you end up stripping comments

- universe: 42

There are 2 current families of solutions in the wild. The first is regex hacking. The second is leaning on https://pypi.org/project/ruamel.yaml/.

Was wondering if having enough internal primitives in psych to implement this is a good idea? Should comment preservation be built into Psych (optionally) or is this the role of some other gem that consumes it?

Activity

  1. qinmingyuan commented on Jan 9, 2026

    @qinmingyuan
  2. headius commented on Jan 9, 2026

    @headius
    Contributor

    FWIW the psych backend for JRuby is SnakeYAML Engine, which does allow preserving comments in the parser graph. We could probably enable this for JRuby pretty easily.

  3. SamSaffron commented on Jan 10, 2026

    @SamSaffron
    Author

    @headius psych-pure should work fine in JRuby it is a native Ruby implementation.

  4. headius commented on Jan 10, 2026

    @headius
    Contributor

    @SamSaffron Oh I'm sure it will! But we can will enable native support for comments in the JRuby extension for better performance.

    Fwiw calling it "Native Ruby" is a little confusing since we have both "native" code and Ruby code in these libraries.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions