lyz-code / yamlfix

A simple opinionated yaml formatter that keeps your comments!
https://lyz-code.github.io/yamlfix
GNU General Public License v3.0
168 stars 38 forks source link

multiline string gets turned into long line #240

Open ggoretkin-bdai opened 1 year ago

ggoretkin-bdai commented 1 year ago

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
------------------------------------------------------------------
lyz-code commented 1 year ago

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

ggoretkin-bdai commented 1 year ago

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 commented 1 year ago

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.

lyz-code commented 1 year ago

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

millin commented 1 year ago

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

rusmux commented 8 months ago

@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!