Skip to content

Add YAML anchor and alias support for GitHub Actions workflows #557

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

alexaandru
Copy link

Implements two-pass YAML parsing to resolve anchors (&anchor) and aliases (*alias) before linting, matching GitHub Actions' native YAML support.

  • Add containsAnchorsOrAliases() to detect anchor usage
  • Add resolveYAMLAnchors() using unmarshal/marshal strategy
  • Modify Parse() to resolve anchors when present
  • Add comprehensive test suite covering various anchor scenarios
  • Preserve backward compatibility for workflows without anchors
  • Maintain error detection and line number reporting after resolution

Fixes workflows that previously failed with "alias node but mapping node is expected" errors when using YAML anchors supported by GitHub Actions.

Fixes #133

The support for this just landed: actions/runner#1182

Implements two-pass YAML parsing to resolve anchors (&anchor) and aliases
(*alias) before linting, matching GitHub Actions' native YAML support.

- Add containsAnchorsOrAliases() to detect anchor usage
- Add resolveYAMLAnchors() using unmarshal/marshal strategy
- Modify Parse() to resolve anchors when present
- Add comprehensive test suite covering various anchor scenarios
- Preserve backward compatibility for workflows without anchors
- Maintain error detection and line number reporting after resolution

Fixes workflows that previously failed with "alias node but mapping node
is expected" errors when using YAML anchors supported by GitHub Actions.
@alexaandru
Copy link
Author

Please note that both lint and test FAIL but they also FAIL on main branch. This PR didn't introduce any new test failures, they are the same as on main. It did however introduce plenty of tests for the feature and they are all passing.

// containsAnchorsOrAliases recursively checks if a YAML node tree contains any anchors or aliases
func containsAnchorsOrAliases(node *yaml.Node) bool {
if node.Anchor != "" || node.Alias != nil {
return true
Copy link

@ChristopherHX ChristopherHX Aug 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had added anchor support in nektos/act yaml validator a while back at nektos/act#5893

e.g. https://github.com/nektos/act/blob/5e62222f80b8847de5dfced0527eaaeca3303168/pkg/schema/schema.go#L241C1-L243C3 I then traverse over alias and just handle it as if there were no alias.

Some suggestions from my side, using yaml.Node also heavily.

Just do *node = node.Alias and skip a lot of serializing / deserializing?

Otherwise assign node.Alias where you read this node pointer.

if node.Anchor != ""
just do node.Anchor = "" or remove the assert that require this to be cleared if any.

If I didn't produce a big bug in act, then this improves the code in my opinion

Otherwise use yaml.Node.Encode() and yaml.Node.Decode to replace marshall and unmarshall, this would avoid generating a text representation of the yaml that is not needed.

Serializing the yaml.Node into an interface may break diagnostic features of actionlint regardless if you use yaml.Node or text as medium for reading the yaml again.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you so much for the help, those are good tips!

@muzimuzhi muzimuzhi mentioned this pull request Aug 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

False Positive On Aliases In Mapping Nodes
2 participants