Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changes/additional-watch-folders.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"tauri-cli": minor:feat
"@tauri-apps/cli": minor:feat
"tauri-utils": minor:feat
---

Introduces a new configuration option that allows you to specify custom folders to watch for changes when running `tauri dev`.
9 changes: 9 additions & 0 deletions crates/tauri-cli/config.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
"build": {
"description": "The build configuration.",
"default": {
"additionalWatchFolders": [],
"removeUnusedCommands": false
},
"allOf": [
Expand Down Expand Up @@ -1880,6 +1881,14 @@
"description": "Try to remove unused commands registered from plugins base on the ACL list during `tauri build`,\n the way it works is that tauri-cli will read this and set the environment variables for the build script and macros,\n and they'll try to get all the allowed commands and remove the rest\n\n Note:\n - This won't be accounting for dynamically added ACLs when you use features from the `dynamic-acl` (currently enabled by default) feature flag, so make sure to check it when using this\n - This feature requires tauri-plugin 2.1 and tauri 2.4",
"default": false,
"type": "boolean"
},
"additionalWatchFolders": {
"description": "Additional paths to watch for changes when running `tauri dev`.",
"default": [],
"type": "array",
"items": {
"type": "string"
}
}
},
"additionalProperties": false
Expand Down
17 changes: 17 additions & 0 deletions crates/tauri-cli/src/dev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use tauri_utils::{config::RunnerConfig, platform::Target};
use std::{
env::set_current_dir,
net::{IpAddr, Ipv4Addr},
path::PathBuf,
process::{exit, Command, Stdio},
sync::{
atomic::{AtomicBool, Ordering},
Expand Down Expand Up @@ -81,6 +82,9 @@ pub struct Options {
/// Disable the file watcher.
#[clap(long)]
pub no_watch: bool,
/// Additional paths to watch for changes.
#[clap(long)]
pub additional_watch_folders: Vec<PathBuf>,

/// Disable the built-in dev server for static files.
#[clap(long)]
Expand Down Expand Up @@ -336,6 +340,19 @@ pub fn setup(interface: &AppInterface, options: &mut Options, config: ConfigHand
}
}

if options.additional_watch_folders.is_empty() {
options.additional_watch_folders.extend(
config
.lock()
.unwrap()
.as_ref()
.unwrap()
.build
.additional_watch_folders
.clone(),
);
}

Ok(())
}

Expand Down
32 changes: 28 additions & 4 deletions crates/tauri-cli/src/interface/rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use std::{
};

use anyhow::Context;
use dunce::canonicalize;
use glob::glob;
use ignore::gitignore::{Gitignore, GitignoreBuilder};
use notify::RecursiveMode;
Expand Down Expand Up @@ -54,6 +55,7 @@ pub struct Options {
pub args: Vec<String>,
pub config: Vec<ConfigValue>,
pub no_watch: bool,
pub additional_watch_folders: Vec<PathBuf>,
}

impl From<crate::build::Options> for Options {
Expand All @@ -66,6 +68,7 @@ impl From<crate::build::Options> for Options {
args: options.args,
config: options.config,
no_watch: true,
additional_watch_folders: Vec::new(),
}
}
}
Expand Down Expand Up @@ -93,6 +96,7 @@ impl From<crate::dev::Options> for Options {
args: options.args,
config: options.config,
no_watch: options.no_watch,
additional_watch_folders: options.additional_watch_folders,
}
}
}
Expand All @@ -104,6 +108,7 @@ pub struct MobileOptions {
pub args: Vec<String>,
pub config: Vec<ConfigValue>,
pub no_watch: bool,
pub additional_watch_folders: Vec<PathBuf>,
}

#[derive(Debug)]
Expand Down Expand Up @@ -209,7 +214,7 @@ impl Interface for Rust {
on_exit(status, reason)
})
});
self.run_dev_watcher(&merge_configs, run)
self.run_dev_watcher(&options.additional_watch_folders, &merge_configs, run)
}
}

