Browse Source

fix(cli): do not generate .cargo/config file (#10785)

Lucas Fernandes Nogueira 11 months ago
parent
commit
fd68b7fdea

+ 6 - 0
.changes/remove-cargo-config-creation.md

@@ -0,0 +1,6 @@
+---
+"tauri-cli": patch:enhance
+"@tauri-apps/cli": patch:enhance
+---
+
+Remove the `.cargo/config` file creation that used to fix mobile build caches.

+ 0 - 1
examples/api/src-tauri/.gitignore

@@ -3,5 +3,4 @@
 /target/
 
 # cargo-mobile
-.cargo/
 /gen

+ 3 - 3
tooling/cli/src/mobile/android/build.rs

@@ -107,7 +107,7 @@ pub fn command(options: Options, noise_level: NoiseLevel) -> Result<()> {
     tauri_utils::platform::Target::Android,
     options.config.as_ref().map(|c| &c.0),
   )?;
-  let (interface, app, config, metadata) = {
+  let (interface, config, metadata) = {
     let tauri_config_guard = tauri_config.lock().unwrap();
     let tauri_config_ = tauri_config_guard.as_ref().unwrap();
 
@@ -121,7 +121,7 @@ pub fn command(options: Options, noise_level: NoiseLevel) -> Result<()> {
       build_options.features.as_ref(),
       &Default::default(),
     );
-    (interface, app, config, metadata)
+    (interface, config, metadata)
   };
 
   let profile = if options.debug {
@@ -141,7 +141,7 @@ pub fn command(options: Options, noise_level: NoiseLevel) -> Result<()> {
   )?;
 
   let mut env = env()?;
-  configure_cargo(&app, Some((&mut env, &config)))?;
+  configure_cargo(&mut env, &config)?;
 
   crate::build::setup(&interface, &mut build_options, tauri_config.clone(), true)?;
 

+ 3 - 6
tooling/cli/src/mobile/android/dev.rs

@@ -27,7 +27,6 @@ use cargo_mobile2::{
     env::Env,
     target::Target,
   },
-  config::app::App,
   opts::{FilterLevel, NoiseLevel, Profile},
   target::TargetTrait,
 };
@@ -128,7 +127,7 @@ fn run_command(options: Options, noise_level: NoiseLevel) -> Result<()> {
     .unwrap_or_else(|| Target::all().values().next().unwrap().triple.into());
   dev_options.target = Some(target_triple.clone());
 
-  let (interface, app, config, metadata) = {
+  let (interface, config, metadata) = {
     let tauri_config_guard = tauri_config.lock().unwrap();
     let tauri_config_ = tauri_config_guard.as_ref().unwrap();
 
@@ -141,7 +140,7 @@ fn run_command(options: Options, noise_level: NoiseLevel) -> Result<()> {
       dev_options.features.as_ref(),
       &Default::default(),
     );
-    (interface, app, config, metadata)
+    (interface, config, metadata)
   };
 
   let tauri_path = tauri_dir();
@@ -160,7 +159,6 @@ fn run_command(options: Options, noise_level: NoiseLevel) -> Result<()> {
     tauri_config,
     device,
     env,
-    &app,
     &config,
     &metadata,
     noise_level,
@@ -175,7 +173,6 @@ fn run_dev(
   tauri_config: ConfigHandle,
   device: Option<Device>,
   mut env: Env,
-  app: &App,
   config: &AndroidConfig,
   metadata: &AndroidMetadata,
   noise_level: NoiseLevel,
@@ -193,7 +190,7 @@ fn run_dev(
   let out_dir = bin_path.parent().unwrap();
   let _lock = flock::open_rw(out_dir.join("lock").with_extension("android"), "Android")?;
 
-  configure_cargo(app, Some((&mut env, config)))?;
+  configure_cargo(&mut env, config)?;
 
   // run an initial build to initialize plugins
   let target_triple = dev_options.target.as_ref().unwrap();

+ 22 - 4
tooling/cli/src/mobile/android/mod.rs

@@ -14,6 +14,7 @@ use cargo_mobile2::{
   config::app::{App, DEFAULT_ASSET_DIR},
   opts::{FilterLevel, NoiseLevel},
   os,
+  target::TargetTrait,
   util::prompt,
 };
 use clap::{Parser, Subcommand};
@@ -28,10 +29,8 @@ use sublime_fuzzy::best_match;
 use tauri_utils::resources::ResourcePaths;
 
 use super::{
-  ensure_init, get_app,
-  init::{command as init_command, configure_cargo},
-  log_finished, read_options, CliOptions, OptionsHandle, Target as MobileTarget,
-  MIN_DEVICE_MATCH_SCORE,
+  ensure_init, get_app, init::command as init_command, log_finished, read_options, CliOptions,
+  OptionsHandle, Target as MobileTarget, MIN_DEVICE_MATCH_SCORE,
 };
 use crate::{
   helpers::config::{BundleResources, Config as TauriConfig},
@@ -327,3 +326,22 @@ fn inject_resources(config: &AndroidConfig, tauri_config: &TauriConfig) -> Resul
 
   Ok(())
 }
+
+fn configure_cargo(env: &mut Env, config: &AndroidConfig) -> Result<()> {
+  for target in Target::all().values() {
+    let config = target.generate_cargo_config(config, env)?;
+    let target_var_name = target.triple.replace('-', "_").to_uppercase();
+    if let Some(linker) = config.linker {
+      env.base.insert_env_var(
+        format!("CARGO_TARGET_{target_var_name}_LINKER"),
+        linker.into(),
+      );
+    }
+    env.base.insert_env_var(
+      format!("CARGO_TARGET_{target_var_name}_RUSTFLAGS"),
+      config.rustflags.join(" ").into(),
+    );
+  }
+
+  Ok(())
+}

+ 1 - 42
tooling/cli/src/mobile/init.rs

@@ -9,13 +9,9 @@ use crate::{
   Result,
 };
 use cargo_mobile2::{
-  android::{
-    config::Config as AndroidConfig, env::Env as AndroidEnv, target::Target as AndroidTarget,
-  },
+  android::env::Env as AndroidEnv,
   config::app::App,
-  dot_cargo,
   reserved_names::KOTLIN_ONLY_KEYWORDS,
-  target::TargetTrait as _,
   util::{
     self,
     cli::{Report, TextWrapper},
@@ -40,43 +36,6 @@ pub fn command(
   Ok(())
 }
 
-pub fn configure_cargo(
-  app: &App,
-  android: Option<(&mut AndroidEnv, &AndroidConfig)>,
-) -> Result<()> {
-  if let Some((env, config)) = android {
-    for target in AndroidTarget::all().values() {
-      let config = target.generate_cargo_config(config, env)?;
-      let target_var_name = target.triple.replace('-', "_").to_uppercase();
-      if let Some(linker) = config.linker {
-        env.base.insert_env_var(
-          format!("CARGO_TARGET_{target_var_name}_LINKER"),
-          linker.into(),
-        );
-      }
-      env.base.insert_env_var(
-        format!("CARGO_TARGET_{target_var_name}_RUSTFLAGS"),
-        config.rustflags.join(" ").into(),
-      );
-    }
-  }
-
-  let mut dot_cargo = dot_cargo::DotCargo::load(app)?;
-  // Mysteriously, builds that don't specify `--target` seem to fight over
-  // the build cache with builds that use `--target`! This means that
-  // alternating between i.e. `cargo run` and `cargo apple run` would
-  // result in clean builds being made each time you switched... which is
-  // pretty nightmarish. Specifying `build.target` in `.cargo/config`
-  // fortunately has the same effect as specifying `--target`, so now we can
-  // `cargo run` with peace of mind!
-  //
-  // This behavior could be explained here:
-  // https://doc.rust-lang.org/cargo/reference/config.html#buildrustflags
-  dot_cargo.set_default_target(util::host_target_triple()?);
-
-  dot_cargo.write(app).map_err(Into::into)
-}
-
 pub fn exec(
   target: Target,
   wrapper: &TextWrapper,

+ 3 - 4
tooling/cli/src/mobile/ios/build.rs

@@ -3,9 +3,9 @@
 // SPDX-License-Identifier: MIT
 
 use super::{
-  configure_cargo, detect_target_ok, ensure_init, env, get_app, get_config, inject_resources,
-  load_pbxproj, log_finished, merge_plist, open_and_wait, project_config,
-  synchronize_project_config, MobileTarget, OptionsHandle,
+  detect_target_ok, ensure_init, env, get_app, get_config, inject_resources, load_pbxproj,
+  log_finished, merge_plist, open_and_wait, project_config, synchronize_project_config,
+  MobileTarget, OptionsHandle,
 };
 use crate::{
   build::Options as BuildOptions,
@@ -186,7 +186,6 @@ pub fn command(options: Options, noise_level: NoiseLevel) -> Result<()> {
   merged_info_plist.to_file_xml(&info_plist_path)?;
 
   let mut env = env()?;
-  configure_cargo(&app, None)?;
 
   let mut export_options_plist = plist::Dictionary::new();
   if let Some(method) = options.export_method {

+ 4 - 9
tooling/cli/src/mobile/ios/dev.rs

@@ -3,8 +3,8 @@
 // SPDX-License-Identifier: MIT
 
 use super::{
-  configure_cargo, device_prompt, ensure_init, env, get_app, get_config, inject_resources,
-  merge_plist, open_and_wait, MobileTarget,
+  device_prompt, ensure_init, env, get_app, get_config, inject_resources, merge_plist,
+  open_and_wait, MobileTarget,
 };
 use crate::{
   dev::Options as DevOptions,
@@ -25,7 +25,6 @@ use cargo_mobile2::{
     config::Config as AppleConfig,
     device::{Device, DeviceKind},
   },
-  config::app::App,
   env::Env,
   opts::{NoiseLevel, Profile},
 };
@@ -153,7 +152,7 @@ fn run_command(options: Options, noise_level: NoiseLevel) -> Result<()> {
     tauri_utils::platform::Target::Ios,
     options.config.as_ref().map(|c| &c.0),
   )?;
-  let (interface, app, config) = {
+  let (interface, config) = {
     let tauri_config_guard = tauri_config.lock().unwrap();
     let tauri_config_ = tauri_config_guard.as_ref().unwrap();
 
@@ -167,7 +166,7 @@ fn run_command(options: Options, noise_level: NoiseLevel) -> Result<()> {
       &Default::default(),
     );
 
-    (interface, app, config)
+    (interface, config)
   };
 
   let tauri_path = tauri_dir();
@@ -199,7 +198,6 @@ fn run_command(options: Options, noise_level: NoiseLevel) -> Result<()> {
     tauri_config,
     device,
     env,
-    &app,
     &config,
     noise_level,
   )
@@ -344,7 +342,6 @@ fn run_dev(
   tauri_config: ConfigHandle,
   device: Option<Device>,
   env: Env,
-  app: &App,
   config: &AppleConfig,
   noise_level: NoiseLevel,
 ) -> Result<()> {
@@ -371,8 +368,6 @@ fn run_dev(
 
   let set_host = options.host.is_some();
 
-  configure_cargo(app, None)?;
-
   let open = options.open;
   let exit_on_panic = options.exit_on_panic;
   let no_watch = options.no_watch;

+ 2 - 4
tooling/cli/src/mobile/ios/mod.rs

@@ -23,10 +23,8 @@ use sublime_fuzzy::best_match;
 use tauri_utils::resources::ResourcePaths;
 
 use super::{
-  ensure_init, env, get_app,
-  init::{command as init_command, configure_cargo},
-  log_finished, read_options, CliOptions, OptionsHandle, Target as MobileTarget,
-  MIN_DEVICE_MATCH_SCORE,
+  ensure_init, env, get_app, init::command as init_command, log_finished, read_options, CliOptions,
+  OptionsHandle, Target as MobileTarget, MIN_DEVICE_MATCH_SCORE,
 };
 use crate::{
   helpers::{