Pinia · Stores
PINIA0003Don't let setup stores read each other's state during setup
Report eager state reads along cycles of setup-store initialization.
vite-doctor/piniastoreserrorNo fix
Why it happens
Pinia registers a store before running its setup. If eager store calls form a cycle, a read can reach a store before it has finished initializing. A single eager read can fail, depending on which store is created first.
Fix
Defer every reported read to a computed() getter or an action that runs after setup, or break the cycle by moving a useStore() call out of setup.
Example
Read the other store in a getter
Before
// stores/x.ts
export const useXStore = defineStore('x', () => {
const y = useYStore()
const greeting = `Hi ${y.name}`
return { name: ref('X'), greeting }
})
// stores/y.ts
export const useYStore = defineStore('y', () => {
const x = useXStore()
const { name: friend } = storeToRefs(x)
return { name: ref('Y'), friend }
})
After
// stores/x.ts
export const useXStore = defineStore('x', () => {
const y = useYStore()
const greeting = computed(() => `Hi ${y.name}`)
return { name: ref('X'), greeting }
})
// stores/y.ts
export const useYStore = defineStore('y', () => {
const x = useXStore()
const friend = computed(() => x.name)
return { name: ref('Y'), friend }
})
Verify the fix
Run only this rule after editing so the report stays focused on the diagnostic you are closing:
pnpm vite-doctor . --rules pinia/stores/no-circular-setup-state-read