Browse Source

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>
Amr Bashir 1 year ago
parent
commit
f5f3ed5f6f

+ 6 - 0
.changes/cli-mobile-init-partition.md

@@ -0,0 +1,6 @@
+---
+'tauri-cli': 'patch:bug'
+'@tauri-apps/cli': 'patch:bug'
+---
+
+Fixes Android and iOS project initialization when the Tauri CLI is on a different disk partition.

+ 10 - 2
.github/workflows/test-android.yml

@@ -84,6 +84,14 @@ jobs:
 
       - name: build CLI
         run: cargo build --manifest-path ./tooling/cli/Cargo.toml
+      
+      - name: move CLI to cargo bin dir
+        if: matrix.os != "windows-latest"
+        run: mv ./tooling/cli/target/debug/cargo-tauri $HOME/.cargo/bin
+          
+      - name: move CLI to cargo bin dir
+        if: matrix.os == "windows-latest"
+        run: mv ./tooling/cli/target/debug/cargo-tauri.exe $HOME/.cargo/bin
 
       - name: build Tauri API
         working-directory: ./tooling/api
@@ -95,12 +103,12 @@ jobs:
 
       - name: init Android Studio project
         working-directory: ./examples/api
-        run: ../../tooling/cli/target/debug/cargo-tauri android init
+        run: cargo tauri android init
         env:
           NDK_HOME: ${{ steps.setup-ndk.outputs.ndk-path }}
 
       - name: build APK
         working-directory: ./examples/api
-        run: ../../tooling/cli/target/debug/cargo-tauri android build
+        run: cargo tauri android build
         env:
           NDK_HOME: ${{ steps.setup-ndk.outputs.ndk-path }}

+ 12 - 55
examples/api/src-tauri/Cargo.lock

@@ -679,7 +679,7 @@ checksum = "4ebda144c4fe02d1f7ea1a7d9641b6fc6b580adcfa024ae48797ecdeb6825b4d"
 dependencies = [
  "libc",
  "redox_users",
- "winapi 0.3.9",
+ "winapi",
 ]
 
 [[package]]
@@ -706,7 +706,7 @@ dependencies = [
  "dlopen2_derive",
  "libc",
  "once_cell",
- "winapi 0.3.9",
+ "winapi",
 ]
 
 [[package]]
@@ -1204,7 +1204,7 @@ dependencies = [
  "gobject-sys",
  "libc",
  "system-deps",
- "winapi 0.3.9",
+ "winapi",
 ]
 
 [[package]]
@@ -1613,7 +1613,7 @@ dependencies = [
  "jni-sys",
  "log",
  "thiserror",
- "walkdir 2.4.0",
+ "walkdir",
  "windows-sys 0.45.0",
 ]
 
@@ -1644,16 +1644,6 @@ dependencies = [
  "treediff",
 ]
 
-[[package]]
-name = "kernel32-sys"
-version = "0.2.2"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "7507624b29483431c0ba2d82aece8ca6cdba9382bff4ddd0f7490560c056098d"
-dependencies = [
- "winapi 0.2.8",
- "winapi-build",
-]
-
 [[package]]
 name = "keyboard-types"
 version = "0.7.0"
@@ -1721,7 +1711,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "b67380fd3b2fbe7527a606e18729d21c6f3951633d0500574c4dc22d2d638b9f"
 dependencies = [
  "cfg-if",
- "winapi 0.3.9",
+ "winapi",
 ]
 
 [[package]]
@@ -1958,7 +1948,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "77a8165726e8236064dbb45459242600304b42a5ea24ee2948e18e023bf7ba84"
 dependencies = [
  "overload",
- "winapi 0.3.9",
+ "winapi",
 ]
 
 [[package]]
@@ -2643,16 +2633,6 @@ version = "0.3.3"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "ef703b7cb59335eae2eb93ceb664c0eb7ea6bf567079d843e09420219668e072"
 
-[[package]]
-name = "same-file"
-version = "0.1.3"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "d931a44fdaa43b8637009e7632a02adc4f2b2e0733c08caa4cf00e8da4a117a7"
-dependencies = [
- "kernel32-sys",
- "winapi 0.2.8",
-]
-
 [[package]]
 name = "same-file"
 version = "1.0.6"
