{"record":{"id":"68129101e1cc7d7a","repo":"vitest-dev/vitest","slug":"root-project-is-not-initialized-this-means-that-t","errorCode":null,"errorMessage":"Root project is not initialized. This means that the Vite server was not established yet and the the workspace config is not resolved.","messagePattern":"Root project is not initialized\\. This means that the Vite server was not established yet and the the workspace config is not resolved\\.","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/vitest/src/node/core.ts","lineNumber":591,"sourceCode":"  public getProvidedContext(): ProvidedContext {\n    return this.getRootProject().getProvidedContext()\n  }\n\n  /** @internal */\n  _ensureRootProject(): TestProject {\n    if (this.coreWorkspaceProject) {\n      return this.coreWorkspaceProject\n    }\n    this.coreWorkspaceProject = TestProject._createBasicProject(this)\n    return this.coreWorkspaceProject\n  }\n\n  /**\n   * Return project that has the root (or \"global\") config.\n   */\n  public getRootProject(): TestProject {\n    if (!this.coreWorkspaceProject) {\n      throw new Error(`Root project is not initialized. This means that the Vite server was not established yet and the the workspace config is not resolved.`)\n    }\n    return this.coreWorkspaceProject\n  }\n\n  public get serializedRootConfig(): SerializedRootConfig {\n    return {\n      ...this.getRootProject().serializedConfig,\n      projects: this.projects.map(project => project.serializedConfig),\n    }\n  }\n\n  public getProjectByName(name: string): TestProject {\n    const project = this.projects.find(p => p.name === name)\n      || this.coreWorkspaceProject\n      || this.projects[0]\n    if (!project) {\n      throw new Error(`Project \"${name}\" was not found.`)\n    }","sourceCodeStart":573,"sourceCodeEnd":609,"githubUrl":"https://github.com/vitest-dev/vitest/blob/1fa9837ec26533512fdcad8baebf249771bd340a/packages/vitest/src/node/core.ts#L573-L609","documentation":"`Vitest.getRootProject()` returns the project that holds the resolved root/global config and is only meaningful once the Vite server is up and the workspace has been resolved (`coreWorkspaceProject` is populated during server bootstrap). Calling it before that handshake — e.g. inside a plugin hook that fires during initial config resolution — throws this internal-state error.","triggerScenarios":"Calling `vitest.getRootProject()` from a `configResolved` hook or before `await createVitest(...)` has resolved; reaching into the Vitest instance from a serverless handler that constructs but does not start Vitest.","commonSituations":"Custom reporter/plugin that eagerly inspects the root project; tests of the Vitest API that forget to await server start.","solutions":["Await the full Vitest constructor (`await createVitest('test')`) before calling `getRootProject()`.","Move the access into a hook that fires after server start (e.g. `onInit` / `onBeforeRun`), where the root project is guaranteed to exist.","Guard with `if (vitest.coreWorkspaceProject)` before access if you must call during early phases."],"exampleFix":"// before\nconst vitest = createVitest('test', config)   // forgot await\nvitest.getRootProject()\n// after\nconst vitest = await createVitest('test', config)\nvitest.getRootProject()","handlingStrategy":"try-catch","validationCode":"const vitest = await createVitest('test', config) // ensure fully started\nif (!vitest.coreWorkspaceProject) {\n  throw new Error('Vitest server not yet initialised; await createVitest before reading root project.')\n}\nconst root = vitest.getRootProject()","typeGuard":"function isVitestReady(vitest: { coreWorkspaceProject: unknown }): boolean {\n  return Boolean(vitest.coreWorkspaceProject)\n}","tryCatchPattern":"try {\n  return vitest.getRootProject()\n} catch (err) {\n  if (err instanceof Error && /Root project is not initialized/.test(err.message)) {\n    // defer access to an onSetServer / onInit hook and retry\n    return null\n  }\n  throw err\n}","preventionTips":["Always `await createVitest(...)` before touching instance state.","Prefer lifecycle hooks (`onInit`, `onBeforeRun`) over eager synchronous access."],"tags":["api","lifecycle","internal-state","async"],"backgroundTag":null,"analyzedSha":"1fa9837ec26533512fdcad8baebf249771bd340a","analyzedAt":"2026-08-11T16:11:39.638Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}