Skip to content

Commit e251e1b

Browse files
authored
fix(cli): kill before dev command recursively on Unix, closes #2794 (#3848)
1 parent 42a32ee commit e251e1b

File tree

6 files changed

+40
-8
lines changed

6 files changed

+40
-8
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"cli.rs": patch
3+
"cli.js": patch
4+
---
5+
6+
Kill the `beforeDevCommand` process recursively on Unix.

tooling/cli/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ include = [
1616
"src/",
1717
"/templates",
1818
"MergeModules/",
19+
"scripts/",
1920
"*.json",
2021
"*.rs",
21-
"vswhere.exe",
2222
"tauri.gitignore"
2323
]
2424

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
function getcpid() {
2+
cpids=`pgrep -P $1|xargs`
3+
for cpid in $cpids;
4+
do
5+
echo "$cpid"
6+
getcpid $cpid
7+
done
8+
}
9+
10+
kill $(getcpid $1)

tooling/cli/src/dev.rs

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ use std::{
3434
static BEFORE_DEV: OnceCell<Mutex<Arc<SharedChild>>> = OnceCell::new();
3535
static KILL_BEFORE_DEV_FLAG: OnceCell<AtomicBool> = OnceCell::new();
3636

37+
#[cfg(unix)]
38+
const KILL_CHILDREN_SCRIPT: &[u8] = include_bytes!("../scripts/kill-children.sh");
39+
3740
#[derive(Debug, Parser)]
3841
#[clap(about = "Tauri dev", trailing_var_arg(true))]
3942
pub struct Options {
@@ -298,11 +301,24 @@ fn kill_before_dev_process() {
298301
.arg("-Command")
299302
.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()))
300303
.status();
301-
#[cfg(not(windows))]
302-
let _ = Command::new("pkill")
303-
.args(&["-TERM", "-P"])
304-
.arg(child.id().to_string())
305-
.status();
304+
#[cfg(unix)]
305+
{
306+
let mut kill_children_script_path = std::env::temp_dir();
307+
kill_children_script_path.push("kill-children.sh");
308+
309+
if !kill_children_script_path.exists() {
310+
if let Ok(mut file) = std::fs::File::create(&kill_children_script_path) {
311+
use std::{io::Write, os::unix::fs::PermissionsExt};
312+
let _ = file.write_all(KILL_CHILDREN_SCRIPT);
313+
let mut permissions = file.metadata().unwrap().permissions();
314+
permissions.set_mode(0o770);
315+
let _ = file.set_permissions(permissions);
316+
}
317+
}
318+
let _ = Command::new(&kill_children_script_path)
319+
.arg(child.id().to_string())
320+
.output();
321+
}
306322
let _ = child.kill();
307323
}
308324
}

tooling/cli/src/info.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -252,15 +252,15 @@ struct VsInstanceInfo {
252252
}
253253

254254
#[cfg(windows)]
255-
const VSWHERE: &[u8] = include_bytes!("../vswhere.exe");
255+
const VSWHERE: &[u8] = include_bytes!("../scripts/vswhere.exe");
256256

257257
#[cfg(windows)]
258258
fn build_tools_version() -> crate::Result<Option<Vec<String>>> {
259259
let mut vswhere = std::env::temp_dir();
260260
vswhere.push("vswhere.exe");
261261

262262
if !vswhere.exists() {
263-
if let Ok(mut file) = std::fs::File::create(vswhere.clone()) {
263+
if let Ok(mut file) = std::fs::File::create(&vswhere) {
264264
use std::io::Write;
265265
let _ = file.write_all(VSWHERE);
266266
}

0 commit comments

Comments
 (0)