Skip to content

Commit

Permalink
Same as #61360 (#61369)
Browse files Browse the repository at this point in the history
## What?

Same PR as #61360, somehow that branch could not be checked out / wasn't
available in git.

<!-- Thanks for opening a PR! Your contribution is much appreciated.
To make sure your PR is handled as smoothly as possible we request that
you follow the checklist sections below.
Choose the right checklist for the change(s) that you're making:

## For Contributors

### Improving Documentation

- Run `pnpm prettier-fix` to fix formatting issues before opening the
PR.
- Read the Docs Contribution Guide to ensure your contribution follows
the docs guidelines:
https://nextjs.org/docs/community/contribution-guide

### Adding or Updating Examples

- The "examples guidelines" are followed from our contributing doc
https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md
- Make sure the linting passes by running `pnpm build && pnpm lint`. See
https://github.com/vercel/next.js/blob/canary/contributing/repository/linting.md

### Fixing a bug

- Related issues linked using `fixes #number`
- Tests added. See:
https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs
- Errors have a helpful link attached, see
https://github.com/vercel/next.js/blob/canary/contributing.md

### Adding a feature

- Implements an existing feature request or RFC. Make sure the feature
request has been accepted for implementation before opening a PR. (A
discussion must be opened, see
https://github.com/vercel/next.js/discussions/new?category=ideas)
- Related issues/discussions are linked using `fixes #number`
- e2e tests added
(https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs)
- Documentation added
- Telemetry added. In case of a feature if it's used or not.
- Errors have a helpful link attached, see
https://github.com/vercel/next.js/blob/canary/contributing.md


## For Maintainers

- Minimal description (aim for explaining to someone not on the team to
understand the PR)
- When linking to a Slack thread, you might want to share details of the
conclusion
- Link both the Linear (Fixes NEXT-xxx) and the GitHub issues
- Add review comments if necessary to explain to the reviewer the logic
behind a change

### What?

### Why?

### How?

Closes NEXT-
Fixes #

-->


Closes NEXT-2295

---------

Co-authored-by: Will Binns-Smith <wbinnssmith@gmail.com>
  • Loading branch information
timneutkens and wbinnssmith committed Jan 30, 2024
1 parent 292cd0d commit e114229
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 43 deletions.
53 changes: 39 additions & 14 deletions packages/next-swc/crates/napi/src/next_api/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,14 @@ use turbopack_binding::{
},
turbopack::{
core::{
chunk::ModuleId,
diagnostics::PlainDiagnostic,
error::PrettyPrintError,
issue::PlainIssue,
source_map::{GenerateSourceMap, Token},
version::{PartialUpdate, TotalUpdate, Update, VersionState},
},
dev::ecmascript::EcmascriptDevChunkContent,
ecmascript_hmr_protocol::{ClientUpdateInstruction, ResourceIdentifier},
trace_utils::{
exit::ExitGuard,
Expand Down Expand Up @@ -804,12 +806,16 @@ pub async fn project_trace_source(
let turbo_tasks = project.turbo_tasks.clone();
let traced_frame = turbo_tasks
.run_once(async move {
let file = match Url::parse(&frame.file) {
let (file, module) = match Url::parse(&frame.file) {
Ok(url) => match url.scheme() {
"file" => urlencoding::decode(url.path())?.to_string(),
"file" => {
let path = urlencoding::decode(url.path())?.to_string();
let module = url.query_pairs().find(|(k, _)| k == "id");
(path, module.map(|(_, m)| m.into_owned()))
}
_ => bail!("Unknown url scheme"),
},
Err(_) => frame.file.to_string(),
Err(_) => (frame.file.to_string(), None),
};

let Some(chunk_base) = file.strip_prefix(
Expand Down Expand Up @@ -837,19 +843,38 @@ pub async fn project_trace_source(
.join(chunk_base.to_owned())
};

let Some(versioned) = Vc::try_resolve_sidecast::<Box<dyn GenerateSourceMap>>(
project.container.get_versioned_content(path),
)
.await?
else {
bail!("Could not GenerateSourceMap")
let content_vc = project.container.get_versioned_content(path);
let map = match module {
Some(module) => {
let Some(content) =
Vc::try_resolve_downcast_type::<EcmascriptDevChunkContent>(content_vc)
.await?
else {
bail!("Was not EcmascriptDevChunkContent")
};

let entries = content.entries().await?;
let entry = entries.get(&ModuleId::String(module).cell().await?);
let map = match entry {
Some(entry) => *entry.code.generate_source_map().await?,
None => None,
};
map.context("Entry is missing sourcemap")?
}
None => {
let Some(versioned) =
Vc::try_resolve_sidecast::<Box<dyn GenerateSourceMap>>(content_vc).await?
else {
bail!("Could not GenerateSourceMap")
};

versioned
.generate_source_map()
.await?
.context("Chunk is missing a sourcemap")?
}
};

let map = versioned
.generate_source_map()
.await?
.context("Chunk is missing a sourcemap")?;

let token = map
.lookup_token(frame.line as usize, frame.column.unwrap_or(0) as usize)
.await?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export function parseStack(stack: string): StackFrame[] {
?.replace(/\\/g, '/')
?.replace(/\/$/, '')
if (distDir) {
frame.file = 'file://' + distDir.concat(res.pop()!)
frame.file = 'file://' + distDir.concat(res.pop()!) + url.search
}
}
} catch {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,17 +118,19 @@ https://nextjs.org/docs/messages/module-not-found"
`;
exports[`ReactRefreshLogBox app turbo Should not show __webpack_exports__ when exporting anonymous arrow function 1`] = `
"index.js (1:13) @ __TURBOPACK__default__export__
"index.js (3:2) @ __TURBOPACK__default__export__
> 1 | export default () => {
| ^
1 | export default () => {
2 | if (typeof window !== 'undefined') {
3 | throw new Error('test')
4 | }"
> 3 | throw new Error('test')
| ^
4 | }
5 |
6 | return null"
`;
exports[`ReactRefreshLogBox app turbo boundaries 1`] = `
"FunctionDefault.js (0:24) @ FunctionDefault
"FunctionDefault.js (0:67) @ FunctionDefault
1 | export default function FunctionDefault() { throw new Error('no'); }"
`;
Expand Down Expand Up @@ -162,13 +164,13 @@ exports[`ReactRefreshLogBox app turbo module init error not shown 1`] = `
`;
exports[`ReactRefreshLogBox app turbo should strip whitespace correctly with newline 1`] = `
"index.js (5:18) @ onClick
3 | <>
4 | <p>index page</p>
> 5 |
| ^
6 | <a onClick={() => {
7 | throw new Error('idk')
8 | }}>"
"index.js (7:6) @ onClick
5 |
6 | <a onClick={() => {
> 7 | throw new Error('idk')
| ^
8 | }}>
9 | click me
10 | </a>"
`;
39 changes: 27 additions & 12 deletions test/development/acceptance-app/error-recovery.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import path from 'path'
import { outdent } from 'outdent'

describe.each(['default', 'turbo'])('Error recovery app %s', () => {
const { next } = nextTestSetup({
const { next, isTurbopack } = nextTestSetup({
files: new FileRef(path.join(__dirname, 'fixtures', 'default-template')),
dependencies: {
react: 'latest',
Expand Down Expand Up @@ -143,17 +143,32 @@ describe.each(['default', 'turbo'])('Error recovery app %s', () => {
).toBe('1')

await session.waitForAndOpenRuntimeError()
expect(await session.getRedboxSource()).toMatchInlineSnapshot(`
"index.js (7:10) @ eval
5 | const increment = useCallback(() => {
6 | setCount(c => c + 1)
> 7 | throw new Error('oops')
| ^
8 | }, [setCount])
9 | return (
10 | <main>"
`)

if (isTurbopack) {
expect(await session.getRedboxSource()).toMatchInlineSnapshot(`
"index.js (7:5) @ eval
5 | const increment = useCallback(() => {
6 | setCount(c => c + 1)
> 7 | throw new Error('oops')
| ^
8 | }, [setCount])
9 | return (
10 | <main>"
`)
} else {
expect(await session.getRedboxSource()).toMatchInlineSnapshot(`
"index.js (7:10) @ eval
5 | const increment = useCallback(() => {
6 | setCount(c => c + 1)
> 7 | throw new Error('oops')
| ^
8 | }, [setCount])
9 | return (
10 | <main>"
`)
}

await session.patch(
'index.js',
Expand Down
2 changes: 1 addition & 1 deletion test/turbopack-tests-manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -1084,6 +1084,7 @@
},
"test/development/acceptance-app/error-recovery.test.ts": {
"passed": [
"Error recovery app turbo can recover from a event handler error",
"Error recovery app turbo can recover from a syntax error without losing state",
"Error recovery app turbo client component can recover from a component error",
"Error recovery app turbo displays build error on initial page load",
Expand All @@ -1092,7 +1093,6 @@
"Error recovery app turbo syntax > runtime error"
],
"failed": [
"Error recovery app turbo can recover from a event handler error",
"Error recovery app turbo client component can recover from syntax error",
"Error recovery app turbo server component can recover from a component error",
"Error recovery app turbo stuck error"
Expand Down

0 comments on commit e114229

Please sign in to comment.