Skip to content

Commit d285b9b

Browse files
committed
Apply code review feedback
1 parent 810d458 commit d285b9b

File tree

1 file changed

+23
-8
lines changed

1 file changed

+23
-8
lines changed

rewatch/src/helpers.rs

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,20 +33,35 @@ pub mod emojis {
3333
}
3434

3535
// Cache existence checks for candidate node_modules paths during upward traversal.
36-
// Keyed by the absolute candidate path string; value is the existence boolean.
37-
static NODE_MODULES_EXIST_CACHE: LazyLock<RwLock<ahash::AHashMap<String, bool>>> =
36+
// Keyed by the absolute candidate path; value is the existence boolean.
37+
static NODE_MODULES_EXIST_CACHE: LazyLock<RwLock<ahash::AHashMap<PathBuf, bool>>> =
3838
LazyLock::new(|| RwLock::new(ahash::AHashMap::new()));
3939

4040
fn cached_path_exists(path: &Path) -> bool {
41-
let key = path.to_string_lossy().to_string();
42-
if let Ok(cache) = NODE_MODULES_EXIST_CACHE.read() {
43-
if let Some(exists) = cache.get(&key) {
44-
return *exists;
41+
match NODE_MODULES_EXIST_CACHE.read() {
42+
Ok(cache) => {
43+
if let Some(exists) = cache.get(path) {
44+
return *exists;
45+
}
46+
}
47+
Err(poisoned) => {
48+
log::warn!("NODE_MODULES_EXIST_CACHE read lock poisoned; recovering");
49+
let cache = poisoned.into_inner();
50+
if let Some(exists) = cache.get(path) {
51+
return *exists;
52+
}
4553
}
4654
}
4755
let exists = path.exists();
48-
if let Ok(mut cache) = NODE_MODULES_EXIST_CACHE.write() {
49-
cache.insert(key, exists);
56+
match NODE_MODULES_EXIST_CACHE.write() {
57+
Ok(mut cache) => {
58+
cache.insert(path.to_path_buf(), exists);
59+
}
60+
Err(poisoned) => {
61+
log::warn!("NODE_MODULES_EXIST_CACHE write lock poisoned; recovering");
62+
let mut cache = poisoned.into_inner();
63+
cache.insert(path.to_path_buf(), exists);
64+
}
5065
}
5166
exists
5267
}

0 commit comments

Comments
 (0)