utils/watcher: Support config includes
Browse files at this commit0673260249d173997298b85f3a84753db71eaa88Parent: 0a33dca2 changed files +126 -15
- src/main.rs +2 -1
- src/utils/watcher.rs +124 -14
src/main.rsmodified+2 -1
| @@ -153,6 +153,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> { | |||
| 153 | warn!("{err:?}"); | 153 | warn!("{err:?}"); |
| 154 | Config::load_default() | 154 | Config::load_default() |
| 155 | }); | 155 | }); |
| 156 | + let config_includes = config_load_result.includes; | ||
| 156 | 157 | ||
| 157 | let spawn_at_startup = mem::take(&mut config.spawn_at_startup); | 158 | let spawn_at_startup = mem::take(&mut config.spawn_at_startup); |
| 158 | let spawn_sh_at_startup = mem::take(&mut config.spawn_sh_at_startup); | 159 | let spawn_sh_at_startup = mem::take(&mut config.spawn_sh_at_startup); |
| @@ -239,7 +240,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> { | |||
| 239 | } | 240 | } |
| 240 | } | 241 | } |
| 241 | 242 | ||
| 242 | - watcher::setup(&mut state, &config_path); | 243 | + watcher::setup(&mut state, &config_path, config_includes); |
| 243 | 244 | ||
| 244 | // Spawn commands from cli and auto-start. | 245 | // Spawn commands from cli and auto-start. |
| 245 | spawn(cli.command, None); | 246 | spawn(cli.command, None); |
src/utils/watcher.rsmodified+124 -14
| @@ -1,11 +1,12 @@ | |||
| 1 | //! File modification watcher. | 1 | //! File modification watcher. |
| 2 | 2 | ||
| 3 | +use std::collections::HashMap; | ||
| 3 | use std::path::{Path, PathBuf}; | 4 | use std::path::{Path, PathBuf}; |
| 4 | use std::sync::mpsc; | 5 | use std::sync::mpsc; |
| 5 | use std::time::{Duration, SystemTime}; | 6 | use std::time::{Duration, SystemTime}; |
| 6 | use std::{io, thread}; | 7 | use std::{io, thread}; |
| 7 | 8 | ||
| 8 | -use niri_config::{Config, ConfigPath}; | 9 | +use niri_config::{Config, ConfigParseResult, ConfigPath}; |
| 9 | use smithay::reexports::calloop::channel::SyncSender; | 10 | use smithay::reexports::calloop::channel::SyncSender; |
| 10 | 11 | ||
| 11 | use crate::niri::State; | 12 | use crate::niri::State; |
| @@ -22,6 +23,9 @@ struct WatcherInner { | |||
| 22 | 23 | ||
| 23 | /// Last observed props of the watched file. | 24 | /// Last observed props of the watched file. |
| 24 | last_props: Option<Props>, | 25 | last_props: Option<Props>, |
| 26 | + | ||
| 27 | + /// Last observed props for included files. | ||
| 28 | + includes: HashMap<PathBuf, Option<Props>>, | ||
| 25 | } | 29 | } |
| 26 | 30 | ||
| 27 | /// Properties of the watched file. | 31 | /// Properties of the watched file. |
| @@ -48,17 +52,18 @@ enum CheckResult { | |||
| 48 | } | 52 | } |
| 49 | 53 | ||
| 50 | impl Watcher { | 54 | impl Watcher { |
| 51 | - pub fn new<T: Send + 'static>( | 55 | + pub fn new( |
| 52 | path: ConfigPath, | 56 | path: ConfigPath, |
| 53 | - mut process: impl FnMut(&ConfigPath) -> T + Send + 'static, | 57 | + includes: Vec<PathBuf>, |
| 54 | - changed: SyncSender<T>, | 58 | + mut process: impl FnMut(&ConfigPath) -> ConfigParseResult<Config, ()> + Send + 'static, |
| 59 | + changed: SyncSender<Result<Config, ()>>, | ||
| 55 | ) -> Self { | 60 | ) -> Self { |
| 56 | let (load_config, load_config_rx) = mpsc::channel(); | 61 | let (load_config, load_config_rx) = mpsc::channel(); |
| 57 | 62 | ||
| 58 | thread::Builder::new() | 63 | thread::Builder::new() |
| 59 | .name(format!("Filesystem Watcher for {path:?}")) | 64 | .name(format!("Filesystem Watcher for {path:?}")) |
| 60 | .spawn(move || { | 65 | .spawn(move || { |
| 61 | - let mut inner = WatcherInner::new(path); | 66 | + let mut inner = WatcherInner::new(path, includes); |
| 62 | 67 | ||
| 63 | loop { | 68 | loop { |
| 64 | let mut should_load = match load_config_rx.recv_timeout(POLLING_INTERVAL) { | 69 | let mut should_load = match load_config_rx.recv_timeout(POLLING_INTERVAL) { |
| @@ -77,12 +82,19 @@ impl Watcher { | |||
| 77 | } | 82 | } |
| 78 | 83 | ||
| 79 | if should_load { | 84 | if should_load { |
| 80 | - let rv = process(&inner.path); | 85 | + let res = process(&inner.path); |
| 81 | 86 | ||
| 82 | - if let Err(err) = changed.send(rv) { | 87 | + if let Err(err) = changed.send(res.config) { |
| 83 | warn!("error sending change notification: {err:?}"); | 88 | warn!("error sending change notification: {err:?}"); |
| 84 | break; | 89 | break; |
| 85 | } | 90 | } |
| 91 | + | ||
| 92 | + // There's a bit of time here between reading the config and reading | ||
| 93 | + // properties of included files where an included file could change and | ||
| 94 | + // remain unnoticed by the watcher. Not sure there's any good way around it | ||
| 95 | + // though since we don't know the final set of includes until the config is | ||
| 96 | + // parsed. | ||
| 97 | + inner.set_includes(res.includes); | ||
| 86 | } | 98 | } |
| 87 | } | 99 | } |
| 88 | 100 | ||
| @@ -117,9 +129,16 @@ impl Props { | |||
| 117 | } | 129 | } |
| 118 | 130 | ||
| 119 | impl WatcherInner { | 131 | impl WatcherInner { |
| 120 | - pub fn new(path: ConfigPath) -> Self { | 132 | + pub fn new(path: ConfigPath, includes: Vec<PathBuf>) -> Self { |
| 121 | let last_props = Props::from_config_path(&path).ok(); | 133 | let last_props = Props::from_config_path(&path).ok(); |
| 122 | - Self { path, last_props } | 134 | + |
| 135 | + let mut rv = Self { | ||
| 136 | + path, | ||
| 137 | + last_props, | ||
| 138 | + includes: HashMap::new(), | ||
| 139 | + }; | ||
| 140 | + rv.set_includes(includes); | ||
| 141 | + rv | ||
| 123 | } | 142 | } |
| 124 | 143 | ||
| 125 | pub fn check(&mut self) -> CheckResult { | 144 | pub fn check(&mut self) -> CheckResult { |
| @@ -128,20 +147,42 @@ impl WatcherInner { | |||
| 128 | self.last_props = Some(new_props); | 147 | self.last_props = Some(new_props); |
| 129 | CheckResult::Changed | 148 | CheckResult::Changed |
| 130 | } else { | 149 | } else { |
| 150 | + for (path, last_props) in &mut self.includes { | ||
| 151 | + let new_props = Props::from_path(path).ok(); | ||
| 152 | + | ||
| 153 | + // If an include goes missing while the main config file is unchanged, we | ||
| 154 | + // consider that a change and reload. | ||
| 155 | + if *last_props != new_props { | ||
| 156 | + return CheckResult::Changed; | ||
| 157 | + } | ||
| 158 | + } | ||
| 159 | + | ||
| 131 | CheckResult::Unchanged | 160 | CheckResult::Unchanged |
| 132 | } | 161 | } |
| 133 | } else { | 162 | } else { |
| 134 | CheckResult::Missing | 163 | CheckResult::Missing |
| 135 | } | 164 | } |
| 136 | } | 165 | } |
| 166 | + | ||
| 167 | + fn set_includes(&mut self, includes: Vec<PathBuf>) { | ||
| 168 | + self.includes = includes | ||
| 169 | + .into_iter() | ||
| 170 | + .map(|path| { | ||
| 171 | + let props = Props::from_path(&path).ok(); | ||
| 172 | + (path, props) | ||
| 173 | + }) | ||
| 174 | + .collect(); | ||
| 175 | + } | ||
| 137 | } | 176 | } |
| 138 | 177 | ||
| 139 | -pub fn setup(state: &mut State, config_path: &ConfigPath) { | 178 | +pub fn setup(state: &mut State, config_path: &ConfigPath, includes: Vec<PathBuf>) { |
| 140 | // Parsing the config actually takes > 20 ms on my beefy machine, so let's do it on the | 179 | // Parsing the config actually takes > 20 ms on my beefy machine, so let's do it on the |
| 141 | // watcher thread. | 180 | // watcher thread. |
| 142 | let process = |path: &ConfigPath| { | 181 | let process = |path: &ConfigPath| { |
| 143 | - path.load().config.map_err(|err| { | 182 | + path.load().map_config_res(|res| { |
| 144 | - warn!("{err:?}"); | 183 | + res.map_err(|err| { |
| 184 | + warn!("{err:?}"); | ||
| 185 | + }) | ||
| 145 | }) | 186 | }) |
| 146 | }; | 187 | }; |
| 147 | 188 | ||
| @@ -162,7 +203,8 @@ pub fn setup(state: &mut State, config_path: &ConfigPath) { | |||
| 162 | ) | 203 | ) |
| 163 | .unwrap(); | 204 | .unwrap(); |
| 164 | 205 | ||
| 165 | - state.niri.config_file_watcher = Some(Watcher::new(config_path.clone(), process, tx)); | 206 | + let watcher = Watcher::new(config_path.clone(), includes, process, tx); |
| 207 | + state.niri.config_file_watcher = Some(watcher); | ||
| 166 | } | 208 | } |
| 167 | 209 | ||
| 168 | #[cfg(test)] | 210 | #[cfg(test)] |
| @@ -266,8 +308,9 @@ mod tests { | |||
| 266 | sh, config_path, .. | 308 | sh, config_path, .. |
| 267 | } = self; | 309 | } = self; |
| 268 | 310 | ||
| 311 | + let includes = config_path.load().includes; | ||
| 269 | let mut test = TestUtil { | 312 | let mut test = TestUtil { |
| 270 | - watcher: WatcherInner::new(config_path), | 313 | + watcher: WatcherInner::new(config_path, includes), |
| 271 | }; | 314 | }; |
| 272 | 315 | ||
| 273 | // don't trigger before we start | 316 | // don't trigger before we start |
| @@ -319,6 +362,8 @@ mod tests { | |||
| 319 | let actual = fs::read_to_string(new_path).unwrap(); | 362 | let actual = fs::read_to_string(new_path).unwrap(); |
| 320 | assert_eq!(actual, expected, "wrong file contents"); | 363 | assert_eq!(actual, expected, "wrong file contents"); |
| 321 | 364 | ||
| 365 | + self.watcher.set_includes(Config::load(new_path).includes); | ||
| 366 | + | ||
| 322 | self.pass_time(); | 367 | self.pass_time(); |
| 323 | } | 368 | } |
| 324 | } | 369 | } |
| @@ -509,6 +554,71 @@ mod tests { | |||
| 509 | }) | 554 | }) |
| 510 | } | 555 | } |
| 511 | 556 | ||
| 557 | + #[test] | ||
| 558 | + fn change_included_file() -> Result { | ||
| 559 | + TestPath::Explicit("niri/config.kdl") | ||
| 560 | + .setup(|sh| { | ||
| 561 | + sh.write_file("niri/config.kdl", "include \"colors.kdl\"")?; | ||
| 562 | + sh.write_file("niri/colors.kdl", "// Colors") | ||
| 563 | + }) | ||
| 564 | + .assert_initial("include \"colors.kdl\"") | ||
| 565 | + .run(|sh, test| { | ||
| 566 | + sh.write_file("niri/colors.kdl", "// Updated colors")?; | ||
| 567 | + test.assert_changed_to("include \"colors.kdl\""); | ||
| 568 | + | ||
| 569 | + Ok(()) | ||
| 570 | + }) | ||
| 571 | + } | ||
| 572 | + | ||
| 573 | + #[test] | ||
| 574 | + fn remove_included_file() -> Result { | ||
| 575 | + TestPath::Explicit("niri/config.kdl") | ||
| 576 | + .setup(|sh| { | ||
| 577 | + sh.write_file("niri/config.kdl", "include \"colors.kdl\"")?; | ||
| 578 | + sh.write_file("niri/colors.kdl", "// Colors") | ||
| 579 | + }) | ||
| 580 | + .assert_initial("include \"colors.kdl\"") | ||
| 581 | + .run(|sh, test| { | ||
| 582 | + sh.remove_path("niri/colors.kdl")?; | ||
| 583 | + test.assert_changed_to("include \"colors.kdl\""); | ||
| 584 | + | ||
| 585 | + Ok(()) | ||
| 586 | + }) | ||
| 587 | + } | ||
| 588 | + | ||
| 589 | + #[test] | ||
| 590 | + fn nested_includes() -> Result { | ||
| 591 | + TestPath::Explicit("niri/config.kdl") | ||
| 592 | + .setup(|sh| { | ||
| 593 | + sh.write_file("niri/config.kdl", "include \"a.kdl\"")?; | ||
| 594 | + sh.write_file("niri/a.kdl", "include \"b.kdl\"")?; | ||
| 595 | + sh.write_file("niri/b.kdl", "// b content") | ||
| 596 | + }) | ||
| 597 | + .assert_initial("include \"a.kdl\"") | ||
| 598 | + .run(|sh, test| { | ||
| 599 | + sh.write_file("niri/b.kdl", "// updated b")?; | ||
| 600 | + test.assert_changed_to("include \"a.kdl\""); | ||
| 601 | + | ||
| 602 | + Ok(()) | ||
| 603 | + }) | ||
| 604 | + } | ||
| 605 | + | ||
| 606 | + #[test] | ||
| 607 | + fn broken_include_still_gets_watched() -> Result { | ||
| 608 | + TestPath::Explicit("niri/config.kdl") | ||
| 609 | + .setup(|sh| { | ||
| 610 | + sh.write_file("niri/config.kdl", "include \"colors.kdl\"")?; | ||
| 611 | + sh.write_file("niri/colors.kdl", "broken") | ||
| 612 | + }) | ||
| 613 | + .assert_initial("include \"colors.kdl\"") | ||
| 614 | + .run(|sh, test| { | ||
| 615 | + sh.write_file("niri/colors.kdl", "// Fixed")?; | ||
| 616 | + test.assert_changed_to("include \"colors.kdl\""); | ||
| 617 | + | ||
| 618 | + Ok(()) | ||
| 619 | + }) | ||
| 620 | + } | ||
| 621 | + | ||
| 512 | // Important: On systems like NixOS, mtime is not kept for config files. | 622 | // Important: On systems like NixOS, mtime is not kept for config files. |
| 513 | // So, this is testing that the watcher handles that correctly. | 623 | // So, this is testing that the watcher handles that correctly. |
| 514 | fn create_epoch(path: impl AsRef<Path>, content: &str) -> Result { | 624 | fn create_epoch(path: impl AsRef<Path>, content: &str) -> Result { |