kpt icon indicating copy to clipboard operation
kpt copied to clipboard

fix(update): preserve Kptfile formatting during upgrades

Open Jaisheesh-2006 opened this issue 8 months ago • 4 comments

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 kptfileutil instead of custom rewrite logic in pkg/lib/kptops.
  • kptfileutil.WriteFile and WriteKptfileToFS preserve formatting by round-tripping through kptfileko and 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: {} and annotations: {} 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.

Jaisheesh-2006 avatar Mar 06 '26 07:03 Jaisheesh-2006

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...

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.

netlify avatar Mar 06 '26 07:03 netlify

Please address the comments from Copilot and @mozesl-nokia and fix the tests.

CsatariGergely avatar Apr 22 '26 12:04 CsatariGergely

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

Jaisheesh-2006 avatar Apr 23 '26 16:04 Jaisheesh-2006

@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

Jaisheesh-2006 avatar Apr 24 '26 14:04 Jaisheesh-2006