Skip to content

Commit e691208

Browse files
authored
fix(cli): prevent creating interface twice avoiding double manifest rewrite (#7837)
1 parent 791e291 commit e691208

15 files changed

Lines changed: 253 additions & 128 deletions

File tree

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"tauri-cli": patch:enhance
3+
"@tauri-apps/cli": patch:enhance
4+
---
5+
6+
Prevent unneeded double Cargo.toml rewrite on `dev` and `build`.

tooling/cli/src/build.rs

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,15 @@ pub fn command(mut options: Options, verbosity: u8) -> Result<()> {
7575
.map(Target::from_triple)
7676
.unwrap_or_else(Target::current);
7777

78-
let mut interface = setup(target, &mut options, false)?;
79-
8078
let config = get_config(target, options.config.as_deref())?;
79+
80+
let mut interface = AppInterface::new(
81+
config.lock().unwrap().as_ref().unwrap(),
82+
options.target.clone(),
83+
)?;
84+
85+
setup(target, &interface, &mut options, false)?;
86+
8187
let config_guard = config.lock().unwrap();
8288
let config_ = config_guard.as_ref().unwrap();
8389

@@ -228,7 +234,12 @@ pub fn command(mut options: Options, verbosity: u8) -> Result<()> {
228234
Ok(())
229235
}
230236

231-
pub fn setup(target: Target, options: &mut Options, mobile: bool) -> Result<AppInterface> {
237+
pub fn setup(
238+
target: Target,
239+
interface: &AppInterface,
240+
options: &mut Options,
241+
mobile: bool,
242+
) -> Result<()> {
232243
let (merge_config, merge_config_path) = resolve_merge_config(&options.config)?;
233244
options.config = merge_config;
234245

@@ -240,8 +251,6 @@ pub fn setup(target: Target, options: &mut Options, mobile: bool) -> Result<AppI
240251
let config_guard = config.lock().unwrap();
241252
let config_ = config_guard.as_ref().unwrap();
242253

243-
let interface = AppInterface::new(config_, options.target.clone())?;
244-
245254
let bundle_identifier_source = match config_.find_bundle_identifier_overwriter() {
246255
Some(source) if source == MERGE_CONFIG_EXTENSION_NAME => merge_config_path.unwrap_or(source),
247256
Some(source) => source,
@@ -272,12 +281,7 @@ pub fn setup(target: Target, options: &mut Options, mobile: bool) -> Result<AppI
272281
}
273282

274283
if let Some(before_build) = config_.build.before_build_command.clone() {
275-
run_hook(
276-
"beforeBuildCommand",
277-
before_build,
278-
&interface,
279-
options.debug,
280-
)?;
284+
run_hook("beforeBuildCommand", before_build, interface, options.debug)?;
281285
}
282286

283287
if let Some(AppUrl::Url(WebviewUrl::App(web_asset_path))) = &config_.build.dist_dir {
@@ -320,7 +324,7 @@ pub fn setup(target: Target, options: &mut Options, mobile: bool) -> Result<AppI
320324
.extend(config_.build.features.clone().unwrap_or_default());
321325
interface.build_options(&mut options.args, &mut options.features, mobile);
322326

323-
Ok(interface)
327+
Ok(())
324328
}
325329

326330
fn run_hook(name: &str, hook: HookCommand, interface: &AppInterface, debug: bool) -> Result<()> {

tooling/cli/src/dev.rs

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,16 @@ fn command_internal(mut options: Options) -> Result<()> {
9999
.as_deref()
100100
.map(Target::from_triple)
101101
.unwrap_or_else(Target::current);
102-
let mut interface = setup(target, &mut options, false)?;
102+
103+
let config = get_config(target, options.config.as_deref())?;
104+
105+
let mut interface = AppInterface::new(
106+
config.lock().unwrap().as_ref().unwrap(),
107+
options.target.clone(),
108+
)?;
109+
110+
setup(target, &interface, &mut options, false)?;
111+
103112
let exit_on_panic = options.exit_on_panic;
104113
let no_watch = options.no_watch;
105114
interface.dev(options.into(), move |status, reason| {
@@ -150,7 +159,12 @@ pub fn local_ip_address(force: bool) -> &'static IpAddr {
150159
})
151160
}
152161

153-
pub fn setup(target: Target, options: &mut Options, mobile: bool) -> Result<AppInterface> {
162+
pub fn setup(
163+
target: Target,
164+
interface: &AppInterface,
165+
options: &mut Options,
166+
mobile: bool,
167+
) -> Result<()> {
154168
let (merge_config, _merge_config_path) = resolve_merge_config(&options.config)?;
155169
options.config = merge_config;
156170

@@ -159,11 +173,6 @@ pub fn setup(target: Target, options: &mut Options, mobile: bool) -> Result<AppI
159173
let tauri_path = tauri_dir();
160174
set_current_dir(tauri_path).with_context(|| "failed to change current working directory")?;
161175

162-
let interface = AppInterface::new(
163-
config.lock().unwrap().as_ref().unwrap(),
164-
options.target.clone(),
165-
)?;
166-
167176
let mut dev_path = config
168177
.lock()
169178
.unwrap()
@@ -397,7 +406,7 @@ pub fn setup(target: Target, options: &mut Options, mobile: bool) -> Result<AppI
397406
}
398407
}
399408

400-
Ok(interface)
409+
Ok(())
401410
}
402411

403412
pub fn wait_dev_process<

tooling/cli/src/interface/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use std::{
88
collections::HashMap,
99
path::{Path, PathBuf},
1010
process::ExitStatus,
11+
sync::Arc,
1112
};
1213

1314
use crate::helpers::config::Config;
@@ -87,7 +88,7 @@ pub trait Interface: Sized {
8788
type AppSettings: AppSettings;
8889

8990
fn new(config: &Config, target: Option<String>) -> crate::Result<Self>;
90-
fn app_settings(&self) -> &Self::AppSettings;
91+
fn app_settings(&self) -> Arc<Self::AppSettings>;
9192
fn env(&self) -> HashMap<&str, String>;
9293
fn build(&mut self, options: Options) -> crate::Result<()>;
9394
fn dev<F: Fn(Option<i32>, ExitReason) + Send + Sync + 'static>(

tooling/cli/src/interface/rust.rs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ pub struct RustupTarget {
9696
}
9797

9898
pub struct Rust {
99-
app_settings: RustAppSettings,
99+
app_settings: Arc<RustAppSettings>,
100100
config_features: Vec<String>,
101101
product_name: Option<String>,
102102
available_targets: Option<Vec<RustupTarget>>,
@@ -138,15 +138,15 @@ impl Interface for Rust {
138138
let app_settings = RustAppSettings::new(config, manifest, target)?;
139139

140140
Ok(Self {
141-
app_settings,
141+
app_settings: Arc::new(app_settings),
142142
config_features: config.build.features.clone().unwrap_or_default(),
143143
product_name: config.package.product_name.clone(),
144144
available_targets: None,
145145
})
146146
}
147147

148-
fn app_settings(&self) -> &Self::AppSettings {
149-
&self.app_settings
148+
fn app_settings(&self) -> Arc<Self::AppSettings> {
149+
self.app_settings.clone()
150150
}
151151

152152
fn build(&mut self, options: Options) -> crate::Result<()> {
@@ -350,6 +350,8 @@ fn shared_options(
350350
args.push("--bins".into());
351351
let all_features = app_settings
352352
.manifest
353+
.lock()
354+
.unwrap()
353355
.all_enabled_features(if let Some(f) = features { f } else { &[] });
354356
if !all_features.contains(&"tauri/rustls-tls".into()) {
355357
features
@@ -382,7 +384,7 @@ fn dev_options(
382384
shared_options(mobile, args, features, app_settings);
383385

384386
if !args.contains(&"--no-default-features".into()) {
385-
let manifest_features = app_settings.manifest.features();
387+
let manifest_features = app_settings.manifest.lock().unwrap().features();
386388
let enable_features: Vec<String> = manifest_features
387389
.get("default")
388390
.cloned()
@@ -504,7 +506,7 @@ impl Rust {
504506
match reload_config(config.as_deref()) {
505507
Ok(config) => {
506508
info!("Tauri configuration changed. Rewriting manifest...");
507-
self.app_settings.manifest =
509+
*self.app_settings.manifest.lock().unwrap() =
508510
rewrite_manifest(config.lock().unwrap().as_ref().unwrap())?
509511
}
510512
Err(err) => {
@@ -672,7 +674,7 @@ impl CargoSettings {
672674
}
673675

674676
pub struct RustAppSettings {
675-
manifest: Manifest,
677+
manifest: Mutex<Manifest>,
676678
cargo_settings: CargoSettings,
677679
cargo_package_settings: CargoPackageSettings,
678680
package_settings: PackageSettings,
@@ -702,7 +704,7 @@ impl AppSettings for RustAppSettings {
702704
self.target_triple.starts_with("x86_64") || self.target_triple.starts_with("aarch64");
703705

704706
let mut settings = tauri_config_to_bundle_settings(
705-
&self.manifest,
707+
&self.manifest.lock().unwrap(),
706708
features,
707709
config.tauri.bundle.clone(),
708710
arch64bits,
@@ -855,6 +857,8 @@ impl AppSettings for RustAppSettings {
855857
fn app_name(&self) -> Option<String> {
856858
self
857859
.manifest
860+
.lock()
861+
.unwrap()
858862
.inner
859863
.as_table()
860864
.get("package")
@@ -867,6 +871,8 @@ impl AppSettings for RustAppSettings {
867871
fn lib_name(&self) -> Option<String> {
868872
self
869873
.manifest
874+
.lock()
875+
.unwrap()
870876
.inner
871877
.as_table()
872878
.get("lib")
@@ -991,7 +997,7 @@ impl RustAppSettings {
991997
let target = Target::from_triple(&target_triple);
992998

993999
Ok(Self {
994-
manifest,
1000+
manifest: Mutex::new(manifest),
9951001
cargo_settings,
9961002
cargo_package_settings,
9971003
package_settings,

tooling/cli/src/mobile/android/android_studio_script.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@
33
// SPDX-License-Identifier: MIT
44

55
use super::{detect_target_ok, ensure_init, env, get_app, get_config, read_options, MobileTarget};
6-
use crate::{helpers::config::get as get_tauri_config, Result};
6+
use crate::{
7+
helpers::config::get as get_tauri_config,
8+
interface::{AppInterface, Interface},
9+
Result,
10+
};
711
use clap::{ArgAction, Parser};
812

913
use cargo_mobile2::{
@@ -42,7 +46,11 @@ pub fn command(options: Options) -> Result<()> {
4246
let tauri_config_guard = tauri_config.lock().unwrap();
4347
let tauri_config_ = tauri_config_guard.as_ref().unwrap();
4448
let cli_options = read_options(&tauri_config_.tauri.bundle.identifier);
45-
let (config, metadata) = get_config(&get_app(tauri_config_), tauri_config_, &cli_options);
49+
let (config, metadata) = get_config(
50+
&get_app(tauri_config_, &AppInterface::new(tauri_config_, None)?),
51+
tauri_config_,
52+
&cli_options,
53+
);
4654
(config, metadata, cli_options)
4755
};
4856
ensure_init(config.project_dir(), MobileTarget::Android)?;

tooling/cli/src/mobile/android/build.rs

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use crate::{
1313
config::{get as get_tauri_config, ConfigHandle},
1414
flock, resolve_merge_config,
1515
},
16-
interface::{AppSettings, Interface, Options as InterfaceOptions},
16+
interface::{AppInterface, AppSettings, Interface, Options as InterfaceOptions},
1717
mobile::{write_options, CliOptions},
1818
Result,
1919
};
@@ -87,16 +87,28 @@ pub fn command(mut options: Options, noise_level: NoiseLevel) -> Result<()> {
8787
let (merge_config, _merge_config_path) = resolve_merge_config(&options.config)?;
8888
options.config = merge_config;
8989

90+
let mut build_options: BuildOptions = options.clone().into();
91+
build_options.target = Some(
92+
Target::all()
93+
.get(Target::DEFAULT_KEY)
94+
.unwrap()
95+
.triple
96+
.into(),
97+
);
98+
9099
let tauri_config = get_tauri_config(
91100
tauri_utils::platform::Target::Android,
92101
options.config.as_deref(),
93102
)?;
94-
let (app, config, metadata) = {
103+
let (interface, app, config, metadata) = {
95104
let tauri_config_guard = tauri_config.lock().unwrap();
96105
let tauri_config_ = tauri_config_guard.as_ref().unwrap();
97-
let app = get_app(tauri_config_);
106+
107+
let interface = AppInterface::new(tauri_config_, build_options.target.clone())?;
108+
109+
let app = get_app(tauri_config_, &interface);
98110
let (config, metadata) = get_config(&app, tauri_config_, &Default::default());
99-
(app, config, metadata)
111+
(interface, app, config, metadata)
100112
};
101113

102114
set_var("WRY_RUSTWEBVIEWCLIENT_CLASS_EXTENSION", "");
@@ -132,7 +144,9 @@ pub fn command(mut options: Options, noise_level: NoiseLevel) -> Result<()> {
132144

133145
let open = options.open;
134146
let _handle = run_build(
147+
interface,
135148
options,
149+
build_options,
136150
tauri_config,
137151
profile,
138152
&config,
@@ -147,8 +161,11 @@ pub fn command(mut options: Options, noise_level: NoiseLevel) -> Result<()> {
147161
Ok(())
148162
}
149163

164+
#[allow(clippy::too_many_arguments)]
150165
fn run_build(
166+
interface: AppInterface,
151167
mut options: Options,
168+
mut build_options: BuildOptions,
152169
tauri_config: ConfigHandle,
153170
profile: Profile,
154171
config: &AndroidConfig,
@@ -161,16 +178,9 @@ fn run_build(
161178
options.aab = true;
162179
}
163180

164-
let mut build_options: BuildOptions = options.clone().into();
165-
build_options.target = Some(
166-
Target::all()
167-
.get(Target::DEFAULT_KEY)
168-
.unwrap()
169-
.triple
170-
.into(),
171-
);
172-
let interface = crate::build::setup(
181+
crate::build::setup(
173182
tauri_utils::platform::Target::Android,
183+
&interface,
174184
&mut build_options,
175185
true,
176186
)?;

0 commit comments

Comments
 (0)