@@ -24,13 +24,15 @@ use std::{
2424 ffi:: OsStr ,
2525 process:: { exit, Command } ,
2626 sync:: {
27+ atomic:: { AtomicBool , Ordering } ,
2728 mpsc:: { channel, Receiver } ,
2829 Arc , Mutex ,
2930 } ,
3031 time:: Duration ,
3132} ;
3233
3334static BEFORE_DEV : OnceCell < Mutex < Arc < SharedChild > > > = OnceCell :: new ( ) ;
35+ static KILL_BEFORE_DEV_FLAG : OnceCell < AtomicBool > = OnceCell :: new ( ) ;
3436
3537#[ derive( Debug , Parser ) ]
3638#[ clap( about = "Tauri dev" , trailing_var_arg( true ) ) ]
@@ -116,13 +118,19 @@ pub fn command(options: Options) -> Result<()> {
116118 let status = child_
117119 . wait ( )
118120 . expect ( "failed to wait on \" beforeDevCommand\" " ) ;
119- if !status. success ( ) {
121+ if !( status. success ( ) || KILL_BEFORE_DEV_FLAG . get ( ) . unwrap ( ) . load ( Ordering :: Relaxed ) ) {
120122 logger_. error ( "The \" beforeDevCommand\" terminated with a non-zero status code." ) ;
121123 exit ( status. code ( ) . unwrap_or ( 1 ) ) ;
122124 }
123125 } ) ;
124126
125127 BEFORE_DEV . set ( Mutex :: new ( child) ) . unwrap ( ) ;
128+ KILL_BEFORE_DEV_FLAG . set ( AtomicBool :: default ( ) ) . unwrap ( ) ;
129+
130+ let _ = ctrlc:: set_handler ( move || {
131+ kill_before_dev_process ( ) ;
132+ exit ( 130 ) ;
133+ } ) ;
126134 }
127135 }
128136
@@ -280,11 +288,15 @@ pub fn command(options: Options) -> Result<()> {
280288fn kill_before_dev_process ( ) {
281289 if let Some ( child) = BEFORE_DEV . get ( ) {
282290 let child = child. lock ( ) . unwrap ( ) ;
291+ KILL_BEFORE_DEV_FLAG
292+ . get ( )
293+ . unwrap ( )
294+ . store ( true , Ordering :: Relaxed ) ;
283295 #[ cfg( windows) ]
284296 let _ = Command :: new ( "powershell" )
285297 . arg ( "-NoProfile" )
286298 . arg ( "-Command" )
287- . arg ( format ! ( "function Kill-Tree {{ Param([int]$ppid); Get-CimInstance Win32_Process | Where-Object {{ $_.ParentProcessId -eq $ppid }} | ForEach-Object {{ Kill-Tree $_.ProcessId }}; Stop-Process -Id $ppid }}; Kill-Tree {}" , child. id( ) ) )
299+ . arg ( format ! ( "function Kill-Tree {{ Param([int]$ppid); Get-CimInstance Win32_Process | Where-Object {{ $_.ParentProcessId -eq $ppid }} | ForEach-Object {{ Kill-Tree $_.ProcessId }}; Stop-Process -Id $ppid -ErrorAction SilentlyContinue }}; Kill-Tree {}" , child. id( ) ) )
288300 . status ( ) ;
289301 #[ cfg( not( windows) ) ]
290302 let _ = Command :: new ( "pkill" )
0 commit comments