BabylonJS/Babylon.js · error · Error

`Mismatched curly braces found near: ${functionName}`

Error message

`Mismatched curly braces found near: ${functionName}`

What it means

After matching a function header, ExtractFunctions balances curly braces from the opening '{' to find the function's end. If it reaches end-of-file with a non-zero brace depth, the function body is unterminated (or braces are unbalanced), so the parser cannot slice the function code and throws naming the function.

Source

Thrown at packages/dev/smartFilters/src/utils/buildTools/shaderConverter.ts:378

        const functionParams = match[2] || "";

        // Store start index of the function definition
        const startIndex = match.index;

        // Balance braces to find end of function, starting just after the opening `{`
        let endIndex = match.index + match[0].length;
        let depth = 1;
        while (depth > 0 && endIndex < fragment.length) {
            if (fragment[endIndex] === "{") {
                depth++;
            } else if (fragment[endIndex] === "}") {
                depth--;
            }
            endIndex++;
        }

        if (depth !== 0) {
            throw new Error(`Mismatched curly braces found near: ${functionName}`);
        }

        // Finally, process the function code
        let functionCode = fragment.substring(startIndex, endIndex).trim();

        // Check if this function is the main function
        if (functionCode.includes("// main")) {
            if (mainFunctionName) {
                throw new Error("Multiple main functions found in shader code");
            }
            mainFunctionName = functionName;
            functionCode = functionCode.replace("// main", "");
        }

        extractedFunctions.push({
            name: functionName,
            code: functionCode,
            params: functionParams.trim(),

View on GitHub (pinned to 0592b347b8)

Solutions

  1. Count and balance braces for the named function; add the missing '}'.
  2. Check braces inside comments, strings, and macro expansions — move or neutralize them, since the parser counts raw '{'/'}' characters.
  3. Run a GLSL validator or editor brace matching on the shader file before conversion.
  4. Re-run the build after fixing; the error names the function where imbalance was detected.

Example fix

// before
void adjust(vec3 c) {
  c *= 2.0;
// missing }
// after
void adjust(vec3 c) {
  c *= 2.0;
}
Defensive patterns

Strategy: validation

Validate before calling

function bracesBalanced(src: string): boolean {
  let depth = 0;
  for (const ch of src) {
    if (ch === "{") depth++;
    else if (ch === "}") { depth--; if (depth < 0) return false; }
  }
  return depth === 0;
}
if (!bracesBalanced(shader)) throw new Error("Unbalanced braces in shader source");

Try / catch

try {
  const info = ParseFragmentShader(blockName, namespace, shader);
} catch (e) {
  if (e instanceof Error && e.message.startsWith("Mismatched curly braces found near")) {
    const fn = /near: (\w+)/.exec(e.message)?.[1];
    console.error(`Check braces in function '${fn}'`);
  } else throw e;
}

Prevention

When it happens

Trigger: ExtractFunctions on a fragment shader where a function body is missing its closing '}', has an extra '{', or contains an unbalanced brace inside a string/comment that the naive depth counter miscounts.

Common situations: Incomplete shaders saved mid-edit; accidentally deleting a closing brace; braces inside #define expansions or string literals confusing the simple depth counting; auto-formatter corruption.

Related errors


AI-assisted analysis of BabylonJS/Babylon.js@0592b347b8 (2026-08-30). Data as JSON: /api/errors/5320549b0a2b5c78. Report an issue: GitHub.