Skip to content

anchors: consider YAML oddities #780

@ccoVeille

Description

@ccoVeille

Right now the YAML syntax is ambiguous for anchors and aliases.

The fact an anchor or alias name can contain a : leads to oddities.

For example, this is valid

---
a: &foo: 42
m:
  - bar
  - *foo:

It will result to this

a: 42
m:
  - bar
  - 42

It's uncommon, but why not after all. The YAML specification supports it.

But now, consider this:

---
a: &foo 42
m:
  *foo: bar

What is it about ?

  • an alias and anchor named foo ?

    So this

     a: 42
     m:
       42: bar

    But then the valid YAML syntax for would be this:

     ---
     a: &foo 42
     m:
       *foo : bar

    With a space after *foo before the :, here it's because of the possible trailing : in alias name

    Please note, that right now, yamllint:
    - does not report using *foo: is ambiguous while it should/could, even if it's via a setting like "forbid ambiguous anchor name".
    - would report there is an extra space after the *foo: while using *foo: bar is invalid in fact in YAML, as it means "the alias named foo: followed by the scalar bar".

  • an anchor named foo and alias named foo: (with trailing :)

    Because then it would be this

     ---
     a: 42
     m:
       NULL bar

    which is invalid

This is unlikely to happen, so it proves it will occur 😄

I would prefer if yamllint could report that using an anchor with a trailing : as invalid, and report using an alias followed by a : in map is invalid.

cc @mikefarah @ingydotnet

I found this while taking a look at

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions