Skip to content

Fix envsubst parse of empty replace pattern#1247

Open
arpitjain099 wants to merge 1 commit into
fluxcd:mainfrom
arpitjain099:chore/envsubst-empty-prefix
Open

Fix envsubst parse of empty replace pattern#1247
arpitjain099 wants to merge 1 commit into
fluxcd:mainfrom
arpitjain099:chore/envsubst-empty-prefix

Conversation

@arpitjain099

@arpitjain099 arpitjain099 commented Jun 26, 2026

Copy link
Copy Markdown

Fixes #947.

An empty pattern in a replace expansion is valid in bash: ${VAR/#/prefix-} prepends and ${VAR/%/-suffix} appends. envsubst rejects both with "unable to parse substitution within function". In parseReplaceFunc, after consuming the / and the # (or %) it scans the pattern with acceptNotSlash, but when the pattern is empty the next rune is already the delimiter, so it accepts zero chars and returns an illegal token.

The fix peeks for the delimiter first and records an empty pattern node instead of scanning, keeping the two-arg shape the eval side expects (replacePrefix/replaceSuffix already handle an empty pattern, since HasPrefix/HasSuffix with "" are true). This mirrors the existing empty-replacement handling a few lines below. Added eval and parse test cases that fail on current code with the same error from the issue and pass with the fix; go test -race, go vet and gofmt are clean for the envsubst packages.

This only covers #947 and its empty-suffix sibling. It doesn't touch #948 (${VAR/#""/prefix-}, an explicitly quoted empty pattern), which goes through a different scanning path and currently produces a silent no-op rather than a parse error - leaving that for a separate change.

The replace parser rejected an empty pattern such as ${VAR/#/prefix-}
or ${VAR/%/-suffix} with ErrParseFuncSubstitution because the arg[1]
scan hit the delimiter immediately and produced an illegal token.

Peek for the delimiter before scanning the pattern and emit an empty
pattern node, mirroring the existing empty-replacement handling. This
keeps the two-argument shape the eval side already supports and matches
bash, where an empty prefix or suffix pattern prepends or appends the
replacement.

Signed-off-by: Arpit Jain <arpitjain099@gmail.com>
Assisted-by: Claude Code
@arpitjain099 arpitjain099 requested a review from a team as a code owner June 26, 2026 02:58
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.

Bug: Substitution fails on prefix replacement with empty prefix

1 participant