Expand All @@ -233,7 +238,7 @@ impl Interface for Rust {
} else {
let merge_configs = options.config.iter().map(|c| &c.0).collect::<Vec<_>>();
let run = Arc::new(|_rust: &mut Rust| runner(options.clone()));
self.run_dev_watcher(&merge_configs, run)
self.run_dev_watcher(&options.additional_watch_folders, &merge_configs, run)
}
}

Expand Down Expand Up @@ -411,13 +416,31 @@ fn expand_member_path(path: &Path) -> crate::Result<Vec<PathBuf>> {
Ok(res)
}

fn get_watch_folders() -> crate::Result<Vec<PathBuf>> {
fn get_watch_folders(additional_watch_folders: &[PathBuf]) -> crate::Result<Vec<PathBuf>> {
let tauri_path = tauri_dir();
let workspace_path = get_workspace_dir()?;

// We always want to watch the main tauri folder.
let mut watch_folders = vec![tauri_path.to_path_buf()];

// Add the additional watch folders, resolving the path from the tauri path if it is relative
watch_folders.extend(additional_watch_folders.iter().filter_map(|dir| {
let path = if dir.is_absolute() {
dir.to_owned()
} else {
tauri_path.join(dir)
Comment thread
pepperoni505 marked this conversation as resolved.
Resolved
};

let canonicalized = canonicalize(&path).ok();
if canonicalized.is_none() {
log::warn!(
"Additional watch folder '{}' not found, ignoring",
path.display()
);
}
canonicalized
}));

// We also try to watch workspace members, no matter if the tauri cargo project is the workspace root or a workspace member
let cargo_settings = CargoSettings::load(&workspace_path)?;
if let Some(members) = cargo_settings.workspace.and_then(|w| w.members) {
Expand Down Expand Up @@ -480,6 +503,7 @@ impl Rust {

fn run_dev_watcher<F: Fn(&mut Rust) -> crate::Result<Box<dyn DevProcess + Send>>>(
&mut self,
additional_watch_folders: &[PathBuf],
merge_configs: &[&serde_json::Value],
run: Arc<F>,
) -> crate::Result<()> {
Expand All @@ -489,7 +513,7 @@ impl Rust {
let (tx, rx) = sync_channel(1);
let frontend_path = frontend_dir();

let watch_folders = get_watch_folders()?;
let watch_folders = get_watch_folders(additional_watch_folders)?;

let common_ancestor = common_path::common_path_all(watch_folders.iter().map(Path::new))
.expect("watch_folders should not be empty");
Expand Down
7 changes: 6 additions & 1 deletion crates/tauri-cli/src/mobile/android/dev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ use cargo_mobile2::{
target::TargetTrait,
};

use std::env::set_current_dir;
use std::{env::set_current_dir, path::PathBuf};

#[derive(Debug, Clone, Parser)]
#[clap(
Expand Down Expand Up @@ -66,6 +66,9 @@ pub struct Options {
/// Disable the file watcher
#[clap(long)]
pub no_watch: bool,
/// Additional paths to watch for changes.
#[clap(long)]
pub additional_watch_folders: Vec<PathBuf>,
/// Open Android Studio instead of trying to run on a connected device
#[clap(short, long)]
pub open: bool,
Expand Down Expand Up @@ -113,6 +116,7 @@ impl From<Options> for DevOptions {
config: options.config,
args: options.args,
no_watch: options.no_watch,
additional_watch_folders: options.additional_watch_folders,
no_dev_server_wait: options.no_dev_server_wait,
no_dev_server: options.no_dev_server,
port: options.port,
Expand Down Expand Up @@ -265,6 +269,7 @@ fn run_dev(
args: options.args,
config: dev_options.config.clone(),
no_watch: options.no_watch,
additional_watch_folders: options.additional_watch_folders,
},
|options| {
let cli_options = CliOptions {
Expand Down
7 changes: 6 additions & 1 deletion crates/tauri-cli/src/mobile/ios/dev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ use cargo_mobile2::{
opts::{NoiseLevel, Profile},
};

use std::env::set_current_dir;
use std::{env::set_current_dir, path::PathBuf};

const PHYSICAL_IPHONE_DEV_WARNING: &str = "To develop on physical phones you need the `--host` option (not required for Simulators). See the documentation for more information: https://v2.tauri.app/develop/#development-server";

Expand Down Expand Up @@ -73,6 +73,9 @@ pub struct Options {
/// Disable the file watcher
#[clap(long)]
pub no_watch: bool,
/// Additional paths to watch for changes.
#[clap(long)]
pub additional_watch_folders: Vec<PathBuf>,
/// Open Xcode instead of trying to run on a connected device
#[clap(short, long)]
pub open: bool,
Expand Down Expand Up @@ -119,6 +122,7 @@ impl From<Options> for DevOptions {
release_mode: options.release_mode,
args: options.args,
no_watch: options.no_watch,
additional_watch_folders: options.additional_watch_folders,
no_dev_server: options.no_dev_server,
no_dev_server_wait: options.no_dev_server_wait,
port: options.port,
Expand Down Expand Up @@ -273,6 +277,7 @@ fn run_dev(
args: options.args,
config: dev_options.config.clone(),
no_watch: options.no_watch,
additional_watch_folders: options.additional_watch_folders,
},
|options| {
let cli_options = CliOptions {
Expand Down
9 changes: 9 additions & 0 deletions crates/tauri-schema-generator/schemas/config.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
"build": {
"description": "The build configuration.",
"default": {
"additionalWatchFolders": [],
"removeUnusedCommands": false
},
"allOf": [
Expand Down Expand Up @@ -1880,6 +1881,14 @@
"description": "Try to remove unused commands registered from plugins base on the ACL list during `tauri build`,\n the way it works is that tauri-cli will read this and set the environment variables for the build script and macros,\n and they'll try to get all the allowed commands and remove the rest\n\n Note:\n - This won't be accounting for dynamically added ACLs when you use features from the `dynamic-acl` (currently enabled by default) feature flag, so make sure to check it when using this\n - This feature requires tauri-plugin 2.1 and tauri 2.4",
"default": false,
"type": "boolean"
},
"additionalWatchFolders": {
"description": "Additional paths to watch for changes when running `tauri dev`.",
"default": [],
"type": "array",
"items": {
"type": "string"
}
}
},
"additionalProperties": false
Expand Down
8 changes: 7 additions & 1 deletion crates/tauri-utils/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2892,6 +2892,9 @@ pub struct BuildConfig {
/// - This feature requires tauri-plugin 2.1 and tauri 2.4
#[serde(alias = "remove-unused-commands", default)]
pub remove_unused_commands: bool,
/// Additional paths to watch for changes when running `tauri dev`.
#[serde(alias = "additional-watch-directories", default)]
pub additional_watch_folders: Vec<PathBuf>,
}

#[derive(Debug, PartialEq, Eq)]
Expand Down Expand Up @@ -3540,6 +3543,7 @@ mod build {
let before_bundle_command = quote!(None);
let features = quote!(None);
let remove_unused_commands = quote!(false);
let additional_watch_folders = quote!(Vec::new());

literal_struct!(
tokens,
Expand All @@ -3551,7 +3555,8 @@ mod build {
before_build_command,
before_bundle_command,
features,
remove_unused_commands
remove_unused_commands,
additional_watch_folders
);
}
}
Expand Down Expand Up @@ -3874,6 +3879,7 @@ mod test {
before_bundle_command: None,
features: None,
remove_unused_commands: false,
additional_watch_folders: Vec::new(),
};

// create a bundle config
Expand Down