-
Notifications
You must be signed in to change notification settings - Fork 283
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
yamllint allows referencing a non-existant anchor #395
Comments
Hello, Indeed yamllint does not currently detect inexistent anchors, or unmatching aliases. It could be implemented in a new yamllint rule. In theory, it should be possible if aliases aren't used before anchors are declared (and this seems to be the case according to the YAML specification 6.9.2.: "An anchor marks a node for future reference"). |
I'd also like to check if there are unused anchors in the yaml files. Is this rule being considered for yamllint? |
Yes, I think this is within the scope of a YAML linter. An option like In the end, a new rule like this would be great: rules:
anchors:
forbid-unknown-aliases: true # true by default, because YAML spec fordids it
forbid-duplicated-anchors: false # false by default, because YAML spec allows it
forbid-unused-anchors: false # false by default, because YAML spec allows it |
That's good to hear @adrienverge . I like the example you gave. It gives more flexibility depending on the use case. Not sure when we can expect those rules. I'm not familiar with the code base in this project and haven't implemented any custom rule. I can try open a PR if you can give some hints or guides on how to build those rules. |
Answering #420 (comment) from @amimas, about the
Sure 👍
I see. As discussed in the original comment, yamllint is designed to output errors right away, which allows: So, maybe a (not perfect) solution would be to output |
Wouldn't it be better to report it at the end of the document instead of stream? |
Sorry, that's what I meant but I twisted my words 🤦♂️ |
I think it's probably not that straight-forward. I tried implementing the While working on this, I also learned something new about yaml. While 3 dashes ( |
That's true,
Does this solution reliably indicates the end of a YAML document, even when there are multiple documents in a same file, and |
According to the YAML specification [^1]: - > It is an error for an alias node to use an anchor that does not > previously occur in the document. The `forbid-undeclared-aliases` option checks that aliases do have a matching anchor declared previously in the document. Since this is required by the YAML spec, this option is enabled by default. - > The alias refers to the most recent preceding node having the same > anchor. This means that having a same anchor repeated in a document is allowed. However users could want to avoid this, so the new option `forbid-duplicated-anchors` allows that. It's disabled by default. - > It is not an error to specify an anchor that is not used by any > alias node. This means that it's OK to declare anchors but don't have any alias referencing them. However users could want to avoid this, so a new option (e.g. `forbid-unused-anchors`) could be implemented in the future. See #537. Fixes #395 Closes #420 [^1]: https://yaml.org/spec/1.2.2/#71-alias-nodes
According to the YAML specification [^1]: - > It is an error for an alias node to use an anchor that does not > previously occur in the document. The `forbid-undeclared-aliases` option checks that aliases do have a matching anchor declared previously in the document. Since this is required by the YAML spec, this option is enabled by default. - > The alias refers to the most recent preceding node having the same > anchor. This means that having a same anchor repeated in a document is allowed. However users could want to avoid this, so the new option `forbid-duplicated-anchors` allows that. It's disabled by default. - > It is not an error to specify an anchor that is not used by any > alias node. This means that it's OK to declare anchors but don't have any alias referencing them. However users could want to avoid this, so a new option (e.g. `forbid-unused-anchors`) could be implemented in the future. See #537. Fixes #395 Closes #420 [^1]: https://yaml.org/spec/1.2.2/#71-alias-nodes
yamllint 1.26.1 (latest) does not flag references to anchors that don't exist.
If you try to parse this using a tool like PyYAML it errors.
The text was updated successfully, but these errors were encountered: