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

  1. Ensure only the entry-point function contains the exact `// main` comment; remove it from all other functions.
  2. Reword any comments in helper functions that contain the literal text '// main'.
  3. 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

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


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