Skip to content

fix: stop duplicating block-scalar lines that look like comments - #6226

Open
pujitha24 wants to merge 1 commit into
kubernetes-sigs:masterfrom
pujitha24:auto/issue-6225
Open

fix: stop duplicating block-scalar lines that look like comments#6226
pujitha24 wants to merge 1 commit into
kubernetes-sigs:masterfrom
pujitha24:auto/issue-6225

Conversation

@pujitha24

Copy link
Copy Markdown

Motivation:
kustomize/commands/internal/kustfile/kustomizationfile.go preserves
comments across kustomize edit calls with a hand-written, line-based
scan (parseCommentedFields) rather than a YAML-aware parser. It treats
any line that, after trimming leading spaces, starts with '#' as a
comment to reattach to the next recognized top-level field. That
heuristic doesn't know about YAML block scalars: when a field's value
is a literal/folded block scalar (e.g. transformers: |-) and its
content happens to contain lines starting with '#' at the scalar's
indentation, those lines are literal string content, not comments.
They already appear once in the correctly marshaled scalar value, but
the parser also mis-captures them as a "comment" and reattaches that
copy ahead of the next field, duplicating them. Because the duplicate
copy is itself re-captured as a "comment" on the next parse, repeated
kustomize edit set image invocations double the duplicated lines
each time (1 -> 2 -> 4 -> 8 -> 16 ...), growing the file without
bound.

Approach:
Track block-scalar state while scanning: when a line ends with a block
scalar header (|, |-, |+, >, >-, >+, and their explicit-indentation
variants like |2 or |-2), remember that line's indentation. While
inside the block, any non-blank line indented more than the header
line is treated as opaque scalar content and bypasses the
comment/field-match logic entirely, so '#'-prefixed content lines are
no longer mistaken for comments. The block ends at the first non-blank
line whose indentation drops to the header's level or below, matching
how YAML determines block scalar extent. Blank-line handling is
otherwise unchanged.

Known limitation, disclosed rather than fixed here: a block scalar
containing a genuinely blank interior line can still cause the number
of blank lines before the next field to grow by one on each repeated
write. This is a distinct, pre-existing bug (confirmed present with
this change reverted too, growing at the same linear rate) about
blank-line accounting, not the exponential content-duplication bug
reported in the issue below. Fixing it would require a larger rework
of this file's ad hoc comment-preservation model, so it's left out of
scope for this minimal fix.

