Unverified Commit bdbf63ae authored by Niklas Mischkulnig's avatar Niklas Mischkulnig Committed by GitHub

fix(next-custom-transforms): never place generated imports before directives (#98717)

## Summary

**What?** Fixes the error reported for a module that has a top-level
`"use client"` directive and an inline `"use server"` function
directive, e.g. `<form action={async () => { "use server" }} />` in a
client page.

**Why?** Under Turbopack this reported a bogus error — `The "use client"
directive must be placed before other expressions` — even though the
directive was already at the top of the file. The actual mistake (an
inline server action inside a Client Component) was never surfaced.

**How?** On Turbopack's RSC layer the server actions transform runs
*before* the React Server Components assert. When it hoists an inline
action it prepended its generated imports (`registerServerReference`,
action encryption, cache runtime) at index 0 of the module — *above* the
`"use client"` directive, which the transform does not consume. The RSC
assert then saw an import before the directive and reported it as
misplaced.
Co-authored-by: 's avatarvercel-fleet-prod[bot] <318278635+vercel-fleet-prod[bot]@users.noreply.github.com>
parent 6cfca45f
......@@ -14,11 +14,13 @@ use swc_core::{
atoms::atom,
common::{FileName, Mark},
ecma::{
ast::fn_pass,
parser::{EsSyntax, Syntax},
transforms::{
base::resolver,
testing::{FixtureTestConfig, test_fixture},
},
visit::VisitWith,
},
};
use testing::fixture;
......@@ -213,6 +215,62 @@ fn react_server_actions_errors(input: PathBuf) {
);
}
// Turbopack runs the server actions transform before the React Server Components assert on
// the RSC layer (unlike webpack, which runs the RSC transform first). See
// `crates/next-core/src/next_server/context.rs` (AppRSC module rules).
#[fixture("tests/errors/server-actions-turbopack-rsc/**/input.js")]
fn server_actions_turbopack_rsc_errors(input: PathBuf) {
use next_custom_transforms::transforms::react_server_components::{
Config, Options, server_components_assert,
};
let output = input.parent().unwrap().join("output.js");
test_fixture(
syntax(),
&|tr| {
let unresolved_mark = Mark::new();
(
resolver(unresolved_mark, Mark::new(), false),
server_actions(
&FileName::Real("/app/item.js".into()),
None,
server_actions::Config {
is_react_server_layer: true,
is_development: true,
use_cache_enabled: true,
hash_salt: "".into(),
cache_kinds: FxHashSet::default(),
},
tr.comments.as_ref().clone(),
unresolved_mark,
tr.cm.clone(),
Default::default(),
ServerActionsMode::Turbopack,
),
fn_pass(|program| {
program.visit_with(&mut server_components_assert(
FileName::Real(PathBuf::from("/app/item.js")),
Config::WithOptions(Options {
is_react_server_layer: true,
cache_components_enabled: true,
use_cache_enabled: true,
taint_enabled: true,
page_extensions: vec![],
}),
None,
));
}),
)
},
&input,
&output,
FixtureTestConfig {
allow_error: true,
module: Some(true),
..Default::default()
},
);
}
#[fixture("tests/errors/strip-page-exports/**/input.js")]
fn next_transform_strip_page_exports_errors(input: PathBuf) {
let output = input.parent().unwrap().join("output.js");
......
'use client'
'use strict'
export default function Page() {
return (
<form
action={async () => {
'use server'
}}
/>
)
}
/* __next_internal_action_entry_do_not_use__ [{"006a88810ecce4a4e8b59d53b8327d7e98bbf251d7":{"name":"$$RSC_SERVER_ACTION_0"}},"/app/item.js",""] */ 'use client';
'use strict';
import { registerServerReference } from "private-next-rsc-server-reference";
export const $$RSC_SERVER_ACTION_0 = async function action() {};
registerServerReference($$RSC_SERVER_ACTION_0, "006a88810ecce4a4e8b59d53b8327d7e98bbf251d7", null);
export default function Page() {
return <form action={$$RSC_SERVER_ACTION_0}/>;
}
'use client'
export default function Page() {
return (
<form
action={async () => {
'use server'
}}
/>
)
}
/* __next_internal_client_entry_do_not_use__ default auto */ export default function Page() {
return <form action={async ()=>{
'use server';
}}/>;
}
x It is not allowed to define inline "use server" annotated Server Actions in Client Components.
| To use Server Actions in a Client Component, you can either export them from a separate file with "use server" at the top, or pass them down through props from a Server Component.
|
| Read more: https://nextjs.org/docs/app/api-reference/directives/use-server#using-server-functions-in-a-client-component
|
,-[input.js:7:1]
6 | action={async () => {
7 | 'use server'
: ^^^^^^^^^^^^
8 | }}
`----
import type { ReactNode } from 'react'
export default function Root({ children }: { children: ReactNode }) {
return (
<html>
<body>{children}</body>
</html>
)
}
'use client'
export default function Page() {
return (
<form
action={async () => {
'use server'
}}
/>
)
}
import { nextTestSetup } from 'e2e-utils'
import { getRedboxSource, waitForRedbox } from 'next-test-utils'
describe('inline-use-server-directive-in-client', () => {
const { next } = nextTestSetup({
files: __dirname,
})
it('reports the inline "use server" error instead of a bogus "use client" placement error', async () => {
const browser = await next.browser('/')
await waitForRedbox(browser)
const source = await getRedboxSource(browser)
expect(source).toContain(
'It is not allowed to define inline "use server" annotated Server Actions in Client Components.'
)
expect(source).not.toContain('must be placed before other expressions')
})
})
import type { NextConfig } from 'next'
const nextConfig: NextConfig = {}
export default nextConfig
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment