Repository: tauri-apps/tauri
File: crates/tauri-cli/src/interface/rust.rs (Rust)

Code before:
impl Interface for Rust {
  type AppSettings = RustAppSettings;

  fn new(config: &Config, target: Option<String>) -> crate::Result<Self> {
    let manifest = {
      let (tx, rx) = sync_channel(1);
      let mut watcher = new_debouncer(Duration::from_secs(1), None, move |r| {
        if let Ok(events) = r {
          let _ = tx.send(events);
        }
      })
      .unwrap();
      watcher.watch(tauri_dir().join("Cargo.toml"), RecursiveMode::NonRecursive)?;
      let (manifest, modified) = rewrite_manifest(config)?;
      if modified {
        let now = Instant::now();
        let timeout = Duration::from_secs(2);
        loop {
          if now.elapsed() >= timeout {
            break;
          }
          if rx.try_recv().is_ok() {
            break;
          }
        }
      }
      manifest
    };

    let target_ios = target
      .as_ref()
      .is_some_and(|target| target.ends_with("ios") || target.ends_with("ios-sim"));
    if target_ios {
      std::env::set_var(
        "IPHONEOS_DEPLOYMENT_TARGET",
        &config.bundle.ios.minimum_system_version,
      );
    } else if let Some(minimum_system_version) = &config.bundle.macos.minimum_system_version {
      std::env::set_var("MACOSX_DEPLOYMENT_TARGET", minimum_system_version);
    }

    let app_settings = RustAppSettings::new(config, manifest, target)?;

    Ok(Self {
      app_settings: Arc::new(app_settings),
      config_features: config.build.features.clone().unwrap_or_default(),
      main_binary_name: config.main_binary_name.clone(),
      available_targets: None,
    })
  }


Review comment (refactor):
This is the changed part, other parts are just some small refactors that I did when looking through them

Also, I'm not entirely sure what does this watch even do, I would assume it's for waiting for the file modification to take effect from `rewrite_manifest`, is it?

Code after:
impl Interface for Rust {
  type AppSettings = RustAppSettings;

  fn new(config: &Config, target: Option<String>) -> crate::Result<Self> {
    let manifest = {
      let (tx, rx) = sync_channel(1);
      let mut watcher = new_debouncer(Duration::from_secs(1), None, move |r| {
        if let Ok(_events) = r {
          let _ = tx.send(());
        }
      })
      .unwrap();
      watcher.watch(tauri_dir().join("Cargo.toml"), RecursiveMode::NonRecursive)?;
      let (manifest, modified) = rewrite_manifest(config)?;
      if modified {
        // Wait for the modified event so we don't trigger a re-build later on
        let _ = rx.recv_timeout(Duration::from_secs(2));
      }
      manifest
    };

    let target_ios = target
      .as_ref()
      .is_some_and(|target| target.ends_with("ios") || target.ends_with("ios-sim"));
    if target_ios {
      std::env::set_var(
        "IPHONEOS_DEPLOYMENT_TARGET",
        &config.bundle.ios.minimum_system_version,
      );
    } else if let Some(minimum_system_version) = &config.bundle.macos.minimum_system_version {
      std::env::set_var("MACOSX_DEPLOYMENT_TARGET", minimum_system_version);
    }

    let app_settings = RustAppSettings::new(config, manifest, target)?;

    Ok(Self {
      app_settings: Arc::new(app_settings),
      config_features: config.build.features.clone().unwrap_or_default(),
      main_binary_name: config.main_binary_name.clone(),
      available_targets: None,
    })
  }

  fn app_settings(&self) -> Arc<Self::AppSettings> {
    self.app_settings.clone()
  }

  fn build(&mut self, options: Options) -> crate::Result<PathBuf> {
    desktop::build(
      options,
      &self.app_settings,