[plugin-rsc] inline "use server" function declarations lose hoisting and throw TDZ ReferenceError
Los mantenedores suelen responder en 1 día
@draarivpatel-ui ya está trabajando en esto.
Desde el 25/11/2026.
- #1583 de @draarivpatel-ui — abierto
Evaluación
- Dificultad
- 3/5
- Tiempo estimado
- 1-2 días
- Aptitud para principiantes
- 55/100
- Tipo de issue
- Error
- Claridad
- Bien especificado
- Estado de actividad
- Activo
- Stack tecnológico
- javascript, react, typescript
- Área
- build-system, tooling
Línea de trabajo
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.
Escrito por el modelo de indexación a partir del texto del issue.
Descripción
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
npm install vite @vitejs/plugin-rsc react react-dom- Save
Page.jsxandrepro.mjsabove in an ESM package ("type": "module"). - 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
- Follow our Code of Conduct
- Read the Contributing Guidelines.
- Read the docs.
- Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
- Make sure this is a Vite issue and not a framework-specific issue.
- Check that this is a concrete bug. For Q&A open a GitHub Discussion or join our Discord Chat Server.
- The provided reproduction is a minimal reproducible example of the bug.
Credit: https://ucsbplse.github.io/
- Lenguaje dominante
- TypeScript
- Estrellas
- 1.2k
- Forks
- 287
- Merge medio
- 1 d 17 h
- PR fusionados (30 d)
- 19
Preparar el entorno
- Sin Dockerfile ni archivo de Docker Compose
- Tiene una plantilla de pull request
- Leer la guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Más de vitejs/vite-plugin-react
-
plugin-rsc: a client reference's CSS is linked before the CSS of the chunks it imports (build)Posiblemente ocupada @wozhenbang2004 la tomó hace 6 días. Abierto
Dificultad 2/5 1-3 horas Aptitud para principiantes 84/100
vitejs/vite-plugin-react#1570 ·
Los mantenedores suelen responder en 1 día
-
pending triage
Dificultad 4/5 3-5 días Aptitud para principiantes 35/100
vitejs/vite-plugin-react#1581 ·
Los mantenedores suelen responder en 1 día
-
[plugin-rsc] Build exits 0, but the app fails at runtime: a server action imported only by browser code is missing from the RSC buildPosiblemente ocupada @NeilPant la tomó hace 3 días. Abierto
Dificultad 4/5 3-5 días Aptitud para principiantes 48/100
vitejs/vite-plugin-react#1578 ·
Los mantenedores suelen responder en 1 día
-
Dificultad 4/5 3-5 días Aptitud para principiantes 42/100
vitejs/vite-plugin-react#1577 ·
Los mantenedores suelen responder en 1 día
-
Dificultad 3/5 1-2 días Aptitud para principiantes 45/100
vitejs/vite-plugin-react#1565 · 2 comentarios ·
Los mantenedores suelen responder en 1 día
Todos los issues de vitejs/vite-plugin-react
Issues similares
-
ui
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
Los mantenedores suelen responder en 1 día
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 82/100
mermaid-js/mermaid-v13#1370 ·
Los mantenedores suelen responder en 1 día
-
compiler: the island_chunks_not_emitted report gives a count but not which islands were skippedAbiertobug
Dificultad 2/5 1-3 horas Aptitud para principiantes 82/100
-
Broken Website
Dificultad 2/5 1-3 horas Aptitud para principiantes 64/100
darkreader/darkreader#16259 ·
Los mantenedores suelen responder en 2 días
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 82/100
ARTPARK-SAHAI-ORG/calibrate-frontend#919 ·
Los mantenedores suelen responder en 1 día