{"record":{"id":"5320549b0a2b5c78","repo":"BabylonJS/Babylon.js","slug":"mismatched-curly-braces-found-near-functionnam","errorCode":null,"errorMessage":"`Mismatched curly braces found near: ${functionName}`","messagePattern":"`Mismatched curly braces found near: (.+?)`","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/dev/smartFilters/src/utils/buildTools/shaderConverter.ts","lineNumber":378,"sourceCode":"        const functionParams = match[2] || \"\";\r\n\r\n        // Store start index of the function definition\r\n        const startIndex = match.index;\r\n\r\n        // Balance braces to find end of function, starting just after the opening `{`\r\n        let endIndex = match.index + match[0].length;\r\n        let depth = 1;\r\n        while (depth > 0 && endIndex < fragment.length) {\r\n            if (fragment[endIndex] === \"{\") {\r\n                depth++;\r\n            } else if (fragment[endIndex] === \"}\") {\r\n                depth--;\r\n            }\r\n            endIndex++;\r\n        }\r\n\r\n        if (depth !== 0) {\r\n            throw new Error(`Mismatched curly braces found near: ${functionName}`);\r\n        }\r\n\r\n        // Finally, process the function code\r\n        let functionCode = fragment.substring(startIndex, endIndex).trim();\r\n\r\n        // Check if this function is the main function\r\n        if (functionCode.includes(\"// main\")) {\r\n            if (mainFunctionName) {\r\n                throw new Error(\"Multiple main functions found in shader code\");\r\n            }\r\n            mainFunctionName = functionName;\r\n            functionCode = functionCode.replace(\"// main\", \"\");\r\n        }\r\n\r\n        extractedFunctions.push({\r\n            name: functionName,\r\n            code: functionCode,\r\n            params: functionParams.trim(),\r","sourceCodeStart":360,"sourceCodeEnd":396,"githubUrl":"https://github.com/BabylonJS/Babylon.js/blob/0592b347b8a4ee0236089ea86a749cacfdb266d8/packages/dev/smartFilters/src/utils/buildTools/shaderConverter.ts#L360-L396","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Count and balance braces for the named function; add the missing '}'.","Check braces inside comments, strings, and macro expansions — move or neutralize them, since the parser counts raw '{'/'}' characters.","Run a GLSL validator or editor brace matching on the shader file before conversion.","Re-run the build after fixing; the error names the function where imbalance was detected."],"exampleFix":"// before\nvoid adjust(vec3 c) {\n  c *= 2.0;\n// missing }\n// after\nvoid adjust(vec3 c) {\n  c *= 2.0;\n}","handlingStrategy":"validation","validationCode":"function bracesBalanced(src: string): boolean {\n  let depth = 0;\n  for (const ch of src) {\n    if (ch === \"{\") depth++;\n    else if (ch === \"}\") { depth--; if (depth < 0) return false; }\n  }\n  return depth === 0;\n}\nif (!bracesBalanced(shader)) throw new Error(\"Unbalanced braces in shader source\");","typeGuard":null,"tryCatchPattern":"try {\n  const info = ParseFragmentShader(blockName, namespace, shader);\n} catch (e) {\n  if (e instanceof Error && e.message.startsWith(\"Mismatched curly braces found near\")) {\n    const fn = /near: (\\w+)/.exec(e.message)?.[1];\n    console.error(`Check braces in function '${fn}'`);\n  } else throw e;\n}","preventionTips":["Use an editor with GLSL brace matching; never save mid-edit shaders.","Avoid unbalanced '{'/'}' inside comments, strings, and macros — the parser counts raw characters.","Run a brace-balance check on shader files in CI."],"tags":["shader","parsing","glsl","syntax","braces"],"backgroundTag":"unbalanced-braces","analyzedSha":"0592b347b8a4ee0236089ea86a749cacfdb266d8","analyzedAt":"2026-08-30T15:11:20.442Z","schemaVersion":2},"datasetVersion":"2026-08-31T09:17:48.483Z"}