diff --git a/.changes/additional-watch-folders.md b/.changes/additional-watch-folders.md new file mode 100644 index 000000000000..7a64b4fa5f70 --- /dev/null +++ b/.changes/additional-watch-folders.md @@ -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`. diff --git a/crates/tauri-cli/config.schema.json b/crates/tauri-cli/config.schema.json index f71b3284fcfe..11ba08e0af80 100644 --- a/crates/tauri-cli/config.schema.json +++ b/crates/tauri-cli/config.schema.json @@ -70,6 +70,7 @@ "build": { "description": "The build configuration.", "default": { + "additionalWatchFolders": [], "removeUnusedCommands": false }, "allOf": [ @@ -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 diff --git a/crates/tauri-cli/src/dev.rs b/crates/tauri-cli/src/dev.rs index f802d4480d9d..05b4dee137b1 100644 --- a/crates/tauri-cli/src/dev.rs +++ b/crates/tauri-cli/src/dev.rs @@ -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}, @@ -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, /// Disable the built-in dev server for static files. #[clap(long)] @@ -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(()) } diff --git a/crates/tauri-cli/src/interface/rust.rs b/crates/tauri-cli/src/interface/rust.rs index b8990fb4591d..dc8a475828e8 100644 --- a/crates/tauri-cli/src/interface/rust.rs +++ b/crates/tauri-cli/src/interface/rust.rs @@ -15,6 +15,7 @@ use std::{ }; use anyhow::Context; +use dunce::canonicalize; use glob::glob; use ignore::gitignore::{Gitignore, GitignoreBuilder}; use notify::RecursiveMode; @@ -54,6 +55,7 @@ pub struct Options { pub args: Vec, pub config: Vec, pub no_watch: bool, + pub additional_watch_folders: Vec, } impl From for Options { @@ -66,6 +68,7 @@ impl From for Options { args: options.args, config: options.config, no_watch: true, + additional_watch_folders: Vec::new(), } } } @@ -93,6 +96,7 @@ impl From for Options { args: options.args, config: options.config, no_watch: options.no_watch, + additional_watch_folders: options.additional_watch_folders, } } } @@ -104,6 +108,7 @@ pub struct MobileOptions { pub args: Vec, pub config: Vec, pub no_watch: bool, + pub additional_watch_folders: Vec, } #[derive(Debug)] @@ -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) } } @@ -233,7 +238,7 @@ impl Interface for Rust { } else { let merge_configs = options.config.iter().map(|c| &c.0).collect::>(); 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) } } @@ -411,13 +416,31 @@ fn expand_member_path(path: &Path) -> crate::Result> { Ok(res) } -fn get_watch_folders() -> crate::Result> { +fn get_watch_folders(additional_watch_folders: &[PathBuf]) -> crate::Result> { 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) + }; + + 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) { @@ -480,6 +503,7 @@ impl Rust { fn run_dev_watcher crate::Result>>( &mut self, + additional_watch_folders: &[PathBuf], merge_configs: &[&serde_json::Value], run: Arc, ) -> crate::Result<()> { @@ -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"); diff --git a/crates/tauri-cli/src/mobile/android/dev.rs b/crates/tauri-cli/src/mobile/android/dev.rs index 30cca450d806..3ad43ecd0048 100644 --- a/crates/tauri-cli/src/mobile/android/dev.rs +++ b/crates/tauri-cli/src/mobile/android/dev.rs @@ -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( @@ -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, /// Open Android Studio instead of trying to run on a connected device #[clap(short, long)] pub open: bool, @@ -113,6 +116,7 @@ impl From 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, @@ -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 { diff --git a/crates/tauri-cli/src/mobile/ios/dev.rs b/crates/tauri-cli/src/mobile/ios/dev.rs index dc7f9fc87431..55f8ae95045a 100644 --- a/crates/tauri-cli/src/mobile/ios/dev.rs +++ b/crates/tauri-cli/src/mobile/ios/dev.rs @@ -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"; @@ -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, /// Open Xcode instead of trying to run on a connected device #[clap(short, long)] pub open: bool, @@ -119,6 +122,7 @@ impl From 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, @@ -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 { diff --git a/crates/tauri-schema-generator/schemas/config.schema.json b/crates/tauri-schema-generator/schemas/config.schema.json index f71b3284fcfe..11ba08e0af80 100644 --- a/crates/tauri-schema-generator/schemas/config.schema.json +++ b/crates/tauri-schema-generator/schemas/config.schema.json @@ -70,6 +70,7 @@ "build": { "description": "The build configuration.", "default": { + "additionalWatchFolders": [], "removeUnusedCommands": false }, "allOf": [ @@ -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 diff --git a/crates/tauri-utils/src/config.rs b/crates/tauri-utils/src/config.rs index 8ca8ff86b8d6..87c002828c3b 100644 --- a/crates/tauri-utils/src/config.rs +++ b/crates/tauri-utils/src/config.rs @@ -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, } #[derive(Debug, PartialEq, Eq)] @@ -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, @@ -3551,7 +3555,8 @@ mod build { before_build_command, before_bundle_command, features, - remove_unused_commands + remove_unused_commands, + additional_watch_folders ); } } @@ -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