From 3d9bd16ce0f322e28e082bbb09f73069310ed6f2 Mon Sep 17 00:00:00 2001 From: Jonathan Cammisuli Date: Mon, 22 Apr 2024 23:06:57 -0400 Subject: [PATCH] fix(core): handle events that do not have paths (#22947) --- packages/nx/src/native/watch/types.rs | 27 +++++++++++++++++-------- packages/nx/src/native/watch/watcher.rs | 11 ++++++---- 2 files changed, 26 insertions(+), 12 deletions(-) diff --git a/packages/nx/src/native/watch/types.rs b/packages/nx/src/native/watch/types.rs index 0760e99487c7f..c68acb2038a8d 100644 --- a/packages/nx/src/native/watch/types.rs +++ b/packages/nx/src/native/watch/types.rs @@ -50,21 +50,32 @@ pub(super) struct WatchEventInternal { pub origin: Option, } -impl From<&Event> for WatchEventInternal { - fn from(value: &Event) -> Self { + + +impl TryFrom<&Event> for WatchEventInternal { + type Error = anyhow::Error; + + fn try_from(value: &Event) -> std::result::Result { let transformed = transform_event(value); let value = transformed.as_ref().unwrap_or(value); - let path = value.paths().next().expect("there should always be a path"); + let Some(path) = value.paths().next() else { + let error_msg = "unable to get path from the event"; + trace!(?value, error_msg); + anyhow::bail!(error_msg) + }; - let event_kind = value + let Some( event_kind ) = value .tags .iter() .find_map(|t| match t { Tag::FileEventKind(event_kind) => Some(event_kind), _ => None, - }) - .expect("there should always be a file event kind"); + }) else { + let error_msg = "unable to get the file event kind"; + trace!(?value, error_msg); + anyhow::bail!(error_msg) + }; let path_ref = path.0; let event_type = if path.1.is_none() && !path_ref.exists() { @@ -112,10 +123,10 @@ impl From<&Event> for WatchEventInternal { trace!(?path, ?event_kind, ?event_type, "event kind -> event type"); - WatchEventInternal { + Ok(WatchEventInternal { path: path.0.into(), r#type: event_type, origin: None, - } + }) } } diff --git a/packages/nx/src/native/watch/watcher.rs b/packages/nx/src/native/watch/watcher.rs index fb7789335b0e2..8b999981e16d7 100644 --- a/packages/nx/src/native/watch/watcher.rs +++ b/packages/nx/src/native/watch/watcher.rs @@ -118,10 +118,13 @@ impl Watcher { let events = action .events .par_iter() - .map(|ev| { - let mut watch_event: WatchEventInternal = ev.into(); - watch_event.origin = Some(origin_path.clone()); - watch_event + .filter_map(|ev| { + ev.try_into() + .map(|mut watch_event: WatchEventInternal| { + watch_event.origin = Some(origin_path.clone()); + watch_event + }) + .ok() }) .collect::>();