ruby / ruby/psych

Default line_width does not reflect the libyaml default of 80

Open
#257 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Ruby
Stars
597
Forks
223
Avg merge
11h 23m
Merged PRs (30d)
3

Description

This causes the folded style to not apply unless line_width: is passed explicitly:

> s = 'asjfal sf;lkasfj;aslkfaj slfkasfkajf a;slkfjaslkfaslkfj aslkfjaslkf asklfj askl fjaklsfj aslkfjaslfjaslkfjlkffjsa'
> Psych.dump(s, line_width: 80) == Psych.dump(s)
 => false
> puts Psych.dump(s, line_width: 80)
--- >-
  asjfal sf;lkasfj;aslkfaj slfkasfkajf a;slkfjaslkfaslkfj aslkfjaslkf asklfj askl
  fjaklsfj aslkfjaslfjaslkfjlkffjsa
> puts Psych.dump(s)
--- asjfal sf;lkasfj;aslkfaj slfkasfkajf a;slkfjaslkfaslkfj aslkfjaslkf asklfj askl
  fjaklsfj aslkfjaslfjaslkfjlkffjsa
...

The libyaml default of 80 happens as follows:

First, the emitter best_width is set to 0 on initialization here:

memset(emitter, 0, sizeof(yaml_emitter_t));

It is then set to 80 in yaml_emitter_emit_stream_start here:

if (emitter->best_width >= 0
        && emitter->best_width <= emitter->best_indent*2) {
    emitter->best_width = 80;
}

On the Ruby side, we pass the options including :line_width into the emitter, but the code that picks the node style does not account for the libyaml defaults:

elsif @line_width && o.length > @line_width
    style = Nodes::Scalar::FOLDED

We should probably also default to 80 in yaml_tree.rb for uniform handling.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in lib/psych/visitors/yaml_tree.rb at the node-style selection and compare its line-width handling with the libyaml default described in ext/psych/yaml/emitter.c. Verify that dumping without an explicit line_width behaves the same as dumping with line_width: 80, including the folded-style example.

Written by the indexing model from the issue text.

Assessment

Tech stack
c, ruby
Domain
backend
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.