Skip to content

Commit f615cdb

Browse files
authored
fix(es/plugin): use #[cfg] to avoid compilation error (#11316)
**Description:** #11267 introduced this mistake. - `SHARED_RUNTIME.block_on(fut)` is enabled with`#[cfg(feature = "plugin")]` - `SHARED_RUNTIME` is enabled with `#[cfg(all(feature = "plugin", not(feature = "manual-tokio-runtime")))]` - If both features are enabled, the compiler goes failed. The problem is that cfg! doesn't remove the code actually. `#[cfg]` is needed here
1 parent f98ca27 commit f615cdb

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

.changeset/large-waves-clean.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
swc_core: patch
3+
swc: patch
4+
---
5+
6+
fix(es/plugin): use `#[cfg]` to avoid compilation error

crates/swc/src/plugin.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,23 +83,30 @@ impl RustPlugins {
8383
#[cfg(feature = "plugin")]
8484
fn apply(&mut self, n: Program) -> Result<Program, anyhow::Error> {
8585
use anyhow::Context;
86+
8687
if self.plugins.is_none() || self.plugins.as_ref().unwrap().is_empty() {
8788
return Ok(n);
8889
}
8990

9091
let filename = self.metadata_context.filename.clone();
9192

92-
if cfg!(feature = "manual-tokio-runtime") {
93-
self.apply_inner(n)
94-
} else {
93+
#[cfg(feature = "manual-tokio-runtime")]
94+
let ret = self
95+
.apply_inner(n)
96+
.with_context(|| format!("failed to invoke plugin on '{filename:?}'"));
97+
98+
#[cfg(not(feature = "manual-tokio-runtime"))]
99+
let ret = {
95100
let fut = async move { self.apply_inner(n) };
96101
if let Ok(handle) = tokio::runtime::Handle::try_current() {
97102
handle.block_on(fut)
98103
} else {
99104
SHARED_RUNTIME.block_on(fut)
100105
}
101-
}
102-
.with_context(|| format!("failed to invoke plugin on '{filename:?}'"))
106+
.with_context(|| format!("failed to invoke plugin on '{filename:?}'"))
107+
};
108+
109+
ret
103110
}
104111

105112
#[tracing::instrument(level = "info", skip_all, name = "apply_plugins")]

0 commit comments

Comments
 (0)