[plugin-rsc] inline "use server" function declarations lose hoisting and throw TDZ ReferenceError

Đang mở
#1,566 1 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

@draarivpatel-ui đang làm issue này rồi.

Từ ngày 25/11/2026.

  • #1583 của @draarivpatel-ui — đang mở

Đánh giá

Độ khó
3/5
Thời gian dự kiến
1-2 ngày
Mức phù hợp với người mới
55/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Lĩnh vực
build-system, tooling

Hướng nghiên cứu

Start in packages/plugin-rsc/src/transforms/hoist.ts, at transformHoistInlineDirective, where a FunctionDeclaration is replaced by const ${declName} = ... at node.start. Run the second snippet in the issue (the direct @vitejs/plugin-rsc/transforms call) to see the emitted output without a full Vite build, then compare how the default path and hoistRuntime: true place the binding. Done means the transformed Page can reference submit above its declaration, as the untransformed code does.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

Related plugins
Describe the bug

When an inline "use server" function declaration is referenced above its declaration in the same scope (for example, return <form action={submit}> with async function submit() { "use server" } below in the component body), rendering throws ReferenceError: Cannot access 'submit' before initialization.

In packages/plugin-rsc/src/transforms/hoist.ts, transformHoistInlineDirective replaces a FunctionDeclaration at its original source position (node.start) with const ${declName} = ... (both by default and with hoistRuntime: true). Standard JavaScript function declarations are initialized when entering the enclosing scope (ECMA-262 FunctionDeclarationInstantiation), whereas const stays in its temporal dead zone until that line executes.

// Page.jsx
export function Page({ x }) {
  return <form action={submit}><button type="submit">Go</button></form>

  async function submit(formData) {
    "use server"
    return x + Number(formData.get("y"))
  }
}
// repro.mjs
import { createServer } from "vite"
import rsc from "@vitejs/plugin-rsc"

const server = await createServer({
  configFile: false,
  logLevel: "silent",
  plugins: [rsc({ serverHandler: false })],
  environments: { rsc: {}, ssr: {}, client: {} },
})
try {
  const mod = await server.environments.rsc.runner.import("./Page.jsx")
  console.log(mod.Page({ x: 1 }))
} finally {
  await server.close()
}

Or directly via @vitejs/plugin-rsc/transforms:

import { parseAst } from "vite"
import { transformHoistInlineDirective } from "@vitejs/plugin-rsc/transforms"

const src = `export function Page(x) {
  return { action: submit }
  async function submit(y) {
    "use server"
    return x + y
  }
}
`
const { output } = transformHoistInlineDirective(src, parseAst(src), {
  directive: "use server",
  runtime: (v) => `reg(${v})`,
})
const mod = await import(
  "data:text/javascript," +
    encodeURIComponent("const reg = (f) => f;\n" + output.toString())
)
console.log(await mod.Page(1).action(2))

Actual: ReferenceError: Cannot access 'submit' before initialization, because the transformed Page body emits const submit = ... after return:

export function Page(x) {
  return { action: submit }
  const submit = /* #__PURE__ */ reg($$hoist_0_submit).bind(null, x);
}

;export async function $$hoist_0_submit(x, y) {
    "use server"
    return x + y
  };
/* #__PURE__ */ Object.defineProperty($$hoist_0_submit, "name", { value: "submit" });

Expected: Page({ x: 1 }) returns the <form> element with submit bound as its action (and await mod.Page(1).action(2) returns 3), matching the untransformed function declaration.

Reproduction

https://gist.github.com/trackoor/794a6019fe03b556b74b3775acb18db8

Steps to reproduce
  1. npm install vite @vitejs/plugin-rsc react react-dom
  2. Save Page.jsx and repro.mjs above in an ESM package ("type": "module").
  3. Run node repro.mjs.
System Info
System:
  OS: Linux 6.1 Debian GNU/Linux 12 (bookworm)
  CPU: (64) x64 Intel(R) Xeon(R) CPU @ 2.80GHz
  Shell: 5.2.15 - /bin/bash
Binaries:
  Node: 26.13.0
  npm: 11.20.0
  pnpm: 12.18.0
npmPackages:
  @vitejs/plugin-react: 6.3.0
  @vitejs/plugin-rsc: 0.5.39 (also on main 2c6e034)
  vite: 8.3.8
Used Package Manager

npm

Logs
Click to expand!
ReferenceError: Cannot access 'submit' before initialization
    at Module.Page (/Page.jsx:2:24)
    at repro.mjs:12:19
Validations

Credit: https://ucsbplse.github.io/

Ngôn ngữ chính
TypeScript
Star
1.2k
Fork
287
Merge trung bình
1 ngày 17 giờ
Pull request đã merge (30 ngày)
19

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của vitejs/vite-plugin-react

Tất cả issue của vitejs/vite-plugin-react

Issue tương tự

Thêm issue về TypeScript

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.