Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 5a4e3ee

Browse files
committedMar 10, 2025·
fix(turbopack): Fix the span of a magic comment (#76891)
### What? Fix the span of the turbopack tree shaking magic comment. ### Why? Previously it was printed on strange position. ### How? Closes NAR-91
1 parent 83dc5ca commit 5a4e3ee

File tree

4 files changed

+30
-35
lines changed

4 files changed

+30
-35
lines changed
 

‎crates/next-custom-transforms/src/transforms/server_actions.rs

+25-30
Original file line numberDiff line numberDiff line change
@@ -438,19 +438,15 @@ impl<C: Comments> ServerActions<C> {
438438
.push((action_name.clone(), action_id.clone()));
439439

440440
let register_action_expr = bind_args_to_ref_expr(
441-
annotate_ident_as_server_reference(
442-
action_ident.clone(),
443-
action_id.clone(),
444-
arrow.span,
445-
&self.comments,
446-
),
441+
annotate_ident_as_server_reference(action_ident.clone(), action_id.clone(), arrow.span),
447442
ids_from_closure
448443
.iter()
449444
.cloned()
450445
.map(|id| Some(id.as_arg()))
451446
.collect(),
452447
action_id.clone(),
453448
);
449+
add_turbopack_disable_export_merging_comment(action_ident.span, &self.comments);
454450

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

575571
let action_name: Atom = self.gen_action_ident();
576-
let action_ident = Ident::new(action_name.clone(), function.span, self.private_ctxt);
572+
let mut action_ident = Ident::new(action_name.clone(), function.span, self.private_ctxt);
573+
if action_ident.span.lo == self.start_pos {
574+
action_ident.span = Span::dummy_with_cmt();
575+
}
576+
577577
let action_id = self.generate_server_reference_id(&action_name, false, Some(&new_params));
578578

579579
self.has_action = true;
@@ -585,7 +585,6 @@ impl<C: Comments> ServerActions<C> {
585585
action_ident.clone(),
586586
action_id.clone(),
587587
function.span,
588-
&self.comments,
589588
),
590589
ids_from_closure
591590
.iter()
@@ -594,6 +593,7 @@ impl<C: Comments> ServerActions<C> {
594593
.collect(),
595594
action_id.clone(),
596595
);
596+
add_turbopack_disable_export_merging_comment(action_ident.span, &self.comments);
597597

598598
function.body.visit_mut_with(&mut ClosureReplacer {
599599
used_ids: &ids_from_closure,
@@ -694,7 +694,7 @@ impl<C: Comments> ServerActions<C> {
694694
}
695695

696696
let cache_name: Atom = self.gen_cache_ident();
697-
let cache_ident = private_ident!(cache_name.clone());
697+
let cache_ident = private_ident!(Span::dummy_with_cmt(), cache_name.clone());
698698
let export_name: Atom = cache_name;
699699

700700
let reference_id = self.generate_server_reference_id(&export_name, true, Some(&new_params));
@@ -769,8 +769,8 @@ impl<C: Comments> ServerActions<C> {
769769
cache_ident.clone(),
770770
reference_id.clone(),
771771
arrow.span,
772-
&self.comments,
773772
);
773+
add_turbopack_disable_export_merging_comment(cache_ident.span, &self.comments);
774774

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

831831
let cache_name: Atom = self.gen_cache_ident();
832-
let cache_ident = private_ident!(cache_name.clone());
832+
let cache_ident = private_ident!(Span::dummy_with_cmt(), cache_name.clone());
833833

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

@@ -841,8 +841,8 @@ impl<C: Comments> ServerActions<C> {
841841
cache_ident.clone(),
842842
reference_id.clone(),
843843
function.span,
844-
&self.comments,
845844
);
845+
add_turbopack_disable_export_merging_comment(cache_ident.span, &self.comments);
846846

847847
function.body.visit_mut_with(&mut ClosureReplacer {
848848
used_ids: &ids_from_closure,
@@ -1941,9 +1941,9 @@ impl<C: Comments> VisitMut for ServerActions<C> {
19411941
ident.clone(),
19421942
ref_id.clone(),
19431943
ident.span,
1944-
&self.comments,
19451944
)),
19461945
}));
1946+
add_turbopack_disable_export_merging_comment(ident.span, &self.comments);
19471947
}
19481948
}
19491949

@@ -2328,23 +2328,7 @@ fn assign_arrow_expr(ident: &Ident, expr: Expr) -> Expr {
23282328
}
23292329
}
23302330

2331-
fn annotate_ident_as_server_reference(
2332-
ident: Ident,
2333-
action_id: Atom,
2334-
original_span: Span,
2335-
comments: &dyn Comments,
2336-
) -> Expr {
2337-
if !original_span.lo.is_dummy() {
2338-
comments.add_leading(
2339-
original_span.lo,
2340-
Comment {
2341-
kind: CommentKind::Block,
2342-
span: original_span,
2343-
text: "#__TURBOPACK_DISABLE_EXPORT_MERGING__".into(),
2344-
},
2345-
);
2346-
}
2347-
2331+
fn annotate_ident_as_server_reference(ident: Ident, action_id: Atom, original_span: Span) -> Expr {
23482332
// registerServerReference(reference, id, null)
23492333
Expr::Call(CallExpr {
23502334
span: original_span,
@@ -2367,6 +2351,17 @@ fn annotate_ident_as_server_reference(
23672351
})
23682352
}
23692353

2354+
fn add_turbopack_disable_export_merging_comment(span: Span, comments: &dyn Comments) {
2355+
comments.add_leading(
2356+
span.lo,
2357+
Comment {
2358+
kind: CommentKind::Block,
2359+
span: DUMMY_SP,
2360+
text: "#__TURBOPACK_DISABLE_EXPORT_MERGING__".into(),
2361+
},
2362+
);
2363+
}
2364+
23702365
fn bind_args_to_ref_expr(expr: Expr, bound: Vec<Option<ExprOrSpread>>, action_id: Atom) -> Expr {
23712366
if bound.is_empty() {
23722367
expr

‎crates/next-custom-transforms/tests/fixture/server-actions/server-graph/27/output.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
// Rules here:
22
// 1. Each exported function should still be exported, but as a reference `registerServerReference(...)`.
33
// 2. Actual action functions should be renamed to `$$ACTION_...` and got exported.
4-
/*#__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";
4+
/* __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";
55
import { encryptActionBoundArgs, decryptActionBoundArgs } from "private-next-rsc-action-encryption";
6-
export const $$RSC_SERVER_ACTION_0 = async function foo() {
6+
export const /*#__TURBOPACK_DISABLE_EXPORT_MERGING__*/ $$RSC_SERVER_ACTION_0 = async function foo() {
77
console.log(1);
88
};
99
var foo = registerServerReference($$RSC_SERVER_ACTION_0, "006a88810ecce4a4e8b59d53b8327d7e98bbf251d7", null);

‎crates/next-custom-transforms/tests/fixture/server-actions/server-graph/46/output.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
// This is for testing the "information byte" of Server Action / Cache IDs.
22
// Should be 1 110000 0, which is "e0" in hex.
3-
/*#__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";
3+
/* __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";
44
import { encryptActionBoundArgs, decryptActionBoundArgs } from "private-next-rsc-action-encryption";
55
import { cache as $$cache__ } from "private-next-rsc-cache-wrapper";
6-
export var $$RSC_SERVER_CACHE_0 = $$cache__("default", "e03128060c414d59f8552e4788b846c0d2b7f74743", 0, async function f1(a, b) {
6+
export var /*#__TURBOPACK_DISABLE_EXPORT_MERGING__*/ $$RSC_SERVER_CACHE_0 = $$cache__("default", "e03128060c414d59f8552e4788b846c0d2b7f74743", 0, async function f1(a, b) {
77
return [
88
a,
99
b

‎crates/next-custom-transforms/tests/fixture/source-maps/server-graph/use-cache/1/output.map

+1-1
Original file line numberDiff line numberDiff line change

0 commit comments

Comments
 (0)
Please sign in to comment.