fix(update): preserve Kptfile formatting during upgrades
Description
This PR fixes Kptfile formatting drift during package upgrades by switching upgrade-time Kptfile reads/updates to the KRM Functions SDK kptfileko flow via kptfileutil, and by removing the legacy rewrite path from kptops. The SDK-backed path preserves user-authored comments, spacing, and ordering when fields are unchanged, while still applying the intended semantic updates.
Key changes include:
- Upgrade path now uses the centralized SDK-backed read/update/write flow in
kptfileutilinstead of custom rewrite logic inpkg/lib/kptops. kptfileutil.WriteFileandWriteKptfileToFSpreserve formatting by round-tripping throughkptfilekoand applying changes to the existing Kptfile instead of re-serializing from scratch.- SDK-generated annotations are stripped on decode/update to avoid leaking internal metadata into user files.
- Empty-but-non-nil maps/slices are removed so
labels: {}andannotations: {}are not introduced during updates.
Test and normalization hardening:
- Added/expanded Kptfile preservation tests covering idempotent round-trips, comment retention, and unchanged-field formatting stability.
- Updated Kptfile diff normalization in the test runner to be cross-platform and stable:
- CRLF -> LF normalization
- Stable normalization of hunk headers and index lines
- Order-insensitive comparisons for Kptfile map keys and certain quoted scalars
- Symlink assertions are now environment-aware to avoid false failures on systems where symlinks are not materialized.
Motivation
During kpt pkg update, Kptfile rewrites could unintentionally reformat user-authored content (comments, spacing, map ordering), producing noisy diffs and regressions. Issue #4306 tracks this behavior and expects upgrades to preserve Kptfile structure as much as possible while still applying necessary semantic updates. This change ensures formatting stability in the upgrade path while keeping behavior consistent across platforms.
Issue
Fixes #4306
AI use
I have used AI in the creation of this PR. The following AI tool was used
- GitHub Copilot (GPT-5.2-Codex).
I reviewed every change and can explain the design and test assertions independently.
Deploy Preview for kptdocs ready!
| Name | Link |
|---|---|
| Latest commit | 2145ae94a35b40621463bc0be0fde24a65de57da |
| Latest deploy log | https://app.netlify.com/projects/kptdocs/deploys/6a1414758c9b8700089a1ce1 |
| Deploy Preview | https://deploy-preview-4427--kptdocs.netlify.app |
| Preview on mobile | Toggle QR Code...Use your smartphone camera to open QR code link. |
| Make changes | Run an agent on this branch |
To edit notification comments on pull requests, go to your Netlify project configuration.
Please address the comments from Copilot and @mozesl-nokia and fix the tests.
Hi @CsatariGergely, was working on the issue with tests, a little more left to correct regarding podman test (network issue). I will resolve the podman issue and copilot suggestions within 1-2 days
@CsatariGergely All the failing test issues and copilot issues have been resolved, kindly let me know if anything else to look into regarding this branch fix