{"id":"7efbb2da39bdfb6f","repo":"vitest-dev/vitest","slug":"failed-to-parse-options-id","errorCode":null,"errorMessage":"failed to parse ${options.id}","messagePattern":"failed to parse (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/mocker/src/node/automock.ts","lineNumber":30,"sourceCode":"  globalThisAccessor?: string\n  id?: string\n}\n\n// TODO: better source map replacement\nexport function automockModule(\n  code: string,\n  mockType: 'automock' | 'autospy',\n  parse: (code: string) => any,\n  options: AutomockOptions = {},\n): MagicString {\n  const globalThisAccessor = options.globalThisAccessor || '\"__vitest_mocker__\"'\n  let ast: Program\n  try {\n    ast = parse(code) as Program\n  }\n  catch (cause) {\n    if (options.id) {\n      throw new Error(`failed to parse ${options.id}`, { cause })\n    }\n    throw cause\n  }\n\n  const m = new MagicString(code)\n\n  const allSpecifiers: { name: string; alias?: string }[] = []\n  const replacers: (() => void)[] = []\n  let importIndex = 0\n  for (const _node of ast.body) {\n    if (_node.type === 'ExportAllDeclaration') {\n      const node = _node as Positioned<ExportAllDeclaration>\n      // TODO: pass it down in the browser mode\n      if (!options.id) {\n        throw new Error(\n          `automocking files with \\`export *\\` is not supported because it cannot be easily statically analysed`,\n        )\n      }","sourceCodeStart":12,"sourceCodeEnd":48,"githubUrl":"https://github.com/vitest-dev/vitest/blob/d568f8ce3739b532d5bf2c1ee1e45e8a8a473d09/packages/mocker/src/node/automock.ts#L12-L48","documentation":"Thrown at packages/mocker/src/node/automock.ts:25-33 when the parser (parse callback) throws while transforming a module's source during automocking. The error wraps the underlying parse error with the module id (when options.id is provided) to indicate which file failed to parse, preserving the original cause via Error cause.","triggerScenarios":"vi.mock(path) (automock/auto mode, no factory) targeting a file whose source the configured parser cannot parse: invalid syntax, an unsupported stage-3 proposal, or a file format the parser does not handle.","commonSituations":"Automocking a TypeScript file with experimental syntax not enabled; a file with a genuine syntax error; a non-JS file being automocked without a transformer; parser version that does not support the syntax in use.","solutions":["Fix any syntax errors in the target module.","Provide an explicit factory to vi.mock so automocking parsing is bypassed: vi.mock('./x', () => ({})).","Ensure the parser/transformer in your Vitest/Vite config supports the syntax (e.g. enable the appropriate TS/JSX/Babel config)."],"exampleFix":"// before\nvi.mock('./legacy.cjs') // parse fails\n\n// after\nvi.mock('./legacy.cjs', () => ({\n  doWork: vi.fn(),\n}))","handlingStrategy":"validation","validationCode":"// Pre-validate the target file is syntactically valid before automocking.\nimport { readFileSync } from 'node:fs'\nimport { parse } from 'acorn'\nfunction isParsable(file: string): boolean {\n  try { parse(readFileSync(file, 'utf-8'), { ecmaVersion: 'latest', sourceType: 'module' }); return true } catch { return false }\n}\n\nif (!isParsable('./target.ts')) {\n  // fall back to an explicit factory instead of automock\n  vi.mock('./target.ts', () => ({}))\n}","typeGuard":null,"tryCatchPattern":"try {\n  vi.mock('./legacy.cjs')\n} catch (e) {\n  if (e instanceof Error && /failed to parse/.test(e.message)) {\n    // provide an explicit factory to bypass automock parsing\n    vi.mock('./legacy.cjs', () => ({}))\n  } else { throw e }\n}","preventionTips":["Prefer explicit vi.mock factories for non-standard or legacy file formats.","Keep target modules syntactically valid and supported by your configured parser.","Avoid automocking files with experimental syntax the parser cannot handle."],"tags":["parser","automock","syntax-error","transform"],"analyzedSha":"d568f8ce3739b532d5bf2c1ee1e45e8a8a473d09","analyzedAt":"2026-08-03T20:23:56.861Z","schemaVersion":2}