Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

YAML export indents multiline strings with block indentation indicator way too much #3432

Open
dupliaka opened this issue Dec 20, 2024 · 2 comments

Comments

@dupliaka
Copy link

tmt plan export does not interpret spaces scalar if attribute content starts with new line

How to reproduce

  1. tmt init --template mini
  2. Change plans/example.fmf so it contains
summary: |2

  Basic smoke test
  Next line to interpret
execute:
    how: tmt
    script: tmt --help
  1. Parse tmt plan export /plans/example | yq '.[0].summary'

Expected

Basic smoke test
Next line to interpret

Actual

  Basic smoke test
  Next line to interpret

Note: If there is no new line between "summary: |2"
and "Basic smoke test" - then export behaves as expected

Use case: if I need to pass a kickstart script where I use a file defined by heredoc (the script would not be able to catch the second EOF)

@happz happz changed the title Multiline attributes fails to interpret |2 YAML export indents multiline strings with block indentation indicator way too much Dec 20, 2024
@happz
Copy link
Collaborator

happz commented Dec 20, 2024

The problem seems to be in the YAML export. summary, as delivered by the parser to tmt, and e.g. JSON export are both correct:

tmt plan export --how=json /plans/example | jq -r '.[0].summary'

Basic smoke test
Next line to interpret

@happz
Copy link
Collaborator

happz commented Dec 20, 2024

Seems like a problem between tmt and ruamel.yaml emitter. Emitter collects hints, indentation and indicator for a given literal (string), which is |2, 2 and "" for this summary, then emits the hints - correctly, I guess - but then uses different indentation because it's not in "root context", which is the context of the very top level of a YAML document.

The problem is gone when I disable indentation enforced by tmt, yaml.indent(mapping=4, sequence=4, offset=2). Well, the whole YAML export is indented, by two characters, which I suppose is caused by this enforced indentation, and seeing it now I don't think it's how it should be. The issue also goes away when I comment out the yaml.indent() call from above. Dropping both offset and mapping also works, and sequence=2 gives out a reasonable YAML:

$ /home/happz/.local/share/hatch/env/virtual/tmt/BuhwJjvK/dev/bin/tmt plan export /plans/example
- name: /plans/example
  summary: |2

    Basic smoke test
    Next line to interpret
  description:
  contact: []
  enabled: true
  order: 50
  link: []
  id:
  tag: []
  tier:
  context: {}
  gate: []
  login:
  sources:
  - /tmp/foo/plans/example.fmf
  discover:
  - name: default-0
    how: shell
  provision:
  - name: default-0
    how: virtual
  prepare:
  - name: default-0
    how: shell
  execute:
  - how: tmt
    script: tmt --help
    name: default-0
  report:
  - name: default-0
    how: display
  finish:
  - name: default-0
    how: shell

Adding offset=2, to indent list items a bit more to the right yields to invalid YAML:

  - name: /plans/example
  summary: |2

    Basic smoke test
    Next line to interpret
  description:
...

Basically, I was unable to find a combination of mapping, sequence, and offset that would work, only setting either none of them or only offset leads to usable YAML.

The indentation setting affects how tmt writes its YAML files, which are either its state files, or the export like this, but also fmf files, e.g. by tmt init. I suppose changing the indentation to produce still readable, not as pretty but valid YAML should be possible for the first two categories, but might produce less perfect fmf files. To me, it sounds like we should make this feature configurable, and disable explicit indentation for the first two categories, where tmt does not control what it's writing out because it's data provided by users - YAML export, tests.yaml, step.yaml, this kind of files; tmt does control content fmf files it writes and never uses this kind of multiline string indicators, so it should be safe to produce more polished and still valid YAML aka fmf for the third category.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants