Unverified Commit 3a5d293c authored by Benjamin Woodruff's avatar Benjamin Woodruff Committed by GitHub

feat(turbopack): Log anonymized internal error (panic) information to telemetry (#81272)

## Goals

- Pass through a custom `TurbopackInternalError` type when a Rust panic or an unexpected top-level `anyhow::Error` occurs.
- Make a best-effort attempt to log a location of the error to telemetry.
  - In the case of a panic, we should have the file, line, and column where the panic occured. This could be obscured inside of a helper method, but it's better than nothing. Since this is a position in Turbopack's source code, there's zero risk of user data being included here, so it should be safe to log to telemetry.

## How it works

In previous PRs, @wbinnssmith did the following:
- Added a thread-local for storing the most recent panic information in Rust
- Added `catch_unwind` logic to intercept the panic in turbo-tasks and read the thread-local information, with care take to make sure we're reading from the same thread (not across an `await` boundary).
- Added a custom `std::error::Error` type for internal errors, with support for storing information about a source panic.
- Ensured we set up `Error::source` correctly everywhere so that you can trace an error back to the root cause (presumably a panic).

In #80881, I added a `NextTurbopackContext` type that we can extend to store additional helpers on.

In this PR:
- Adds a callback to the `Project` constructor called `throwTurbopackInternalError` that constructs and throws a custom `TurbopackInternalError`. The definition of `TurbopackInternalError` must be in JS, as we cannot reasonably define the error subclass using NAPI.
- Wraps the callback in a `ThreadsafeFunction` so that it can be called in any of our async functions.
- Replaces all the places we were previously converting to `napi::Error` with logic to run `throwTurbopackInternalError`.

In a subsequent PR, we could move more of the panic handling into JS using `throwTurbopackInternalError`.

## Testing

Added a panic and ran:

```
NEXT_TELEMETRY_DEBUG=1 pnpm next dev --turbopack examples/basic-css/
```

<img src="https://graphite-user-uploaded-assets-prod.s3.amazonaws.com/HAZVitxRNnZz8QMiPn4a/7f261d3f-351e-436a-9345-fd6e9056d20c.png" width="600">
parent f8afa172
...@@ -2321,9 +2321,9 @@ checksum = "e6d5a32815ae3f33302d95fdcb2ce17862f8c65363dcfd29360480ba1001fc9c" ...@@ -2321,9 +2321,9 @@ checksum = "e6d5a32815ae3f33302d95fdcb2ce17862f8c65363dcfd29360480ba1001fc9c"
[[package]] [[package]]
name = "futures" name = "futures"
version = "0.3.30" version = "0.3.31"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "645c6916888f6cb6350d2550b80fb63e734897a8498abe35cfb732b6487804b0" checksum = "65bc07b1a8bc7c85c5f2e110c476c7389b4554ba72af57d8445ea63a576b0876"
dependencies = [ dependencies = [
"futures-channel", "futures-channel",
"futures-core", "futures-core",
...@@ -2352,9 +2352,9 @@ checksum = "05f29059c0c2090612e8d742178b0580d2dc940c837851ad723096f87af6663e" ...@@ -2352,9 +2352,9 @@ checksum = "05f29059c0c2090612e8d742178b0580d2dc940c837851ad723096f87af6663e"
[[package]] [[package]]
name = "futures-executor" name = "futures-executor"
version = "0.3.30" version = "0.3.31"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a576fc72ae164fca6b9db127eaa9a9dda0d61316034f33a0a0d4eda41f02b01d" checksum = "1e28d1d997f585e54aebc3f97d39e72338912123a67330d723fdbb564d646c9f"
dependencies = [ dependencies = [
"futures-core", "futures-core",
"futures-task", "futures-task",
...@@ -4415,6 +4415,7 @@ dependencies = [ ...@@ -4415,6 +4415,7 @@ dependencies = [
"console-subscriber", "console-subscriber",
"dhat", "dhat",
"either", "either",
"futures-util",
"getrandom 0.2.15", "getrandom 0.2.15",
"iana-time-zone", "iana-time-zone",
"lightningcss-napi", "lightningcss-napi",
......
...@@ -349,7 +349,8 @@ dhat = { version = "0.3.2" } ...@@ -349,7 +349,8 @@ dhat = { version = "0.3.2" }
dunce = "1.0.3" dunce = "1.0.3"
either = "1.9.0" either = "1.9.0"
erased-serde = "0.4.5" erased-serde = "0.4.5"
futures = "0.3.26" futures = "0.3.31"
futures-util = "0.3.31"
futures-retry = "0.6.0" futures-retry = "0.6.0"
hashbrown = "0.14.5" hashbrown = "0.14.5"
image = { version = "0.25.0", default-features = false } image = { version = "0.25.0", default-features = false }
......
...@@ -58,6 +58,7 @@ anyhow = "1.0.66" ...@@ -58,6 +58,7 @@ anyhow = "1.0.66"
console-subscriber = { workspace = true, optional = true } console-subscriber = { workspace = true, optional = true }
dhat = { workspace = true, optional = true } dhat = { workspace = true, optional = true }
either = { workspace = true } either = { workspace = true }
futures-util = { workspace = true }
owo-colors = { workspace = true } owo-colors = { workspace = true }
napi = { workspace = true } napi = { workspace = true }
napi-derive = "2" napi-derive = "2"
......
use std::{ops::Deref, sync::Arc}; use std::{ops::Deref, sync::Arc};
use anyhow::Result; use anyhow::Result;
use futures_util::TryFutureExt;
use napi::{JsFunction, bindgen_prelude::External}; use napi::{JsFunction, bindgen_prelude::External};
use next_api::{ use next_api::{
operation::OptionEndpoint, operation::OptionEndpoint,
...@@ -12,7 +13,7 @@ use next_api::{ ...@@ -12,7 +13,7 @@ use next_api::{
}; };
use tracing::Instrument; use tracing::Instrument;
use turbo_tasks::{Completion, Effects, OperationVc, ReadRef, Vc}; use turbo_tasks::{Completion, Effects, OperationVc, ReadRef, Vc};
use turbopack_core::{diagnostics::PlainDiagnostic, error::PrettyPrintError, issue::PlainIssue}; use turbopack_core::{diagnostics::PlainDiagnostic, issue::PlainIssue};
use super::utils::{ use super::utils::{
DetachedVc, NapiDiagnostic, NapiIssue, RootTask, TurbopackResult, DetachedVc, NapiDiagnostic, NapiIssue, RootTask, TurbopackResult,
...@@ -125,6 +126,7 @@ async fn get_written_endpoint_with_issues_operation( ...@@ -125,6 +126,7 @@ async fn get_written_endpoint_with_issues_operation(
pub async fn endpoint_write_to_disk( pub async fn endpoint_write_to_disk(
#[napi(ts_arg_type = "{ __napiType: \"Endpoint\" }")] endpoint: External<ExternalEndpoint>, #[napi(ts_arg_type = "{ __napiType: \"Endpoint\" }")] endpoint: External<ExternalEndpoint>,
) -> napi::Result<TurbopackResult<NapiWrittenEndpoint>> { ) -> napi::Result<TurbopackResult<NapiWrittenEndpoint>> {
let ctx = endpoint.turbopack_ctx();
let endpoint_op = ***endpoint; let endpoint_op = ***endpoint;
let (written, issues, diags) = endpoint let (written, issues, diags) = endpoint
.turbopack_ctx() .turbopack_ctx()
...@@ -144,8 +146,8 @@ pub async fn endpoint_write_to_disk( ...@@ -144,8 +146,8 @@ pub async fn endpoint_write_to_disk(
Ok((written.clone(), issues.clone(), diagnostics.clone())) Ok((written.clone(), issues.clone(), diagnostics.clone()))
}) })
.await .or_else(|e| ctx.throw_turbopack_internal_result(&e))
.map_err(|e| napi::Error::from_reason(PrettyPrintError(&e).to_string()))?; .await?;
Ok(TurbopackResult { Ok(TurbopackResult {
result: NapiWrittenEndpoint::from(written.map(ReadRef::into_owned)), result: NapiWrittenEndpoint::from(written.map(ReadRef::into_owned)),
issues: issues.iter().map(|i| NapiIssue::from(&**i)).collect(), issues: issues.iter().map(|i| NapiIssue::from(&**i)).collect(),
......
This diff is collapsed.
This diff is collapsed.
use std::{future::Future, ops::Deref, sync::Arc}; use std::{future::Future, ops::Deref, sync::Arc};
use anyhow::{Context, Result, anyhow}; use anyhow::{Context, Result, anyhow};
use futures_util::TryFutureExt;
use napi::{ use napi::{
JsFunction, JsObject, JsUnknown, NapiRaw, NapiValue, Status, JsFunction, JsObject, JsUnknown, NapiRaw, NapiValue, Status,
bindgen_prelude::{External, ToNapiValue}, bindgen_prelude::{External, ToNapiValue},
...@@ -14,14 +15,13 @@ use turbo_tasks::{ ...@@ -14,14 +15,13 @@ use turbo_tasks::{
use turbo_tasks_fs::FileContent; use turbo_tasks_fs::FileContent;
use turbopack_core::{ use turbopack_core::{
diagnostics::{Diagnostic, DiagnosticContextExt, PlainDiagnostic}, diagnostics::{Diagnostic, DiagnosticContextExt, PlainDiagnostic},
error::PrettyPrintError,
issue::{ issue::{
IssueDescriptionExt, IssueSeverity, PlainIssue, PlainIssueSource, PlainSource, StyledString, IssueDescriptionExt, IssueSeverity, PlainIssue, PlainIssueSource, PlainSource, StyledString,
}, },
source_pos::SourcePos, source_pos::SourcePos,
}; };
use crate::{next_api::turbopack_ctx::NextTurbopackContext, util::log_internal_error_and_inform}; use crate::next_api::turbopack_ctx::NextTurbopackContext;
/// An [`OperationVc`] that can be passed back and forth to JS across the [`napi`][mod@napi] /// An [`OperationVc`] that can be passed back and forth to JS across the [`napi`][mod@napi]
/// boundary via [`External`]. /// boundary via [`External`].
...@@ -324,35 +324,35 @@ impl<T: ToNapiValue> ToNapiValue for TurbopackResult<T> { ...@@ -324,35 +324,35 @@ impl<T: ToNapiValue> ToNapiValue for TurbopackResult<T> {
} }
pub fn subscribe<T: 'static + Send + Sync, F: Future<Output = Result<T>> + Send, V: ToNapiValue>( pub fn subscribe<T: 'static + Send + Sync, F: Future<Output = Result<T>> + Send, V: ToNapiValue>(
turbopack_ctx: NextTurbopackContext, ctx: NextTurbopackContext,
func: JsFunction, func: JsFunction,
handler: impl 'static + Sync + Send + Clone + Fn() -> F, handler: impl 'static + Sync + Send + Clone + Fn() -> F,
mapper: impl 'static + Sync + Send + FnMut(ThreadSafeCallContext<T>) -> napi::Result<Vec<V>>, mapper: impl 'static + Sync + Send + FnMut(ThreadSafeCallContext<T>) -> napi::Result<Vec<V>>,
) -> napi::Result<External<RootTask>> { ) -> napi::Result<External<RootTask>> {
let func: ThreadsafeFunction<T> = func.create_threadsafe_function(0, mapper)?; let func: ThreadsafeFunction<T> = func.create_threadsafe_function(0, mapper)?;
let task_id = turbopack_ctx.turbo_tasks().spawn_root_task(move || { let task_id = ctx.turbo_tasks().spawn_root_task({
let handler = handler.clone(); let ctx = ctx.clone();
let func = func.clone(); move || {
Box::pin(async move { let ctx = ctx.clone();
let result = handler().await; let handler = handler.clone();
let func = func.clone();
let status = func.call( async move {
result.map_err(|e| { let result = handler()
log_internal_error_and_inform(&e); .or_else(|e| ctx.throw_turbopack_internal_result(&e))
napi::Error::from_reason(PrettyPrintError(&e).to_string()) .await;
}),
ThreadsafeFunctionCallMode::NonBlocking, let status = func.call(result, ThreadsafeFunctionCallMode::NonBlocking);
); if !matches!(status, Status::Ok) {
if !matches!(status, Status::Ok) { let error = anyhow!("Error calling JS function: {}", status);
let error = anyhow!("Error calling JS function: {}", status); eprintln!("{error}");
eprintln!("{error}"); return Err::<Vc<()>, _>(error);
return Err::<Vc<()>, _>(error); }
Ok(Default::default())
} }
Ok(Default::default()) }
})
}); });
Ok(External::new(RootTask { Ok(External::new(RootTask {
turbopack_ctx, turbopack_ctx: ctx,
task_id: Some(task_id), task_id: Some(task_id),
})) }))
} }
......
...@@ -26,140 +26,12 @@ IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER ...@@ -26,140 +26,12 @@ IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE. DEALINGS IN THE SOFTWARE.
*/ */
use std::{ use std::{cell::RefCell, env, path::PathBuf};
cell::RefCell,
env,
fs::OpenOptions,
io::{self, BufRead, Write},
path::PathBuf,
sync::Mutex,
time::Instant,
};
use anyhow::anyhow; use anyhow::anyhow;
use napi::bindgen_prelude::{External, Status}; use napi::bindgen_prelude::{External, Status};
use once_cell::sync::Lazy;
use owo_colors::OwoColorize;
use terminal_hyperlink::Hyperlink;
use tracing_chrome::{ChromeLayerBuilder, FlushGuard}; use tracing_chrome::{ChromeLayerBuilder, FlushGuard};
use tracing_subscriber::{Layer, filter, prelude::*, util::SubscriberInitExt}; use tracing_subscriber::{Layer, filter, layer::SubscriberExt, util::SubscriberInitExt};
use turbopack_core::error::PrettyPrintError;
static LOG_THROTTLE: Mutex<Option<Instant>> = Mutex::new(None);
static LOG_DIVIDER: &str = "---------------------------";
static PANIC_LOG: Lazy<PathBuf> = Lazy::new(|| {
let mut path = env::temp_dir();
path.push(format!("next-panic-{:x}.log", rand::random::<u128>()));
path
});
pub fn log_internal_error_and_inform(internal_error: &anyhow::Error) {
if cfg!(debug_assertions)
|| env::var("SWC_DEBUG") == Ok("1".to_string())
|| env::var("CI").is_ok_and(|v| !v.is_empty())
// Next's run-tests unsets CI and sets NEXT_TEST_CI
|| env::var("NEXT_TEST_CI").is_ok_and(|v| !v.is_empty())
{
eprintln!(
"{}: An unexpected Turbopack error occurred:\n{}",
"FATAL".red().bold(),
PrettyPrintError(internal_error)
);
return;
}
// hold open this mutex guard to prevent concurrent writes to the file!
let mut last_error_time = LOG_THROTTLE.lock().unwrap();
if let Some(last_error_time) = last_error_time.as_ref()
&& last_error_time.elapsed().as_secs() < 1
{
// Throttle panic logging to once per second
return;
}
*last_error_time = Some(Instant::now());
let size = std::fs::metadata(PANIC_LOG.as_path()).map(|m| m.len());
if let Ok(size) = size
&& size > 512 * 1024
{
// Truncate the earliest error from log file if it's larger than 512KB
let new_lines = {
let log_read = OpenOptions::new()
.read(true)
.open(PANIC_LOG.as_path())
.unwrap_or_else(|_| panic!("Failed to open {}", PANIC_LOG.to_string_lossy()));
io::BufReader::new(&log_read)
.lines()
.skip(1)
.skip_while(|line| match line {
Ok(line) => !line.starts_with(LOG_DIVIDER),
Err(_) => false,
})
.collect::<Vec<_>>()
};
let mut log_write = OpenOptions::new()
.create(true)
.truncate(true)
.write(true)
.open(PANIC_LOG.as_path())
.unwrap_or_else(|_| panic!("Failed to open {}", PANIC_LOG.to_string_lossy()));
for line in new_lines {
match line {
Ok(line) => {
writeln!(log_write, "{line}").unwrap();
}
Err(_) => {
break;
}
}
}
}
let mut log_file = OpenOptions::new()
.create(true)
.append(true)
.open(PANIC_LOG.as_path())
.unwrap_or_else(|_| panic!("Failed to open {}", PANIC_LOG.to_string_lossy()));
let internal_error_str: String = PrettyPrintError(internal_error).to_string();
writeln!(log_file, "{}\n{}", LOG_DIVIDER, &internal_error_str).unwrap();
let title = format!(
"Turbopack Error: {}",
internal_error_str.lines().next().unwrap_or("Unknown")
);
let version_str = format!(
"Turbopack version: `{}`\nNext.js version: `{}`",
env!("VERGEN_GIT_DESCRIBE"),
env!("NEXTJS_VERSION")
);
let new_discussion_url = if supports_hyperlinks::supports_hyperlinks() {
"clicking here.".hyperlink(
format!(
"https://github.com/vercel/next.js/discussions/new?category=turbopack-error-report&title={}&body={}&labels=Turbopack,Turbopack%20Panic%20Backtrace",
&urlencoding::encode(&title),
&urlencoding::encode(&format!("{}\n\nError message:\n```\n{}\n```", &version_str, &internal_error_str))
)
)
} else {
format!(
"clicking here: https://github.com/vercel/next.js/discussions/new?category=turbopack-error-report&title={}&body={}&labels=Turbopack,Turbopack%20Panic%20Backtrace",
&urlencoding::encode(&title),
&urlencoding::encode(&format!("{}\n\nError message:\n```\n{}\n```", &version_str, &title))
)
};
eprintln!(
"\n-----\n{}: An unexpected Turbopack error occurred. A panic log has been written to \
{}.\n\nTo help make Turbopack better, report this error by {}\n-----\n",
"FATAL".red().bold(),
PANIC_LOG.to_string_lossy(),
&new_discussion_url
);
}
#[napi] #[napi]
pub fn get_target_triple() -> &'static str { pub fn get_target_triple() -> &'static str {
......
...@@ -720,5 +720,6 @@ ...@@ -720,5 +720,6 @@
"719": "Failed to get source map for '%s'. This is a bug in Next.js", "719": "Failed to get source map for '%s'. This is a bug in Next.js",
"720": "A client prerender store should not be used for a route handler.", "720": "A client prerender store should not be used for a route handler.",
"721": "Render in Browser", "721": "Render in Browser",
"722": "Unable to match pathname to a dynamic route" "722": "Unable to match pathname to a dynamic route",
"723": "NAPI type conversion error in throwTurbopackInternalError"
} }
...@@ -204,7 +204,8 @@ export interface NapiTurboEngineOptions { ...@@ -204,7 +204,8 @@ export interface NapiTurboEngineOptions {
} }
export declare function projectNew( export declare function projectNew(
options: NapiProjectOptions, options: NapiProjectOptions,
turboEngineOptions: NapiTurboEngineOptions turboEngineOptions: NapiTurboEngineOptions,
napiCallbacks: NapiNextTurbopackCallbacksJsObject
): Promise<{ __napiType: 'Project' }> ): Promise<{ __napiType: 'Project' }>
export declare function projectUpdate( export declare function projectUpdate(
project: { __napiType: 'Project' }, project: { __napiType: 'Project' },
...@@ -350,6 +351,31 @@ export declare function projectGetSourceMapSync( ...@@ -350,6 +351,31 @@ export declare function projectGetSourceMapSync(
project: { __napiType: 'Project' }, project: { __napiType: 'Project' },
filePath: RcStr filePath: RcStr
): string | null ): string | null
/**
* A version of [`NapiNextTurbopackCallbacks`] that can accepted as an argument to a napi function.
*
* This can be converted into a [`NapiNextTurbopackCallbacks`] with
* [`NapiNextTurbopackCallbacks::from_js`].
*/
export interface NapiNextTurbopackCallbacksJsObject {
/**
* Called when we've encountered a bug in Turbopack and not in the user's code. Constructs and
* throws a `TurbopackInternalError` type. Logs to anonymized telemetry.
*
* As a result of the use of `ErrorStrategy::CalleeHandled`, the first argument is an error if
* there's a runtime conversion error. This should never happen, but if it does, the function
* can throw it instead.
*/
throwTurbopackInternalError: (
conversionError: Error | null,
opts: TurbopackInternalErrorOpts
) => never
}
/** Arguments for [`NapiNextTurbopackCallbacks::throw_turbopack_internal_error`]. */
export interface TurbopackInternalErrorOpts {
message: string
anonymizedLocation?: string
}
export declare function rootTaskDispose(rootTask: { export declare function rootTaskDispose(rootTask: {
__napiType: 'RootTask' __napiType: 'RootTask'
}): void }): void
......
...@@ -3,11 +3,11 @@ ...@@ -3,11 +3,11 @@
/* tslint:disable */ /* tslint:disable */
/* eslint-disable */ /* eslint-disable */
export function mdxCompileSync(value: string, opts: any): any
export function mdxCompile(value: string, opts: any): Promise<any>
export function minifySync(s: string, opts: any): any export function minifySync(s: string, opts: any): any
export function minify(s: string, opts: any): Promise<any> export function minify(s: string, opts: any): Promise<any>
export function transformSync(s: any, opts: any): any export function transformSync(s: any, opts: any): any
export function transform(s: any, opts: any): Promise<any> export function transform(s: any, opts: any): Promise<any>
export function parseSync(s: string, opts: any): any export function parseSync(s: string, opts: any): any
export function parse(s: string, opts: any): Promise<any> export function parse(s: string, opts: any): Promise<any>
export function mdxCompileSync(value: string, opts: any): any
export function mdxCompile(value: string, opts: any): Promise<any>
...@@ -40,7 +40,7 @@ import type { ...@@ -40,7 +40,7 @@ import type {
UpdateMessage, UpdateMessage,
WrittenEndpoint, WrittenEndpoint,
} from './types' } from './types'
import { TurbopackInternalError } from '../../shared/lib/turbopack/utils' import { throwTurbopackInternalError } from '../../shared/lib/turbopack/internal-error'
type RawBindings = typeof import('./generated-native') type RawBindings = typeof import('./generated-native')
type RawWasmBindings = typeof import('./generated-wasm') & { type RawWasmBindings = typeof import('./generated-wasm') & {
...@@ -536,14 +536,6 @@ function bindingToApi( ...@@ -536,14 +536,6 @@ function bindingToApi(
throw new Error(`Invariant: ${computeMessage(never)}`) throw new Error(`Invariant: ${computeMessage(never)}`)
} }
async function withErrorCause<T>(fn: () => Promise<T>): Promise<T> {
try {
return await fn()
} catch (nativeError: any) {
throw TurbopackInternalError.createAndRecordTelemetry(nativeError)
}
}
/** /**
* Calls a native function and streams the result. * Calls a native function and streams the result.
* If useBuffer is true, all values will be preserved, potentially buffered * If useBuffer is true, all values will be preserved, potentially buffered
...@@ -589,9 +581,7 @@ function bindingToApi( ...@@ -589,9 +581,7 @@ function bindingToApi(
} }
async function* createIterator() { async function* createIterator() {
const task = await withErrorCause<{ __napiType: 'RootTask' } | void>(() => const task = await nativeFunction(emitResult)
nativeFunction(emitResult)
)
try { try {
while (!canceled) { while (!canceled) {
if (buffer.length > 0) { if (buffer.length > 0) {
...@@ -607,9 +597,6 @@ function bindingToApi( ...@@ -607,9 +597,6 @@ function bindingToApi(
} }
} catch (e) { } catch (e) {
if (e === cancel) return if (e === cancel) return
if (e instanceof Error) {
throw TurbopackInternalError.createAndRecordTelemetry(e)
}
throw e throw e
} finally { } finally {
if (task) { if (task) {
...@@ -662,25 +649,21 @@ function bindingToApi( ...@@ -662,25 +649,21 @@ function bindingToApi(
} }
async update(options: Partial<ProjectOptions>) { async update(options: Partial<ProjectOptions>) {
await withErrorCause(async () => await binding.projectUpdate(
binding.projectUpdate( this._nativeProject,
this._nativeProject, await rustifyPartialProjectOptions(options)
await rustifyPartialProjectOptions(options)
)
) )
} }
async writeAllEntrypointsToDisk( async writeAllEntrypointsToDisk(
appDirOnly: boolean appDirOnly: boolean
): Promise<TurbopackResult<RawEntrypoints>> { ): Promise<TurbopackResult<RawEntrypoints>> {
return await withErrorCause(async () => { const napiEndpoints = (await binding.projectWriteAllEntrypointsToDisk(
const napiEndpoints = (await binding.projectWriteAllEntrypointsToDisk( this._nativeProject,
this._nativeProject, appDirOnly
appDirOnly )) as TurbopackResult<NapiEntrypoints>
)) as TurbopackResult<NapiEntrypoints>
return napiEntrypointsToRawEntrypoints(napiEndpoints) return napiEntrypointsToRawEntrypoints(napiEndpoints)
})
} }
entrypointsSubscribe() { entrypointsSubscribe() {
...@@ -777,22 +760,16 @@ function bindingToApi( ...@@ -777,22 +760,16 @@ function bindingToApi(
} }
async writeToDisk(): Promise<TurbopackResult<WrittenEndpoint>> { async writeToDisk(): Promise<TurbopackResult<WrittenEndpoint>> {
return await withErrorCause( return (await binding.endpointWriteToDisk(
() => this._nativeEndpoint
binding.endpointWriteToDisk(this._nativeEndpoint) as Promise< )) as TurbopackResult<WrittenEndpoint>
TurbopackResult<WrittenEndpoint>
>
)
} }
async clientChanged(): Promise<AsyncIterableIterator<TurbopackResult<{}>>> { async clientChanged(): Promise<AsyncIterableIterator<TurbopackResult<{}>>> {
const clientSubscription = subscribe<TurbopackResult>( const clientSubscription = subscribe<TurbopackResult>(
false, false,
async (callback) => async (callback) =>
binding.endpointClientChangedSubscribe( binding.endpointClientChangedSubscribe(this._nativeEndpoint, callback)
await this._nativeEndpoint,
callback
)
) )
await clientSubscription.next() await clientSubscription.next()
return clientSubscription return clientSubscription
...@@ -805,7 +782,7 @@ function bindingToApi( ...@@ -805,7 +782,7 @@ function bindingToApi(
false, false,
async (callback) => async (callback) =>
binding.endpointServerChangedSubscribe( binding.endpointServerChangedSubscribe(
await this._nativeEndpoint, this._nativeEndpoint,
includeIssues, includeIssues,
callback callback
) )
...@@ -1085,7 +1062,10 @@ function bindingToApi( ...@@ -1085,7 +1062,10 @@ function bindingToApi(
return new ProjectImpl( return new ProjectImpl(
await binding.projectNew( await binding.projectNew(
await rustifyProjectOptions(options), await rustifyProjectOptions(options),
turboEngineOptions || {} turboEngineOptions || {},
{
throwTurbopackInternalError,
}
) )
) )
} }
......
...@@ -80,9 +80,9 @@ import { store as consoleStore } from '../../../build/output/store' ...@@ -80,9 +80,9 @@ import { store as consoleStore } from '../../../build/output/store'
import { import {
isPersistentCachingEnabled, isPersistentCachingEnabled,
ModuleBuildError, ModuleBuildError,
TurbopackInternalError,
} from '../../../shared/lib/turbopack/utils' } from '../../../shared/lib/turbopack/utils'
import { getDefineEnv } from '../../../build/define-env' import { getDefineEnv } from '../../../build/define-env'
import { TurbopackInternalError } from '../../../shared/lib/turbopack/internal-error'
export type SetupOpts = { export type SetupOpts = {
renderServer: LazyRenderServerInstance renderServer: LazyRenderServerInstance
......
import type { TurbopackInternalErrorOpts } from '../../../build/swc/generated-native'
import { eventErrorThrown } from '../../../telemetry/events'
import { traceGlobals } from '../../../trace/shared'
/**
* An error caused by a bug in Turbopack, and not the user's code (e.g. a Rust panic). These should
* be written to a log file and details should not be shown to the user.
*
* These are constructed in Turbopack by calling `throwTurbopackInternalError`.
*/
export class TurbopackInternalError extends Error {
name = 'TurbopackInternalError'
location: string | undefined
// Manually set this as this isn't statically determinable
__NEXT_ERROR_CODE = 'TurbopackInternalError'
constructor({ message, anonymizedLocation }: TurbopackInternalErrorOpts) {
super(message)
this.location = anonymizedLocation
}
}
/**
* A helper used by the napi Rust entrypoints to construct and throw a `TurbopackInternalError`.
*
* When called, this will emit a telemetry event.
*/
export function throwTurbopackInternalError(
conversionError: Error | null,
opts: TurbopackInternalErrorOpts
): never {
if (conversionError != null) {
// Somehow napi failed to convert `opts` to a JS object??? Just give up and throw that instead.
throw new Error(
'NAPI type conversion error in throwTurbopackInternalError',
{
cause: conversionError,
}
)
}
const err = new TurbopackInternalError(opts)
const telemetry = traceGlobals.get('telemetry')
if (telemetry) {
telemetry.record(eventErrorThrown(err, opts.anonymizedLocation))
} else {
console.error('Expected `telemetry` to be set in globals')
}
throw err
}
...@@ -15,47 +15,20 @@ import type { EntryKey } from './entry-key' ...@@ -15,47 +15,20 @@ import type { EntryKey } from './entry-key'
import * as Log from '../../../build/output/log' import * as Log from '../../../build/output/log'
import type { NextConfigComplete } from '../../../server/config-shared' import type { NextConfigComplete } from '../../../server/config-shared'
import loadJsConfig from '../../../build/load-jsconfig' import loadJsConfig from '../../../build/load-jsconfig'
import { eventErrorThrown } from '../../../telemetry/events'
import { traceGlobals } from '../../../trace/shared'
type IssueKey = `${Issue['severity']}-${Issue['filePath']}-${string}-${string}` type IssueKey = `${Issue['severity']}-${Issue['filePath']}-${string}-${string}`
export type IssuesMap = Map<IssueKey, Issue> export type IssuesMap = Map<IssueKey, Issue>
export type EntryIssuesMap = Map<EntryKey, IssuesMap> export type EntryIssuesMap = Map<EntryKey, IssuesMap>
export type TopLevelIssuesMap = IssuesMap export type TopLevelIssuesMap = IssuesMap
// An error generated from emitted Turbopack issues. This can include build /**
// errors caused by issues with user code. * An error generated from emitted Turbopack issues. This can include build
* errors caused by issues with user code.
*/
export class ModuleBuildError extends Error { export class ModuleBuildError extends Error {
name = 'ModuleBuildError' name = 'ModuleBuildError'
} }
// An error caused by an internal issue in Turbopack. These should be written
// to a log file and details should not be shown to the user.
export class TurbopackInternalError extends Error {
name = 'TurbopackInternalError'
// Manually set this as this isn't statically determinable
__NEXT_ERROR_CODE = 'TurbopackInternalError'
static createAndRecordTelemetry(cause: Error) {
const error = new TurbopackInternalError(cause)
const telemetry = traceGlobals.get('telemetry')
if (telemetry) {
telemetry.record(eventErrorThrown(error))
} else {
console.error('Expected `telemetry` to be set in globals')
}
return error
}
constructor(cause: Error) {
super(cause.message)
this.stack = cause.stack
}
}
/** /**
* Thin stopgap workaround layer to mimic existing wellknown-errors-plugin in webpack's build * Thin stopgap workaround layer to mimic existing wellknown-errors-plugin in webpack's build
* to emit certain type of errors into cli. * to emit certain type of errors into cli.
......
...@@ -235,15 +235,24 @@ type ErrorThrownEvent = { ...@@ -235,15 +235,24 @@ type ErrorThrownEvent = {
eventName: typeof ERROR_THROWN_EVENT eventName: typeof ERROR_THROWN_EVENT
payload: { payload: {
errorCode: string | undefined errorCode: string | undefined
location: string | undefined
} }
} }
// Creates a Telemetry event for errors. For privacy, only includes the error code. // Creates a Telemetry event for errors. For privacy, only includes the error code and not the error
export function eventErrorThrown(error: Error): ErrorThrownEvent { // message.
//
// `location` may be included if it's a location internal to the next.js source tree (i.e. a
// non-absolute path).
export function eventErrorThrown(
error: Error,
anonymizedLocation: string | undefined
): ErrorThrownEvent {
return { return {
eventName: ERROR_THROWN_EVENT, eventName: ERROR_THROWN_EVENT,
payload: { payload: {
errorCode: extractNextErrorCode(error) || 'Unknown', errorCode: extractNextErrorCode(error) || 'Unknown',
location: anonymizedLocation,
}, },
} }
} }
import path from 'path' import path from 'path'
import os from 'os' import os from 'os'
import { TurbopackInternalError } from 'next/dist/shared/lib/turbopack/utils' import {
throwTurbopackInternalError,
TurbopackInternalError,
} from 'next/dist/shared/lib/turbopack/internal-error'
import { Telemetry } from 'next/dist/telemetry/storage' import { Telemetry } from 'next/dist/telemetry/storage'
import { setGlobal } from 'next/dist/trace' import { setGlobal } from 'next/dist/trace'
import { traceGlobals } from 'next/dist/trace/shared' import { traceGlobals } from 'next/dist/trace/shared'
describe('TurbopackInternalError', () => { describe('TurbopackInternalError', () => {
it('sends a telemetry event when TurbopackInternalError.createAndRecordTelemetry() is called', async () => { it('sends a telemetry event when throwTurbopackInternalError() is called', async () => {
const oldTelemetry = traceGlobals.get('telemetry') const oldTelemetry = traceGlobals.get('telemetry')
try { try {
...@@ -18,12 +21,24 @@ describe('TurbopackInternalError', () => { ...@@ -18,12 +21,24 @@ describe('TurbopackInternalError', () => {
.spyOn(telemetry, 'submitRecord') .spyOn(telemetry, 'submitRecord')
// @ts-ignore // @ts-ignore
.mockImplementation(() => Promise.resolve()) .mockImplementation(() => Promise.resolve())
TurbopackInternalError.createAndRecordTelemetry(new Error('test error'))
let internalError = null
try {
throwTurbopackInternalError(null, {
message: 'test error',
anonymizedLocation: 'file.rs:120:1',
})
} catch (err) {
internalError = err
}
expect(internalError).toBeInstanceOf(TurbopackInternalError)
expect(submitRecord).toHaveBeenCalledWith({ expect(submitRecord).toHaveBeenCalledWith({
eventName: 'NEXT_ERROR_THROWN', eventName: 'NEXT_ERROR_THROWN',
payload: { payload: {
errorCode: 'TurbopackInternalError', errorCode: 'TurbopackInternalError',
location: 'file.rs:120:1',
}, },
}) })
} finally { } finally {
......
...@@ -447,7 +447,7 @@ pub enum TurboTasksExecutionError { ...@@ -447,7 +447,7 @@ pub enum TurboTasksExecutionError {
} }
impl TurboTasksExecutionError { impl TurboTasksExecutionError {
pub fn task_context(&self, task: impl Display) -> Self { pub fn with_task_context(&self, task: impl Display) -> Self {
TurboTasksExecutionError::TaskContext(Arc::new(TurboTaskContextError { TurboTasksExecutionError::TaskContext(Arc::new(TurboTaskContextError {
task: RcStr::from(task.to_string()), task: RcStr::from(task.to_string()),
source: Some(self.clone()), source: Some(self.clone()),
......
...@@ -831,7 +831,7 @@ impl<B: Backend + 'static> TurboTasks<B> { ...@@ -831,7 +831,7 @@ impl<B: Backend + 'static> TurboTasks<B> {
let local_task = LocalTask::Done { let local_task = LocalTask::Done {
output: match result { output: match result {
Ok(raw_vc) => OutputContent::Link(raw_vc), Ok(raw_vc) => OutputContent::Link(raw_vc),
Err(err) => OutputContent::Error(err.task_context(task_type)), Err(err) => OutputContent::Error(err.with_task_context(task_type)),
}, },
}; };
......
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