Skip to content

Commit 7d974a8

Browse files
Fix Compile.rs: Use spawn_blocking for CPU-bound compilation
1 parent a47c3bb commit 7d974a8

1 file changed

Lines changed: 24 additions & 10 deletions

File tree

Source/Fn/SWC/Watch/Compile.rs

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,32 @@ pub async fn Fn(Option: super::Option) -> anyhow::Result<()> {
2121
Queue.push(tokio::spawn(async move {
2222
match tokio::fs::read_to_string(&file).await {
2323
Ok(input) => {
24-
match Compiler.compile_file(&file, input).await {
25-
Ok(output) => {
26-
if let Err(e) = Allow.send((file.clone(), Ok(output))) {
27-
error!("Cannot send compilation result: {}", e);
28-
}
24+
// Use spawn_blocking for CPU-intensive compilation
25+
let file_clone = file.clone();
26+
let result = tokio::task::spawn_blocking(move || {
27+
Compiler.compile_file(&file_clone, input)
28+
}).await;
29+
30+
match result {
31+
Ok(inner_result) => match inner_result {
32+
Ok(output) => {
33+
if let Err(e) = Allow.send((file.clone(), Ok(output))) {
34+
error!("Cannot send compilation result: {}", e);
35+
}
36+
},
37+
Err(e) => {
38+
error!("Compilation error for {}: {}", file, e);
39+
if let Err(e) = Allow.send((file.clone(), Err(e))) {
40+
error!("Cannot send compilation error: {}", e);
41+
}
42+
},
2943
},
30-
Err(e) => {
31-
error!("Compilation error for {}: {}", file, e);
32-
if let Err(e) = Allow.send((file.clone(), Err(e))) {
33-
error!("Cannot send compilation error: {}", e);
44+
Err(join_err) => {
45+
error!("Task join error for {}: {}", file, join_err);
46+
if let Err(e) = Allow.send((file.clone(), Err(anyhow::anyhow!(join_err)))) {
47+
error!("Cannot send join error: {}", e);
3448
}
35-
},
49+
}
3650
}
3751
},
3852
Err(e) => {

0 commit comments

Comments
 (0)