{"record":{"id":"56a67719177d166c","repo":"vitest-dev/vitest","slug":"bench-from-could-not-find-a-result-file-at","errorCode":null,"errorMessage":"`bench.from()` could not find a result file at \"${resolved}\". Run the source benchmark first to create it.","messagePattern":"`bench\\.from\\(\\)` could not find a result file at \"(.+?)\"\\. Run the source benchmark first to create it\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/vitest/src/runtime/benchmark.ts","lineNumber":244,"sourceCode":"}\n\nexport function createBench(\n  test: Test,\n  config: SerializedConfig,\n  moduleRunner: TestModuleRunner,\n): Bench {\n  const pending = new Set<BenchRegistration<any>>()\n\n  const resolveTemplate = (template: string) => substitutePath(template, config.benchmark.projectName)\n\n  const resolveFromSource = async (source: string | BenchFromSource): Promise<BaselineData> => {\n    if (typeof source === 'function') {\n      return source()\n    }\n    const resolved = resolveTemplate(source)\n    const data = await rpc().readBenchmarkResult(resolved)\n    if (data == null) {\n      throw new Error(`\\`bench.from()\\` could not find a result file at \"${resolved}\". Run the source benchmark first to create it.`)\n    }\n    return data\n  }\n\n  const taskFromBaseline = (\n    name: string,\n    data: BaselineData,\n  ): TestBenchmarkTask => ({\n    name,\n    latency: data.latency,\n    throughput: data.throughput,\n    period: data.period,\n    totalTime: data.totalTime,\n    rank: 0,\n    fromStore: true,\n  })\n\n  const createCompareStorage = <T extends string>(","sourceCodeStart":226,"sourceCodeEnd":262,"githubUrl":"https://github.com/vitest-dev/vitest/blob/1fa9837ec26533512fdcad8baebf249771bd340a/packages/vitest/src/runtime/benchmark.ts#L226-L262","documentation":"`bench.from(name, source)` loads a previously stored benchmark baseline. When `source` is a string path, Vitest resolves it (with `${projectName}` substitution) and asks the server via `rpc().readBenchmarkResult(resolved)` for stored data. If nothing was written at that path, this error fires. Baselines are created by registering a benchmark with the `writeResult` option, which calls `writeBenchmarkResult` after a successful run.","triggerScenarios":"Calling `bench.from('baseline', './results/my-bench.json')` before any benchmark has written results to that path. Also triggered by a mismatched `${projectName}` substitution — `source` uses `./${projectName}/baseline.json` but the writer used a different project name, so the resolved paths diverge.","commonSituations":"Running `bench.from` in CI before the source benchmark ran in the same or a prior step; the `writeResult` template in the source benchmark does not match the `from` source string; the results directory was cleaned; the project name changed between write and read.","solutions":["Run the source benchmark first — register it with `bench('src', { writeResult: './results/src.json' }, fn)` and ensure it runs (`bench('src', fn).run()` or via `bench.compare`).","Make the `writeResult` template in the source and the `source` argument to `bench.from()` use the identical path string and the same `${projectName}` substitution.","If using a function source instead of a string, return the `BaselineData` directly to bypass file lookup.","Verify the benchmark project's `projectName` config is identical between the writing run and the reading run."],"exampleFix":"// before\nbench('v1', { writeResult: './bench-v1.json' }, v1fn)\nconst baseline = bench.from('v1', './results/bench-v1.json') // wrong path\n\n// after\nbench('v1', { writeResult: './results/bench-v1.json' }, v1fn)\nconst baseline = bench.from('v1', './results/bench-v1.json')","handlingStrategy":"validation","validationCode":"const exists = await rpc().readBenchmarkResult(resolved); if (exists == null) throw new Error(`run the source benchmark with writeResult='${resolved}' first`)","typeGuard":"null","tryCatchPattern":"try { reg = bench.from('base', './r.json') } catch (e) { if (/could not find a result file/.test(e.message)) { await writeSourceFirst(); reg = bench.from('base', './r.json') } else throw e }","preventionTips":["Keep the `writeResult` template and the `bench.from` source string in a shared constant.","Run the writing benchmark in the same Vitest invocation (or a prior CI step) before the reader.","Use the same `projectName` for both writer and reader to keep `${projectName}` substitution consistent."],"tags":["benchmark","baseline","file-not-found","bench-from"],"backgroundTag":null,"analyzedSha":"1fa9837ec26533512fdcad8baebf249771bd340a","analyzedAt":"2026-08-11T16:11:39.638Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}