{"id":"e66969004bd71fb9","repo":"vitejs/vite","slug":"can-not-commit-a-deps-optimization-run-as-it-was-c","errorCode":null,"errorMessage":"Can not commit a Deps Optimization run as it was cancelled","messagePattern":"Can not commit a Deps Optimization run as it was cancelled","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/vite/src/node/optimizer/index.ts","lineNumber":581,"sourceCode":"      cleaned = true\n      // No need to wait, we can clean up in the background because temp folders\n      // are unique per run\n      debug?.(colors.green(`removing cache dir ${processingCacheDir}`))\n      try {\n        // When exiting the process, `fsp.rm` may not take effect, so we use `fs.rmSync`\n        fs.rmSync(processingCacheDir, { recursive: true, force: true })\n      } catch {\n        // Ignore errors\n      }\n    }\n  }\n\n  const successfulResult: DepOptimizationResult = {\n    metadata,\n    cancel: cleanUp,\n    commit: async () => {\n      if (cleaned) {\n        throw new Error(\n          'Can not commit a Deps Optimization run as it was cancelled',\n        )\n      }\n      // Ignore clean up requests after this point so the temp folder isn't deleted before\n      // we finish committing the new deps cache files to the deps folder\n      committed = true\n\n      // Write metadata file, then commit the processing folder to the global deps cache\n      // Rewire the file paths from the temporary processing dir to the final deps cache dir\n      const dataPath = path.join(processingCacheDir, METADATA_FILENAME)\n      debug?.(\n        colors.green(`creating ${METADATA_FILENAME} in ${processingCacheDir}`),\n      )\n      fs.writeFileSync(\n        dataPath,\n        stringifyDepsOptimizerMetadata(metadata, depsCacheDir),\n      )\n","sourceCodeStart":563,"sourceCodeEnd":599,"githubUrl":"https://github.com/vitejs/vite/blob/89620f09afcfef6b35e7bb8660132ab5b4d0cd3b/packages/vite/src/node/optimizer/index.ts#L563-L599","documentation":"A deps-optimization run produces a result object with `commit()` and `cancel()`; `cancel()` sets `cleaned` and deletes the processing dir. If `commit()` is then invoked, it detects `cleaned === true` and refuses to publish a half-deleted cache, throwing at optimizer/index.ts:581. This protects cache integrity: a cancelled run must never be promoted to the live cache.","triggerScenarios":"Internally: the optimizer's processing run is cancelled (e.g. server close, new discovery invalidating the run, or explicit `cancel()`) and a code path still calls `commit()` on that same result object. Users rarely trigger this directly.","commonSituations":"Rapid config/source churn that invalidates the in-flight optimization; closing the dev server while optimization is mid-flight; race between a re-optimize and a browser request triggering commit; plugins or custom environment code calling the internal optimizer API out of order.","solutions":["If you call the optimizer API programmatically, check the result's lifecycle — never call `commit()` after `cancel()`; track which one won the race.","Restart the dev server cleanly to reset optimizer state, and clear `node_modules/.vite` if the cache is suspect.","Reduce rapid config edits that invalidate the run; debounce file saves.","Update Vite — lifecycle races around cancel/commit have been fixed across versions."],"exampleFix":"// before\nconst result = await runOptimize()\nresult.cancel()\nawait result.commit() // throws\n\n// after\nconst result = await runOptimize()\nif (!result.cancelled) await result.commit()","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"// If you drive the optimizer API, guard commit after cancel:\nconst result = await runOptimizeDeps(environment)\ntry { await result.commit() }\ncatch (e) {\n  if (e instanceof Error && /cancelled/.test(e.message)) { /* expected after cancel; ignore */ return }\n  throw e\n}","preventionTips":["Never call commit() after cancel() on the same optimizer result.","Treat cancel/commit as mutually exclusive; track the winner explicitly.","Don't churn config files while an optimize run is in flight."],"tags":["optimizer","internal","race-condition","cache"],"analyzedSha":"89620f09afcfef6b35e7bb8660132ab5b4d0cd3b","analyzedAt":"2026-08-03T19:28:02.920Z","schemaVersion":2}