Add YAML anchor and alias support for GitHub Actions workflows#557
Add YAML anchor and alias support for GitHub Actions workflows#557alexaandru wants to merge 1 commit intorhysd:mainfrom
Conversation
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.
|
Please note that both lint and test FAIL but they also FAIL on |
| // 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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Thank you so much for the help, those are good tips!
|
Any idea of when this could be merged and released? |
b249531 to
bdb47a5
Compare
|
I'm closing and reopening this PR to kick CI workflows because GitHub seemed to dismiss 'Approve CI' button. |
|
Can you fix the failing checks? @alexaandru |
|
Thank you for working on this feature and the approach used in this branch is interesting. However I found a critical issue that the error position can be completely corrupted and cannot be recovered. For example on: push
jobs:
test:
runs-on: ubuntu-latest
steps:
- run: env | grep MY_TEST_
env: &default_env
INVALID_ENV: []
- run: env | grep MY_TEST_
env: *default_envThis workflow causes the following errors: However the positions |
Implements two-pass YAML parsing to resolve anchors (&anchor) and aliases (*alias) before linting, matching GitHub Actions' native YAML support.
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