Validation:

  • go test ./commands/internal/kustfile/... -run TestPreserve -v
    (in the kustomize module): passes, including a new regression test,
    TestPreserveBlockScalarCommentLikeContent, which performs 3 simulated
    kustomize edit set image invocations (fresh NewKustomizationFile /
    Read / Write per iteration, matching RunSetImage's real usage) against
    a kustomization.yaml with a '#'-prefixed line inside a transformers
    block scalar, and asserts the line appears exactly once afterward.
    Confirmed this test fails (line duplicated) on the pre-fix code and
    passes after the fix.
  • go build ./... and go test ./... in the kustomize module: all pass.
  • Built the kustomize binary and reran the exact reproduction from the
    issue (transformers block scalar with two '#'-prefixed marker lines,
    4 repeated kustomize edit set image calls): marker count stays at 1
    throughout, versus exponential 1/2/4/8/16 before the fix.
  • gofmt -l and go vet ./commands/internal/kustfile/...: clean.
  • make lint (this module's golangci-lint target, matching this repo's
    CI Lint job for the kustomize module): exits 0 with no findings
    against the changed files.

Report: #6225
Signed-off-by: Pujitha Paladugu 10557236+pujitha24@users.noreply.github.com

Fixes #6225

fix: stop duplicating block-scalar lines that look like comments

Motivation:
kustomize/commands/internal/kustfile/kustomizationfile.go preserves
comments across `kustomize edit` calls with a hand-written, line-based
scan (parseCommentedFields) rather than a YAML-aware parser. It treats
any line that, after trimming leading spaces, starts with '#' as a
comment to reattach to the next recognized top-level field. That
heuristic doesn't know about YAML block scalars: when a field's value
is a literal/folded block scalar (e.g. `transformers: |-`) and its
content happens to contain lines starting with '#' at the scalar's
indentation, those lines are literal string content, not comments.
They already appear once in the correctly marshaled scalar value, but
the parser also mis-captures them as a "comment" and reattaches that
copy ahead of the next field, duplicating them. Because the duplicate
copy is itself re-captured as a "comment" on the next parse, repeated
`kustomize edit set image` invocations double the duplicated lines
each time (1 -> 2 -> 4 -> 8 -> 16 ...), growing the file without
bound.

Approach:
Track block-scalar state while scanning: when a line ends with a block
scalar header (|, |-, |+, >, >-, >+, and their explicit-indentation
variants like |2 or |-2), remember that line's indentation. While
inside the block, any non-blank line indented more than the header
line is treated as opaque scalar content and bypasses the
comment/field-match logic entirely, so '#'-prefixed content lines are
no longer mistaken for comments. The block ends at the first non-blank
line whose indentation drops to the header's level or below, matching
how YAML determines block scalar extent. Blank-line handling is
otherwise unchanged.

Known limitation, disclosed rather than fixed here: a block scalar
containing a genuinely blank interior line can still cause the number
of blank lines before the next field to grow by one on each repeated
write. This is a distinct, pre-existing bug (confirmed present with
this change reverted too, growing at the same linear rate) about
blank-line accounting, not the exponential content-duplication bug
reported in the issue below. Fixing it would require a larger rework
of this file's ad hoc comment-preservation model, so it's left out of
scope for this minimal fix.

Validation:
- go test ./commands/internal/kustfile/... -run TestPreserve -v
  (in the kustomize module): passes, including a new regression test,
  TestPreserveBlockScalarCommentLikeContent, which performs 3 simulated
  `kustomize edit set image` invocations (fresh NewKustomizationFile /
  Read / Write per iteration, matching RunSetImage's real usage) against
  a kustomization.yaml with a '#'-prefixed line inside a transformers
  block scalar, and asserts the line appears exactly once afterward.
  Confirmed this test fails (line duplicated) on the pre-fix code and
  passes after the fix.
- go build ./... and go test ./... in the kustomize module: all pass.
- Built the kustomize binary and reran the exact reproduction from the
  issue (transformers block scalar with two '#'-prefixed marker lines,
  4 repeated `kustomize edit set image` calls): marker count stays at 1
  throughout, versus exponential 1/2/4/8/16 before the fix.
- gofmt -l and go vet ./commands/internal/kustfile/...: clean.
- make lint (this module's golangci-lint target, matching this repo's
  CI Lint job for the kustomize module): exits 0 with no findings
  against the changed files.

Report: kubernetes-sigs#6225
Signed-off-by: Pujitha Paladugu <10557236+pujitha24@users.noreply.github.com>
@kubernetes-prow

Copy link
Copy Markdown
Contributor

Welcome @pujitha24!

It looks like this is your first PR to kubernetes-sigs/kustomize 🎉. Please refer to our pull request process documentation to help your PR have a smooth ride to approval.

You will be prompted by a bot to use commands during the review process. Do not be afraid to follow the prompts! It is okay to experiment. Here is the bot commands documentation.

You can also check if kubernetes-sigs/kustomize has its own contribution guidelines.

You may want to refer to our testing guide if you run into trouble with your tests not passing.

If you are having difficulty getting your pull request seen, please follow the recommended escalation practices. Also, for tips and tricks in the contribution process you may want to read the Kubernetes contributor cheat sheet. We want to make sure your contribution gets all the attention it needs!

Thank you, and welcome to Kubernetes. 😃

@kubernetes-prow
kubernetes-prow Bot requested a review from koba1t August 14, 2026 23:15
@kubernetes-prow kubernetes-prow Bot added cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Aug 14, 2026
@kubernetes-prow

Copy link
Copy Markdown
Contributor

Hi @pujitha24. Thanks for your PR.

I'm waiting for a kubernetes-sigs member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work.

Tip

We noticed you've done this a few times! Consider joining the org to skip this step and gain /lgtm and other bot rights. We recommend asking approvers on your previous PRs to sponsor you.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@kubernetes-prow kubernetes-prow Bot added the size/M Denotes a PR that changes 30-99 lines, ignoring generated files. label Aug 14, 2026

@saitejabandaru-in saitejabandaru-in left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

LGTM! This fixes a very annoying and hard-to-debug issue with comment duplication in kustomize edit set image. The approach of tracking when we are inside a literal block scalar and respecting the indentation level to determine scalar boundaries is extremely sound. Thanks for catching this!

@kubernetes-prow

Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: pujitha24, saitejabandaru-in
Once this PR has been reviewed and has the lgtm label, please assign varshaprasad96 for approval. For more information see the Code Review Process.

The full list of commands accepted by this bot can be found 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. 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.

kustomize edit set image exponentially duplicates block-scalar content

2 participants