Refactor brace handling in strFormat() #3446
Open
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.
Problem
The
str.format()function in Please was not properly handling escaped braces ({{and}}), which should be converted to single braces ({and}) according to Python's standard behavior. This caused issues when users tried to generate JSON or other text with literal braces in BUILD files.Bug Report: Issue #3356
Root Cause
The original implementation of
strFormat()in builtins.go only looked for{followed by}to identify placeholders for variable interpolation. It did not check if braces were escaped by doubling them ({{or}}), which is the standard Python syntax for literal braces.Solution
Modified the
strFormat()function (lines 585-672 in builtins.go) to implement a proper state machine that handles:{{): Convert to single{}}): Convert to single}{name}): Still interpolates variables (unchanged)${{var}}): Special case for Please - converts to${var}(unchanged)Example Behavior
Before the fix:
After the fix:
Real-world use case (from issue #3356):
Output:
{ "name": "my_app", "version": "1.0.0" }