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

Inconsistent formatting depending on platform: comment above list item moved below it on Windows #198

Open
lovetheguitar opened this issue Jul 29, 2024 · 4 comments
Labels
yaml_v3_problem A bug in the underlying yaml library. These issues are vastly harder to fix.

Comments

@lovetheguitar
Copy link

Hi,

thanks for the great library. ❤️

I found an inconsistency between a run on Ubuntu 22.04 and Windows 11.
I run yamlfmt via pre-commit. In a second step I tried to pin the go-lang version by adding language_version: 1.22.5 to the hook definition.

.pre-commit-config.yaml

.pre-commit-config.yaml

️️️  - repo: https://github.com/google/yamlfmt
    rev: 5607b62b215d44fe90ca71a5329efff63a1a0d18  # frozen: v0.13.0
    hooks:
      - id: yamlfmt
        language_version: 1.22.5

Originally I was using those formatter arguments, but removed them for the output file below.

        args:
          - -formatter
          - retain_line_breaks_single=true
          - -formatter
          - pad_line_comments=2  # two spaces before # comments, like in python
          - -formatter
          - scan_folded_as_literal=true  # so > blocks will not get collapsed

️️️

Diff

diff --git a/demo_yamlfmt_comment_swap.yaml b/demo_yamlfmt_comment_swap.yaml
index d042395..ba164a5 100644
--- a/demo_yamlfmt_comment_swap.yaml
+++ b/demo_yamlfmt_comment_swap.yaml
@@ -3,8 +3,8 @@ some_gitlab_ci_job:
     # when $TAG_LATEST_COMMIT_SOURCE_BRANCH is defined and matches $CI_COMMIT_BRANCH -> add job
     - if: $TAG_LATEST_COMMIT_SOURCE_BRANCH != null && $TAG_LATEST_COMMIT_SOURCE_BRANCH==$CI_COMMIT_BRANCH
       when: always
-    # when $TAG_LATEST_COMMIT_SOURCE_BRANCH is not defined, and we run on $CI_DEFAULT_BRANCH -> add job
     - if: $TAG_LATEST_COMMIT_SOURCE_BRANCH == null && $CI_DEFAULT_BRANCH == $CI_COMMIT_BRANCH
+      # when $TAG_LATEST_COMMIT_SOURCE_BRANCH is not defined, and we run on $CI_DEFAULT_BRANCH -> add job
       when: always
-    # demo comment above when:never
     - when: never  # do not run otherwise
+      # demo comment above when:never
Before: demo_yamlfmt_comment_swap.yaml

️️️
Input file

some_gitlab_ci_job:
  rules:
    # when $TAG_LATEST_COMMIT_SOURCE_BRANCH is defined and matches $CI_COMMIT_BRANCH -> add job
    - if: $TAG_LATEST_COMMIT_SOURCE_BRANCH != null && $TAG_LATEST_COMMIT_SOURCE_BRANCH==$CI_COMMIT_BRANCH
      when: always
    # when $TAG_LATEST_COMMIT_SOURCE_BRANCH is not defined, and we run on $CI_DEFAULT_BRANCH -> add job
    - if: $TAG_LATEST_COMMIT_SOURCE_BRANCH == null && $CI_DEFAULT_BRANCH == $CI_COMMIT_BRANCH
      when: always
    # demo comment above when:never
    - when: never  # do not run otherwise
    

️️️

After: demo_yamlfmt_comment_swap.yaml

️️️
Output file

some_gitlab_ci_job:
  rules:
    # when $TAG_LATEST_COMMIT_SOURCE_BRANCH is defined and matches $CI_COMMIT_BRANCH -> add job
    - if: $TAG_LATEST_COMMIT_SOURCE_BRANCH != null && $TAG_LATEST_COMMIT_SOURCE_BRANCH==$CI_COMMIT_BRANCH
      when: always
    - if: $TAG_LATEST_COMMIT_SOURCE_BRANCH == null && $CI_DEFAULT_BRANCH == $CI_COMMIT_BRANCH
      # when $TAG_LATEST_COMMIT_SOURCE_BRANCH is not defined, and we run on $CI_DEFAULT_BRANCH -> add job

      when: always
    - when: never # do not run otherwise
      # demo comment above when:never

️️️

The same file passes yamlfmt on linux in both versions.

Is there some workaround to have the same behavior cross platform?

@braydonk
Copy link
Collaborator

Hi @lovetheguitar thanks for opening an issue.

Interesting, I've seen the library be temperamental about scanning comment clusters in the past but never seen it do different things on different platforms.

I'm not sure if I'll be able to come up with a solution for this. I've tried to fix these kinds of weird comment placement bugs in the yaml library in the past and always came up short. This usually comes as a result of the way we have to hack retain_line_breaks to work, another thing I essentially can't fix in the library without rewriting it.

I'm sorry it's broken for this case, I would really like to fix it but I've never been able to.

Since you're already using pre-commit, I might recommend trying out this alternative: https://github.com/jumanjihouse/pre-commit-hook-yamlfmt
I've not tried it myself as I'm not a pre-commit user, but it's possible that it's more accurate than this tool is.

@braydonk braydonk added the yaml_v3_problem A bug in the underlying yaml library. These issues are vastly harder to fix. label Jul 29, 2024
@lovetheguitar
Copy link
Author

Hey @braydonk,

thanks a lot for your fast response. Just had some time to play around with the alternative you recommended, but the maintainer sadly does not seem to care about windows support at all (https://github.com/jumanjihouse/pre-commit-hook-yamlfmt/issues?q=is%3Aissue+is%3Aopen+windows, jumanjihouse/pre-commit-hook-yamlfmt#42).

So, for now we'll probably adopt your solution and live with this small inconsistency. Thanks again! (:

@kenzht
Copy link

kenzht commented Oct 29, 2024

v0.13.0 results in inconsistency, but v0.12.1 worked as expected on windows:)

@stevehipwell
Copy link

stevehipwell commented Jan 15, 2025

@braydonk I'm seeing this on Windows with the latest version and the line endings set to lf. I'd expect this to work correctly given the same line endings being used across both OS'. Do you have any thoughts?

After resetting git and running this again the difference have gone.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
yaml_v3_problem A bug in the underlying yaml library. These issues are vastly harder to fix.
Projects
None yet
Development

No branches or pull requests

4 participants