yamlfix icon indicating copy to clipboard operation
yamlfix copied to clipboard

multiline string gets turned into long line

Open ggoretkin-bdai opened this issue 1 year ago • 6 comments

Description and Current Behavior

yamlfix turns

---
jobs:
  run_jobs:
    steps:
      - name: Upload coverage to Codecov
        with:
          files: ./projects/aaaaaaaaaaaaaaaaa/ws/build/formation_planner/coverage.xml,
            ./projects/aaaaaaaaaaaaaaaaa/ws/build/aaaaaaaaaaaaaaaaa_abc/coverage.xml,
            ./projects/bbbb_examples/ws/build/bbbb_examples/coverage.xml,
            ./projects/ccccccccccccccccccc/ws/build/diagnose_repair/coverage.xml,
            projects/aaaaaaaaaaaaaaaaa/ws/build/aaaaaaaaaaaaaaaaa_abc/coverage.xml,
            ./ws/build/abcd_abc/coverage.xml, ./ws/build/bbbb_bbbbter/coverage.xml,
            ./ws/build/abcd_abc_wrappers/coverage.xml

into

---
jobs:
  run_jobs:
    steps:
      - name: Upload coverage to Codecov
        with:
          files: ./projects/aaaaaaaaaaaaaaaaa/ws/build/formation_planner/coverage.xml,
            ./projects/aaaaaaaaaaaaaaaaa/ws/build/aaaaaaaaaaaaaaaaa_abc/coverage.xml,
            ./projects/bbbb_examples/ws/build/bbbb_examples/coverage.xml, ./projects/ccccccccccccccccccc/ws/build/diagnose_repair/coverage.xml,
            projects/aaaaaaaaaaaaaaaaa/ws/build/aaaaaaaaaaaaaaaaa_abc/coverage.xml,
            ./ws/build/abcd_abc/coverage.xml, ./ws/build/bbbb_bbbbter/coverage.xml,
            ./ws/build/abcd_abc_wrappers/coverage.xml

Desired behavior

breakable lines should not exceed the default column width

Environment

$ python -c "import yamlfix.version; print(yamlfix.version.version_info())"
------------------------------------------------------------------
     yamlfix: 1.9.0
     Python: 3.10.6
     Platform: Linux-5.19.0-40-generic-x86_64-with-glibc2.35
------------------------------------------------------------------

ggoretkin-bdai avatar May 03 '23 21:05 ggoretkin-bdai

Hi @ggoretkin-bdai thanks for taking the time to open an issue. Maybe I'm understanding you wrong but the snippet you provided is not a valid yaml way to break long lines. You need to use the > or | operators. For example the next snippet is untouched by yamlfix:

---
jobs:
  run_jobs:
    steps:
      - name: Upload coverage to Codecov
        with:
          files: >
            ./projects/aaaaaaaaaaaaaaaaa/ws/build/formation_planner/coverage.xml,
            ./projects/aaaaaaaaaaaaaaaaa/ws/build/aaaaaaaaaaaaaaaaa_abc/coverage.xml,
            ./projects/bbbb_examples/ws/build/bbbb_examples/coverage.xml,
            ./projects/ccccccccccccccccccc/ws/build/diagnose_repair/coverage.xml,
            projects/aaaaaaaaaaaaaaaaa/ws/build/aaaaaaaaaaaaaaaaa_abc/coverage.xml,
            ./ws/build/abcd_abc/coverage.xml, ./ws/build/bbbb_bbbbter/coverage.xml,
            ./ws/build/abcd_abc_wrappers/coverage.xml

In case you're not familiar with these operators here is a little cheatsheet

Break long lines

  • Use > most of the time: interior line breaks are stripped out, although you get one at the end:

    key: >
      Your long
      string here.
    
  • Use | if you want those line breaks to be preserved as \n (for instance, embedded markdown with paragraphs):

    key: |
      ### Heading
    
      * Bullet
      * Points
    
  • Use >- or |- instead if you don't want a line break appended at the end.

lyz-code avatar May 04 '23 09:05 lyz-code

Thanks for the quick reply, and the explanation. I believe there is still something going on that I could use help understanding. The exact situation I am running into is:

jobs:
  run_jobs:
    steps:
      - name: Upload coverage to Codecov
        with:
          files: ./projects/aaaaaaaaaaaaaaaaa/ws/build/formation_planner/coverage.xml, ./projects/aaaaaaaaaaaaaaaaa/ws/build/aaaaaaaaaaaaaaaaa_abc/coverage.xml, ./projects/bbbb_examples/ws/build/bbbb_examples/coverage.xml, ./projects/ccccccccccccccccccc/ws/build/diagnose_repair/coverage.xml, projects/aaaaaaaaaaaaaaaaa/ws/build/aaaaaaaaaaaaaaaaa_abc/coverage.xml, ./ws/build/abcd_abc/coverage.xml, ./ws/build/bbbb_bbbbter/coverage.xml, ./ws/build/abcd_abc_wrappers/coverage.xml

