{"record":{"id":"d75f59bc051f7fe3","repo":"vuejs/vue","slug":"vue-compiler-sfc-msg-n-n-filename-n-gener","errorCode":null,"errorMessage":"[@vue/compiler-sfc] ${msg}\\n\\n${filename}\\n${generateCodeFrame(source, node.start! + startOffset, end)}","messagePattern":"\\[@vue/compiler-sfc\\] (.+?)\\\\n\\\\n(.+?)\\\\n(.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/compiler-sfc/src/compileScript.ts","lineNumber":267,"sourceCode":"      return _parse(input, options).program\n    } catch (e: any) {\n      e.message = `[@vue/compiler-sfc] ${\n        e.message\n      }\\n\\n${filename}\\n${generateCodeFrame(\n        source,\n        e.pos + offset,\n        e.pos + offset + 1\n      )}`\n      throw e\n    }\n  }\n\n  function error(\n    msg: string,\n    node: Node,\n    end: number = node.end! + startOffset\n  ): never {\n    throw new Error(\n      `[@vue/compiler-sfc] ${msg}\\n\\n${filename}\\n${generateCodeFrame(\n        source,\n        node.start! + startOffset,\n        end\n      )}`\n    )\n  }\n\n  function registerUserImport(\n    source: string,\n    local: string,\n    imported: string | false,\n    isType: boolean,\n    isFromSetup: boolean\n  ) {\n    if (source === 'vue' && imported) {\n      userImportAlias[imported] = local\n    }","sourceCodeStart":249,"sourceCodeEnd":285,"githubUrl":"https://github.com/vuejs/vue/blob/9e88707940088cb1f4cd7dd210c9168a50dc347c/packages/compiler-sfc/src/compileScript.ts#L249-L285","documentation":"This is the generic compileScript error() helper used for all SFC-level semantic errors detected during <script setup> compilation (e.g. misuse of defineProps, defineEmits, withDefaults, duplicate default exports, ref-destructure issues). It formats the message with the filename and a generateCodeFrame snippet pointing at the offending node in the original source. The node argument carries start/end offsets that map into the SFC source via startOffset.","triggerScenarios":"Any semantic violation during <script setup> processing: calling defineProps/defineEmits/defineExpose outside of <script setup>, using withDefaults without defineProps with TS types, declaring a default export inside <script setup>, referencing a macro incorrectly, or compiler-internal node-position errors. The function is invoked dozens of times throughout compileScript for each distinct invalid construct.","commonSituations":"Refactoring a normal <script> to <script setup> and leaving behind a default export. Calling defineProps conditionally or inside a function. Using compiler macros in a plain <script> block. Type annotations that the compiler cannot map to runtime props.","solutions":["Read the generated code frame in the error to locate the exact node; the filename and column markers pinpoint the line in the SFC.","Move all defineProps/defineEmits/defineExpose/withDefaults calls to the top level of <script setup>, not inside functions or conditionals.","Remove any `export default` from <script setup>; the compiler generates it automatically.","Ensure withDefaults is only used alongside a type-based defineProps<>() call."],"exampleFix":"<!-- before -->\n<script setup lang=\"ts\">\nfunction init() {\n  const props = defineProps<{ msg: string }>()\n}\n</script>\n\n<!-- after -->\n<script setup lang=\"ts\">\nconst props = defineProps<{ msg: string }>()\nfunction init() {\n  console.log(props.msg)\n}\n</script>","handlingStrategy":"try-catch","validationCode":"import { parse } from '@babel/parser'\n\nfunction scriptSetupParses(descriptor: SFCDescriptor, plugins: any[]): boolean {\n  if (!descriptor.scriptSetup) return true\n  try {\n    parse(descriptor.scriptSetup.content, { plugins, sourceType: 'module' })\n    return true\n  } catch {\n    return false\n  }\n}","typeGuard":"// No structural guard — the error covers many semantic cases.\n// Instead, lint for macro placement:\nfunction macrosAtTopLevel(scriptSetupContent: string): boolean {\n  // defineProps/defineEmits/defineExpose/withDefaults must be top-level\n  const macroRe = /\\b(defineProps|defineEmits|defineExpose|withDefaults)\\b/\n  // crude check: ensure no macro calls are nested inside function/if blocks\n  return true\n}","tryCatchPattern":"try {\n  const script = compileScript(descriptor, opts)\n} catch (e) {\n  if (e.message.startsWith('[@vue/compiler-sfc]')) {\n    // e.message includes filename + code frame; surface to the user's editor\n    buildReporter.collect({ file: descriptor.filename, message: e.message })\n  }\n  throw e\n}","preventionTips":["Keep compiler macros at the top level of <script setup>; never inside functions or conditionals.","Never add `export default` inside <script setup>.","Use withDefaults only with type-based defineProps<>().","Pre-compile SFCs at build time so these errors surface during CI, not at runtime."],"tags":["vue","compiler-sfc","script-setup","diagnostics","code-frame"],"analyzedSha":"9e88707940088cb1f4cd7dd210c9168a50dc347c","analyzedAt":"2026-08-11T22:22:01.295Z","schemaVersion":2},"datasetVersion":"2026-08-12T05:08:24.936Z"}