chainguard-dev / yam

A sweet little formatter for YAML
Apache License 2.0
20 stars 10 forks source link

Anchor syntax incorrectly formatted #43

Open xnox opened 7 months ago

xnox commented 7 months ago

yaml files with anchors are not correctly formatted.

luhring commented 7 months ago

@xnox thanks! any chance you could add an example? Like steps to reproduce and how that differs from what we want it to look like?

shyim commented 6 months ago

Running yam on following YAML

public: &public_bucket
    type: "amazon-s3"
    url: "%env(CDN_URL)%"
    visibility: "public"
    config:
        bucket: "%env(AWS_PUBLIC_FILESYSTEM_BUCKET)%"
        region: "%env(AWS_REGION)%"
        endpoint: "%env(AWS_ENDPOINT_URL)%"
        use_path_style_endpoint: true

theme: *public_bucket
asset: *public_bucket

generates invalid yaml to:

public:
  type: "amazon-s3"
  url: "%env(CDN_URL)%"
  visibility: "public"
  config:
    bucket: "%env(AWS_PUBLIC_FILESYSTEM_BUCKET)%"
    region: "%env(AWS_REGION)%"
    endpoint: "%env(AWS_ENDPOINT_URL)%"
    use_path_style_endpoint: true
theme:
*public_bucket
asset:
*public_bucket
luhring commented 6 months ago

Thanks @shyim, that's helpful. 🙏

This is most likely a bug in the underlying YAML library, gopkg.in/yaml.v3. My medium-term plan is to swap that out with a better YAML library.

Any ideas for shorter-term fixes are welcome, too.

xnox commented 6 months ago

Thanks @shyim, that's helpful. 🙏

This is most likely a bug in the underlying YAML library, gopkg.in/yaml.v3. My medium-term plan is to swap that out with a better YAML library.

Any ideas for shorter-term fixes are welcome, too.

it's difference of parsing expanded yaml versus unexpanded. Note that because it is references, one has to take special care when parsing them "literary" or not. It does have support for anchor types, and how to print them. I haven't looked at yam code if it checks for anchors or not.

None theless, I think @stormqueen1990 mentioned one of the better maintained forks of yam.v3..... was it a hashicorp fork? or something else?

stormqueen1990 commented 6 months ago

None theless, I think @stormqueen1990 mentioned one of the better maintained forks of yam.v3..... was it a hashicorp fork? or something else?

The one I know about is https://github.com/kubernetes-sigs/yaml, though I'm unsure if it would fix the issue at hand 🤔

xnox commented 6 months ago

Some food for thought:

https://github.com/google/yamlfmt/blob/main/README.md

https://github.com/google/yamlfmt/discussions/149

https://github.com/google/yamlfmt/discussions/148

https://github.com/braydonk/yaml/blob/v3/README.md