Skip to content

Commit 30acf45

Browse files
committed
Path validation and event checking for correct file watching
1 parent 3f2afc1 commit 30acf45

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

src/main.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ async fn async_watch<P: AsRef<Path>>(
4040
path: P,
4141
mut change_tx: UnboundedSender<()>,
4242
) -> Result<(), Box<dyn Error>> {
43+
let path = path.as_ref();
44+
let path = std::fs::canonicalize(path)
45+
.unwrap_or_else(|e| panic!("Failed to canonicalize path {path:?}: {e:}"));
46+
4347
let (mut watcher, mut rx) = async_watcher()?;
4448

4549
// Add a path to be watched. All files and directories at that path and
@@ -48,8 +52,14 @@ async fn async_watch<P: AsRef<Path>>(
4852

4953
while let Some(res) = rx.next().await {
5054
match res {
51-
Ok(_) => {
52-
change_tx.send(()).await.unwrap();
55+
Ok(event) => {
56+
if path.is_dir() || event.paths.iter().find(|candidate| {
57+
std::fs::canonicalize(candidate)
58+
.unwrap_or_else(|e| panic!("Failed to canonicalize path {path:?}: {e:}"))
59+
== path
60+
}).is_some() {
61+
change_tx.send(()).await.unwrap();
62+
}
5363
}
5464
Err(e) => error!("Watch error: {:?}", e),
5565
}
@@ -98,7 +108,7 @@ struct ShaderBuilder {
98108
#[arg(long, default_value = "false")]
99109
preserve_bindings: bool,
100110
/// If set, will watch the provided directory and recompile on change.
101-
///
111+
///
102112
/// Can be specified multiple times to watch more than one directory.
103113
#[arg(short, long)]
104114
watch_paths: Option<Vec<String>>,

0 commit comments

Comments
 (0)