{"record":{"id":"42d648bcdc4e7b03","repo":"different-ai/openwork","slug":"failed-to-parse-typescript-flattendiagnosticmes","errorCode":null,"errorMessage":"Failed to parse TypeScript: ${flattenDiagnosticMessageText(diagnostic.messageText, \"\\n\")}","messagePattern":"Failed to parse TypeScript: (.+?)","errorType":"exception","errorClass":"InterpreterRuntimeError","httpStatus":null,"severity":"error","filePath":"packages/codemode/src/interpreter/runtime.ts","lineNumber":127,"sourceCode":"  SandboxPromise,\n  SandboxRegExp,\n  SandboxSet,\n  SandboxURL,\n  SandboxURLSearchParams,\n} from \"../values.js\"\n\nconst parseProgram = (code: string): ProgramNode => {\n  const transpiled = transpileModule(`async function __codemode__() {\\n${code}\\n}`, {\n    reportDiagnostics: true,\n    compilerOptions: {\n      target: ScriptTarget.ESNext,\n      module: ModuleKind.ESNext,\n    },\n  })\n  const diagnostic = transpiled.diagnostics?.find((item) => item.category === DiagnosticCategory.Error)\n\n  if (diagnostic) {\n    throw new InterpreterRuntimeError(\n      `Failed to parse TypeScript: ${flattenDiagnosticMessageText(diagnostic.messageText, \"\\n\")}`,\n      undefined,\n      \"ParseError\",\n    )\n  }\n\n  const bodyStart = transpiled.outputText.indexOf(\"{\") + 1\n  const bodyEnd = transpiled.outputText.lastIndexOf(\"}\")\n  const executableCode = transpiled.outputText.slice(bodyStart, bodyEnd)\n  const parsed = parse(executableCode, {\n    ecmaVersion: \"latest\",\n    sourceType: \"script\",\n    allowReturnOutsideFunction: true,\n    allowAwaitOutsideFunction: true,\n    locations: true,\n  }) as unknown\n\n  if (!isRecord(parsed) || parsed.type !== \"Program\" || !Array.isArray(parsed.body)) {","sourceCodeStart":109,"sourceCodeEnd":145,"githubUrl":"https://github.com/different-ai/openwork/blob/2b7df46e8ae1517d64c896c7793d2d52ec845669/packages/codemode/src/interpreter/runtime.ts#L109-L145","documentation":"parseProgram first transpiles the submitted TypeScript with the TypeScript compiler (transpileModule) and inspects diagnostics of category Error. If any parse-level diagnostic exists, it throws InterpreterRuntimeError with code \"ParseError\" carrying the flattened compiler diagnostic message. This is the library's front-line guard: only syntactically valid TypeScript ever reaches the sandboxed interpreter.","triggerScenarios":"Passing a script string with TypeScript syntax errors (unclosed brackets, invalid tokens, bad TS type syntax) to the codemode runtime's program()/parseProgram entry point.","commonSituations":"Dynamically generated code snippets with template-string quoting mistakes, code copied from a different language, scripts truncated mid-expression, or TS-only syntax emitted by an LLM that doesn't compile.","solutions":["Read the diagnostic text after \"Failed to parse TypeScript:\" — it names the exact syntax problem and often the position.","Compile the script locally with `npx tsc --noEmit` (or run it through transpileModule) to reproduce and locate the error.","Fix the syntax in the generated script string; check for truncated output if the code is produced by a model or template.","If parsing user-supplied code, wrap the parse call in try/catch and surface the ParseError message to the author of the script."],"exampleFix":"// before\nconst script = \"const x: number = ; return x\"\n// after\nconst script = \"const x: number = 1; return x\"","handlingStrategy":"try-catch","validationCode":"import ts from \"typescript\"\nexport const validateScript = (script: string): string | null => {\n  const { diagnostics } = ts.transpileModule(script, { compilerOptions: { module: ts.ModuleKind.ESNext, target: ts.ScriptTarget.ESNext }, reportDiagnostics: true })\n  const first = diagnostics?.find((d) => d.category === ts.DiagnosticCategory.Error)\n  return first ? ts.flattenDiagnosticMessageText(first.messageText, \"\\n\") : null\n}","typeGuard":null,"tryCatchPattern":"try {\n  await program(script)\n} catch (e) {\n  if (e instanceof Error && e.message.startsWith(\"Failed to parse TypeScript:\")) {\n    console.error(\"Script syntax error:\", e.message.replace(\"Failed to parse TypeScript: \", \"\"))\n  } else throw e\n}","preventionTips":["Validate generated scripts with the TypeScript compiler before handing them to the runtime.","If scripts come from an LLM, cap/verify output isn't truncated mid-statement.","Keep template-string quoting simple; prefer building code with tagged templates or builders.","Surface the ParseError message verbatim to script authors — it contains the exact location."],"tags":["typescript","parse-error","syntax","transpiler"],"backgroundTag":"typescript-syntax-error","analyzedSha":"2b7df46e8ae1517d64c896c7793d2d52ec845669","analyzedAt":"2026-09-01T07:59:23.713Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}