|
| 1 | +//! Windows process hygiene for the shell (issue #141). |
| 2 | +//! |
| 3 | +//! Two things that are invisible on macOS and broke the Windows app: |
| 4 | +//! |
| 5 | +//! 1. The release shell is `windows_subsystem = "windows"` (main.rs) — it has |
| 6 | +//! no console. Every console program it starts (`curl.exe`) therefore gets a |
| 7 | +//! NEW, visible console window, which flashes open and shut. The activity |
| 8 | +//! poller ran one every 6 s. [`hidden_command`] sets `CREATE_NO_WINDOW`. |
| 9 | +//! (The sidecar itself is fine: tauri-plugin-shell sets the flag for it.) |
| 10 | +//! |
| 11 | +//! 2. Tauri derives `resource_dir()` from a `std::fs::canonicalize`d exe path, |
| 12 | +//! which on Windows is a verbatim path (`\\?\C:\…`). Exported to the sidecar |
| 13 | +//! as `MAUDE_DEV_SERVER_ROOT`, it reached the ACP adapter's entry path, and |
| 14 | +//! `node \\?\C:\…\index.js` dies with `EISDIR: lstat 'C:'` (nodejs/node#62446) |
| 15 | +//! — the Assistant never started for anyone with Node installed. |
| 16 | +//! [`strip_verbatim`] turns it back into an ordinary path. |
| 17 | +
|
| 18 | +use std::ffi::OsStr; |
| 19 | +use std::path::{Path, PathBuf}; |
| 20 | +use std::process::Command; |
| 21 | + |
| 22 | +/// A `std::process::Command` that never opens a console window on Windows. |
| 23 | +/// Use this for every child the shell spawns directly; the guard test below |
| 24 | +/// fails on any other direct spawn in `src/`. |
| 25 | +pub(crate) fn hidden_command<S: AsRef<OsStr>>(program: S) -> Command { |
| 26 | + // no-console-window: this IS the helper. |
| 27 | + #[allow(unused_mut)] |
| 28 | + let mut cmd = Command::new(program); |
| 29 | + #[cfg(windows)] |
| 30 | + { |
| 31 | + use std::os::windows::process::CommandExt; |
| 32 | + const CREATE_NO_WINDOW: u32 = 0x0800_0000; |
| 33 | + cmd.creation_flags(CREATE_NO_WINDOW); |
| 34 | + } |
| 35 | + cmd |
| 36 | +} |
| 37 | + |
| 38 | +/// `\\?\C:\x` → `C:\x`, `\\?\UNC\srv\share\x` → `\\srv\share\x`; anything else |
| 39 | +/// unchanged. Only the two forms that have an ordinary equivalent are rewritten |
| 40 | +/// (`\\?\Volume{…}` has none and is left alone). |
| 41 | +pub(crate) fn strip_verbatim(path: &Path) -> PathBuf { |
| 42 | + match strip_verbatim_str(&path.to_string_lossy()) { |
| 43 | + Some(s) => PathBuf::from(s), |
| 44 | + None => path.to_path_buf(), |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +fn strip_verbatim_str(s: &str) -> Option<String> { |
| 49 | + if let Some(rest) = s.strip_prefix(r"\\?\UNC\") { |
| 50 | + return Some(format!(r"\\{rest}")); |
| 51 | + } |
| 52 | + let rest = s.strip_prefix(r"\\?\")?; |
| 53 | + let b = rest.as_bytes(); |
| 54 | + let is_drive = b.len() >= 2 && b[0].is_ascii_alphabetic() && b[1] == b':'; |
| 55 | + is_drive.then(|| rest.to_string()) |
| 56 | +} |
| 57 | + |
| 58 | +#[cfg(test)] |
| 59 | +mod tests { |
| 60 | + use super::*; |
| 61 | + |
| 62 | + #[test] |
| 63 | + fn strips_drive_verbatim_prefix() { |
| 64 | + assert_eq!( |
| 65 | + strip_verbatim_str(r"\\?\C:\Program Files\Maude\resources\apps\studio").as_deref(), |
| 66 | + Some(r"C:\Program Files\Maude\resources\apps\studio") |
| 67 | + ); |
| 68 | + } |
| 69 | + |
| 70 | + #[test] |
| 71 | + fn strips_unc_verbatim_prefix() { |
| 72 | + assert_eq!( |
| 73 | + strip_verbatim_str(r"\\?\UNC\srv\share\Maude").as_deref(), |
| 74 | + Some(r"\\srv\share\Maude") |
| 75 | + ); |
| 76 | + } |
| 77 | + |
| 78 | + #[test] |
| 79 | + fn leaves_ordinary_and_volume_paths_alone() { |
| 80 | + assert_eq!(strip_verbatim_str(r"C:\Program Files\Maude"), None); |
| 81 | + assert_eq!( |
| 82 | + strip_verbatim_str("/Applications/Maude.app/Contents/Resources"), |
| 83 | + None |
| 84 | + ); |
| 85 | + assert_eq!(strip_verbatim_str(r"\\?\Volume{1234}\Maude"), None); |
| 86 | + assert_eq!(strip_verbatim_str(r"\\srv\share"), None); |
| 87 | + let p = Path::new("/Applications/Maude.app"); |
| 88 | + assert_eq!(strip_verbatim(p), p.to_path_buf()); |
| 89 | + } |
| 90 | + |
| 91 | + /// The flash in #141 could not turn a Mac test red, so pin the rule |
| 92 | + /// structurally: every direct spawn in the shell goes through |
| 93 | + /// `hidden_command`, or says on the line (or the three above) why it may not |
| 94 | + /// — `no-console-window: <reason>` (unix-only, test-only). |
| 95 | + #[test] |
| 96 | + fn every_spawn_is_hidden_or_justified() { |
| 97 | + let src = Path::new(env!("CARGO_MANIFEST_DIR")).join("src"); |
| 98 | + let mut offenders = Vec::new(); |
| 99 | + for entry in std::fs::read_dir(&src).expect("read src/") { |
| 100 | + let path = entry.expect("dir entry").path(); |
| 101 | + if path.extension().and_then(|e| e.to_str()) != Some("rs") { |
| 102 | + continue; |
| 103 | + } |
| 104 | + let text = std::fs::read_to_string(&path).expect("read source"); |
| 105 | + let lines: Vec<&str> = text.lines().collect(); |
| 106 | + for (i, line) in lines.iter().enumerate() { |
| 107 | + if !line.contains(concat!("Command", "::new(")) { |
| 108 | + continue; |
| 109 | + } |
| 110 | + let from = i.saturating_sub(3); |
| 111 | + if !lines[from..=i] |
| 112 | + .iter() |
| 113 | + .any(|l| l.contains("no-console-window:")) |
| 114 | + { |
| 115 | + offenders.push(format!("{}:{}", path.display(), i + 1)); |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + assert!( |
| 120 | + offenders.is_empty(), |
| 121 | + "use win_process::hidden_command (or justify with `no-console-window:`): {offenders:?}" |
| 122 | + ); |
| 123 | + } |
| 124 | +} |
0 commit comments