Skip to content

Commit aa36a14

Browse files
committed
Fix file watching
1 parent acdfab3 commit aa36a14

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

src/main.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ fn async_watcher() -> notify::Result<(RecommendedWatcher, Receiver<notify::Resul
4040
/// Watch a file or directory, sending relevant events through the provided channel.
4141
async fn async_watch<P: AsRef<Path>>(
4242
path: P,
43-
mut event_tx: UnboundedSender<()>,
43+
mut change_tx: UnboundedSender<()>,
4444
) -> Result<(), Box<dyn Error>> {
4545
let path = path.as_ref();
4646
let path = std::fs::canonicalize(path)
@@ -50,7 +50,12 @@ async fn async_watch<P: AsRef<Path>>(
5050

5151
// Add a path to be watched. All files and directories at that path and
5252
// below will be monitored for changes.
53-
watcher.watch(path.as_ref(), RecursiveMode::Recursive)?;
53+
let watch_path = if path.is_dir() {
54+
path.clone()
55+
} else {
56+
path.parent().unwrap().to_owned()
57+
};
58+
watcher.watch(watch_path.as_ref(), RecursiveMode::Recursive)?;
5459

5560
while let Some(res) = rx.next().await {
5661
match res {
@@ -59,14 +64,10 @@ async fn async_watch<P: AsRef<Path>>(
5964
|| event
6065
.paths
6166
.iter()
62-
.find(|candidate| {
63-
std::fs::canonicalize(candidate).unwrap_or_else(|e| {
64-
panic!("Failed to canonicalize path {path:?}: {e:}")
65-
}) == path
66-
})
67+
.find(|candidate| **candidate == path)
6768
.is_some()
6869
{
69-
event_tx.send(()).await.unwrap();
70+
change_tx.send(()).await.unwrap();
7071
}
7172
}
7273
Err(e) => error!("Watch error: {:?}", e),

0 commit comments

Comments
 (0)