Fix envsubst parse of empty replace pattern#1247
Open
arpitjain099 wants to merge 1 commit into
Open
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.