Skip to content

fix: report malformed --state-values-set-string assignments - #2855

Merged
yxxhero merged 4 commits into
helmfile:mainfrom
spicysharkco:fix/state-values-set-string-malformed
Oct 30, 2026
Merged

yxxhero merged 4 commits into
helmfile:mainfrom
spicysharkco:fix/state-values-set-string-malformed

Conversation

@spicysharkco

Copy link
Copy Markdown
Contributor

Problem

--state-values-set-string 'a=1,b' sets a and drops b with no error. 'a=1,b,c=3' sets a key named b,c; 'a=1,' and '=x' are ignored. --state-values-set rejects all four with invalid assignment "b": expected <key>=<value>.

The docs say malformed assignments are reported as errors, not partially applied.

Solution

parseStateValuesSetString applies what its pattern matches and skips the rest, and its key pattern accepts a bare comma.

Text between matches now returns the same error before anything is applied, and a key cannot hold a bare comma. Values, escaped and quoted commas, and repeated flags parse as before. Text after a quoted value ('a="x"junk') or a comma and space ('a=1, b=2') now fails too.

Test

Five rows go into TestNewCLIConfigImplStateValuesSetString: the four cases above and an escaped comma in a key.

go test ./pkg/config -run '^TestNewCLIConfigImplStateValuesSetString$' -count=1 -p 1

13 of 13 rows pass. With the fix reverted, the four error rows fail and the other nine pass.

yxxhero
yxxhero previously approved these changes Oct 27, 2026

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

Verified by running both parsers side by side (--state-values-set vs --state-values-set-string) over the edge cases:

input before after --state-values-set
a=1,b set a, drop b error "b" error "b" ✅
a=1,b,c=3 key b,c error "b" error "b" ✅
=hello ignored error "=hello" error "=hello" ✅
a=1, ignored error "" error "" ✅
a=1,,b=2 key ,b error "" error "" ✅
a="x"junk silently applied error "junk" n/a (quotes are set-string only)
a\,b=1 key a,b key a,b key a,b ✅

Positive behavior is unchanged: quoted values, escaped commas in keys and values, trailing backslashes, repeated flags, and dotted keys all parse as before. The error message style matches --state-values-set (flag-prefixed, same wording), and nothing is applied to g before the error (SetSet only runs after all raw values parse) — the new assert.Empty guard covers that.

One gap I fixed in 7212e7f: a leading comma (,count=2) was swallowed by the following assignment's match and went unreported, while --state-values-set rejects the same input with invalid assignment "". It now reports the same error, and a test pins the deliberate no-op on an empty flag value (--state-values-set-string ''), which stays lenient on purpose — erroring there would break the common "${MAYBE_EMPTY},key=val" scripting pattern.

Remaining divergences worth a follow-up on the --state-values-set side (not this PR's scope):

  • --state-values-set '' errors with invalid assignment ""; set-string is a no-op. The sibling's error looks accidental (splitOnUnescapedCommas always appends the final, empty segment).
  • --state-values-set 'a=1, b=2' is accepted with a key literally named " b" — a space-led key is a surprising result; set-string's rejection is the better behavior.

go test ./pkg/config -count=1 -p 1, go build ./..., go vet, and golangci-lint run pkg/config/... all clean with this on top.

@yxxhero

yxxhero commented Oct 27, 2026

Copy link
Copy Markdown
Member

@spicysharkco — following up on the two --state-values-set (sibling parser) divergences noted in my review: do you think they're worth fixing, and if so would you like to take them as a follow-up PR? This one is nicely scoped to --state-values-set-string, so a separate PR seems right.

  1. --state-values-set '' fails with invalid assignment "". splitOnUnescapedCommas always emits the trailing empty segment, so this looks accidental. I'd lean toward making it a no-op, matching --state-values-set-string ''.

  2. --state-values-set 'a=1, b=2' is accepted with a key literally named " b". A space-led key is almost certainly a typo. Two plausible directions: reject the assignment (consistent with this PR's strictness on the set-string side), or trim whitespace after the separator comma (friendlier, but then --state-values-set-string should follow for consistency).

No pressure either way — happy to review the follow-up if you decide to take it on.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

Even-length backslash runs before commas in keys still allow malformed assignments to pass validation and silently merge into one key.

1 open issue
What changed in this PR

This PR tightens Helmfile’s --state-values-set-string parsing to report malformed assignments rather than silently skip them or merge them into another key.

Changes:

  • Reject unmatched text and leading or trailing commas before applying overrides.
  • Restrict bare commas in keys while retaining escaped commas.
  • Add parsing regression cases and verify errors leave overrides empty.
File Description
pkg/​config/​config.go Tightens assignment matching and validates unmatched input.
pkg/​config/​config_test.go Adds malformed-input and escaped-key cases, plus error atomicity assertions.

Review effort: Balanced

Comment thread pkg/config/config.go Resolved
@spicysharkco

spicysharkco commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor Author

Added even/odd backslash parity regressions in 482ce67e; the branch already contained the regex correction. Two backslashes before a comma now expose the separator and reject the orphan key, while three preserve the escaped comma. Focused/package tests and vet pass.

The two --state-values-set divergences are separate from this fix, so I am keeping this PR scoped and will not open a follow-up under the current scope.

@spicysharkco
spicysharkco force-pushed the fix/state-values-set-string-malformed branch from 36070ff to 482ce67 Compare October 29, 2026 09:34
spicysharkco and others added 4 commits October 29, 2026 18:03
--state-values-set-string 'a=1,b' kept a=1 and dropped b without an
error, and 'a=1,b,c=3' set a key named "b,c". A missing "=", an empty
key and a trailing comma were ignored, although the docs say malformed
assignments are reported as errors instead of being partially applied.

Return an invalid assignment error, worded like the one
--state-values-set returns, for any text the assignment pattern does not
match, before anything is applied, and stop a key from holding a bare
comma. Values, escaped and quoted commas, empty values and repeated
flags parse as before.

Signed-off-by: spicysharkco <[email protected]>
A leading comma was swallowed by the following assignment's match and
went unreported, while --state-values-set rejects the same input with
invalid assignment "". Report it the same way, and pin the deliberate
no-op on an empty flag value with a test.

Signed-off-by: yxxhero <[email protected]>
Update regular expression to handle escaped characters correctly.

Co-authored-by: Copilot Autofix powered by AI <[email protected]>
@yxxhero
yxxhero force-pushed the fix/state-values-set-string-malformed branch from 482ce67 to 503e9aa Compare October 29, 2026 10:04
@yxxhero
yxxhero merged commit 3267e70 into helmfile:main Oct 30, 2026
28 of 39 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants