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

Turbopack: Fix server action sourcemaps #69884

Merged
merged 2 commits into from
Sep 10, 2024
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
38 changes: 11 additions & 27 deletions crates/next-api/src/server_actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,19 @@ use turbo_tasks::{
};
use turbo_tasks_fs::{self, rope::RopeBuilder, File, FileSystemPath};
use turbopack_core::{
asset::{Asset, AssetContent},
asset::AssetContent,
chunk::{ChunkItemExt, ChunkableModule, ChunkingContext, EvaluatableAsset},
context::AssetContext,
file_source::FileSource,
module::Module,
output::OutputAsset,
reference::primary_referenced_modules,
reference_type::{
EcmaScriptModulesReferenceSubType, ReferenceType, TypeScriptReferenceSubType,
},
reference_type::{EcmaScriptModulesReferenceSubType, ReferenceType},
virtual_output::VirtualOutputAsset,
virtual_source::VirtualSource,
};
use turbopack_ecmascript::{
chunk::EcmascriptChunkPlaceable, parse::ParseResult, EcmascriptModuleAssetType,
EcmascriptParsable,
chunk::EcmascriptChunkPlaceable, parse::ParseResult, EcmascriptParsable,
};

/// Scans the RSC entry point's full module graph looking for exported Server
Expand Down Expand Up @@ -158,11 +156,6 @@ async fn build_manifest(
)))
}

#[turbo_tasks::function]
fn action_modifier() -> Vc<RcStr> {
Vc::cell("action".into())
}

/// Traverses the entire module graph starting from [Module], looking for magic
/// comment which identifies server actions. Every found server action will be
/// returned along with the module which exports that action.
Expand Down Expand Up @@ -230,23 +223,14 @@ async fn to_rsc_context(
module: Vc<Box<dyn Module>>,
asset_context: Vc<Box<dyn AssetContext>>,
) -> Result<Vc<Box<dyn Module>>> {
let source = VirtualSource::new_with_ident(
module.ident().with_modifier(action_modifier()),
module.content(),
);
let ty = if let Some(module) =
Vc::try_resolve_sidecast::<Box<dyn EcmascriptParsable>>(module).await?
{
if *module.ty().await? == EcmascriptModuleAssetType::Ecmascript {
ReferenceType::EcmaScriptModules(EcmaScriptModulesReferenceSubType::Undefined)
} else {
ReferenceType::TypeScript(TypeScriptReferenceSubType::Undefined)
}
} else {
ReferenceType::TypeScript(TypeScriptReferenceSubType::Undefined)
};
let source = FileSource::new_with_query(module.ident().path(), module.ident().query());
let module = asset_context
.process(Vc::upcast(source), Value::new(ty))
.process(
Vc::upcast(source),
Value::new(ReferenceType::EcmaScriptModules(
EcmaScriptModulesReferenceSubType::Undefined,
)),
)
.module();
Ok(module)
}
Expand Down
97 changes: 97 additions & 0 deletions test/development/acceptance-app/ReactRefreshLogBox.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1111,4 +1111,101 @@ describe.each(['default', 'turbo'])('ReactRefreshLogBox app %s', () => {
await cleanup()
})
}

test('Should show error location for server actions in client component', async () => {
const { session, browser, cleanup } = await sandbox(
next,
new Map([
[
'app/actions.ts',
`"use server";

export async function serverAction(a) {
throw new Error("server action was here");
}`,
],
[
'app/page.tsx',
`"use client";
import { serverAction } from "./actions";

export default function Home() {
return (
<>
<form action={serverAction}>
<button id="trigger-action">Submit</button>
</form>
</>
);
}`,
],
])
)

await browser.elementByCss('#trigger-action').click()

// Wait for patch to apply and new error to show.
await session.assertHasRedbox()
await retry(async () => {
expect(await session.getRedboxSource()).toMatchInlineSnapshot(`
"app/actions.ts (4:9) @ serverAction

2 |
3 | export async function serverAction(a) {
> 4 | throw new Error("server action was here");
| ^
5 | }"
`)
})

await cleanup()
})

test('Should show error location for server actions in server component', async () => {
const { session, browser, cleanup } = await sandbox(
next,
new Map([
[
'app/actions.ts',
`"use server";

export async function serverAction(a) {
throw new Error("server action was here");
}`,
],
[
'app/page.tsx',
`import { serverAction } from "./actions";

export default function Home() {
return (
<>
<form action={serverAction}>
<button id="trigger-action">Submit</button>
</form>
</>
);
}`,
],
])
)

await browser.elementByCss('#trigger-action').click()

// Wait for patch to apply and new error to show.
await session.assertHasRedbox()
await retry(async () => {
expect(await session.getRedboxSource()).toMatchInlineSnapshot(`
"app/actions.ts (4:9) @ serverAction

2 |
3 | export async function serverAction(a) {
> 4 | throw new Error("server action was here");
| ^
5 | }"
`)
})

await cleanup()
})
})
Loading