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

fix(plugin): Set swc_common::errors::HANDLER while invoking plugins #8511

Merged
merged 3 commits into from
Jan 17, 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
7 changes: 1 addition & 6 deletions crates/swc_core/src/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,7 @@ pub mod memory {
)))
)]
pub mod errors {
/// global context HANDLER in plugin's transform function.
pub static HANDLER: swc_plugin::pseudo_scoped_key::PseudoScopedKey<
swc_common::errors::Handler,
> = swc_plugin::pseudo_scoped_key::PseudoScopedKey {
inner: once_cell::sync::OnceCell::new(),
};
pub use swc_common::errors::HANDLER;
}

/// Plugin's environment metadata context.
Expand Down
12 changes: 3 additions & 9 deletions crates/swc_plugin_macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,14 +143,6 @@ fn handle_func(func: ItemFn, ast_type: Ident) -> TokenStream {
false,
Box::new(PluginDiagnosticsEmitter)
);
let handler_set_result = swc_core::plugin::errors::HANDLER.inner.set(handler);

if handler_set_result.is_err() {
let err = swc_core::common::plugin::serialized::PluginError::Serialize(
"Failed to set handler for plugin".to_string()
);
return construct_error_ptr(err);
}

// Construct metadata to the `Program` for the transform plugin.
let plugin_comments_proxy = if should_enable_comments_proxy == 1 { Some(swc_core::plugin::proxies::PluginCommentsProxy) } else { None };
Expand All @@ -161,7 +153,9 @@ fn handle_func(func: ItemFn, ast_type: Ident) -> TokenStream {
};

// Take original plugin fn ident, then call it with interop'ed args
let transformed_program = swc_core::common::plugin::serialized::VersionedSerializable::new(#ident(program, metadata));
let transformed_program = swc_core::common::plugin::serialized::VersionedSerializable::new(swc_core::common::errors::HANDLER.set(&handler, || {
#ident(program, metadata)
}));

// Serialize transformed result, return back to the host.
let serialized_result = swc_core::common::plugin::serialized::PluginSerializedBytes::try_serialize(
Expand Down
Loading