Dependency Optimizer: re-optimization can flip which duplicated chunk is "canonical" for a module, breaking module singletons (duplicate React on a cold-started dev server)

未关闭
#1,273 3 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
5/5
预计耗时
一周以上
新手友好度
38/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
冷清
技术栈
react, typescript, vite

调研方向

先从链接的 duplicate-react-cold-start 复现及其 README 开始,然后跟踪 Vite 的 dependency-optimization re-optimization path 和 chunk canonicalization。完成的标准是,重复的优化器处理始终保持规范映射稳定,并且该复现不再创建重复的 React 实例,也不再产生首次渲染时的 invalid hook call。

由索引模型根据 Issue 内容生成。

描述

pending triage plugin: rsc
Describe the bug

🤖 AI Disclosure: Claude Code has written up majority of this issue and most of the reproduction README. I, as a human, still take responsibility for it.

Summary

When two optimizeDeps.include entries resolve to the same module (e.g. react and @vitejs/plugin-rsc > react, where @vitejs/plugin-rsc can be any package that peer-depends on react), the optimizer emits two chunks for it — one holds the real module, the other re-exports it. Which one is "canonical" vs. the re-export is not stable across optimizer passes: a re-optimization can swap them.

chunk pass 1 (cold) pass 2 (after re-optimization)
react.js canonical alias → @vitejs_plugin-rsc_n_react.js
@vitejs_plugin-rsc_n_react.js alias → react.js canonical

If that re-optimization is triggered after some modules have already been served (a dependency discovered at request time), a consumer loaded before the swap and one loaded after end up bound to two different real instances of the module. For React this is Invalid hook call / Cannot read properties of null (reading 'useState'); the page then auto-reloads (optimized dependencies changed. reloading) and the error vanishes — so it's cold-start-only, first-render-only.

Detailed explanation can be found in the reproduction repository's README.

Context

I've been developing a React framework based on @vitejs/plugin-rsc. This issue originally surfaced as a framework bug, but a thorough investigation made us believe that this is a Vite issue.

The repro still uses @vitejs/plugin-rsc because it is the way we are aware of for reliably reproducing the issue, but we believe this issue is in Vite's Deps Optimization, hence a bug report in this repo.

The trigger path (via @vitejs/plugin-rsc): a "use client" package imported by a server component is reached on the browser through a request-time virtual:vite-rsc/client-package-proxy/<pkg> module that the optimizer's initial scan can't follow — so the package (and the React it imports) is discovered on the first render and forces the re-optimization.

Side Note: possible another form of this bug

Initially this bug surfaced as a different error like this:

The requested module '/node_modules/.vite/deps/react_jsx-runtime.js?v=...' does not provide an export named 't'

a severer ES Modules link time error. This version of error is not included in this bug report because (1) we couldn't reproduce this without involving our framework and (2) we are not sure whether they are truly same origin. Read more about this at https://github.com/uhyo/funstack-static/issues/124 and https://github.com/uhyo/funstack-static/issues/128.

Reproduction

https://github.com/uhyo/vite-rsc-deps-issue-repro/tree/repro/duplicate-react-cold-start

Steps to reproduce
pnpm install && rm -rf node_modules/.vite && pnpm dev
# open the URL; first load throws Invalid hook call, then auto-reloads
System Info
System:
    OS: Linux 6.18 Ubuntu 22.04.1 LTS 22.04.1 LTS (Jammy Jellyfish)
    CPU: (16) x64 AMD Ryzen 7 PRO 6850U with Radeon Graphics
    Memory: 12.37 GB / 15.01 GB
    Container: Yes
    Shell: 5.1.16 - /bin/bash
  Binaries:
    Node: 24.13.1 - /home/uhyo/.volta/tools/image/node/24.13.1/bin/node
    Yarn: 4.1.1 - /home/uhyo/.volta/tools/image/yarn/4.1.1/bin/yarn
    npm: 11.4.2 - /home/uhyo/.volta/tools/image/npm/11.4.2/bin/npm
    pnpm: 10.30.1 - /home/uhyo/.volta/bin/pnpm
    Deno: 2.2.1 - /home/uhyo/.deno/bin/deno
  npmPackages:
    @vitejs/plugin-react: latest => 6.0.3 
    @vitejs/plugin-rsc: latest => 0.5.27 
    vite: ^8.0.16 => 8.1.0
Used Package Manager

pnpm

Logs

No response

Validations
主要语言
TypeScript
星标
1.2k
派生
287
平均合并
1 天 19 小时
30 天内合并 PR
21

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

vitejs/vite-plugin-react 的其他 Issue

查看 vitejs/vite-plugin-react 的全部 Issue

相似的 Issue

更多 TypeScript Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。