Skip to content

Commit

Permalink
fix(core): always ignore ".git/", "node_modules" and ".nx/" directori…
Browse files Browse the repository at this point in the history
…es even when `use_ignore` is set to false for the watcher (#20126)
  • Loading branch information
Cammisuli authored Nov 9, 2023
1 parent dc51a90 commit dcbf60b
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 66 deletions.
6 changes: 0 additions & 6 deletions packages/nx/src/daemon/server/watcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,6 @@ export async function watchOutputFiles(cb: FileWatcherCallback) {
return cb(err, null);
}

events = events.filter((event) => {
return (
!event.path.startsWith('.git') && !event.path.includes('node_modules')
);
});

if (events.length !== 0) {
cb(null, events);
}
Expand Down
5 changes: 4 additions & 1 deletion packages/nx/src/native/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,10 @@ export class Watcher {
origin: string
/**
* Creates a new Watcher instance.
* If `useIgnore` is set to false, no ignores will be used, even when `additionalGlobs` is set
* Will always ignore the following directories:
* * .git/
* * node_modules/
* * .nx/
*/
constructor(origin: string, additionalGlobs?: Array<string> | undefined | null, useIgnore?: boolean | undefined | null)
watch(callback: (err: string | null, events: WatchEvent[]) => void): void
Expand Down
70 changes: 30 additions & 40 deletions packages/nx/src/native/watch/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,40 @@ 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>, Option<IgnoreFile>) {
pub(super) fn get_ignore_files<T: AsRef<str>>(
use_ignore: bool,
root: T,
) -> Option<Vec<IgnoreFile>> {
let root = root.as_ref();
if use_ignore {
let mut walker = WalkBuilder::new(root);
walker.hidden(false);
walker.git_ignore(false);

let mut walker = WalkBuilder::new(root);
walker.hidden(false);
walker.git_ignore(false);

let node_folder = PathBuf::from(root).join("node_modules");
walker.filter_entry(move |entry| !entry.path().starts_with(&node_folder));
let gitignore_files = walker
.build()
.flatten()
.filter(|result| result.path().ends_with(".gitignore"))
.map(|result| {
let path: PathBuf = result.path().into();
let parent: PathBuf = path.parent().unwrap_or(&path).into();
IgnoreFile {
path,
applies_in: Some(parent),
applies_to: None,
}
})
.collect();
(gitignore_files, get_nx_ignore(root))
let node_folder = PathBuf::from(root).join("node_modules");
walker.filter_entry(move |entry| !entry.path().starts_with(&node_folder));
Some(
walker
.build()
.flatten()
.filter(|result| result.path().ends_with(".gitignore"))
.map(|result| {
let path: PathBuf = result.path().into();
let parent: PathBuf = path.parent().unwrap_or(&path).into();
IgnoreFile {
path,
applies_in: Some(parent),
applies_to: None,
}
})
.collect(),
)
} else {
None
}
}

fn get_nx_ignore<P: AsRef<Path>>(origin: P) -> Option<IgnoreFile> {
pub(super) fn get_nx_ignore<P: AsRef<Path>>(origin: P) -> Option<IgnoreFile> {
let nx_ignore_path = PathBuf::from(origin.as_ref()).join(".nxignore");
if nx_ignore_path.exists() {
Some(IgnoreFile {
Expand All @@ -44,23 +51,6 @@ fn get_nx_ignore<P: AsRef<Path>>(origin: P) -> Option<IgnoreFile> {
}
}

// /// Get only the root level folders to watch.
// /// These will not include git ignored folders
// pub(super) fn get_watch_directories<T: AsRef<str>>(root: T) -> Vec<PathBuf> {
// let root = root.as_ref();
//
// let mut walker = WalkBuilder::new(root);
// walker.hidden(false);
// walker.max_depth(Some(1));
// walker.filter_entry(|entry| entry.path().is_dir());
//
// walker
// .build()
// .flatten()
// .map(|result| result.path().into())
// .collect()
// }

pub(super) fn transform_event(watch_event: &Event) -> Option<Event> {
if cfg!(linux) {
let tags = watch_event
Expand Down
25 changes: 14 additions & 11 deletions packages/nx/src/native/watch/watch_config.rs
Original file line number Diff line number Diff line change
@@ -1,31 +1,34 @@
use crate::native::watch::utils::get_ignore_files;
use crate::native::watch::watch_filterer::WatchFilterer;
use ignore_files::IgnoreFilter;
use std::sync::Arc;

use ignore_files::IgnoreFilter;
use tracing::trace;
use watchexec::config::RuntimeConfig;
use watchexec_filterer_ignore::IgnoreFilterer;

use crate::native::watch::utils::{get_ignore_files, get_nx_ignore};
use crate::native::watch::watch_filterer::WatchFilterer;

pub(super) async fn create_runtime(
origin: &str,
additional_globs: &[&str],
use_ignore: bool,
) -> napi::Result<RuntimeConfig> {
let (ignore_files, nx_ignore_file) = if use_ignore {
get_ignore_files(origin)
} else {
(vec![], None)
};
let ignore_files = get_ignore_files(use_ignore, origin);
let nx_ignore_file = get_nx_ignore(origin);

trace!(
?use_ignore,
?additional_globs,
?ignore_files,
"Using these ignore files for the watcher"
);
let mut filter = IgnoreFilter::new(origin, &ignore_files)
.await
.map_err(anyhow::Error::from)?;
let mut filter = if let Some(ignore_files) = ignore_files {
IgnoreFilter::new(origin, &ignore_files)
.await
.map_err(anyhow::Error::from)?
} else {
IgnoreFilter::empty(origin)
};

filter
.add_globs(additional_globs, Some(&origin.into()))
Expand Down
19 changes: 11 additions & 8 deletions packages/nx/src/native/watch/watcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ pub struct Watcher {
#[napi]
impl Watcher {
/// Creates a new Watcher instance.
/// If `useIgnore` is set to false, no ignores will be used, even when `additionalGlobs` is set
/// Will always ignore the following directories:
/// * .git/
/// * node_modules/
/// * .nx/
#[napi(constructor)]
pub fn new(
origin: String,
Expand All @@ -43,15 +46,15 @@ impl Watcher {
let watch_exec = Watchexec::new(InitConfig::default(), RuntimeConfig::default())
.map_err(anyhow::Error::from)?;

let mut globs = additional_globs.unwrap_or_default();

// always ignore the .git and node_modules folder
globs.push(".git/".into());
globs.push("node_modules/".into());
// always have these globs come before the additional globs
let mut globs = vec![".git/".into(), "node_modules/".into(), ".nx/".into()];
if let Some(additional_globs) = additional_globs {
globs.extend(additional_globs);
}

Ok(Watcher {
origin: if cfg!(window) {
origin.replace("/", "\\")
origin.replace('/', "\\")
} else {
origin
},
Expand Down Expand Up @@ -94,7 +97,7 @@ impl Watcher {
let origin = self.origin.clone();
let watch_exec = self.watch_exec.clone();
let additional_globs = self.additional_globs.clone();
let use_ignore = self.use_ignore.clone();
let use_ignore = self.use_ignore;
let start = async move {
let mut runtime = watch_config::create_runtime(
&origin,
Expand Down

0 comments on commit dcbf60b

Please sign in to comment.