Skip to content
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

fix(turbopack): Fix the span of a magic comment #76891

Merged
merged 6 commits into from
Mar 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 25 additions & 30 deletions crates/next-custom-transforms/src/transforms/server_actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -438,19 +438,15 @@ impl<C: Comments> ServerActions<C> {
.push((action_name.clone(), action_id.clone()));

let register_action_expr = bind_args_to_ref_expr(
annotate_ident_as_server_reference(
action_ident.clone(),
action_id.clone(),
arrow.span,
&self.comments,
),
annotate_ident_as_server_reference(action_ident.clone(), action_id.clone(), arrow.span),
ids_from_closure
.iter()
.cloned()
.map(|id| Some(id.as_arg()))
.collect(),
action_id.clone(),
);
add_turbopack_disable_export_merging_comment(action_ident.span, &self.comments);

if let BlockStmtOrExpr::BlockStmt(block) = &mut *arrow.body {
block.visit_mut_with(&mut ClosureReplacer {
Expand Down Expand Up @@ -573,7 +569,11 @@ impl<C: Comments> ServerActions<C> {
new_params.append(&mut function.params);

let action_name: Atom = self.gen_action_ident();
let action_ident = Ident::new(action_name.clone(), function.span, self.private_ctxt);
let mut action_ident = Ident::new(action_name.clone(), function.span, self.private_ctxt);
if action_ident.span.lo == self.start_pos {
action_ident.span = Span::dummy_with_cmt();
}

let action_id = self.generate_server_reference_id(&action_name, false, Some(&new_params));

self.has_action = true;
Expand All @@ -585,7 +585,6 @@ impl<C: Comments> ServerActions<C> {
action_ident.clone(),
action_id.clone(),
function.span,
&self.comments,
),
ids_from_closure
.iter()
Expand All @@ -594,6 +593,7 @@ impl<C: Comments> ServerActions<C> {
.collect(),
action_id.clone(),
);
add_turbopack_disable_export_merging_comment(action_ident.span, &self.comments);

function.body.visit_mut_with(&mut ClosureReplacer {
used_ids: &ids_from_closure,
Expand Down Expand Up @@ -694,7 +694,7 @@ impl<C: Comments> ServerActions<C> {
}

let cache_name: Atom = self.gen_cache_ident();
let cache_ident = private_ident!(cache_name.clone());
let cache_ident = private_ident!(Span::dummy_with_cmt(), cache_name.clone());
let export_name: Atom = cache_name;

let reference_id = self.generate_server_reference_id(&export_name, true, Some(&new_params));
Expand Down Expand Up @@ -769,8 +769,8 @@ impl<C: Comments> ServerActions<C> {
cache_ident.clone(),
reference_id.clone(),
arrow.span,
&self.comments,
);
add_turbopack_disable_export_merging_comment(cache_ident.span, &self.comments);

// If there're any bound args from the closure, we need to hoist the
// register action expression to the top-level, and return the bind
Expand Down Expand Up @@ -829,7 +829,7 @@ impl<C: Comments> ServerActions<C> {
}

let cache_name: Atom = self.gen_cache_ident();
let cache_ident = private_ident!(cache_name.clone());
let cache_ident = private_ident!(Span::dummy_with_cmt(), cache_name.clone());

let reference_id = self.generate_server_reference_id(&cache_name, true, Some(&new_params));

Expand All @@ -841,8 +841,8 @@ impl<C: Comments> ServerActions<C> {
cache_ident.clone(),
reference_id.clone(),
function.span,
&self.comments,
);
add_turbopack_disable_export_merging_comment(cache_ident.span, &self.comments);

function.body.visit_mut_with(&mut ClosureReplacer {
used_ids: &ids_from_closure,
Expand Down Expand Up @@ -1941,9 +1941,9 @@ impl<C: Comments> VisitMut for ServerActions<C> {
ident.clone(),
ref_id.clone(),
ident.span,
&self.comments,
)),
}));
add_turbopack_disable_export_merging_comment(ident.span, &self.comments);
}
}

Expand Down Expand Up @@ -2328,23 +2328,7 @@ fn assign_arrow_expr(ident: &Ident, expr: Expr) -> Expr {
}
}

fn annotate_ident_as_server_reference(
ident: Ident,
action_id: Atom,
original_span: Span,
comments: &dyn Comments,
) -> Expr {
if !original_span.lo.is_dummy() {
comments.add_leading(
original_span.lo,
Comment {
kind: CommentKind::Block,
span: original_span,
text: "#__TURBOPACK_DISABLE_EXPORT_MERGING__".into(),
},
);
}

fn annotate_ident_as_server_reference(ident: Ident, action_id: Atom, original_span: Span) -> Expr {
// registerServerReference(reference, id, null)
Expr::Call(CallExpr {
span: original_span,
Expand All @@ -2367,6 +2351,17 @@ fn annotate_ident_as_server_reference(
})
}

fn add_turbopack_disable_export_merging_comment(span: Span, comments: &dyn Comments) {
comments.add_leading(
span.lo,
Comment {
kind: CommentKind::Block,
span: DUMMY_SP,
text: "#__TURBOPACK_DISABLE_EXPORT_MERGING__".into(),
},
);
}

fn bind_args_to_ref_expr(expr: Expr, bound: Vec<Option<ExprOrSpread>>, action_id: Atom) -> Expr {
if bound.is_empty() {
expr
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// Rules here:
// 1. Each exported function should still be exported, but as a reference `registerServerReference(...)`.
// 2. Actual action functions should be renamed to `$$ACTION_...` and got exported.
/*#__TURBOPACK_DISABLE_EXPORT_MERGING__*/ /* __next_internal_action_entry_do_not_use__ {"001c36b06e398c97abe5d5d7ae8c672bfddf4e1b91":"$$RSC_SERVER_ACTION_2","006a88810ecce4a4e8b59d53b8327d7e98bbf251d7":"$$RSC_SERVER_ACTION_0","0090b5db271335765a4b0eab01f044b381b5ebd5cd":"$$RSC_SERVER_ACTION_1","009ed0cc47abc4e1c64320cf42b74ae60b58c40f00":"$$RSC_SERVER_ACTION_3","00a9b2939c1f39073a6bed227fd20233064c8b7869":"$$RSC_SERVER_ACTION_4"} */ import { registerServerReference } from "private-next-rsc-server-reference";
/* __next_internal_action_entry_do_not_use__ {"001c36b06e398c97abe5d5d7ae8c672bfddf4e1b91":"$$RSC_SERVER_ACTION_2","006a88810ecce4a4e8b59d53b8327d7e98bbf251d7":"$$RSC_SERVER_ACTION_0","0090b5db271335765a4b0eab01f044b381b5ebd5cd":"$$RSC_SERVER_ACTION_1","009ed0cc47abc4e1c64320cf42b74ae60b58c40f00":"$$RSC_SERVER_ACTION_3","00a9b2939c1f39073a6bed227fd20233064c8b7869":"$$RSC_SERVER_ACTION_4"} */ import { registerServerReference } from "private-next-rsc-server-reference";
import { encryptActionBoundArgs, decryptActionBoundArgs } from "private-next-rsc-action-encryption";
export const $$RSC_SERVER_ACTION_0 = async function foo() {
export const /*#__TURBOPACK_DISABLE_EXPORT_MERGING__*/ $$RSC_SERVER_ACTION_0 = async function foo() {
console.log(1);
};
var foo = registerServerReference($$RSC_SERVER_ACTION_0, "006a88810ecce4a4e8b59d53b8327d7e98bbf251d7", null);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// This is for testing the "information byte" of Server Action / Cache IDs.
// Should be 1 110000 0, which is "e0" in hex.
/*#__TURBOPACK_DISABLE_EXPORT_MERGING__*/ /* __next_internal_action_entry_do_not_use__ {"6090b5db271335765a4b0eab01f044b381b5ebd5cd":"$$RSC_SERVER_ACTION_1","7c9ed0cc47abc4e1c64320cf42b74ae60b58c40f00":"$$RSC_SERVER_ACTION_3","7ea9b2939c1f39073a6bed227fd20233064c8b7869":"$$RSC_SERVER_ACTION_4","e03128060c414d59f8552e4788b846c0d2b7f74743":"$$RSC_SERVER_CACHE_0","ff471a5eb0be1c31686dd4ba938a80328b80b1615d":"$$RSC_SERVER_CACHE_5","ff69348c79fce073bae2f70f139565a2fda1c74c74":"$$RSC_SERVER_CACHE_2"} */ import { registerServerReference } from "private-next-rsc-server-reference";
/* __next_internal_action_entry_do_not_use__ {"6090b5db271335765a4b0eab01f044b381b5ebd5cd":"$$RSC_SERVER_ACTION_1","7c9ed0cc47abc4e1c64320cf42b74ae60b58c40f00":"$$RSC_SERVER_ACTION_3","7ea9b2939c1f39073a6bed227fd20233064c8b7869":"$$RSC_SERVER_ACTION_4","e03128060c414d59f8552e4788b846c0d2b7f74743":"$$RSC_SERVER_CACHE_0","ff471a5eb0be1c31686dd4ba938a80328b80b1615d":"$$RSC_SERVER_CACHE_5","ff69348c79fce073bae2f70f139565a2fda1c74c74":"$$RSC_SERVER_CACHE_2"} */ import { registerServerReference } from "private-next-rsc-server-reference";
import { encryptActionBoundArgs, decryptActionBoundArgs } from "private-next-rsc-action-encryption";
import { cache as $$cache__ } from "private-next-rsc-cache-wrapper";
export var $$RSC_SERVER_CACHE_0 = $$cache__("default", "e03128060c414d59f8552e4788b846c0d2b7f74743", 0, async function f1(a, b) {
export var /*#__TURBOPACK_DISABLE_EXPORT_MERGING__*/ $$RSC_SERVER_CACHE_0 = $$cache__("default", "e03128060c414d59f8552e4788b846c0d2b7f74743", 0, async function f1(a, b) {
return [
a,
b
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading