WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Conversation

@scyyx5
Copy link

@scyyx5 scyyx5 commented Nov 3, 2025

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:

  1. Escaped opening braces ({{): Convert to single {
  2. Escaped closing braces (}}): Convert to single }
  3. Placeholder syntax ({name}): Still interpolates variables (unchanged)
  4. Shell variable syntax (${{var}}): Special case for Please - converts to ${var} (unchanged)

Example Behavior

Before the fix:

"{{hello}}".format()  # Returned: "{{hello}}"

After the fix:

"{{hello}}".format()  # Returns: "{hello}"

Real-world use case (from issue #3356):

cmd = """
cat > $OUT << EOF
{{
  "name": "{name}",
  "version": "{version}"
}}
EOF
""".format(name="my_app", version="1.0.0")

Output:

{
  "name": "my_app",
  "version": "1.0.0"
}

Copy link
Contributor

@toastwaffle toastwaffle left a comment

Choose a reason for hiding this comment

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

I think #3412 fixes the same thing as this - @chrisnovakovic are you planning to push that forward?

@chrisnovakovic
Copy link
Contributor

I think #3412 fixes the same thing as this - @chrisnovakovic are you planning to push that forward?

Yes, I'm planning on addressing the remaining comments in #3412 (which really amount to comments and error messages).

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.

4 participants