Skip to content

fix: preserve manually added files when dir contains fileTree and file nodes#483

Merged
VelmiraS merged 2 commits into
masterfrom
fix/filetree-manual-file-lost
Jul 21, 2026
Merged

fix: preserve manually added files when dir contains fileTree and file nodes#483
VelmiraS merged 2 commits into
masterfrom
fix/filetree-manual-file-lost

Conversation

@VelmiraS

Copy link
Copy Markdown
Contributor

Summary

  • RemoveNodeFromParent used a swap-and-truncate pattern to remove nodes, which silently deleted the last sibling of the removed fileTree node
  • Replaced with order-preserving deletion (append(s[:i], s[i+1:]...)) so manually added file: nodes are not lost during fileTree expansion
  • Updated existing result files to reflect the now-correct stable ordering
  • Added regression test covering a dir with both fileTree and a manually added file entry

Fixes the bug reported where a manually added file appears at the top level of the TOC instead of inside its parent directory's subnav.

Test plan

  • go test ./pkg/manifest/... — 6/6 pass
  • go test ./... — all pass
  • Run gen-toc with a manifest containing a dir with both fileTree: and file: — manually added file appears under the correct directory in the TOC

…e nodes

RemoveNodeFromParent used swap-and-truncate which silently deleted the
last sibling of a removed fileTree node. Replace with order-preserving
deletion so manually added file nodes are not lost during fileTree
expansion.

Add regression test covering a dir with both fileTree and a manually
added file entry.
@gardener-prow gardener-prow Bot added do-not-merge/needs-kind Indicates a PR lacks a `kind/foo` label and requires one. cla: yes Indicates the PR's author has signed the cla-assistant.io CLA. size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Jul 17, 2026
@VelmiraS VelmiraS self-assigned this Jul 17, 2026
RemoveNodeFromParent inside a range loop shifted the underlying array
while the range header still held the original length, causing elements
to be visited twice and triggering a false file-collision error.

Collect nodes to remove during the loop and delete them after.
Comment thread pkg/manifest/manifest.go
Comment thread pkg/manifest/manifest.go
nodeNameToNode[child.File] = child
}
}
for _, child := range toRemove {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Phase 1 – We loop through node.Structure with for _, child := range node.Structure. During this loop, we do not change node.Structure. If we find a node that should be removed, we only do toRemove = append(toRemove, child). toRemove is a different slice, so it does not affect node.Structure. The loop continues safely because node.Structure does not change while we are iterating over it.

Phase 2 – After the first loop is completely finished, we start a second loop: for _, child := range toRemove. Now it is safe to modify node.Structure because we are no longer iterating over it.
The old version of the remove function moved the last element into the removed element's position.

@klocke-io klocke-io left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

/lgtm
/approve

@gardener-prow gardener-prow Bot added the lgtm Indicates that a PR is ready to be merged. label Jul 21, 2026
@gardener-prow

gardener-prow Bot commented Jul 21, 2026

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: klocke-io

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@gardener-prow

gardener-prow Bot commented Jul 21, 2026

Copy link
Copy Markdown

LGTM label has been added.

DetailsGit tree hash: 2e03392a6e466b0fa44da0c4c527a530a136f2f3

@gardener-prow gardener-prow Bot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Jul 21, 2026
@VelmiraS
VelmiraS merged commit 23491ce into master Jul 21, 2026
39 of 40 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approved Indicates a PR has been approved by an approver from all required OWNERS files. cla: yes Indicates the PR's author has signed the cla-assistant.io CLA. do-not-merge/needs-kind Indicates a PR lacks a `kind/foo` label and requires one. lgtm Indicates that a PR is ready to be merged. size/M Denotes a PR that changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants