Skip to content

Commit

Permalink
fix(core): fix globbing in rust (#17803)
Browse files Browse the repository at this point in the history
  • Loading branch information
FrozenPandaz authored Jun 26, 2023
1 parent a995940 commit 38101fd
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 7 deletions.
13 changes: 10 additions & 3 deletions packages/nx/src/native/parallel_walker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ use std::thread::available_parallelism;
use crossbeam_channel::{unbounded, Receiver};
use ignore::WalkBuilder;

use crate::native::utils::glob::build_glob_set;

pub fn nx_walker<P, Fn, Re>(directory: P, f: Fn) -> Re
where
P: AsRef<Path>,
Expand All @@ -13,16 +15,21 @@ where
{
let directory = directory.as_ref();
let nx_ignore = directory.join(".nxignore");
let git_folder = directory.join(".git");
let node_folder = directory.join("node_modules");

let ignore_glob_set = build_glob_set(vec![
String::from("**/node_modules"),
String::from("**/.git"),
])
.expect("These static ignores always build");

let mut walker = WalkBuilder::new(directory);
walker.hidden(false);
walker.add_custom_ignore_filename(&nx_ignore);

// We should make sure to always ignore node_modules and the .git folder
walker.filter_entry(move |entry| {
!(entry.path().starts_with(&git_folder) || entry.path().starts_with(&node_folder))
let path = entry.path().to_string_lossy();
!ignore_glob_set.is_match(path.as_ref())
});

let cpus = available_parallelism().map_or(2, |n| n.get()) - 1;
Expand Down
42 changes: 38 additions & 4 deletions packages/nx/src/native/utils/glob.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,44 @@
use globset::{Glob, GlobSet, GlobSetBuilder};
use globset::{GlobBuilder, GlobSet, GlobSetBuilder};

pub(crate) fn build_glob_set(globs: Vec<String>) -> anyhow::Result<GlobSet> {
let mut glob_builder = GlobSetBuilder::new();
let mut glob_set_builder = GlobSetBuilder::new();
for glob in globs {
glob_builder.add(Glob::new(&glob).map_err(anyhow::Error::from)?);
let glob = GlobBuilder::new(&glob)
.literal_separator(true)
.build()
.map_err(anyhow::Error::from)?;
glob_set_builder.add(glob);
}

glob_builder.build().map_err(anyhow::Error::from)
glob_set_builder.build().map_err(anyhow::Error::from)
}

#[cfg(test)]
mod test {
use super::*;

#[test]
fn should_detect_package_json() {
let glob_set = build_glob_set(vec![String::from("packages/*/package.json")]).unwrap();
assert!(glob_set.is_match("packages/nx/package.json"))
}

#[test]
fn should_not_detect_deeply_nested_package_json() {
let glob_set = build_glob_set(vec![String::from("packages/*/package.json")]).unwrap();
assert!(!glob_set.is_match("packages/nx/test-files/package.json"))
}

#[test]
fn should_detect_deeply_nested_package_json() {
let glob_set = build_glob_set(vec![String::from("packages/**/package.json")]).unwrap();
assert!(glob_set.is_match("packages/nx/test-files/package.json"))
}

#[test]
fn should_detect_node_modules() {
let glob_set = build_glob_set(vec![String::from("**/node_modules")]).unwrap();
assert!(glob_set.is_match("node_modules"));
assert!(glob_set.is_match("packages/nx/node_modules"));
}
}

1 comment on commit 38101fd

@vercel
Copy link

@vercel vercel bot commented on 38101fd Jun 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

nx-dev – ./

nx-five.vercel.app
nx-dev-nrwl.vercel.app
nx.dev
nx-dev-git-master-nrwl.vercel.app

Please sign in to comment.