@@ -3238,7 +3218,7 @@ dependencies = [
  "tauri-utils",
  "tauri-winres",
  "toml 0.8.2",
- "walkdir 2.4.0",
+ "walkdir",
 ]
 
 [[package]]
@@ -3263,7 +3243,7 @@ dependencies = [
  "time",
  "url",
  "uuid",
- "walkdir 2.4.0",
+ "walkdir",
 ]
 
 [[package]]
@@ -3290,7 +3270,7 @@ dependencies = [
  "serde_json",
  "tauri-utils",
  "toml 0.8.2",
- "walkdir 1.0.7",
+ "walkdir",
 ]
 
 [[package]]
@@ -3372,7 +3352,7 @@ dependencies = [
  "thiserror",
  "toml 0.8.2",
  "url",
- "walkdir 2.4.0",
+ "walkdir",
 ]
 
 [[package]]
@@ -3802,24 +3782,13 @@ dependencies = [
  "libc",
 ]
 
-[[package]]
-name = "walkdir"
-version = "1.0.7"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "bb08f9e670fab86099470b97cd2b252d6527f0b3cc1401acdb595ffc9dd288ff"
-dependencies = [
- "kernel32-sys",
- "same-file 0.1.3",
- "winapi 0.2.8",
-]
-
 [[package]]
 name = "walkdir"
 version = "2.4.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "d71d857dc86794ca4c280d616f7da00d2dbfd8cd788846559a6813e6aa4b54ee"
 dependencies = [
- "same-file 1.0.6",
+ "same-file",
  "winapi-util",
 ]
 
@@ -4062,12 +4031,6 @@ dependencies = [
  "windows-core",
 ]
 
-[[package]]
-name = "winapi"
-version = "0.2.8"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "167dc9d6949a9b857f3451275e911c3f44255842c1f7a76f33c55103a909087a"
-
 [[package]]
 name = "winapi"
 version = "0.3.9"
@@ -4078,12 +4041,6 @@ dependencies = [
  "winapi-x86_64-pc-windows-gnu",
 ]
 
-[[package]]
-name = "winapi-build"
-version = "0.1.1"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "2d315eee3b34aca4797b2da6b13ed88266e6d612562a0c46390af8299fc699bc"
-
 [[package]]
 name = "winapi-i686-pc-windows-gnu"
 version = "0.4.0"
@@ -4096,7 +4053,7 @@ version = "0.1.6"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "f29e6f9198ba0d26b4c9f07dbe6f9ed633e1f3d5b8b414090084349e46a52596"
 dependencies = [
- "winapi 0.3.9",
+ "winapi",
 ]
 
 [[package]]

+ 40 - 71
tooling/cli/src/mobile/init.rs

@@ -4,7 +4,7 @@
 
 use super::{get_app, Target};
 use crate::{
-  helpers::{app_paths::tauri_dir, config::get as get_tauri_config, template::JsonMap},
+  helpers::{config::get as get_tauri_config, template::JsonMap},
   interface::{AppInterface, Interface},
   Result,
 };
@@ -18,17 +18,13 @@ use cargo_mobile2::{
   util::{
     self,
     cli::{Report, TextWrapper},
-    relativize_path,
   },
 };
 use handlebars::{
   Context, Handlebars, Helper, HelperResult, Output, RenderContext, RenderError, RenderErrorReason,
 };
 
