dev.rs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634
  1. // Copyright 2019-2021 Tauri Programme within The Commons Conservancy
  2. // SPDX-License-Identifier: Apache-2.0
  3. // SPDX-License-Identifier: MIT
  4. use crate::{
  5. helpers::{
  6. app_paths::{app_dir, tauri_dir},
  7. command_env,
  8. config::{get as get_config, reload as reload_config, AppUrl, ConfigHandle, WindowUrl},
  9. manifest::{rewrite_manifest, Manifest},
  10. },
  11. CommandExt, Result,
  12. };
  13. use clap::Parser;
  14. use anyhow::Context;
  15. use log::{error, info, warn};
  16. use notify::{watcher, DebouncedEvent, RecursiveMode, Watcher};
  17. use once_cell::sync::OnceCell;
  18. use shared_child::SharedChild;
  19. use std::{
  20. env::set_current_dir,
  21. ffi::OsStr,
  22. fs::FileType,
  23. io::{BufReader, Write},
  24. path::{Path, PathBuf},
  25. process::{exit, Command, Stdio},
  26. sync::{
  27. atomic::{AtomicBool, Ordering},
  28. mpsc::channel,
  29. Arc, Mutex,
  30. },
  31. time::Duration,
  32. };
  33. static BEFORE_DEV: OnceCell<Mutex<Arc<SharedChild>>> = OnceCell::new();
  34. static KILL_BEFORE_DEV_FLAG: OnceCell<AtomicBool> = OnceCell::new();
  35. #[cfg(unix)]
  36. const KILL_CHILDREN_SCRIPT: &[u8] = include_bytes!("../scripts/kill-children.sh");
  37. const TAURI_DEV_WATCHER_GITIGNORE: &[u8] = include_bytes!("../tauri-dev-watcher.gitignore");
  38. #[derive(Debug, Parser)]
  39. #[clap(about = "Tauri dev", trailing_var_arg(true))]
  40. pub struct Options {
  41. /// Binary to use to run the application
  42. #[clap(short, long)]
  43. runner: Option<String>,
  44. /// Target triple to build against
  45. #[clap(short, long)]
  46. target: Option<String>,
  47. /// List of cargo features to activate
  48. #[clap(short, long)]
  49. features: Option<Vec<String>>,
  50. /// Exit on panic
  51. #[clap(short, long)]
  52. exit_on_panic: bool,
  53. /// JSON string or path to JSON file to merge with tauri.conf.json
  54. #[clap(short, long)]
  55. config: Option<String>,
  56. /// Run the code in release mode
  57. #[clap(long = "release")]
  58. release_mode: bool,
  59. /// Command line arguments passed to the runner
  60. args: Vec<String>,
  61. }
  62. pub fn command(options: Options) -> Result<()> {
  63. let r = command_internal(options);
  64. if r.is_err() {
  65. kill_before_dev_process();
  66. #[cfg(not(debug_assertions))]
  67. let _ = check_for_updates();
  68. }
  69. r
  70. }
  71. fn command_internal(options: Options) -> Result<()> {
  72. let tauri_path = tauri_dir();
  73. let merge_config = if let Some(config) = &options.config {
  74. Some(if config.starts_with('{') {
  75. config.to_string()
  76. } else {
  77. std::fs::read_to_string(&config).with_context(|| "failed to read custom configuration")?
  78. })
  79. } else {
  80. None
  81. };
  82. set_current_dir(&tauri_path).with_context(|| "failed to change current working directory")?;
  83. let config = get_config(merge_config.as_deref())?;
  84. if let Some(before_dev) = &config
  85. .lock()
  86. .unwrap()
  87. .as_ref()
  88. .unwrap()
  89. .build
  90. .before_dev_command
  91. {
  92. if !before_dev.is_empty() {
  93. info!(action = "Running"; "BeforeDevCommand (`{}`)", before_dev);
  94. #[cfg(target_os = "windows")]
  95. let mut command = {
  96. let mut command = Command::new("cmd");
  97. command
  98. .arg("/S")
  99. .arg("/C")
  100. .arg(before_dev)
  101. .current_dir(app_dir())
  102. .envs(command_env(true))
  103. .pipe()?; // development build always includes debug information
  104. command
  105. };
  106. #[cfg(not(target_os = "windows"))]
  107. let mut command = {
  108. let mut command = Command::new("sh");
  109. command
  110. .arg("-c")
  111. .arg(before_dev)
  112. .current_dir(app_dir())
  113. .envs(command_env(true))
  114. .pipe()?; // development build always includes debug information
  115. command
  116. };
  117. command.stdin(Stdio::piped());
  118. let child = SharedChild::spawn(&mut command)
  119. .unwrap_or_else(|_| panic!("failed to run `{}`", before_dev));
  120. let child = Arc::new(child);
  121. let child_ = child.clone();
  122. std::thread::spawn(move || {
  123. let status = child_
  124. .wait()
  125. .expect("failed to wait on \"beforeDevCommand\"");
  126. if !(status.success() || KILL_BEFORE_DEV_FLAG.get().unwrap().load(Ordering::Relaxed)) {
  127. error!("The \"beforeDevCommand\" terminated with a non-zero status code.");
  128. exit(status.code().unwrap_or(1));
  129. }
  130. });
  131. BEFORE_DEV.set(Mutex::new(child)).unwrap();
  132. KILL_BEFORE_DEV_FLAG.set(AtomicBool::default()).unwrap();
  133. let _ = ctrlc::set_handler(move || {
  134. kill_before_dev_process();
  135. #[cfg(not(debug_assertions))]
  136. let _ = check_for_updates();
  137. exit(130);
  138. });
  139. }
  140. }
  141. let runner_from_config = config
  142. .lock()
  143. .unwrap()
  144. .as_ref()
  145. .unwrap()
  146. .build
  147. .runner
  148. .clone();
  149. let runner = options
  150. .runner
  151. .clone()
  152. .or(runner_from_config)
  153. .unwrap_or_else(|| "cargo".to_string());
  154. let manifest = {
  155. let (tx, rx) = channel();
  156. let mut watcher = watcher(tx, Duration::from_secs(1)).unwrap();
  157. watcher.watch(tauri_path.join("Cargo.toml"), RecursiveMode::Recursive)?;
  158. let manifest = rewrite_manifest(config.clone())?;
  159. loop {
  160. if let Ok(DebouncedEvent::NoticeWrite(_)) = rx.recv() {
  161. break;
  162. }
  163. }
  164. manifest
  165. };
  166. let mut cargo_features = config
  167. .lock()
  168. .unwrap()
  169. .as_ref()
  170. .unwrap()
  171. .build
  172. .features
  173. .clone()
  174. .unwrap_or_default();
  175. if let Some(features) = &options.features {
  176. cargo_features.extend(features.clone());
  177. }
  178. let manually_killed_app = Arc::new(AtomicBool::default());
  179. if std::env::var_os("TAURI_SKIP_DEVSERVER_CHECK") != Some("true".into()) {
  180. if let AppUrl::Url(WindowUrl::External(dev_server_url)) = config
  181. .lock()
  182. .unwrap()
  183. .as_ref()
  184. .unwrap()
  185. .build
  186. .dev_path
  187. .clone()
  188. {
  189. let host = dev_server_url
  190. .host()
  191. .unwrap_or_else(|| panic!("No host name in the URL"));
  192. let port = dev_server_url
  193. .port_or_known_default()
  194. .unwrap_or_else(|| panic!("No port number in the URL"));
  195. let addrs;
  196. let addr;
  197. let addrs = match host {
  198. url::Host::Domain(domain) => {
  199. use std::net::ToSocketAddrs;
  200. addrs = (domain, port).to_socket_addrs()?;
  201. addrs.as_slice()
  202. }
  203. url::Host::Ipv4(ip) => {
  204. addr = (ip, port).into();
  205. std::slice::from_ref(&addr)
  206. }
  207. url::Host::Ipv6(ip) => {
  208. addr = (ip, port).into();
  209. std::slice::from_ref(&addr)
  210. }
  211. };
  212. let mut i = 0;
  213. let sleep_interval = std::time::Duration::from_secs(2);
  214. let max_attempts = 90;
  215. loop {
  216. if std::net::TcpStream::connect(addrs).is_ok() {
  217. break;
  218. }
  219. if i % 3 == 0 {
  220. warn!(
  221. "Waiting for your frontend dev server to start on {}...",
  222. dev_server_url
  223. );
  224. }
  225. i += 1;
  226. if i == max_attempts {
  227. error!(
  228. "Could not connect to `{}` after {}s. Please make sure that is the URL to your dev server.",
  229. dev_server_url, i * sleep_interval.as_secs()
  230. );
  231. exit(1);
  232. }
  233. std::thread::sleep(sleep_interval);
  234. }
  235. }
  236. }
  237. let process = start_app(
  238. &options,
  239. &runner,
  240. &manifest,
  241. &cargo_features,
  242. manually_killed_app.clone(),
  243. )?;
  244. let shared_process = Arc::new(Mutex::new(process));
  245. if let Err(e) = watch(
  246. shared_process.clone(),
  247. manually_killed_app,
  248. tauri_path,
  249. merge_config,
  250. config,
  251. options,
  252. runner,
  253. manifest,
  254. cargo_features,
  255. ) {
  256. shared_process
  257. .lock()
  258. .unwrap()
  259. .kill()
  260. .with_context(|| "failed to kill app process")?;
  261. Err(e)
  262. } else {
  263. Ok(())
  264. }
  265. }
  266. #[cfg(not(debug_assertions))]
  267. fn check_for_updates() -> Result<()> {
  268. if std::env::var_os("TAURI_SKIP_UPDATE_CHECK") != Some("true".into()) {
  269. let current_version = crate::info::cli_current_version()?;
  270. let current = semver::Version::parse(&current_version)?;
  271. let upstream_version = crate::info::cli_upstream_version()?;
  272. let upstream = semver::Version::parse(&upstream_version)?;
  273. if current < upstream {
  274. println!(
  275. "🚀 A new version of Tauri CLI is avaliable! [{}]",
  276. upstream.to_string()
  277. );
  278. };
  279. }
  280. Ok(())
  281. }
  282. fn lookup<F: FnMut(FileType, PathBuf)>(dir: &Path, mut f: F) {
  283. let mut default_gitignore = std::env::temp_dir();
  284. default_gitignore.push(".tauri-dev");
  285. let _ = std::fs::create_dir_all(&default_gitignore);
  286. default_gitignore.push(".gitignore");
  287. if !default_gitignore.exists() {
  288. if let Ok(mut file) = std::fs::File::create(default_gitignore.clone()) {
  289. let _ = file.write_all(TAURI_DEV_WATCHER_GITIGNORE);
  290. }
  291. }
  292. let mut builder = ignore::WalkBuilder::new(dir);
  293. let _ = builder.add_ignore(default_gitignore);
  294. builder.require_git(false).ignore(false).max_depth(Some(1));
  295. for entry in builder.build().flatten() {
  296. f(entry.file_type().unwrap(), dir.join(entry.path()));
  297. }
  298. }
  299. #[allow(clippy::too_many_arguments)]
  300. fn watch(
  301. process: Arc<Mutex<Arc<SharedChild>>>,
  302. manually_killed_app: Arc<AtomicBool>,
  303. tauri_path: PathBuf,
  304. merge_config: Option<String>,
  305. config: ConfigHandle,
  306. options: Options,
  307. runner: String,
  308. mut manifest: Manifest,
  309. cargo_features: Vec<String>,
  310. ) -> Result<()> {
  311. let (tx, rx) = channel();
  312. let mut watcher = watcher(tx, Duration::from_secs(1)).unwrap();
  313. lookup(&tauri_path, |file_type, path| {
  314. if path != tauri_path {
  315. let _ = watcher.watch(
  316. path,
  317. if file_type.is_dir() {
  318. RecursiveMode::Recursive
  319. } else {
  320. RecursiveMode::NonRecursive
  321. },
  322. );
  323. }
  324. });
  325. loop {
  326. if let Ok(event) = rx.recv() {
  327. let event_path = match event {
  328. DebouncedEvent::Create(path) => Some(path),
  329. DebouncedEvent::Remove(path) => Some(path),
  330. DebouncedEvent::Rename(_, dest) => Some(dest),
  331. DebouncedEvent::Write(path) => Some(path),
  332. _ => None,
  333. };
  334. if let Some(event_path) = event_path {
  335. if event_path.file_name() == Some(OsStr::new("tauri.conf.json")) {
  336. reload_config(merge_config.as_deref())?;
  337. manifest = rewrite_manifest(config.clone())?;
  338. } else {
  339. // When tauri.conf.json is changed, rewrite_manifest will be called
  340. // which will trigger the watcher again
  341. // So the app should only be started when a file other than tauri.conf.json is changed
  342. manually_killed_app.store(true, Ordering::Relaxed);
  343. let mut p = process.lock().unwrap();
  344. p.kill().with_context(|| "failed to kill app process")?;
  345. // wait for the process to exit
  346. loop {
  347. if let Ok(Some(_)) = p.try_wait() {
  348. break;
  349. }
  350. }
  351. *p = start_app(
  352. &options,
  353. &runner,
  354. &manifest,
  355. &cargo_features,
  356. manually_killed_app.clone(),
  357. )?;
  358. }
  359. }
  360. }
  361. }
  362. }
  363. fn kill_before_dev_process() {
  364. if let Some(child) = BEFORE_DEV.get() {
  365. let child = child.lock().unwrap();
  366. KILL_BEFORE_DEV_FLAG
  367. .get()
  368. .unwrap()
  369. .store(true, Ordering::Relaxed);
  370. #[cfg(windows)]
  371. let _ = Command::new("powershell")
  372. .arg("-NoProfile")
  373. .arg("-Command")
  374. .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()))
  375. .status();
  376. #[cfg(unix)]
  377. {
  378. let mut kill_children_script_path = std::env::temp_dir();
  379. kill_children_script_path.push("kill-children.sh");
  380. if !kill_children_script_path.exists() {
  381. if let Ok(mut file) = std::fs::File::create(&kill_children_script_path) {
  382. use std::os::unix::fs::PermissionsExt;
  383. let _ = file.write_all(KILL_CHILDREN_SCRIPT);
  384. let mut permissions = file.metadata().unwrap().permissions();
  385. permissions.set_mode(0o770);
  386. let _ = file.set_permissions(permissions);
  387. }
  388. }
  389. let _ = Command::new(&kill_children_script_path)
  390. .arg(child.id().to_string())
  391. .output();
  392. }
  393. let _ = child.kill();
  394. }
  395. }
  396. fn start_app(
  397. options: &Options,
  398. runner: &str,
  399. manifest: &Manifest,
  400. features: &[String],
  401. manually_killed_app: Arc<AtomicBool>,
  402. ) -> Result<Arc<SharedChild>> {
  403. let mut command = Command::new(runner);
  404. command
  405. .env(
  406. "CARGO_TERM_PROGRESS_WIDTH",
  407. terminal::stderr_width()
  408. .map(|width| {
  409. if cfg!(windows) {
  410. std::cmp::min(60, width)
  411. } else {
  412. width
  413. }
  414. })
  415. .unwrap_or(if cfg!(windows) { 60 } else { 80 })
  416. .to_string(),
  417. )
  418. .env("CARGO_TERM_PROGRESS_WHEN", "always");
  419. command.arg("run").arg("--color").arg("always");
  420. if !options.args.contains(&"--no-default-features".into()) {
  421. let manifest_features = manifest.features();
  422. let enable_features: Vec<String> = manifest_features
  423. .get("default")
  424. .cloned()
  425. .unwrap_or_default()
  426. .into_iter()
  427. .filter(|feature| {
  428. if let Some(manifest_feature) = manifest_features.get(feature) {
  429. !manifest_feature.contains(&"tauri/custom-protocol".into())
  430. } else {
  431. feature != "tauri/custom-protocol"
  432. }
  433. })
  434. .collect();
  435. command.arg("--no-default-features");
  436. if !enable_features.is_empty() {
  437. command.args(&["--features", &enable_features.join(",")]);
  438. }
  439. }
  440. if options.release_mode {
  441. command.args(&["--release"]);
  442. }
  443. if let Some(target) = &options.target {
  444. command.args(&["--target", target]);
  445. }
  446. if !features.is_empty() {
  447. command.args(&["--features", &features.join(",")]);
  448. }
  449. if !options.args.is_empty() {
  450. command.args(&options.args);
  451. }
  452. command.stdout(os_pipe::dup_stdout().unwrap());
  453. command.stderr(Stdio::piped());
  454. let child =
  455. SharedChild::spawn(&mut command).with_context(|| format!("failed to run {}", runner))?;
  456. let child_arc = Arc::new(child);
  457. let child_stderr = child_arc.take_stderr().unwrap();
  458. let mut stderr = BufReader::new(child_stderr);
  459. let stderr_lines = Arc::new(Mutex::new(Vec::new()));
  460. let stderr_lines_ = stderr_lines.clone();
  461. std::thread::spawn(move || {
  462. let mut buf = Vec::new();
  463. let mut lines = stderr_lines_.lock().unwrap();
  464. let mut io_stderr = std::io::stderr();
  465. loop {
  466. buf.clear();
  467. match tauri_utils::io::read_line(&mut stderr, &mut buf) {
  468. Ok(s) if s == 0 => break,
  469. _ => (),
  470. }
  471. let _ = io_stderr.write_all(&buf);
  472. if !buf.ends_with(&[b'\r']) {
  473. let _ = io_stderr.write_all(b"\n");
  474. }
  475. lines.push(String::from_utf8_lossy(&buf).into_owned());
  476. }
  477. });
  478. let child_clone = child_arc.clone();
  479. let exit_on_panic = options.exit_on_panic;
  480. std::thread::spawn(move || {
  481. let status = child_clone.wait().expect("failed to wait on child");
  482. if exit_on_panic {
  483. if !manually_killed_app.load(Ordering::Relaxed) {
  484. kill_before_dev_process();
  485. #[cfg(not(debug_assertions))]
  486. let _ = check_for_updates();
  487. exit(status.code().unwrap_or(0));
  488. }
  489. } else {
  490. let is_cargo_compile_error = stderr_lines
  491. .lock()
  492. .unwrap()
  493. .last()
  494. .map(|l| l.contains("could not compile"))
  495. .unwrap_or_default();
  496. stderr_lines.lock().unwrap().clear();
  497. // if we're no exiting on panic, we only exit if:
  498. // - the status is a success code (app closed)
  499. // - status code is the Cargo error code
  500. // - and error is not a cargo compilation error (using stderr heuristics)
  501. if status.success() || (status.code() == Some(101) && !is_cargo_compile_error) {
  502. kill_before_dev_process();
  503. #[cfg(not(debug_assertions))]
  504. let _ = check_for_updates();
  505. exit(status.code().unwrap_or(1));
  506. }
  507. }
  508. });
  509. Ok(child_arc)
  510. }
  511. // taken from https://github.com/rust-lang/cargo/blob/78b10d4e611ab0721fc3aeaf0edd5dd8f4fdc372/src/cargo/core/shell.rs#L514
  512. #[cfg(unix)]
  513. mod terminal {
  514. use std::mem;
  515. pub fn stderr_width() -> Option<usize> {
  516. unsafe {
  517. let mut winsize: libc::winsize = mem::zeroed();
  518. // The .into() here is needed for FreeBSD which defines TIOCGWINSZ
  519. // as c_uint but ioctl wants c_ulong.
  520. #[allow(clippy::useless_conversion)]
  521. if libc::ioctl(libc::STDERR_FILENO, libc::TIOCGWINSZ.into(), &mut winsize) < 0 {
  522. return None;
  523. }
  524. if winsize.ws_col > 0 {
  525. Some(winsize.ws_col as usize)
  526. } else {
  527. None
  528. }
  529. }
  530. }
  531. }
  532. // taken from https://github.com/rust-lang/cargo/blob/78b10d4e611ab0721fc3aeaf0edd5dd8f4fdc372/src/cargo/core/shell.rs#L543
  533. #[cfg(windows)]
  534. mod terminal {
  535. use std::{cmp, mem, ptr};
  536. use winapi::um::fileapi::*;
  537. use winapi::um::handleapi::*;
  538. use winapi::um::processenv::*;
  539. use winapi::um::winbase::*;
  540. use winapi::um::wincon::*;
  541. use winapi::um::winnt::*;
  542. pub fn stderr_width() -> Option<usize> {
  543. unsafe {
  544. let stdout = GetStdHandle(STD_ERROR_HANDLE);
  545. let mut csbi: CONSOLE_SCREEN_BUFFER_INFO = mem::zeroed();
  546. if GetConsoleScreenBufferInfo(stdout, &mut csbi) != 0 {
  547. return Some((csbi.srWindow.Right - csbi.srWindow.Left) as usize);
  548. }
  549. // On mintty/msys/cygwin based terminals, the above fails with
  550. // INVALID_HANDLE_VALUE. Use an alternate method which works
  551. // in that case as well.
  552. let h = CreateFileA(
  553. "CONOUT$\0".as_ptr() as *const CHAR,
  554. GENERIC_READ | GENERIC_WRITE,
  555. FILE_SHARE_READ | FILE_SHARE_WRITE,
  556. ptr::null_mut(),
  557. OPEN_EXISTING,
  558. 0,
  559. ptr::null_mut(),
  560. );
  561. if h == INVALID_HANDLE_VALUE {
  562. return None;
  563. }
  564. let mut csbi: CONSOLE_SCREEN_BUFFER_INFO = mem::zeroed();
  565. let rc = GetConsoleScreenBufferInfo(h, &mut csbi);
  566. CloseHandle(h);
  567. if rc != 0 {
  568. let width = (csbi.srWindow.Right - csbi.srWindow.Left) as usize;
  569. // Unfortunately cygwin/mintty does not set the size of the
  570. // backing console to match the actual window size. This
  571. // always reports a size of 80 or 120 (not sure what
  572. // determines that). Use a conservative max of 60 which should
  573. // work in most circumstances. ConEmu does some magic to
  574. // resize the console correctly, but there's no reasonable way
  575. // to detect which kind of terminal we are running in, or if
  576. // GetConsoleScreenBufferInfo returns accurate information.
  577. return Some(cmp::min(60, width));
  578. }
  579. None
  580. }
  581. }
  582. }