Skip to content

Commit

Permalink
fix(cli): make app_dir() logic consistent (#10418)
Browse files Browse the repository at this point in the history
* fix(cli): Make app_dir() consistent by basing it on the explicit invocation directory rather than the current working directory

* resolve app paths before everything else

* fix xcode script

* fix test

---------

Co-authored-by: Lucas Nogueira <lucas@tauri.app>
  • Loading branch information
samkearney and lucasfernog authored Aug 11, 2024
1 parent 0afee5e commit 2d47352
Show file tree
Hide file tree
Showing 28 changed files with 145 additions and 74 deletions.
6 changes: 6 additions & 0 deletions .changes/cli-make-app-dir-consistent.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"tauri-cli": patch:bug
"@tauri-apps/cli": patch:bug
---

CLI commands will now consistently search for the `app_dir` (the directory containing `package.json`) from the current working directory of the command invocation.
41 changes: 31 additions & 10 deletions examples/api/src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions tooling/cli/src/acl/capability/new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ pub struct Options {
}

pub fn command(options: Options) -> Result<()> {
crate::helpers::app_paths::resolve();

let identifier = match options.identifier {
Some(i) => i,
None => prompts::input("What's the capability identifier?", None, false, false)?.unwrap(),
Expand Down
4 changes: 2 additions & 2 deletions tooling/cli/src/acl/permission/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::path::Path;
use clap::Parser;

use crate::{
helpers::{app_paths::tauri_dir_opt, prompts},
helpers::{app_paths::resolve_tauri_dir, prompts},
Result,
};

Expand Down Expand Up @@ -87,7 +87,7 @@ pub struct Options {
}

pub fn command(options: Options) -> Result<()> {
let dir = match tauri_dir_opt() {
let dir = match resolve_tauri_dir() {
Some(t) => t,
None => std::env::current_dir()?,
};
Expand Down
2 changes: 2 additions & 0 deletions tooling/cli/src/acl/permission/ls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ pub struct Options {
}

pub fn command(options: Options) -> Result<()> {
crate::helpers::app_paths::resolve();

let tauri_dir = tauri_dir();
let acl_manifests_path = tauri_dir
.join("gen")
Expand Down
4 changes: 2 additions & 2 deletions tooling/cli/src/acl/permission/new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use clap::Parser;

use crate::{
acl::FileFormat,
helpers::{app_paths::tauri_dir_opt, prompts},
helpers::{app_paths::resolve_tauri_dir, prompts},
Result,
};

Expand Down Expand Up @@ -69,7 +69,7 @@ pub fn command(options: Options) -> Result<()> {
let path = match options.out {
Some(o) => o.canonicalize()?,
None => {
let dir = match tauri_dir_opt() {
let dir = match resolve_tauri_dir() {
Some(t) => t,
None => std::env::current_dir()?,
};
Expand Down
4 changes: 2 additions & 2 deletions tooling/cli/src/acl/permission/rm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::path::Path;
use clap::Parser;
use tauri_utils::acl::{manifest::PermissionFile, PERMISSION_SCHEMA_FILE_NAME};

use crate::{acl::FileFormat, helpers::app_paths::tauri_dir_opt, Result};
use crate::{acl::FileFormat, helpers::app_paths::resolve_tauri_dir, Result};

fn rm_permission_files(identifier: &str, dir: &Path) -> Result<()> {
for entry in std::fs::read_dir(dir)?.flatten() {
Expand Down Expand Up @@ -126,7 +126,7 @@ pub fn command(options: Options) -> Result<()> {
rm_permission_files(&options.identifier, &permissions_dir)?;
}

if let Some(tauri_dir) = tauri_dir_opt() {
if let Some(tauri_dir) = resolve_tauri_dir() {
let capabilities_dir = tauri_dir.join("capabilities");
if capabilities_dir.exists() {
rm_permission_from_capabilities(&options.identifier, &capabilities_dir)?;
Expand Down
15 changes: 8 additions & 7 deletions tooling/cli/src/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use regex::Regex;
use crate::{
acl,
helpers::{
app_paths::{app_dir, tauri_dir},
app_paths::{resolve_app_dir, tauri_dir},
cargo,
npm::PackageManager,
},
Expand Down Expand Up @@ -92,6 +92,8 @@ pub struct Options {
}

pub fn command(options: Options) -> Result<()> {
crate::helpers::app_paths::resolve();

let (plugin, version) = options
.plugin
.split_once('@')
Expand All @@ -105,6 +107,7 @@ pub fn command(options: Options) -> Result<()> {
let mut plugins = plugins();
let metadata = plugins.remove(plugin).unwrap_or_default();

let app_dir = resolve_app_dir();
let tauri_dir = tauri_dir();

let target_str = metadata
Expand All @@ -122,14 +125,12 @@ pub fn command(options: Options) -> Result<()> {
branch: options.branch.as_deref(),
rev: options.rev.as_deref(),
tag: options.tag.as_deref(),
cwd: Some(&tauri_dir),
cwd: Some(tauri_dir),
target: target_str,
})?;

if !metadata.rust_only {
if let Some(manager) = std::panic::catch_unwind(app_dir)
.map(Some)
.unwrap_or_default()
if let Some(manager) = app_dir
.map(PackageManager::from_project)
.and_then(|managers| managers.into_iter().next())
{
Expand All @@ -149,7 +150,7 @@ pub fn command(options: Options) -> Result<()> {
(None, None, None, None) => npm_name,
_ => anyhow::bail!("Only one of --tag, --rev and --branch can be specified"),
};
manager.install(&[npm_spec], &tauri_dir)?;
manager.install(&[npm_spec], tauri_dir)?;
}

let _ = acl::permission::add::command(acl::permission::add::Options {
Expand Down Expand Up @@ -193,7 +194,7 @@ pub fn command(options: Options) -> Result<()> {
log::info!("Running `cargo fmt`...");
let _ = Command::new("cargo")
.arg("fmt")
.current_dir(&tauri_dir)
.current_dir(tauri_dir)
.status();
}

Expand Down
2 changes: 2 additions & 0 deletions tooling/cli/src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ pub struct Options {
}

pub fn command(mut options: Options, verbosity: u8) -> Result<()> {
crate::helpers::app_paths::resolve();

let ci = options.ci;

let target = options
Expand Down
3 changes: 3 additions & 0 deletions tooling/cli/src/bundle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ impl From<crate::build::Options> for Options {
}

pub fn command(options: Options, verbosity: u8) -> crate::Result<()> {
crate::helpers::app_paths::resolve();

let ci = options.ci;

let target = options
Expand Down Expand Up @@ -133,6 +135,7 @@ pub fn command(options: Options, verbosity: u8) -> crate::Result<()> {
)
}

#[allow(clippy::too_many_arguments)]
pub fn bundle<A: AppSettings>(
options: &Options,
verbosity: u8,
Expand Down
2 changes: 2 additions & 0 deletions tooling/cli/src/dev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ pub struct Options {
}

pub fn command(options: Options) -> Result<()> {
crate::helpers::app_paths::resolve();

let r = command_internal(options);
if r.is_err() {
kill_before_dev_process();
Expand Down
26 changes: 18 additions & 8 deletions tooling/cli/src/helpers/app_paths.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ use tauri_utils::{
};

const TAURI_GITIGNORE: &[u8] = include_bytes!("../../tauri.gitignore");
static APP_DIR: OnceLock<PathBuf> = OnceLock::new();
static TAURI_DIR: OnceLock<PathBuf> = OnceLock::new();

pub fn walk_builder(path: &Path) -> WalkBuilder {
let mut default_gitignore = std::env::temp_dir();
Expand Down Expand Up @@ -66,7 +68,7 @@ fn lookup<F: Fn(&PathBuf) -> bool>(dir: &Path, checker: F) -> Option<PathBuf> {
None
}

pub fn tauri_dir_opt() -> Option<PathBuf> {
pub fn resolve_tauri_dir() -> Option<PathBuf> {
let Ok(cwd) = current_dir() else {
return None;
};
Expand Down Expand Up @@ -100,19 +102,27 @@ pub fn tauri_dir_opt() -> Option<PathBuf> {
})
}

pub fn tauri_dir() -> PathBuf {
tauri_dir_opt().unwrap_or_else(||
pub fn resolve() {
TAURI_DIR.set(resolve_tauri_dir().unwrap_or_else(||
panic!("Couldn't recognize the current folder as a Tauri project. It must contain a `{}`, `{}` or `{}` file in any subfolder.",
ConfigFormat::Json.into_file_name(),
ConfigFormat::Json5.into_file_name(),
ConfigFormat::Toml.into_file_name()
)
)
)).expect("tauri dir already resolved");
APP_DIR
.set(resolve_app_dir().unwrap_or_else(|| tauri_dir().parent().unwrap().to_path_buf()))
.expect("app dir already resolved");
}

fn get_app_dir() -> Option<PathBuf> {
let cwd = current_dir().expect("failed to read cwd");
pub fn tauri_dir() -> &'static PathBuf {
TAURI_DIR
.get()
.expect("app paths not initialized, this is a Tauri CLI bug")
}

pub fn resolve_app_dir() -> Option<PathBuf> {
let cwd = current_dir().expect("failed to read cwd");
if cwd.join("package.json").exists() {
return Some(cwd);
}
Expand All @@ -128,7 +138,7 @@ fn get_app_dir() -> Option<PathBuf> {
}

pub fn app_dir() -> &'static PathBuf {
static APP_DIR: OnceLock<PathBuf> = OnceLock::new();
APP_DIR
.get_or_init(|| get_app_dir().unwrap_or_else(|| tauri_dir().parent().unwrap().to_path_buf()))
.get()
.expect("app paths not initialized, this is a Tauri CLI bug")
}
2 changes: 1 addition & 1 deletion tooling/cli/src/helpers/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ fn get_internal(
let mut extensions = HashMap::new();

if let Some((platform_config, config_path)) =
tauri_utils::config::parse::read_platform(target, tauri_dir)?
tauri_utils::config::parse::read_platform(target, tauri_dir.to_path_buf())?
{
merge(&mut config, &platform_config);
extensions.insert(
Expand Down
5 changes: 4 additions & 1 deletion tooling/cli/src/icon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,10 @@ impl Source {

pub fn command(options: Options) -> Result<()> {
let input = options.input;
let out_dir = options.output.unwrap_or_else(|| tauri_dir().join("icons"));
let out_dir = options.output.unwrap_or_else(|| {
crate::helpers::app_paths::resolve();
tauri_dir().join("icons")
});
let png_icon_sizes = options.png.unwrap_or_default();
let ios_color = css_color::Srgb::from_str(&options.ios_color)
.map(|color| {
Expand Down
Loading

0 comments on commit 2d47352

Please sign in to comment.