-use std::{
-  env::{current_dir, var, var_os},
-  path::PathBuf,
-};
+use std::{env::var_os, path::PathBuf};
 
 pub fn command(
   target: Target,
@@ -87,7 +83,6 @@ pub fn exec(
   #[allow(unused_variables)] reinstall_deps: bool,
   skip_targets_install: bool,
 ) -> Result<App> {
-  let current_dir = current_dir()?;
   let tauri_config = get_tauri_config(target.platform_target(), None)?;
 
   let tauri_config_guard = tauri_config.lock().unwrap();
@@ -97,75 +92,49 @@ pub fn exec(
 
   let (handlebars, mut map) = handlebars(&app);
 
-  // the CWD used when the the IDE runs the android-studio-script or the xcode-script
-  let ide_run_cwd = if target == Target::Android {
-    tauri_dir()
-  } else {
-    tauri_dir().join("gen/apple")
-  };
-
   let mut args = std::env::args_os();
-  let mut binary = args
+
+  let (binary, mut build_args) = args
     .next()
     .map(|bin| {
-      let path = PathBuf::from(&bin);
-      if path.exists() {
-        let absolute_path = util::prefix_path(&current_dir, path);
-        return relativize_path(absolute_path, &ide_run_cwd).into_os_string();
+      let bin_path = PathBuf::from(&bin);
+      let mut build_args = vec!["tauri"];
+
+      if let Some(bin_stem) = bin_path.file_stem() {
+        let r = regex::Regex::new("(nodejs|node)\\-?([1-9]*)*$").unwrap();
+        if r.is_match(&bin_stem.to_string_lossy()) {
+          if let Some(npm_execpath) = var_os("npm_execpath") {
+            let manager_stem = PathBuf::from(&npm_execpath)
+              .file_stem()
+              .unwrap()
+              .to_os_string();
+            let is_npm = manager_stem == "npm-cli";
+            let binary = if is_npm {
+              "npm".into()
+            } else if manager_stem == "npx-cli" {
+              "npx".into()
+            } else {
+              manager_stem
+            };
+
+            if is_npm {
+              build_args.insert(0, "run");
+              build_args.insert(1, "--");
+            }
+
+            return (binary, build_args);
+          }
+        } else if !cfg!(debug_assertions) && bin_stem == "cargo-tauri" {
+          return (std::ffi::OsString::from("cargo"), build_args);
+        }
       }
-      bin
+
+      (bin, build_args)
     })
-    .unwrap_or_else(|| std::ffi::OsString::from("cargo"));
-  let mut build_args = Vec::new();
-  for arg in args {
-    let path = PathBuf::from(&arg);
-    if path.exists() {
-      let absolute_path = util::prefix_path(&current_dir, path);
-      build_args.push(
-        relativize_path(absolute_path, &ide_run_cwd)
-          .to_string_lossy()
-          .into_owned(),
-      );
-      continue;
-    }
-    let is_mobile_cmd_arg = arg == "android" || arg == "ios";
-    build_args.push(arg.to_string_lossy().into_owned());
-    if is_mobile_cmd_arg {
-      break;
-    }
-  }
-  build_args.push(target.ide_build_script_name().into());
-
-  let binary_path = PathBuf::from(&binary);
-  let bin_stem = binary_path.file_stem().unwrap().to_string_lossy();
-  let r = regex::Regex::new("(nodejs|node)\\-?([1-9]*)*$").unwrap();
-  if r.is_match(&bin_stem) {
-    if let Some(npm_execpath) = var_os("npm_execpath").map(PathBuf::from) {
-      let manager_stem = npm_execpath.file_stem().unwrap().to_os_string();
-      let is_npm = manager_stem == "npm-cli";
-      let is_npx = manager_stem == "npx-cli";
-      binary = if is_npm {
-        "npm".into()
-      } else if is_npx {
-        "npx".into()
-      } else {
-        manager_stem
-      };
-      if !(build_args.is_empty() || is_npx) {
-        // remove script path, we'll use `npm_lifecycle_event` instead
-        build_args.remove(0);
-      }
-      if is_npm {
-        build_args.insert(0, "--".into());
-      }
-      if !is_npx {
-        build_args.insert(0, var("npm_lifecycle_event").unwrap());
-      }
-      if is_npm {
-        build_args.insert(0, "run".into());
-      }
-    }
-  }
+    .unwrap_or_else(|| (std::ffi::OsString::from("cargo"), vec!["tauri"]));
+
+  build_args.push(target.command_name());
+  build_args.push(target.ide_build_script_name());
 
   map.insert("tauri-binary", binary.to_string_lossy());
   map.insert("tauri-binary-args", &build_args);