BabylonJS/Babylon.js · error · Error
"Const line not found"
Error message
"Const line not found"
What it means
ParseFragmentShader in the smartFilters shader converter throws this while iterating const declarations that carry a {"property":...} annotation. The regex matched a 'const' group, but capture group 2 (the const line text itself) is empty. It is a defensive invariant check — it almost always means the annotation/const pairing in the shader source is malformed rather than a real empty const line.
Source
Thrown at packages/dev/smartFilters/src/utils/buildTools/shaderConverter.ts:210
for (const matches of constGroups) {
const annotationJSON = matches[1];
// If it doesn't have any annotation, treat it as a regular const
if (!annotationJSON) {
continue;
}
const annotation = JSON.parse(annotationJSON.replace("//", "").trim()) as ConstMetadataAnnotation;
// If the annotation doesn't have a "property" field, treat it as a regular const
if (!annotation.property) {
continue;
}
const constLine = matches[2];
if (!constLine) {
throw new Error("Const line not found");
}
const constLineMatches = new RegExp(/^const\s+(\w+)\s+(\w+)\s*=\s*([^\s;]+)\s*;?\s*$/gm).exec(constLine);
if (!constLineMatches || constLineMatches.length < 4) {
throw new Error(`Consts must have a name, type, and a default value: '${constLine}'`);
}
const type = constLineMatches[1];
const friendlyName = constLineMatches[2];
const defaultValue = constLineMatches[3];
if (!friendlyName) {
throw new Error(`Consts must have a name: '${constLine}'`);
}
if (defaultValue === null) {
throw new Error(`Consts must have a value: '${constLine}'`);
}
const constProperty: ConstPropertyMetadata | null =
View on GitHub (pinned to 0592b347b8)
Solutions
- Ensure every // {"property":...} annotation is immediately followed (next line) by a full `const <type> <name> = <value>;` declaration.
- Keep the annotation and its const in the same block and do not run code strippers/minifiers on the shader source before conversion.
- If the const should not be exposed as a property, remove the 'property' field from the annotation (or the whole annotation) so the parser skips it.
- Add the const line back explicitly, e.g. `const float myVal = 0.5;` right after the annotation.
Example fix
// before
// {"property": {"type": "float", "options": {...}}}
float unrelated = 1.0;
// after
// {"property": {"type": "float", "options": {...}}}
const float brightness = 0.5; Defensive patterns
Strategy: validation
Validate before calling
const annotated = shader.match(/\/\/\s*\{.*\}\s*\n\s*const .*/g);
if (!annotated || annotated.some(m => !/^\/\/\s*\{.*\}\s*\n\s*const \w+ \w+ = [^\s;]+;?\s*$/m.test(m))) {
throw new Error("Every property annotation must be immediately followed by a single-line const declaration");
} Type guard
function hasConstAfterAnnotation(shader: string): boolean {
return [...shader.matchAll(/(\/\/\s*\{.*\}\s*(?:\r?\n)+)(const .*)/gm)].every(m => typeof m[2] === "string" && m[2].length > 0);
} Try / catch
try {
const info = ParseFragmentShader(blockName, namespace, shader);
} catch (e) {
if (e instanceof Error && e.message === "Const line not found") {
// fix the annotation/const pairing in the shader source and retry once
} else throw e;
} Prevention
- Always place property annotations on the line directly above their const declaration.
- Never run minifiers or code strippers on shader source before ParseFragmentShader.
- Keep a unit test that converts every shipped shader at build time.
- Validate shaders with a lint rule that pairs // {...} annotations with following const lines.
When it happens
Trigger: Calling ParseFragmentShader (via fragmentShaderInfo/result) on a fragment shader where a // {"property":...} annotation is present but the immediately following const declaration cannot be captured by the regex /(\/\/\s*\{.*\}\s*(?:\r\n|\r|\n)+)?(const .*)/gm (e.g. annotation separated from a 'const' keyword by intervening code, or the const text was mangled by preprocessing).
Common situations: Hand-edited shaders where the annotation comment and the const line got separated or merged; shader strings built programmatically with wrong line endings; minified/stripped shaders where the const line was removed but the annotation comment left behind; copy-paste of annotations without their const.
Related errors
- `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}'`
- "No function name found in shader code"
AI-assisted analysis of BabylonJS/Babylon.js@0592b347b8 (2026-08-30).
Data as JSON: /api/errors/81ea22ee2a2a26b8.
Report an issue: GitHub.