Skip to content

Commit f5f3ed5

Browse files
fix(cli): CLI path issues on mobile project initialization (#9009)
* fix(cli): fix panic when `android init` using cargo or yarn closes #8531 * clippy * try with fullpath * clippy * move cli * Update test-android.yml * add to path instead * clippy * try moving * use cargo subcommand * delete unused logic [skip ci] * truncate on init [skip ci] * enhance binary/args check * update change files --------- Co-authored-by: Lucas Nogueira <lucas@tauri.studio>
1 parent cb92cfd commit f5f3ed5

File tree

4 files changed

+68
-128
lines changed

4 files changed

+68
-128
lines changed

.changes/cli-mobile-init-partition.md

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+
Fixes Android and iOS project initialization when the Tauri CLI is on a different disk partition.

.github/workflows/test-android.yml

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,14 @@ jobs:
8484
8585
- name: build CLI
8686
run: cargo build --manifest-path ./tooling/cli/Cargo.toml
87+
88+
- name: move CLI to cargo bin dir
89+
if: matrix.os != "windows-latest"
90+
run: mv ./tooling/cli/target/debug/cargo-tauri $HOME/.cargo/bin
91+
92+
- name: move CLI to cargo bin dir
93+
if: matrix.os == "windows-latest"
94+
run: mv ./tooling/cli/target/debug/cargo-tauri.exe $HOME/.cargo/bin
8795

8896
- name: build Tauri API
8997
working-directory: ./tooling/api
@@ -95,12 +103,12 @@ jobs:
95103

96104
- name: init Android Studio project
97105
working-directory: ./examples/api
98-
run: ../../tooling/cli/target/debug/cargo-tauri android init
106+
run: cargo tauri android init
99107
env:
100108
NDK_HOME: ${{ steps.setup-ndk.outputs.ndk-path }}
101109

102110
- name: build APK
103111
working-directory: ./examples/api
104-
run: ../../tooling/cli/target/debug/cargo-tauri android build
112+
run: cargo tauri android build
105113
env:
106114
NDK_HOME: ${{ steps.setup-ndk.outputs.ndk-path }}

examples/api/src-tauri/Cargo.lock

Lines changed: 12 additions & 55 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tooling/cli/src/mobile/init.rs

Lines changed: 40 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use super::{get_app, Target};
66
use crate::{
7-
helpers::{app_paths::tauri_dir, config::get as get_tauri_config, template::JsonMap},
7+
helpers::{config::get as get_tauri_config, template::JsonMap},
88
interface::{AppInterface, Interface},
99
Result,
1010
};
@@ -18,17 +18,13 @@ use cargo_mobile2::{
1818
util::{
1919
self,
2020
cli::{Report, TextWrapper},
21-
relativize_path,
2221
},
2322
};
2423
use handlebars::{
2524
Context, Handlebars, Helper, HelperResult, Output, RenderContext, RenderError, RenderErrorReason,
2625
};
2726

28-
use std::{
29-
env::{current_dir, var, var_os},
30-
path::PathBuf,
31-
};
27+
use std::{env::var_os, path::PathBuf};
3228

3329
pub fn command(
3430
target: Target,
@@ -87,7 +83,6 @@ pub fn exec(
8783
#[allow(unused_variables)] reinstall_deps: bool,
8884
skip_targets_install: bool,
8985
) -> Result<App> {
90-
let current_dir = current_dir()?;
9186
let tauri_config = get_tauri_config(target.platform_target(), None)?;
9287

9388
let tauri_config_guard = tauri_config.lock().unwrap();
@@ -97,75 +92,49 @@ pub fn exec(
9792

9893
let (handlebars, mut map) = handlebars(&app);
9994

100-
// the CWD used when the the IDE runs the android-studio-script or the xcode-script
101-
let ide_run_cwd = if target == Target::Android {
102-
tauri_dir()
103-
} else {
104-
tauri_dir().join("gen/apple")
105-
};
106-
10795
let mut args = std::env::args_os();
108-
let mut binary = args
96+
97+
let (binary, mut build_args) = args
10998
.next()
11099
.map(|bin| {
111-
let path = PathBuf::from(&bin);
112-
if path.exists() {
113-
let absolute_path = util::prefix_path(&current_dir, path);
114-
return relativize_path(absolute_path, &ide_run_cwd).into_os_string();
100+
let bin_path = PathBuf::from(&bin);
101+
let mut build_args = vec!["tauri"];
102+
103+
if let Some(bin_stem) = bin_path.file_stem() {
104+
let r = regex::Regex::new("(nodejs|node)\\-?([1-9]*)*$").unwrap();
105+
if r.is_match(&bin_stem.to_string_lossy()) {
106+
if let Some(npm_execpath) = var_os("npm_execpath") {
107+
let manager_stem = PathBuf::from(&npm_execpath)
108+
.file_stem()
109+
.unwrap()
110+
.to_os_string();
111+
let is_npm = manager_stem == "npm-cli";
112+
let binary = if is_npm {
113+
"npm".into()
114+
} else if manager_stem == "npx-cli" {
115+
"npx".into()
116+
} else {
117+
manager_stem
118+
};
119+
120+
if is_npm {
121+
build_args.insert(0, "run");
122+
build_args.insert(1, "--");
123+
}
124+
125+
return (binary, build_args);
126+
}
127+
} else if !cfg!(debug_assertions) && bin_stem == "cargo-tauri" {
128+
return (std::ffi::OsString::from("cargo"), build_args);
129+
}
115130
}
116-
bin
131+
132+
(bin, build_args)
117133
})
118-
.unwrap_or_else(|| std::ffi::OsString::from("cargo"));
119-
let mut build_args = Vec::new();
120-
for arg in args {
121-
let path = PathBuf::from(&arg);
122-
if path.exists() {
123-
let absolute_path = util::prefix_path(&current_dir, path);
124-
build_args.push(
125-
relativize_path(absolute_path, &ide_run_cwd)
126-
.to_string_lossy()
127-
.into_owned(),
128-
);
129-
continue;
130-
}
131-
let is_mobile_cmd_arg = arg == "android" || arg == "ios";
132-
build_args.push(arg.to_string_lossy().into_owned());
133-
if is_mobile_cmd_arg {
134-
break;
135-
}
136-
}
137-
build_args.push(target.ide_build_script_name().into());
138-
139-
let binary_path = PathBuf::from(&binary);
140-
let bin_stem = binary_path.file_stem().unwrap().to_string_lossy();
141-
let r = regex::Regex::new("(nodejs|node)\\-?([1-9]*)*$").unwrap();
142-
if r.is_match(&bin_stem) {
143-
if let Some(npm_execpath) = var_os("npm_execpath").map(PathBuf::from) {
144-
let manager_stem = npm_execpath.file_stem().unwrap().to_os_string();
145-
let is_npm = manager_stem == "npm-cli";
146-
let is_npx = manager_stem == "npx-cli";
147-
binary = if is_npm {
148-
"npm".into()
149-
} else if is_npx {
150-
"npx".into()
151-
} else {
152-
manager_stem
153-
};
154-
if !(build_args.is_empty() || is_npx) {
155-
// remove script path, we'll use `npm_lifecycle_event` instead
156-
build_args.remove(0);
157-
}
158-
if is_npm {
159-
build_args.insert(0, "--".into());
160-
}
161-
if !is_npx {
162-
build_args.insert(0, var("npm_lifecycle_event").unwrap());
163-
}
164-
if is_npm {
165-
build_args.insert(0, "run".into());
166-
}
167-
}
168-
}
134+
.unwrap_or_else(|| (std::ffi::OsString::from("cargo"), vec!["tauri"]));
135+
136+
build_args.push(target.command_name());
137+
build_args.push(target.ide_build_script_name());
169138

170139
map.insert("tauri-binary", binary.to_string_lossy());
171140
map.insert("tauri-binary-args", &build_args);

0 commit comments

Comments
 (0)