Skip to content

Commit

Permalink
fix(core): handle symlinks on debian for watch events (#18636)
Browse files Browse the repository at this point in the history
(cherry picked from commit cf0b2fd)
  • Loading branch information
Cammisuli authored and FrozenPandaz committed Aug 16, 2023
1 parent 6a8a8a4 commit 28c3fa3
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 2 deletions.
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/nx/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ ignore = '0.4'
ignore-files = "1.3.0"
itertools = "0.10.5"
once_cell = "1.18.0"
os_type = "2.6.0"
napi = { version = '2.12.6', default-features = false, features = ['anyhow', 'napi4', 'tokio_rt'] }
napi-derive = '2.9.3'
regex = "1.9.1"
Expand Down
5 changes: 5 additions & 0 deletions packages/nx/src/native/watch/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use std::path::PathBuf;
use tracing::trace;
use watchexec_events::{Event, Tag};

use crate::native::watch::utils::transform_event;

#[napi(string_enum)]
#[derive(Debug)]
/// Newly created files will have the `update` EventType as well.
Expand Down Expand Up @@ -52,6 +54,9 @@ pub(super) struct WatchEventInternal {

impl From<&Event> for WatchEventInternal {
fn from(value: &Event) -> Self {
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 event_kind = value
Expand Down
37 changes: 36 additions & 1 deletion packages/nx/src/native/watch/utils.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
use ignore::WalkBuilder;
use ignore_files::IgnoreFile;
use std::path::PathBuf;
use once_cell::sync::Lazy;
use os_type::{OSInformation, OSType};
use std::{fs, path::PathBuf};
use tracing::trace;
use watchexec_events::{Event, Tag};

pub(super) fn get_ignore_files<T: AsRef<str>>(root: T) -> Vec<IgnoreFile> {
let root = root.as_ref();
Expand Down Expand Up @@ -45,3 +49,34 @@ pub(super) fn get_ignore_files<T: AsRef<str>>(root: T) -> Vec<IgnoreFile> {
// .map(|result| result.path().into())
// .collect()
// }

static OS_PLATFORM: Lazy<OSInformation> = Lazy::new(os_type::current_platform);

pub(super) fn transform_event(watch_event: &Event) -> Option<Event> {
if OS_PLATFORM.os_type == OSType::Debian {
let tags = watch_event
.tags
.clone()
.into_iter()
.map(|tag| match tag {
Tag::Path { path, file_type } => {
trace!("canonicalizing {:?}", path);
let real_path = fs::canonicalize(&path).unwrap_or(path);
trace!("real path {:?}", real_path);
Tag::Path {
path: real_path,
file_type,
}
}
_ => tag,
})
.collect();

Some(Event {
tags,
metadata: watch_event.metadata.clone(),
})
} else {
None
}
}
7 changes: 6 additions & 1 deletion packages/nx/src/native/watch/watch_filterer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,19 @@ use watchexec_events::filekind::{CreateKind, FileEventKind, ModifyKind, RemoveKi
use watchexec_events::{Event, FileType, Priority, Source, Tag};
use watchexec_filterer_ignore::IgnoreFilterer;

use crate::native::watch::utils::transform_event;

#[derive(Debug)]
pub struct WatchFilterer {
pub inner: IgnoreFilterer,
}

/// Used to filter out events that that come from watchexec
impl Filterer for WatchFilterer {
fn check_event(&self, event: &Event, priority: Priority) -> Result<bool, RuntimeError> {
fn check_event(&self, watch_event: &Event, priority: Priority) -> Result<bool, RuntimeError> {
let transformed = transform_event(watch_event);
let event = transformed.as_ref().unwrap_or(watch_event);

if !self.inner.check_event(event, priority)? {
return Ok(false);
}
Expand Down

0 comments on commit 28c3fa3

Please sign in to comment.