Repository navigation
fix(overlay): expand YAML aliases before actions apply - #740
daveshanley wants to merge 2 commits into
Conversation
An overlay addresses the JSON data model, where each value belongs to
one place. A YAML anchor shares one node between all the aliases and
merge keys that name it. This caused two failures:
- A remove action that deleted the node that defines an anchor left
the aliases with no anchor. The output did not parse ("unknown
anchor").
- An action that reached a node through an alias changed every place
that shared the node.
The engine now expands the target document before it applies an
action. Each alias gets its own copy of the node it names, merge keys
are applied, and anchor names are removed. An update value that has an
alias, an anchor or a merge key is copied and expanded in the same
way. The caller's overlay does not change. The expansion uses the
default alias limit of go-yaml and finds cycles.
Behaviour changes:
- A document that has anchors comes out with the anchors expanded.
- A cyclic alias in the target now fails with ErrInvalidAlias, not
ErrIncompatibleUpdate.
New errors: ErrInvalidAlias and ErrExcessiveAliases.
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #740 +/- ##
==========================================
Coverage 100.00% 100.00%
==========================================
Files 316 317 +1
Lines 40239 40375 +136
==========================================
+ Hits 40239 40375 +136
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
daveshanley
left a comment
There was a problem hiding this comment.
The full Go suite and overlay race tests passed. I found seven issues in alias expansion.
| return err | ||
| } | ||
| node.Content[i] = e.copyOf(child.Alias) | ||
| if err := aliasLimits.CheckAlias(e.aliasNodes, e.nodes); err != nil { |
There was a problem hiding this comment.
Check the node limit before copyOf allocates the subtree. Also bound expanded scalar bytes before marshaling, since node count alone cannot limit output size.
There was a problem hiding this comment.
Fixed in 04337f7. copyAlias measures the named tree once (measureTree, kept in sizes) and counts the full copy against the node limit before copyTree allocates it. The alias ratio only increases while a copy adds nodes, so one check of the full copy gives the same result as one check for each node.
Alias copies can also add at most 64 MiB of text (values and comments). More fails with ErrExcessiveAliases.
TestExpandAliases_LimitCheckedBeforeCopy: an alias to a tree of 10,000 nodes over the limit makes fewer than 100 allocations.TestApply_ExcessiveAliasText: 65 aliases to a 1 MiB scalar are in the count limit, and fail on the text limit.
| } | ||
| // The named node is expanded once, in place. Every alias then takes a | ||
| // copy of a tree that has no alias left in it. | ||
| if err := e.expand(child.Alias, true); err != nil { |
There was a problem hiding this comment.
Count the alias itself in e.nodes before visiting its target. A flat list of 2,048 scalar aliases passes go-yaml and fails here with ErrExcessiveAliases.
There was a problem hiding this comment.
Fixed in 04337f7. The alias counts in e.nodes before its target is visited, as in go-yaml. TestApply_FlatAliasList has 2,048 aliases to one scalar. It checks that go-yaml accepts the list, and that the overlay result holds all 2,048 values.
| if err := e.expand(child.Alias, true); err != nil { | ||
| return err | ||
| } | ||
| node.Content[i] = e.copyOf(child.Alias) |
There was a problem hiding this comment.
When an alias supplies the literal mapping key <<, emit it as a quoted string. Copying its tag turns it into a merge directive and can break JSON conversion.
There was a problem hiding this comment.
Fixed in 04337f7. A copy in a key position that is the scalar << gets the tag !!str and the double-quoted style. TestApply_AliasKeyIsLiteralMergeText has *key : literal next to a real merge key. It compares the result with go-yaml's own reading of the source: {"<<": "literal", "merged": true}.
Also, only a key can start merge resolution now. Before, a << value also did.
| } | ||
| } | ||
| if merges && node.Kind == yaml.MappingNode { | ||
| node.Content = utils.ResolveMergeNodes(node).Content |
There was a problem hiding this comment.
ResolveMergeNodes leaves {<<: {}} intact. Normalize it to {} so a wildcard action matches zero properties.
There was a problem hiding this comment.
Fixed in 04337f7. When ResolveMergeNodes returns the mapping unchanged (the merge gives no entry), the mapping gets no content. TestApply_EmptyMerge_HasNoProperties updates $['x-empty'].*. The result is x-empty: {}, and the update matches nothing.
A remove action did not show this fault: it removed the << entry together with the {} that it matched.
| } | ||
| } | ||
| if merges && node.Kind == yaml.MappingNode { | ||
| node.Content = utils.ResolveMergeNodes(node).Content |
There was a problem hiding this comment.
Reject scalar merge sources before resolving the mapping. For {<<: name, enabled: true}, this silently drops the invalid directive and returns success.
There was a problem hiding this comment.
Fixed in 04337f7. resolveMerges checks each merge value before it resolves the mapping. A value that is not a mapping or a list of mappings fails with the new ErrInvalidMergeKey, with the line of the merge key.
TestApply_MergeValueNotMapping: a scalar, a null, a list with a scalar, and an alias to a scalar. Each case also checks that go-yaml rejects the same document.TestApply_MergeKeyList: a valid list merge gives the same mapping as go-yaml.
| if len(node.Content) > 0 { | ||
| copied.Content = make([]*yaml.Node, len(node.Content)) | ||
| for i, child := range node.Content { | ||
| copied.Content[i] = e.copyOf(child) |
There was a problem hiding this comment.
A nil child in a caller-built aliased sequence reaches copyOf(nil) and panics. Return an error and cover this case in TestPlainUpdate_NilChild.
There was a problem hiding this comment.
Fixed in 04337f7. measureTree runs before the copy and finds a nil node at any depth. copyAlias then returns ErrInvalidAlias. TestPlainUpdate_NilChild now has a nil node one level below the named node. A nil child that no alias names stays as it is, as before.
| if err := e.expand(child.Alias, true); err != nil { | ||
| return err | ||
| } | ||
| node.Content[i] = e.copyOf(child.Alias) |
There was a problem hiding this comment.
A title-only update replaced # staging deployment on the alias with the anchor’s comment. Preserve the comment at the alias site.
There was a problem hiding this comment.
Fixed in 04337f7. A copy takes the head, line and foot comments of its alias. go-yaml keeps the head and line comment at the start of a block mapping or sequence on its first entry. So in a block copy, the first entry loses the comments of the anchor and gets the line comment of the alias. Flow collections and scalars keep their comments on the node. TestApply_AliasKeepsItsComments checks the exact output for block mappings, block sequences, flow mappings and scalars.
One limit: a copy does not keep a head comment on the first key inside an anchored block mapping, because go-yaml puts the comment of the anchor's place there too. The original keeps it.
Fixes from the review of the alias expansion:
- The engine checks the node limit before it makes a copy. It measures
each named tree once, and it counts the full copy before it allocates.
- Alias copies can add at most 64 MiB of text (values and comments). The
node limit cannot limit the size of the output, because a copy shares
its strings with the node that it copies.
- The alias node counts as a node, as in go-yaml. A flat list of 2,048
aliases to one scalar does not fail with ErrExcessiveAliases now.
- An alias key that names "<<" stays a literal key, as go-yaml reads it.
The copy is a quoted string, so it is not a merge key.
- {<<: {}} becomes {}, so a wildcard target matches no property.
- A merge value that is not a mapping or a list of mappings fails with
the new ErrInvalidMergeKey, as in go-yaml. Before, the engine removed
the merge key and did not report an error.
- A nil node in a tree that an alias names fails with ErrInvalidAlias.
Before, it caused a panic.
- A copy keeps the comments at its alias, not the comments at the
anchor. go-yaml keeps the head and line comment of a block mapping or
sequence on its first entry, so the copy moves the comments there too.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The fault
An overlay failed on a document that uses YAML anchors:
The remove action deleted the node that defines
&shared. The alias under/bstayed, so the output had an alias with no anchor and did not parse:yaml: unknown anchor 'shared' referenced.The same sharing caused a second fault. An action that reached a node through an alias changed every place that shared the node, because an alias and its anchor are one
yaml.Node.The fix
An overlay addresses the JSON data model, where each value belongs to one place. The engine now makes the document a plain tree before it applies an action (
overlay/aliases.go):<<) are applied, with the same precedence asutils.ResolveMergeNodes.{<<: {}}becomes{};An update value that has an alias, an anchor or a merge key gets the same treatment, on a copy. The caller's overlay does not change.
A document with no alias, anchor or merge key keeps every node it has. The cost for it is one walk of the tree.
The expansion follows the rules of go-yaml:
ErrExcessiveAliases.ErrExcessiveAliases.ErrInvalidAlias.<<is a literal key, not a merge key.ErrInvalidMergeKey.go-yaml keeps the head and line comment at the start of a block mapping or sequence on its first entry. So a block copy moves the comments of its alias there. A copy does not keep a head comment on the first key inside an anchored block mapping, because go-yaml puts the comment of the anchor's place there too.
Behaviour changes
ErrInvalidAlias, notErrIncompatibleUpdate.TestApply_RejectCyclicAliasMergeis changed to match.ErrInvalidMergeKey. Before, the overlay kept it unchanged in the output.Tests
overlay/aliases_test.go: remove the anchor owner (one alias, several aliases, a merge key), remove and update through an alias, update the anchor owner, copy the anchor owner, an update value that has an alias, the alias limit, a cycle.<<,{<<: {}}, invalid merge values, a valid list merge, a nil node, and the comments at the alias. Each test failed before the fix. Where go-yaml can read the input, the tests compare the result with go-yaml.go test ./...passes. Review fixes:go test ./overlay/ .passes (the root package is the only one that importsoverlay).overlaypackage is at 100.0% of statements.go test -race ./overlay/passes.go vetandgofmtare clean.🤖 Generated with Claude Code