BabylonJS/Babylon.js · error · Error
"Multiple main functions found in shader code"
Error message
"Multiple main functions found in shader code"
What it means
ExtractFunctions identifies the shader entry point by finding a function whose code contains the `// main` marker. If a second function also contains `// main`, the parser cannot decide which is the entry point and throws. Exactly one function must be marked `// main`.
Source
Thrown at packages/dev/smartFilters/src/utils/buildTools/shaderConverter.ts:387
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(),
});
pos = endIndex;
}
if (!mainFunctionName) {
throw new Error("No main function found in shader code");
}
View on GitHub (pinned to 0592b347b8)
Solutions
- Ensure only the entry-point function contains the exact `// main` comment; remove it from all other functions.
- Reword any comments in helper functions that contain the literal text '// main'.
- Search the whole shader for '// main' occurrences and keep exactly one.
Example fix
// before
void helper() { /* mirrors // main */ }
void main(...) // main
{ ... }
// after
void helper() { /* mirrors the entry point */ }
void main(...) // main
{ ... } Defensive patterns
Strategy: validation
Validate before calling
if ((shader.match(/\/\/ main/g) || []).length !== 1) throw new Error("Shader must contain exactly one '// main' marker"); Type guard
function hasSingleMain(shader: string): boolean {
return (shader.match(/\/\/\s*main/g) || []).length === 1;
} Try / catch
try {
const info = ParseFragmentShader(blockName, namespace, shader);
} catch (e) {
if (e instanceof Error && e.message === "Multiple main functions found in shader code") {
// remove the extra '// main' marker(s) before retrying
} else throw e;
} Prevention
- Mark only the entry-point function with '// main'.
- Avoid the literal text '// main' anywhere else, including helper comments.
- When duplicating main for experimentation, strip the marker from the copy immediately.
- CI-grep shader assets: exactly one '// main' allowed.
When it happens
Trigger: ExtractFunctions (via ParseFragmentInfo) on a shader where the `// main` comment appears in two function bodies — e.g. the marker was left in a copied helper function as well as the real entry function, or a comment inside a function literally says '// main'.
Common situations: Duplicating the main function to modify it and forgetting to remove the original's `// main` marker; stray comments like 'this mirrors // main' inside helpers; template concatenation merging two main-containing shaders.
Related errors
- "Const line not found"
- `Consts must have a name, type, and a default value: '${cons
- `Consts must have a name: '${constLine}'`
- `Consts must have a value: '${constLine}'`
- `Unsupported const property type: '${type}'`
AI-assisted analysis of BabylonJS/Babylon.js@0592b347b8 (2026-08-30).
Data as JSON: /api/errors/cfc1d5007b3b76e5.
Report an issue: GitHub.