Skip to content

Commit 100d9ed

Browse files
i-c-blucasfernog
andauthored
fix(core): Correctly detect Android Tauri configuration file, closes #7785 (#7802)
Co-authored-by: Lucas Nogueira <lucas@tauri.studio>
1 parent 3c66a53 commit 100d9ed

File tree

27 files changed

+269
-165
lines changed

27 files changed

+269
-165
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"tauri-cli": patch:bug
3+
"@tauri-apps/cli": patch:bug
4+
---
5+
6+
Properly read platform-specific configuration files for mobile targets.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"tauri-codegen": patch:enhance
3+
"tauri-macros": patch:enhance
4+
---
5+
6+
Use `Target` enum from `tauri_utils::platform`.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"tauri-utils": patch:breaking
3+
---
4+
5+
Follow file name conventions set by desktop for mobile Tauri configuration files. Added `target` argument on most `config::parse` methods.

core/tauri-build/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,7 @@ pub fn try_build(attributes: Attributes) -> Result<()> {
294294
cfg_alias("mobile", mobile);
295295

296296
let mut config = serde_json::from_value(tauri_utils::config::parse::read_from(
297+
tauri_utils::platform::Target::from_triple(&std::env::var("TARGET").unwrap()),
297298
std::env::current_dir().unwrap(),
298299
)?)?;
299300
if let Ok(env) = std::env::var("TAURI_CONFIG") {

core/tauri-codegen/src/context.rs

Lines changed: 6 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use tauri_utils::config::{AppUrl, Config, PatternKind, WindowUrl};
1515
use tauri_utils::html::{
1616
inject_nonce_token, parse as parse_html, serialize_node as serialize_html_node,
1717
};
18+
use tauri_utils::platform::Target;
1819

1920
use crate::embedded_assets::{AssetOptions, CspHashes, EmbeddedAssets, EmbeddedAssetsError};
2021

@@ -112,26 +113,6 @@ fn map_isolation(
112113
}
113114
}
114115

115-
#[derive(PartialEq, Eq, Clone, Copy)]
116-
enum Target {
117-
Linux,
118-
Windows,
119-
Darwin,
120-
Android,
121-
// iOS.
122-
Ios,
123-
}
124-
125-
impl Target {
126-
fn is_mobile(&self) -> bool {
127-
matches!(self, Target::Android | Target::Ios)
128-
}
129-
130-
fn is_desktop(&self) -> bool {
131-
!self.is_mobile()
132-
}
133-
}
134-
135116
/// Build a `tauri::Context` for including in application code.
136117
pub fn context_codegen(data: ContextData) -> Result<TokenStream, EmbeddedAssetsError> {
137118
let ContextData {
@@ -141,34 +122,11 @@ pub fn context_codegen(data: ContextData) -> Result<TokenStream, EmbeddedAssetsE
141122
root,
142123
} = data;
143124

144-
let target =
145-
if let Ok(target) = std::env::var("TARGET").or_else(|_| std::env::var("TAURI_TARGET_TRIPLE")) {
146-
if target.contains("unknown-linux") {
147-
Target::Linux
148-
} else if target.contains("pc-windows") {
149-
Target::Windows
150-
} else if target.contains("apple-darwin") {
151-
Target::Darwin
152-
} else if target.contains("android") {
153-
Target::Android
154-
} else if target.contains("apple-ios") {
155-
Target::Ios
156-
} else {
157-
panic!("unknown codegen target {target}");
158-
}
159-
} else if cfg!(target_os = "linux") {
160-
Target::Linux
161-
} else if cfg!(windows) {
162-
Target::Windows
163-
} else if cfg!(target_os = "macos") {
164-
Target::Darwin
165-
} else if cfg!(target_os = "android") {
166-
Target::Android
167-
} else if cfg!(target_os = "ios") {
168-
Target::Ios
169-
} else {
170-
panic!("unknown codegen target")
171-
};
125+
let target = std::env::var("TARGET")
126+
.or_else(|_| std::env::var("TAURI_TARGET_TRIPLE"))
127+
.as_deref()
128+
.map(Target::from_triple)
129+
.unwrap_or_else(|_| Target::current());
172130

173131
let mut options = AssetOptions::new(config.tauri.pattern.clone())
174132
.freeze_prototype(config.tauri.security.freeze_prototype)

core/tauri-codegen/src/lib.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,10 @@ pub fn get_config(path: &Path) -> Result<(Config, PathBuf), CodegenConfigError>
6767
// it is impossible for the content of two separate configs to get mixed up. The chances are
6868
// already unlikely unless the developer goes out of their way to run the cli on a different
6969
// project than the target crate.
70-
let mut config = serde_json::from_value(tauri_utils::config::parse::read_from(parent.clone())?)?;
70+
let mut config = serde_json::from_value(tauri_utils::config::parse::read_from(
71+
tauri_utils::platform::Target::current(),
72+
parent.clone(),
73+
)?)?;
7174
if let Ok(env) = std::env::var("TAURI_CONFIG") {
7275
let merge_config: serde_json::Value =
7376
serde_json::from_str(&env).map_err(CodegenConfigError::FormatInline)?;

core/tauri-macros/src/context.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use syn::{
1111
LitStr, PathArguments, PathSegment, Token,
1212
};
1313
use tauri_codegen::{context_codegen, get_config, ContextData};
14-
use tauri_utils::config::parse::does_supported_file_name_exist;
14+
use tauri_utils::{config::parse::does_supported_file_name_exist, platform::Target};
1515

1616
pub(crate) struct ContextItems {
1717
config_file: PathBuf,
@@ -20,6 +20,12 @@ pub(crate) struct ContextItems {
2020

2121
impl Parse for ContextItems {
2222
fn parse(input: &ParseBuffer<'_>) -> syn::parse::Result<Self> {
23+
let target = std::env::var("TARGET")
24+
.or_else(|_| std::env::var("TAURI_TARGET_TRIPLE"))
25+
.as_deref()
26+
.map(Target::from_triple)
27+
.unwrap_or_else(|_| Target::current());
28+
2329
let config_file = if input.is_empty() {
2430
std::env::var("CARGO_MANIFEST_DIR").map(|m| PathBuf::from(m).join("tauri.conf.json"))
2531
} else {
@@ -36,7 +42,7 @@ impl Parse for ContextItems {
3642
VarError::NotUnicode(_) => "CARGO_MANIFEST_DIR env var contained invalid utf8".into(),
3743
})
3844
.and_then(|path| {
39-
if does_supported_file_name_exist(&path) {
45+
if does_supported_file_name_exist(target, &path) {
4046
Ok(path)
4147
} else {
4248
Err(format!(

core/tauri-utils/src/config/parse.rs

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

55
use crate::config::Config;
6+
use crate::platform::Target;
67
use json_patch::merge;
78
use serde::de::DeserializeOwned;
89
use serde_json::Value;
@@ -47,47 +48,29 @@ impl ConfigFormat {
4748
}
4849
}
4950

50-
fn into_platform_file_name(self) -> &'static str {
51+
fn into_platform_file_name(self, target: Target) -> &'static str {
5152
match self {
52-
Self::Json => {
53-
if cfg!(target_os = "macos") {
54-
"tauri.macos.conf.json"
55-
} else if cfg!(windows) {
56-
"tauri.windows.conf.json"
57-
} else if cfg!(target_os = "android") {
58-
"tauri.android.conf.json"
59-
} else if cfg!(target_os = "ios") {
60-
"tauri.ios.conf.json"
61-
} else {
62-
"tauri.linux.conf.json"
63-
}
64-
}
65-
Self::Json5 => {
66-
if cfg!(target_os = "macos") {
67-
"tauri.macos.conf.json5"
68-
} else if cfg!(windows) {
69-
"tauri.windows.conf.json5"
70-
} else if cfg!(target_os = "android") {
71-
"tauri.android.conf.json"
72-
} else if cfg!(target_os = "ios") {
73-
"tauri.ios.conf.json"
74-
} else {
75-
"tauri.linux.conf.json5"
76-
}
77-
}
78-
Self::Toml => {
79-
if cfg!(target_os = "macos") {
80-
"Tauri.macos.toml"
81-
} else if cfg!(windows) {
82-
"Tauri.windows.toml"
83-
} else if cfg!(target_os = "android") {
84-
"tauri.android.toml"
85-
} else if cfg!(target_os = "ios") {
86-
"tauri.ios.toml"
87-
} else {
88-
"Tauri.linux.toml"
89-
}
90-
}
53+
Self::Json => match target {
54+
Target::Darwin => "tauri.macos.conf.json",
55+
Target::Windows => "tauri.windows.conf.json",
56+
Target::Linux => "tauri.linux.conf.json",
57+
Target::Android => "tauri.android.conf.json",
58+
Target::Ios => "tauri.ios.conf.json",
59+
},
60+
Self::Json5 => match target {
61+
Target::Darwin => "tauri.macos.conf.json5",
62+
Target::Windows => "tauri.windows.conf.json5",
63+
Target::Linux => "tauri.linux.conf.json5",
64+
Target::Android => "tauri.android.conf.json5",
65+
Target::Ios => "tauri.ios.conf.json5",
66+
},
67+
Self::Toml => match target {
68+
Target::Darwin => "Tauri.macos.toml",
69+
Target::Windows => "Tauri.windows.toml",
70+
Target::Linux => "Tauri.linux.toml",
71+
Target::Android => "Tauri.android.toml",
72+
Target::Ios => "Tauri.ios.toml",
73+
},
9174
}
9275
}
9376
}
@@ -154,28 +137,28 @@ pub enum ConfigError {
154137
}
155138

156139
/// Determines if the given folder has a configuration file.
157-
pub fn folder_has_configuration_file(folder: &Path) -> bool {
140+
pub fn folder_has_configuration_file(target: Target, folder: &Path) -> bool {
158141
folder.join(ConfigFormat::Json.into_file_name()).exists()
159142
|| folder.join(ConfigFormat::Json5.into_file_name()).exists()
160143
|| folder.join(ConfigFormat::Toml.into_file_name()).exists()
161144
// platform file names
162-
|| folder.join(ConfigFormat::Json.into_platform_file_name()).exists()
163-
|| folder.join(ConfigFormat::Json5.into_platform_file_name()).exists()
164-
|| folder.join(ConfigFormat::Toml.into_platform_file_name()).exists()
145+
|| folder.join(ConfigFormat::Json.into_platform_file_name(target)).exists()
146+
|| folder.join(ConfigFormat::Json5.into_platform_file_name(target)).exists()
147+
|| folder.join(ConfigFormat::Toml.into_platform_file_name(target)).exists()
165148
}
166149

167150
/// Determines if the given file path represents a Tauri configuration file.
168-
pub fn is_configuration_file(path: &Path) -> bool {
151+
pub fn is_configuration_file(target: Target, path: &Path) -> bool {
169152
path
170153
.file_name()
171154
.map(|file_name| {
172155
file_name == OsStr::new(ConfigFormat::Json.into_file_name())
173156
|| file_name == OsStr::new(ConfigFormat::Json5.into_file_name())
174157
|| file_name == OsStr::new(ConfigFormat::Toml.into_file_name())
175158
// platform file names
176-
|| file_name == OsStr::new(ConfigFormat::Json.into_platform_file_name())
177-
|| file_name == OsStr::new(ConfigFormat::Json5.into_platform_file_name())
178-
|| file_name == OsStr::new(ConfigFormat::Toml.into_platform_file_name())
159+
|| file_name == OsStr::new(ConfigFormat::Json.into_platform_file_name(target))
160+
|| file_name == OsStr::new(ConfigFormat::Json5.into_platform_file_name(target))
161+
|| file_name == OsStr::new(ConfigFormat::Toml.into_platform_file_name(target))
179162
})
180163
.unwrap_or_default()
181164
}
@@ -192,9 +175,9 @@ pub fn is_configuration_file(path: &Path) -> bool {
192175
/// Merging the configurations using [JSON Merge Patch (RFC 7396)].
193176
///
194177
/// [JSON Merge Patch (RFC 7396)]: https://datatracker.ietf.org/doc/html/rfc7396.
195-
pub fn read_from(root_dir: PathBuf) -> Result<Value, ConfigError> {
196-
let mut config: Value = parse_value(root_dir.join("tauri.conf.json"))?.0;
197-
if let Some((platform_config, _)) = read_platform(root_dir)? {
178+
pub fn read_from(target: Target, root_dir: PathBuf) -> Result<Value, ConfigError> {
179+
let mut config: Value = parse_value(target, root_dir.join("tauri.conf.json"))?.0;
180+
if let Some((platform_config, _)) = read_platform(target, root_dir)? {
198181
merge(&mut config, &platform_config);
199182
}
200183
Ok(config)
@@ -203,10 +186,13 @@ pub fn read_from(root_dir: PathBuf) -> Result<Value, ConfigError> {
203186
/// Reads the platform-specific configuration file from the given root directory if it exists.
204187
///
205188
/// Check [`read_from`] for more information.
206-
pub fn read_platform(root_dir: PathBuf) -> Result<Option<(Value, PathBuf)>, ConfigError> {
207-
let platform_config_path = root_dir.join(ConfigFormat::Json.into_platform_file_name());
208-
if does_supported_file_name_exist(&platform_config_path) {
209-
let (platform_config, path): (Value, PathBuf) = parse_value(platform_config_path)?;
189+
pub fn read_platform(
190+
target: Target,
191+
root_dir: PathBuf,
192+
) -> Result<Option<(Value, PathBuf)>, ConfigError> {
193+
let platform_config_path = root_dir.join(ConfigFormat::Json.into_platform_file_name(target));
194+
if does_supported_file_name_exist(target, &platform_config_path) {
195+
let (platform_config, path): (Value, PathBuf) = parse_value(target, platform_config_path)?;
210196
Ok(Some((platform_config, path)))
211197
} else {
212198
Ok(None)
@@ -217,16 +203,16 @@ pub fn read_platform(root_dir: PathBuf) -> Result<Option<(Value, PathBuf)>, Conf
217203
///
218204
/// The passed path is expected to be the path to the "default" configuration format, in this case
219205
/// JSON with `.json`.
220-
pub fn does_supported_file_name_exist(path: impl Into<PathBuf>) -> bool {
206+
pub fn does_supported_file_name_exist(target: Target, path: impl Into<PathBuf>) -> bool {
221207
let path = path.into();
222208
let source_file_name = path.file_name().unwrap().to_str().unwrap();
223209
let lookup_platform_config = ENABLED_FORMATS
224210
.iter()
225-
.any(|format| source_file_name == format.into_platform_file_name());
211+
.any(|format| source_file_name == format.into_platform_file_name(target));
226212
ENABLED_FORMATS.iter().any(|format| {
227213
path
228214
.with_file_name(if lookup_platform_config {
229-
format.into_platform_file_name()
215+
format.into_platform_file_name(target)
230216
} else {
231217
format.into_file_name()
232218
})
@@ -248,31 +234,37 @@ pub fn does_supported_file_name_exist(path: impl Into<PathBuf>) -> bool {
248234
/// a. Parse it with `toml`
249235
/// b. Return error if all above steps failed
250236
/// 4. Return error if all above steps failed
251-
pub fn parse(path: impl Into<PathBuf>) -> Result<(Config, PathBuf), ConfigError> {
252-
do_parse(path.into())
237+
pub fn parse(target: Target, path: impl Into<PathBuf>) -> Result<(Config, PathBuf), ConfigError> {
238+
do_parse(target, path.into())
253239
}
254240

255241
/// See [`parse`] for specifics, returns a JSON [`Value`] instead of [`Config`].
256-
pub fn parse_value(path: impl Into<PathBuf>) -> Result<(Value, PathBuf), ConfigError> {
257-
do_parse(path.into())
242+
pub fn parse_value(
243+
target: Target,
244+
path: impl Into<PathBuf>,
245+
) -> Result<(Value, PathBuf), ConfigError> {
246+
do_parse(target, path.into())
258247
}
259248

260-
fn do_parse<D: DeserializeOwned>(path: PathBuf) -> Result<(D, PathBuf), ConfigError> {
249+
fn do_parse<D: DeserializeOwned>(
250+
target: Target,
251+
path: PathBuf,
252+
) -> Result<(D, PathBuf), ConfigError> {
261253
let file_name = path
262254
.file_name()
263255
.map(OsStr::to_string_lossy)
264256
.unwrap_or_default();
265257
let lookup_platform_config = ENABLED_FORMATS
266258
.iter()
267-
.any(|format| file_name == format.into_platform_file_name());
259+
.any(|format| file_name == format.into_platform_file_name(target));
268260

269261
let json5 = path.with_file_name(if lookup_platform_config {
270-
ConfigFormat::Json5.into_platform_file_name()
262+
ConfigFormat::Json5.into_platform_file_name(target)
271263
} else {
272264
ConfigFormat::Json5.into_file_name()
273265
});
274266
let toml = path.with_file_name(if lookup_platform_config {
275-
ConfigFormat::Toml.into_platform_file_name()
267+
ConfigFormat::Toml.into_platform_file_name(target)
276268
} else {
277269
ConfigFormat::Toml.into_file_name()
278270
});

0 commit comments

Comments
 (0)