Skip to content

Commit

Permalink
refactor(next/swc): disable default features for crash report (#38531)
Browse files Browse the repository at this point in the history
As we have disabled crash reporter for now, this PR removes feature from `default` to not to include it in `next-swc` as well. Feature itself remains so we can adjust it later by enabling it depends on the progress.

## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have helpful link attached, see `contributing.md`

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes by running `pnpm lint`
- [ ] The examples guidelines are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing.md#adding-examples)


Co-authored-by: JJ Kasper <22380829+ijjk@users.noreply.github.com>
  • Loading branch information
kwonoj and ijjk committed Jul 11, 2022
1 parent 8ee05b7 commit 6804711
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 19 deletions.
2 changes: 1 addition & 1 deletion packages/next-swc/crates/napi/Cargo.toml
Expand Up @@ -8,7 +8,7 @@ publish = false
crate-type = ["cdylib", "rlib"]

[features]
default = ["sentry_rustls"]
default = []
# Instead of enabling all the plugin-related features by default, make it explicitly specified
# when build (i.e napi --build --features plugin), same for the wasm as well.
# this is due to some of transitive dependencies have features cannot be enabled at the same time
Expand Down
61 changes: 43 additions & 18 deletions packages/next-swc/crates/napi/src/util.rs
Expand Up @@ -34,6 +34,7 @@ use tracing_chrome::{ChromeLayerBuilder, FlushGuard};
use tracing_subscriber::{filter, prelude::*, util::SubscriberInitExt, Layer};

static TARGET_TRIPLE: &str = include_str!(concat!(env!("OUT_DIR"), "/triple.txt"));
#[allow(unused)]
static PACKAGE_VERSION: &str = include_str!(concat!(env!("OUT_DIR"), "/package.txt"));

#[contextless_function]
Expand Down Expand Up @@ -147,15 +148,31 @@ pub fn teardown_trace_subscriber(cx: CallContext) -> napi::Result<JsUndefined> {
cx.env.get_undefined()
}

#[cfg(any(
target_arch = "wasm32",
all(target_os = "windows", target_arch = "aarch64"),
not(all(feature = "sentry_native_tls", feature = "sentry_rustls"))
))]
#[js_function(1)]
pub fn init_crash_reporter(cx: CallContext) -> napi::Result<JsExternal> {
let guard: Option<usize> = None;
let guard_cell = RefCell::new(guard);
cx.env.create_external(guard_cell, None)
}

/// Initialize crash reporter to collect unexpected native next-swc crashes.
#[cfg(all(
not(target_arch = "wasm32"),
not(all(target_os = "windows", target_arch = "aarch64")),
any(feature = "sentry_native_tls", feature = "sentry_rustls")
))]
#[js_function(1)]
pub fn init_crash_reporter(cx: CallContext) -> napi::Result<JsExternal> {
// Attempts to follow https://nextjs.org/telemetry's debug behavior.
// However, this is techinically not identical to the behavior of the telemetry
// itself as sentry's debug option does not provides full payuload output.
let debug = env::var("NEXT_TELEMETRY_DEBUG").map_or_else(|_| false, |v| v == "1");

#[cfg(not(all(target_os = "windows", target_arch = "aarch64")))]
let guard = {
#[cfg(feature = "sentry_native_tls")]
use _sentry_native_tls::{init, types::Dsn, ClientOptions};
Expand Down Expand Up @@ -183,34 +200,42 @@ pub fn init_crash_reporter(cx: CallContext) -> napi::Result<JsExternal> {
}))
};

// aarch64_msvc neither compiles native-tls nor rustls for sentry transport
#[cfg(all(target_os = "windows", target_arch = "aarch64"))]
let guard: Option<usize> = None;

let guard_cell = RefCell::new(guard);
cx.env.create_external(guard_cell, None)
}

#[cfg(any(
target_arch = "wasm32",
all(target_os = "windows", target_arch = "aarch64"),
not(all(feature = "sentry_native_tls", feature = "sentry_rustls"))
))]
#[js_function(1)]
pub fn teardown_crash_reporter(cx: CallContext) -> napi::Result<JsUndefined> {
cx.env.get_undefined()
}

/// Trying to drop crash reporter guard if exists. This is the way to hold
/// guards to not to be dropped immediately after crash reporter is initialized
/// in napi context.
#[cfg(all(
not(target_arch = "wasm32"),
not(all(target_os = "windows", target_arch = "aarch64")),
any(feature = "sentry_native_tls", feature = "sentry_rustls")
))]
#[js_function(1)]
pub fn teardown_crash_reporter(cx: CallContext) -> napi::Result<JsUndefined> {
#[cfg(not(all(target_os = "windows", target_arch = "aarch64")))]
{
#[cfg(feature = "sentry_native_tls")]
use _sentry_native_tls::ClientInitGuard;
#[cfg(feature = "sentry_rustls")]
use _sentry_rustls::ClientInitGuard;
#[cfg(feature = "sentry_native_tls")]
use _sentry_native_tls::ClientInitGuard;
#[cfg(feature = "sentry_rustls")]
use _sentry_rustls::ClientInitGuard;

let guard_external = cx.get::<JsExternal>(0)?;
let guard_cell = &*cx
.env
.get_value_external::<RefCell<Option<ClientInitGuard>>>(&guard_external)?;
let guard_external = cx.get::<JsExternal>(0)?;
let guard_cell = &*cx
.env
.get_value_external::<RefCell<Option<ClientInitGuard>>>(&guard_external)?;

if let Some(guard) = guard_cell.take() {
drop(guard);
}
if let Some(guard) = guard_cell.take() {
drop(guard);
}

cx.env.get_undefined()
Expand Down

0 comments on commit 6804711

Please sign in to comment.