google/yamlfmt

Exclude is still a little bit wonky

esn89 opened this issue · 8 comments

esn89 commented

I have yamlfmt installed via nvim mason and it resides in ~/.local/share/nvim/mason/bin.

I have a file named env-config.yaml located in

/Users/esn89/Documents/Projects/helm-charts/application.git/develop/services/superapp/templates

which looks like this:

apiVersion: v1
data:
  PROJECT: {{ .Values.project }}
  JOB_NAME: {{ .Values.jobName }}

kind: ConfigMap
metadata:
  namespace: {{ .Values.namespace }}

I ran ~/.local/share/nvim/mason/bin/yamlfmt -conf ~/.config/nvim/.yamlfmt env-config.yaml and it formats my file to look like this:

---
apiVersion: v1
data:
  PROJECT: {? {.Values.project: ''} : ''}
  JOB_NAME: {? {.Values.jobName: ''} : ''}
kind: ConfigMap
metadata:
  namespace: {? {.Values.namespace: ''} : ''}

These is really strange, as I have set the **template/*.yaml path to be excluded.

My ~/.config/nvim/.yamlfmt looks as follows:

doublestar: true
exclude:
  - "/Users/esn89/Documents/Projects/**/templates/*.yaml"
  - "/Users/esn89/Documents/Projects/**/.gitlab-ci.yml"
formatter:
  type: basic
  indent: 2
  include_document_start: true

Any ideas?

I think this is coming down to excludes being absolute paths again. When passing the path into the argument, then the included path is env-config.yaml which according to doublestar does not match /Users/esn89/Documents/Projects/**/templates/*.yaml.

I still don't handle mixed absolute path/relative path excludes well. I note it in the docs. Likely what I should do is check whether either one is an absolute path, and if one is then convert the other. But I would need to test that thoroughly to make sure it works in all scenarios.

Either way I can see how this sucks for the editor plugin use-case; usually the content is just passed to yamlfmt through stdin, with no path-awareness other than the current working directory. I'll try to find a way to address this too.

As a short-term workaround, you can use ignore metadata on this file, which I know is not ideal but still should work for this usecase.

esn89 commented

I see. So from what I've read the paths need to be specified as relative to where the yamlfmt binary is, right?

https://github.com/google/yamlfmt/blob/main/docs/paths.md#include-and-exclude

Not relative to where the yamlfmt binary is, rather relative to the current working directory that yamlfmt is run from, which in most cases would be the project directory, or in neovim the directory that you have open (I think).

Starting with ** can help here, since that just means "any directory/directories appearing until the next thing in the match".

esn89 commented

So something like this, @braydonk ?

doublestar: true
exclude:
  - "**/templates/*.yaml"
  - "/Users/esn89/Documents/Projects/**/.gitlab-ci.yml"
formatter:
  type: basic
  indent: 2
  include_document_start: true

Yeah, I think if your neovim is open in a folder that has a templates subfolder somewhere in it, then any yaml file in it should be excluded. But I'm not 100% sure, I've never tried this exact use case.

esn89 commented

Great, thanks.

esn89 commented

@braydonk

Hey, just an idea. Even if exclude is a bit wonky, that's alright, as long as there is a way to have yamlfmt not mess with anything similar to: {{ .Values.helmstuff }}. Is there such a config/setting?

Unfortunately not, and any way I can think of to add one would be challenging. That syntax from the helm charts is Go templating. The yaml library that yamlfmt uses for formatting is not built to recognize this syntax since it's compliant only to the YAML spec. So yamlfmt can't actually recognize the syntax difference in any way today (and even if it could wouldn't know how to operate differently on it).