-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
fix(react-start): Do not optimizeDeps in VITEST environment #6074
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
WalkthroughAdds a Vitest-aware guard condition to the Vite plugin's optimizeDeps configuration in Changes
Estimated code review effort🎯 1 (Trivial) | ⏱️ ~3 minutes Possibly related PRs
Suggested labels
Suggested reviewers
Poem
Pre-merge checks and finishing touches❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
|
View your CI Pipeline Execution ↗ for commit b025a57
☁️ Nx Cloud last updated this comment at |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
packages/react-start/src/plugin/vite.ts(1 hunks)
🧰 Additional context used
📓 Path-based instructions (2)
**/*.{ts,tsx}
📄 CodeRabbit inference engine (AGENTS.md)
Use TypeScript strict mode with extensive type safety for all code
Files:
packages/react-start/src/plugin/vite.ts
**/*.{js,ts,tsx}
📄 CodeRabbit inference engine (AGENTS.md)
Implement ESLint rules for router best practices using the ESLint plugin router
Files:
packages/react-start/src/plugin/vite.ts
🧠 Learnings (1)
📚 Learning: 2025-11-02T16:16:24.898Z
Learnt from: nlynzaad
Repo: TanStack/router PR: 5732
File: packages/start-client-core/src/client/hydrateStart.ts:6-9
Timestamp: 2025-11-02T16:16:24.898Z
Learning: In packages/start-client-core/src/client/hydrateStart.ts, the `import/no-duplicates` ESLint disable is necessary for imports from `#tanstack-router-entry` and `#tanstack-start-entry` because both aliases resolve to the same placeholder file (`fake-start-entry.js`) in package.json during static analysis, even though they resolve to different files at runtime.
Applied to files:
packages/react-start/src/plugin/vite.ts
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (2)
- GitHub Check: Test
- GitHub Check: Preview
Fix: Disable forced optimizeDeps in Vitest to prevent duplicate React instances
The Issue
When using
@tanstack/react-startin a monorepo setup with Vitest, the plugin unconditionally addsreactandreact-domtooptimizeDeps.include. This forces Vite to pre-bundle React into an ESM wrapper (e.g.,.vite/deps/react.js).This causes a "Dual React Instance" conflict (leading to
Invalid hook callerrors) under the following common testing scenario:reactto the pre-bundled ESM version forced by the plugin.node_modules(e.g.,@testing-library/reactor UI libraries) are externalized by Vitest for performance. They use Node's native resolution, which resolvesreactto the standard CJS/ESM build innode_modules/react.Because the pre-bundled React and the native React are different physical files/modules, they do not share the global
ReactCurrentDispatcher. When a workspace component (using Pre-bundled React) is rendered by a test utility (using Native React), hooks fail.The Fix
This PR updates the
tanstackStartVite plugin to skip the aggressiveoptimizeDepsconfiguration when running in Vitest (process.env.VITEST === 'true').By disabling this enforced optimization during tests:
node_modules/react.Verification
I verified this fix in a pnpm monorepo using set to
hoist: false(strict isolation) andvitest.TypeError: Cannot read properties of null (reading 'useState').Summary by CodeRabbit
✏️ Tip: You can customize this high-level summary in your review settings.