Note that it is a single long line at the end. yamlfix turns it into

---
jobs:
  run_jobs:
    steps:
      - name: Upload coverage to Codecov
        with:
          files: ./projects/aaaaaaaaaaaaaaaaa/ws/build/formation_planner/coverage.xml,
            ./projects/aaaaaaaaaaaaaaaaa/ws/build/aaaaaaaaaaaaaaaaa_abc/coverage.xml,
            ./projects/bbbb_examples/ws/build/bbbb_examples/coverage.xml, ./projects/ccccccccccccccccccc/ws/build/diagnose_repair/coverage.xml,
            projects/aaaaaaaaaaaaaaaaa/ws/build/aaaaaaaaaaaaaaaaa_abc/coverage.xml,
            ./ws/build/abcd_abc/coverage.xml, ./ws/build/bbbb_bbbbter/coverage.xml,
            ./ws/build/abcd_abc_wrappers/coverage.xml

From that I inferred some things about YAML that were not true (thank you for pointing out the >).

ggoretkin-bdai avatar May 04 '23 13:05 ggoretkin-bdai

Furthermore, when I give this to yamlfix

---
jobs:
  run_jobs:
    steps:
      - name: Upload coverage to Codecov
        with:
          files: >
            ./projects/aaaaaaaaaaaaaaaaa/ws/build/formation_planner/coverage.xml,
            ./projects/aaaaaaaaaaaaaaaaa/ws/build/aaaaaaaaaaaaaaaaa_abc/coverage.xml,
            ./projects/bbbb_examples/ws/build/bbbb_examples/coverage.xml, ./projects/ccccccccccccccccccc/ws/build/diagnose_repair/coverage.xml,
            projects/aaaaaaaaaaaaaaaaa/ws/build/aaaaaaaaaaaaaaaaa_abc/coverage.xml,
            ./ws/build/abcd_abc/coverage.xml, ./ws/build/bbbb_bbbbter/coverage.xml,
            ./ws/build/abcd_abc_wrappers/coverage.xml

it becomes

---
jobs:
  run_jobs:
    steps:
      - name: Upload coverage to Codecov
        with:
          files: >-
            ./projects/aaaaaaaaaaaaaaaaa/ws/build/formation_planner/coverage.xml,
            ./projects/aaaaaaaaaaaaaaaaa/ws/build/aaaaaaaaaaaaaaaaa_abc/coverage.xml,
            ./projects/bbbb_examples/ws/build/bbbb_examples/coverage.xml, ./projects/ccccccccccccccccccc/ws/build/diagnose_repair/coverage.xml,
            projects/aaaaaaaaaaaaaaaaa/ws/build/aaaaaaaaaaaaaaaaa_abc/coverage.xml,
            ./ws/build/abcd_abc/coverage.xml, ./ws/build/bbbb_bbbbter/coverage.xml,
            ./ws/build/abcd_abc_wrappers/coverage.xml

> becomes >- . I also expected that the default line length of 80 chars would force each path on its own line.

ggoretkin-bdai avatar May 04 '23 13:05 ggoretkin-bdai

The > to >- is not a big deal because it's always best not to have the \n at the end of the string.

I can reproduce the long line not being handled. It seems that the string ./projects/bbbb_examples/ws/build/bbbb_examples/coverage.xml, is smaller than 80 characters and therefore it doesn't know how to split it. The issue can be either in the patch_sequence_style function or upstream from ruyaml which we use to handle the long string splitting

lyz-code avatar May 04 '23 15:05 lyz-code

Apparently it's a bug in the ruyaml side, but it looks like it's dead

millin avatar Jun 15 '23 19:06 millin

@lyz-code How can I avoid formatting list items? For example:

build:
  stage: build
  before_script:
    - *docker_login
  script:
    - DOCKER_BUILDKIT=1 docker build .
      --cache-from $CI_REGISTRY_IMAGE:latest
      --platform=linux/amd64
      --network=host
      --progress=plain
      --tag $CI_REGISTRY_IMAGE:$CI_COMMIT_SHORT_SHA

Transformed into:

build:
  stage: build
  before_script:
    - *docker_login
  script:
    - DOCKER_BUILDKIT=1 docker build . --cache-from $CI_REGISTRY_IMAGE:latest --platform=linux/amd64 --network=host --progress=plain
      --tag $CI_REGISTRY_IMAGE:$CI_COMMIT_SHORT_SHA

Thank you!

rusmux avatar Feb 06 '24 16:02 rusmux