1use std::ffi::OsStr;
2use std::path::{Path, PathBuf};
3use std::sync::{Arc, Mutex, OnceLock};
4
5use fallow_config::{
6 DEFAULT_IGNORE_PATTERNS, IgnorePatternSet, PermutationSite, ResolvedConfig,
7 WorkspaceDiagnostic, WorkspaceDiagnosticKind, sort_permuted,
8};
9use fallow_types::discover::{DiscoveredFile, FileId};
10use fallow_types::path_util::display_relative;
11use fallow_types::workspace::glob_first_literal_segment;
12use ignore::WalkBuilder;
13use rustc_hash::{FxHashMap, FxHashSet};
14
15use super::{ALLOWED_HIDDEN_DIRS, SCRIPT_SCOPE_DENYLIST};
16
17pub(super) fn should_emit_note_once(key: String) -> bool {
23 static EMITTED: OnceLock<Mutex<FxHashSet<String>>> = OnceLock::new();
24 EMITTED
25 .get_or_init(|| Mutex::new(FxHashSet::default()))
26 .lock()
27 .map_or(true, |mut set| set.insert(key))
28}
29
30type SizedFile = (PathBuf, u64);
33
34type SkippedDotdirSink = Arc<Mutex<Vec<PathBuf>>>;
42
43#[derive(Default)]
60struct ExcludedByPattern {
61 file_count: u32,
63 scopes: FxHashMap<PathBuf, u32>,
65}
66
67impl ExcludedByPattern {
68 fn record(&mut self, scope: PathBuf) {
69 self.file_count = self.file_count.saturating_add(1);
70 *self.scopes.entry(scope).or_insert(0) += 1;
71 }
72
73 fn merge(&mut self, other: Self) {
74 self.file_count = self.file_count.saturating_add(other.file_count);
75 for (scope, count) in other.scopes {
76 *self.scopes.entry(scope).or_insert(0) += count;
77 }
78 }
79
80 fn directory_count(&self) -> u32 {
87 u32::try_from(self.scopes.len()).unwrap_or(u32::MAX)
88 }
89
90 fn anchor(&self) -> PathBuf {
94 self.scopes
95 .iter()
96 .max_by(|(left_path, left_count), (right_path, right_count)| {
97 left_count
98 .cmp(right_count)
99 .then_with(|| right_path.cmp(left_path))
100 })
101 .map(|(path, _)| path.clone())
102 .unwrap_or_default()
103 }
104}
105
106type ExclusionTally = FxHashMap<usize, ExcludedByPattern>;
109
110const UNREPORTED_DEFAULT_IGNORES: &[&str] = &["**/node_modules/**", "**/.git/**", VIZ_MAP];
120const VIZ_MAP: &str = "fallow-viz.html";
121
122fn exclusion_scope(relative: &Path, pattern: &str) -> PathBuf {
136 let parent = relative.parent().unwrap_or_else(|| Path::new(""));
137 let Some(literal) = glob_first_literal_segment(pattern) else {
138 return parent.to_path_buf();
139 };
140 let mut prefix = PathBuf::new();
141 let mut deepest = None;
142 for component in parent.components() {
143 prefix.push(component);
144 if component.as_os_str() == OsStr::new(literal) {
145 deepest = Some(prefix.clone());
146 }
147 }
148 deepest.unwrap_or_else(|| parent.to_path_buf())
149}
150
151const NOTE_EXAMPLE_CAP: usize = 5;
155
156const DOTDIR_SCAN_MAX_DEPTH: usize = 2;
161
162const DOTDIR_SCAN_MAX_ENTRIES: usize = 256;
168
169const DOTDIR_SCAN_TOTAL_ENTRIES: usize = 1024;
180
181const DOTDIR_SCAN_MAX_CANDIDATES: usize = 64;
188
189const DOTDIR_MODULE_EXTENSIONS: &[&str] = &[
196 "ts", "tsx", "mts", "cts", "gts", "js", "jsx", "mjs", "cjs", "gjs", "vue", "svelte", "astro",
197 "mdx",
198];
199
200const LARGE_SET_THRESHOLD: usize = 20_000;
204
205const LARGE_FILE_NOTE_BYTES: u64 = 4 * 1024 * 1024;
210
211const NOTE_FILE_FLOOR_BYTES: u64 = 256 * 1024;
215
216const MINIFIED_FILE_SKIP_BYTES: u64 = 1024 * 1024;
220
221const MINIFIED_SAMPLE_BYTES: usize = 256 * 1024;
223
224const MINIFIED_LONG_LINE_BYTES: usize = 128 * 1024;
227
228fn is_declaration_file(path: &Path) -> bool {
233 let name = path.file_name().and_then(|n| n.to_str()).unwrap_or("");
234 name.ends_with(".d.ts") || name.ends_with(".d.mts") || name.ends_with(".d.cts")
235}
236
237fn is_plain_js_file(path: &Path) -> bool {
238 matches!(
239 path.extension().and_then(|ext| ext.to_str()),
240 Some("js" | "mjs" | "cjs")
241 )
242}
243
244fn has_minified_line_shape(path: &Path) -> bool {
245 use std::io::Read;
246
247 let Ok(mut file) = std::fs::File::open(path) else {
248 return false;
249 };
250 let mut sample = vec![0; MINIFIED_SAMPLE_BYTES];
251 let Ok(len) = file.read(&mut sample) else {
252 return false;
253 };
254 sample.truncate(len);
255 if sample.is_empty() {
256 return false;
257 }
258
259 let mut current_line = 0usize;
260 for byte in sample {
261 if byte == b'\n' || byte == b'\r' {
262 current_line = 0;
263 continue;
264 }
265 current_line += 1;
266 if current_line >= MINIFIED_LONG_LINE_BYTES {
267 return true;
268 }
269 }
270 false
271}
272
273fn is_probably_minified_generated_js(path: &Path, size_bytes: u64) -> bool {
274 size_bytes >= MINIFIED_FILE_SKIP_BYTES
275 && is_plain_js_file(path)
276 && !is_declaration_file(path)
277 && has_minified_line_shape(path)
278}
279
280fn format_size_mb(bytes: u64) -> String {
282 #[expect(
283 clippy::cast_precision_loss,
284 reason = "display-only size figure; precision loss past 2^53 bytes is irrelevant"
285 )]
286 let mb = bytes as f64 / (1024.0 * 1024.0);
287 format!("{mb:.1} MB")
288}
289
290fn summarize_examples(root: &Path, examples: &[SizedFile]) -> String {
293 let shown: Vec<String> = examples
294 .iter()
295 .take(NOTE_EXAMPLE_CAP)
296 .map(|(path, size)| {
297 let display = display_relative(root, path);
298 format!("{display} ({})", format_size_mb(*size))
299 })
300 .collect();
301 let remaining = examples.len().saturating_sub(NOTE_EXAMPLE_CAP);
302 if remaining > 0 {
303 format!("{}, and {remaining} more", shown.join(", "))
304 } else {
305 shown.join(", ")
306 }
307}
308
309fn partition_by_size(
312 raw: Vec<SizedFile>,
313 max_file_size_bytes: Option<u64>,
314) -> (Vec<SizedFile>, Vec<SizedFile>) {
315 let Some(limit) = max_file_size_bytes else {
316 return (raw, Vec::new());
317 };
318 raw.into_iter()
319 .partition(|(path, size)| *size <= limit || is_declaration_file(path))
320}
321
322fn partition_minified_generated_js(
325 raw: Vec<SizedFile>,
326 max_file_size_bytes: Option<u64>,
327) -> (Vec<SizedFile>, Vec<SizedFile>) {
328 if max_file_size_bytes.is_none() {
329 return (raw, Vec::new());
330 }
331 raw.into_iter()
332 .partition(|(path, size)| !is_probably_minified_generated_js(path, *size))
333}
334
335fn report_missing_node_modules(config: &ResolvedConfig) -> Vec<WorkspaceDiagnostic> {
338 let Some(diagnostic) = fallow_config::missing_node_modules_diagnostic(&config.root) else {
339 return Vec::new();
340 };
341 if !config.quiet
342 && should_emit_note_once(format!("node-modules-missing::{}", config.root.display()))
343 {
344 tracing::warn!("fallow: {}", diagnostic.message);
345 }
346 vec![diagnostic]
347}
348
349fn report_skipped_large_files(
351 config: &ResolvedConfig,
352 skipped: &[SizedFile],
353) -> Vec<WorkspaceDiagnostic> {
354 if skipped.is_empty() {
355 return Vec::new();
356 }
357 let diagnostics: Vec<WorkspaceDiagnostic> = skipped
358 .iter()
359 .map(|(path, size_bytes)| {
360 WorkspaceDiagnostic::new(
361 &config.root,
362 path.clone(),
363 WorkspaceDiagnosticKind::SkippedLargeFile {
364 size_bytes: *size_bytes,
365 },
366 )
367 })
368 .collect();
369
370 let mut sorted: Vec<SizedFile> = skipped.to_vec();
371 sorted.sort_unstable_by_key(|f| std::cmp::Reverse(f.1));
372 let count = skipped.len();
373 if !config.quiet
374 && should_emit_note_once(format!(
375 "skip::{}::{count}::{}",
376 config.root.display(),
377 sorted.first().map_or(0, |f| f.1)
378 ))
379 {
380 let examples = summarize_examples(&config.root, &sorted);
381 let noun = if count == 1 { "file" } else { "files" };
382 tracing::warn!(
383 "fallow: skipped {count} {noun} over the max file size limit ({examples}). \
384 Raise the limit with --max-file-size <MB> (or FALLOW_MAX_FILE_SIZE), or add them to ignorePatterns."
385 );
386 }
387 diagnostics
388}
389
390fn report_skipped_minified_files(
393 config: &ResolvedConfig,
394 skipped: &[SizedFile],
395) -> Vec<WorkspaceDiagnostic> {
396 if skipped.is_empty() {
397 return Vec::new();
398 }
399 let diagnostics: Vec<WorkspaceDiagnostic> = skipped
400 .iter()
401 .map(|(path, size_bytes)| {
402 WorkspaceDiagnostic::new(
403 &config.root,
404 path.clone(),
405 WorkspaceDiagnosticKind::SkippedMinifiedFile {
406 size_bytes: *size_bytes,
407 },
408 )
409 })
410 .collect();
411
412 let mut sorted: Vec<SizedFile> = skipped.to_vec();
413 sorted.sort_unstable_by_key(|f| std::cmp::Reverse(f.1));
414 let count = skipped.len();
415 if !config.quiet
416 && should_emit_note_once(format!(
417 "minified::{}::{count}::{}",
418 config.root.display(),
419 sorted.first().map_or(0, |f| f.1)
420 ))
421 {
422 let examples = summarize_examples(&config.root, &sorted);
423 let noun = if count == 1 { "file" } else { "files" };
424 let pronoun = if count == 1 { "it" } else { "them" };
425 tracing::warn!(
426 "fallow: skipped {count} minified generated JS {noun} ({examples}). \
427 Add {pronoun} to ignorePatterns, rename {pronoun} with a .min.js suffix, or use --max-file-size 0 to analyze {pronoun}."
428 );
429 }
430 diagnostics
431}
432
433fn summarize_paths(root: &Path, examples: &[&PathBuf]) -> String {
437 let shown: Vec<String> = examples
438 .iter()
439 .take(NOTE_EXAMPLE_CAP)
440 .map(|path| display_relative(root, path))
441 .collect();
442 let remaining = examples.len().saturating_sub(NOTE_EXAMPLE_CAP);
443 if remaining > 0 {
444 format!("{}, and {remaining} more", shown.join(", "))
445 } else {
446 shown.join(", ")
447 }
448}
449
450fn summarize_paths_open_ended(root: &Path, examples: &[&PathBuf]) -> String {
453 let shown: Vec<String> = examples
454 .iter()
455 .take(NOTE_EXAMPLE_CAP)
456 .map(|path| display_relative(root, path))
457 .collect();
458 if examples.len() > NOTE_EXAMPLE_CAP {
459 format!("{}, and more", shown.join(", "))
460 } else {
461 shown.join(", ")
462 }
463}
464
465fn is_excluded_from_analysis(
483 config: &ResolvedConfig,
484 production_excludes: Option<&globset::GlobSet>,
485 path: &Path,
486) -> bool {
487 let relative = path.strip_prefix(&config.root).unwrap_or(path);
488 config.ignore_patterns.is_match(relative)
489 || production_excludes.is_some_and(|excludes| excludes.is_match(relative))
490}
491
492fn has_module_extension(path: &Path) -> bool {
496 path.extension()
497 .and_then(OsStr::to_str)
498 .is_some_and(|ext| DOTDIR_MODULE_EXTENSIONS.contains(&ext))
499}
500
501fn scan_for_reportable_source(
524 config: &ResolvedConfig,
525 production_excludes: Option<&globset::GlobSet>,
526 dir: &Path,
527 budget: &mut usize,
528) -> bool {
529 let mut builder = fallow_config::source_walk_builder(dir);
530 builder
531 .follow_links(false)
532 .max_depth(Some(DOTDIR_SCAN_MAX_DEPTH + 1))
533 .threads(1);
534 builder.filter_entry(|entry| {
535 if entry.depth() == 0 || !entry.file_type().is_some_and(|ft| ft.is_dir()) {
536 return true;
537 }
538 entry
539 .file_name()
540 .to_str()
541 .is_none_or(|name| !SCRIPT_SCOPE_DENYLIST.contains(&name) && name != "node_modules")
542 });
543
544 let mut per_dotdir = DOTDIR_SCAN_MAX_ENTRIES;
545 for entry in builder.build() {
546 if per_dotdir == 0 || *budget == 0 {
547 return false;
548 }
549 per_dotdir -= 1;
550 *budget -= 1;
551 let Ok(entry) = entry else {
552 continue;
553 };
554 #[expect(
557 clippy::filetype_is_file,
558 reason = "regular files only is the point: !is_dir() would readmit fifos and sockets"
559 )]
560 let is_regular_file = entry
561 .file_type()
562 .is_some_and(|file_type| file_type.is_file());
563 if !is_regular_file {
564 continue;
565 }
566 if has_module_extension(entry.path())
567 && !is_excluded_from_analysis(config, production_excludes, entry.path())
568 {
569 return true;
570 }
571 }
572 false
573}
574
575const DOTDIR_NOISE_PATH_COMPONENTS: &[&str] = &[
579 "__fixtures__",
580 "__mocks__",
581 "__tests__",
582 "e2e",
583 "fixture",
584 "fixtures",
585 "playground",
586 "playgrounds",
587 "spec",
588 "test",
589 "tests",
590];
591
592fn dotdir_is_scan_candidate(config: &ResolvedConfig, dir: &Path) -> bool {
599 let Some(name) = dir.file_name().and_then(OsStr::to_str) else {
600 return false;
601 };
602 if SCRIPT_SCOPE_DENYLIST.contains(&name) {
603 return false;
604 }
605 let relative = dir.strip_prefix(&config.root).unwrap_or(dir);
606 if relative
610 .components()
611 .any(|component| component.as_os_str() == OsStr::new("node_modules"))
612 {
613 return false;
614 }
615 !relative.components().any(|component| {
624 DOTDIR_NOISE_PATH_COMPONENTS.contains(&component.as_os_str().to_string_lossy().as_ref())
625 })
626}
627
628fn report_default_ignore_exclusions(
637 config: &ResolvedConfig,
638 tally: &ExclusionTally,
639) -> Vec<WorkspaceDiagnostic> {
640 let mut indices: Vec<usize> = tally.keys().copied().collect();
641 indices.sort_unstable();
642 indices
643 .into_iter()
644 .filter_map(|index| {
645 let excluded = tally.get(&index)?;
646 let pattern = DEFAULT_IGNORE_PATTERNS.get(index)?;
647 Some(
648 WorkspaceDiagnostic::new(
649 &config.root,
650 config.root.join(excluded.anchor()),
651 WorkspaceDiagnosticKind::ExcludedByDefaultIgnore {
652 pattern: (*pattern).to_owned(),
653 file_count: excluded.file_count,
654 directory_count: excluded.directory_count(),
655 },
656 )
657 .into_root_relative(&config.root),
664 )
665 })
666 .collect()
667}
668
669fn report_no_source_files_analyzed(
682 config: &ResolvedConfig,
683 analyzed_file_count: usize,
684 tally: &ExclusionTally,
685) -> Vec<WorkspaceDiagnostic> {
686 if analyzed_file_count > 0 {
687 return Vec::new();
688 }
689 let excluded_file_count = tally.values().map(|excluded| excluded.file_count).sum();
690 vec![
691 WorkspaceDiagnostic::new(
692 &config.root,
693 config.root.clone(),
694 WorkspaceDiagnosticKind::NoSourceFilesAnalyzed {
695 excluded_file_count,
696 },
697 )
698 .into_root_relative(&config.root),
699 ]
700}
701
702fn report_skipped_source_dotdirs(
708 config: &ResolvedConfig,
709 production_excludes: Option<&globset::GlobSet>,
710 candidates: &[PathBuf],
711) -> Vec<WorkspaceDiagnostic> {
712 if candidates.is_empty() {
713 return Vec::new();
714 }
715 let mut budget = DOTDIR_SCAN_TOTAL_ENTRIES;
718 let scannable: Vec<&PathBuf> = candidates
719 .iter()
720 .filter(|dir| dotdir_is_scan_candidate(config, dir))
721 .collect();
722 let reportable: Vec<&PathBuf> = scannable
723 .iter()
724 .copied()
725 .take(DOTDIR_SCAN_MAX_CANDIDATES)
726 .filter(|dir| scan_for_reportable_source(config, production_excludes, dir, &mut budget))
727 .collect();
728 let truncated = scannable.len() > DOTDIR_SCAN_MAX_CANDIDATES || budget == 0;
731 if reportable.is_empty() {
732 return Vec::new();
733 }
734
735 let diagnostics: Vec<WorkspaceDiagnostic> = reportable
736 .iter()
737 .map(|dir| {
738 WorkspaceDiagnostic::new(
739 &config.root,
740 (*dir).clone(),
741 WorkspaceDiagnosticKind::SkippedSourceDotdir,
742 )
743 })
744 .collect();
745
746 let count = reportable.len();
747 if !config.quiet
748 && should_emit_note_once(format!(
749 "dotdir::{}::{count}::{}",
750 config.root.display(),
751 reportable
752 .first()
753 .map_or_else(String::new, |dir| display_relative(&config.root, dir))
754 ))
755 {
756 tracing::warn!(
757 "{}",
758 build_skipped_dotdirs_note(&config.root, &reportable, truncated)
759 );
760 }
761 diagnostics
762}
763
764fn build_skipped_dotdirs_note(root: &Path, reportable: &[&PathBuf], truncated: bool) -> String {
774 let count = reportable.len();
775 let examples = if truncated {
779 summarize_paths_open_ended(root, reportable)
780 } else {
781 summarize_paths(root, reportable)
782 };
783 let noun = if count == 1 {
784 "directory"
785 } else {
786 "directories"
787 };
788 let verb = if count == 1 { "contains" } else { "contain" };
789 let at_least = if truncated { "at least " } else { "" };
790 let (target, pronoun) = match reportable {
791 [only] => (display_relative(root, only), "it"),
792 _ => ("<dir>".to_owned(), "one"),
793 };
794 format!(
795 "fallow: skipped {at_least}{count} hidden {noun} that {verb} source files ({examples}). \
796 Hidden directories are not traversed, so a file, export or dependency used only \
797 there can be reported as unused: add '!{target}/**' to ignorePatterns to analyze \
798 {pronoun}, or add it to entry, ignoreExports or ignoreDependencies, or add \
799 '{target}/**' to ignorePatterns to silence this. fallow --root {target} analyzes \
800 {pronoun} on its own and does not fix this run."
801 )
802}
803
804fn build_largest_files_note(root: &Path, files: &[DiscoveredFile]) -> Option<String> {
809 if files.is_empty() {
810 return None;
811 }
812 let largest = files.iter().map(|f| f.size_bytes).max().unwrap_or(0);
813 if files.len() <= LARGE_SET_THRESHOLD && largest < LARGE_FILE_NOTE_BYTES {
814 return None;
815 }
816 let count = files.len();
817 let noun = if count == 1 { "file" } else { "files" };
818 let mut by_size: Vec<SizedFile> = files
819 .iter()
820 .filter(|f| f.size_bytes >= NOTE_FILE_FLOOR_BYTES)
821 .map(|f| (f.path.clone(), f.size_bytes))
822 .collect();
823 by_size.sort_unstable_by_key(|f| std::cmp::Reverse(f.1));
824 if by_size.is_empty() {
825 return Some(format!(
828 "fallow: discovered {count} {noun}. If analysis stalls or runs out of memory, \
829 exclude large generated files via ignorePatterns or --max-file-size."
830 ));
831 }
832 let examples = summarize_examples(root, &by_size);
833 Some(format!(
834 "fallow: discovered {count} {noun}; largest: {examples}. If analysis stalls or runs out of memory, \
835 exclude large generated files via ignorePatterns or --max-file-size."
836 ))
837}
838
839fn note_largest_files(config: &ResolvedConfig, files: &[DiscoveredFile]) {
844 if config.quiet {
845 return;
846 }
847 if let Some(message) = build_largest_files_note(&config.root, files)
848 && should_emit_note_once(format!("note::{}::{}", config.root.display(), files.len()))
849 {
850 tracing::warn!("{message}");
851 }
852}
853
854#[derive(Debug, Clone, Copy, PartialEq, Eq)]
856pub enum HiddenDirMatch {
857 AnyDepth,
863 ExactPath,
870}
871
872#[derive(Debug, Clone, PartialEq, Eq)]
874pub struct HiddenDirScope {
875 root: PathBuf,
876 dirs: Vec<String>,
877 match_mode: HiddenDirMatch,
878}
879
880impl HiddenDirScope {
881 #[must_use]
888 pub fn new(root: PathBuf, dirs: Vec<String>) -> Self {
889 Self {
890 root,
891 dirs,
892 match_mode: HiddenDirMatch::AnyDepth,
893 }
894 }
895
896 #[must_use]
899 pub fn new_exact_paths(root: PathBuf, dirs: Vec<String>) -> Self {
900 Self {
901 root,
902 dirs,
903 match_mode: HiddenDirMatch::ExactPath,
904 }
905 }
906
907 #[must_use]
912 pub fn with_match_mode(root: PathBuf, dirs: Vec<String>, match_mode: HiddenDirMatch) -> Self {
913 Self {
914 root,
915 dirs,
916 match_mode,
917 }
918 }
919
920 #[must_use]
921 pub fn root(&self) -> &Path {
922 &self.root
923 }
924
925 #[must_use]
926 pub fn dirs(&self) -> &[String] {
927 &self.dirs
928 }
929
930 #[must_use]
931 pub fn match_mode(&self) -> HiddenDirMatch {
932 self.match_mode
933 }
934
935 fn allows(&self, path: &Path, name: &OsStr) -> bool {
936 match self.match_mode {
937 HiddenDirMatch::AnyDepth => {
938 path.starts_with(&self.root) && self.dirs.iter().any(|dir| OsStr::new(dir) == name)
939 }
940 HiddenDirMatch::ExactPath => {
941 let Ok(relative) = path.strip_prefix(&self.root) else {
944 return false;
945 };
946 self.dirs.iter().any(|dir| Path::new(dir) == relative)
947 }
948 }
949 }
950}
951
952struct FileVisitor<'a> {
959 root: &'a Path,
960 canonical_root: Option<&'a Path>,
961 ignore_patterns: &'a IgnorePatternSet,
962 user_ignore_pattern_count: usize,
965 hidden_dir_scopes: &'a [HiddenDirScope],
969 production_excludes: &'a Option<globset::GlobSet>,
970 shared: &'a Mutex<Vec<(std::path::PathBuf, u64)>>,
971 config_shared: Option<&'a Mutex<Vec<std::path::PathBuf>>>,
972 excluded_shared: &'a Mutex<ExclusionTally>,
973 local: Vec<(std::path::PathBuf, u64)>,
974 config_local: Vec<std::path::PathBuf>,
975 excluded_local: ExclusionTally,
976 match_buf: Vec<usize>,
979}
980
981impl FileVisitor<'_> {
982 fn record_ignore_exclusion(&mut self, relative: &Path, is_source: bool) {
989 if (!is_source && self.user_ignore_pattern_count == 0)
995 || relative
996 .components()
997 .any(|component| component.as_os_str() == OsStr::new("node_modules"))
998 {
999 return;
1000 }
1001 self.match_buf.clear();
1002 self.ignore_patterns
1003 .excluded_matches_into(relative, &mut self.match_buf);
1004 let Some(&first) = self.match_buf.first().filter(|_| is_source) else {
1007 return;
1008 };
1009 if first < self.user_ignore_pattern_count {
1010 return;
1013 }
1014 let Some(pattern) = DEFAULT_IGNORE_PATTERNS.get(first - self.user_ignore_pattern_count)
1015 else {
1016 return;
1017 };
1018 if UNREPORTED_DEFAULT_IGNORES.contains(pattern) {
1019 return;
1020 }
1021 self.excluded_local
1022 .entry(first - self.user_ignore_pattern_count)
1023 .or_default()
1024 .record(exclusion_scope(relative, pattern));
1025 }
1026}
1027
1028impl ignore::ParallelVisitor for FileVisitor<'_> {
1029 fn visit(&mut self, result: Result<ignore::DirEntry, ignore::Error>) -> ignore::WalkState {
1030 let Ok(entry) = result else {
1031 return ignore::WalkState::Continue;
1032 };
1033 if entry.file_type().is_some_and(|ft| ft.is_dir()) {
1034 return ignore::WalkState::Continue;
1035 }
1036 let relative = entry
1037 .path()
1038 .strip_prefix(self.root)
1039 .unwrap_or_else(|_| entry.path());
1040 if self.ignore_patterns.is_match(relative) {
1041 self.record_ignore_exclusion(relative, has_source_extension(entry.path()));
1042 return ignore::WalkState::Continue;
1043 }
1044 if self
1045 .production_excludes
1046 .as_ref()
1047 .is_some_and(|excludes| excludes.is_match(relative))
1048 {
1049 return ignore::WalkState::Continue;
1050 }
1051 if self.ignore_patterns.has_exceptions()
1052 && is_under_exception_only_hidden_dir(self.root, entry.path(), self.hidden_dir_scopes)
1053 && !self.ignore_patterns.is_lifted(relative)
1054 {
1055 return ignore::WalkState::Continue;
1056 }
1057 let symlink_size = if entry.file_type().is_some_and(|ft| ft.is_symlink()) {
1058 let Some(size) = contained_symlink_file_size(entry.path(), self.canonical_root) else {
1059 tracing::debug!(
1060 path = %entry.path().display(),
1061 "skipping source symlink with a broken, non-file, or outside-root target"
1062 );
1063 return ignore::WalkState::Continue;
1064 };
1065 Some(size)
1066 } else {
1067 None
1068 };
1069 if has_source_extension(entry.path()) {
1070 let size_bytes =
1071 symlink_size.unwrap_or_else(|| entry.metadata().map_or(0, |m| m.len()));
1072 self.local.push((entry.into_path(), size_bytes));
1073 } else if self.config_shared.is_some() {
1074 self.config_local.push(entry.into_path());
1077 }
1078 ignore::WalkState::Continue
1079 }
1080}
1081
1082fn contained_symlink_file_size(path: &Path, canonical_root: Option<&Path>) -> Option<u64> {
1083 let root = canonical_root?;
1084 let target = path.canonicalize().ok()?;
1085 if !target.starts_with(root) {
1086 return None;
1087 }
1088 let metadata = target.metadata().ok()?;
1089 metadata.is_file().then_some(metadata.len())
1090}
1091
1092impl Drop for FileVisitor<'_> {
1093 #[expect(
1094 clippy::expect_used,
1095 reason = "poisoned walk collector lock means worker state is unrecoverable"
1096 )]
1097 fn drop(&mut self) {
1098 if !self.local.is_empty() {
1099 self.shared
1100 .lock()
1101 .expect("walk collector lock poisoned")
1102 .append(&mut self.local);
1103 }
1104 if let Some(config_shared) = self.config_shared
1105 && !self.config_local.is_empty()
1106 {
1107 config_shared
1108 .lock()
1109 .expect("walk config collector lock poisoned")
1110 .append(&mut self.config_local);
1111 }
1112 if !self.excluded_local.is_empty() {
1113 let mut shared = self
1114 .excluded_shared
1115 .lock()
1116 .expect("walk exclusion collector lock poisoned");
1117 for (index, tally) in std::mem::take(&mut self.excluded_local) {
1118 shared.entry(index).or_default().merge(tally);
1119 }
1120 }
1121 }
1122}
1123
1124struct FileVisitorBuilder<'a> {
1126 root: &'a Path,
1127 canonical_root: Option<&'a Path>,
1128 ignore_patterns: &'a IgnorePatternSet,
1129 user_ignore_pattern_count: usize,
1130 hidden_dir_scopes: &'a [HiddenDirScope],
1131 production_excludes: &'a Option<globset::GlobSet>,
1132 shared: &'a Mutex<Vec<(std::path::PathBuf, u64)>>,
1133 config_shared: Option<&'a Mutex<Vec<std::path::PathBuf>>>,
1134 excluded_shared: &'a Mutex<ExclusionTally>,
1135}
1136
1137impl<'s> ignore::ParallelVisitorBuilder<'s> for FileVisitorBuilder<'s> {
1138 fn build(&mut self) -> Box<dyn ignore::ParallelVisitor + 's> {
1139 Box::new(FileVisitor {
1140 root: self.root,
1141 canonical_root: self.canonical_root,
1142 ignore_patterns: self.ignore_patterns,
1143 user_ignore_pattern_count: self.user_ignore_pattern_count,
1144 hidden_dir_scopes: self.hidden_dir_scopes,
1145 production_excludes: self.production_excludes,
1146 shared: self.shared,
1147 config_shared: self.config_shared,
1148 excluded_shared: self.excluded_shared,
1149 local: Vec::new(),
1150 config_local: Vec::new(),
1151 excluded_local: ExclusionTally::default(),
1152 match_buf: Vec::new(),
1153 })
1154 }
1155}
1156
1157pub const SOURCE_EXTENSIONS: &[&str] = &[
1159 "ts", "tsx", "mts", "cts", "gts", "js", "jsx", "mjs", "cjs", "gjs", "vue", "svelte", "astro",
1160 "mdx", "css", "scss", "sass", "less", "html", "graphql", "gql",
1161];
1162
1163pub const PRODUCTION_EXCLUDE_PATTERNS: &[&str] = &[
1165 "**/*.test.*",
1166 "**/*.spec.*",
1167 "**/*.e2e.*",
1168 "**/*.e2e-spec.*",
1169 "**/*.bench.*",
1170 "**/*.fixture.*",
1171 "**/*.stories.*",
1172 "**/*.story.*",
1173 "**/__tests__/**",
1174 "**/__mocks__/**",
1175 "**/__snapshots__/**",
1176 "**/__fixtures__/**",
1177 "**/test-d/**",
1178 "**/test/**",
1179 "**/tests/**",
1180 "*.config.*",
1181 "**/.*.js",
1182 "**/.*.ts",
1183 "**/.*.mjs",
1184 "**/.*.cjs",
1185];
1186
1187pub fn is_allowed_hidden_dir(name: &OsStr) -> bool {
1189 ALLOWED_HIDDEN_DIRS.iter().any(|&d| OsStr::new(d) == name)
1190}
1191
1192fn is_allowed_scoped_hidden_dir(
1193 name: &OsStr,
1194 path: &Path,
1195 additional_hidden_dir_scopes: &[HiddenDirScope],
1196) -> bool {
1197 additional_hidden_dir_scopes
1198 .iter()
1199 .any(|scope| scope.allows(path, name))
1200}
1201
1202const YARN_PNP_GENERATED_FILES: &[&str] = &[".pnp.cjs", ".pnp.loader.mjs"];
1207
1208fn is_yarn_pnp_generated_file(name: &OsStr) -> bool {
1209 YARN_PNP_GENERATED_FILES
1210 .iter()
1211 .any(|&f| OsStr::new(f) == name)
1212}
1213
1214fn is_allowed_hidden_with_scopes(
1220 entry: &ignore::DirEntry,
1221 additional_hidden_dir_scopes: &[HiddenDirScope],
1222) -> bool {
1223 let name = entry.file_name();
1224 let name_str = name.to_string_lossy();
1225
1226 if !name_str.starts_with('.') {
1227 return true;
1228 }
1229
1230 if entry.file_type().is_some_and(|ft| !ft.is_dir()) {
1231 return !is_yarn_pnp_generated_file(name);
1232 }
1233
1234 is_allowed_hidden_dir(name)
1235 || is_allowed_scoped_hidden_dir(name, entry.path(), additional_hidden_dir_scopes)
1236}
1237
1238fn is_under_exception_only_hidden_dir(
1243 root: &Path,
1244 path: &Path,
1245 hidden_dir_scopes: &[HiddenDirScope],
1246) -> bool {
1247 path.ancestors()
1248 .skip(1)
1249 .take_while(|dir| *dir != root && dir.starts_with(root))
1250 .any(|dir| {
1251 dir.file_name().is_some_and(|name| {
1252 name.to_string_lossy().starts_with('.')
1253 && !is_allowed_hidden_dir(name)
1254 && !is_allowed_scoped_hidden_dir(name, dir, hidden_dir_scopes)
1255 })
1256 })
1257}
1258
1259pub fn discover_files(config: &ResolvedConfig) -> Vec<DiscoveredFile> {
1265 discover_files_with_additional_hidden_dirs(config, &[])
1266}
1267
1268fn config_candidate_basename_globs() -> &'static [String] {
1280 static GLOBS: OnceLock<Vec<String>> = OnceLock::new();
1281 GLOBS.get_or_init(|| {
1282 let mut set: FxHashSet<String> = FxHashSet::default();
1283 for plugin in crate::plugins::registry::builtin::create_builtin_plugins() {
1284 for pattern in plugin.config_patterns() {
1285 let basename = pattern.rsplit('/').next().unwrap_or(pattern);
1286 set.insert(basename.to_string());
1287 }
1288 if plugin.tooling_dependencies().is_empty() {
1292 continue;
1293 }
1294 for pattern in plugin.always_used() {
1295 let basename = pattern.rsplit('/').next().unwrap_or(pattern);
1296 if !basename.starts_with('!') && !basename.contains(['*', '?', '[']) {
1297 set.insert(basename.to_string());
1298 }
1299 }
1300 }
1301 let mut globs: Vec<String> = set.into_iter().collect();
1302 globs.sort_unstable();
1303 globs
1304 })
1305}
1306
1307fn has_source_extension(path: &Path) -> bool {
1310 path.extension()
1311 .and_then(OsStr::to_str)
1312 .is_some_and(|ext| SOURCE_EXTENSIONS.contains(&ext))
1313}
1314
1315#[expect(
1319 clippy::expect_used,
1320 reason = "source file globs are hard-coded compile-time constants"
1321)]
1322fn build_walk_types(capture_config: bool) -> ignore::types::Types {
1323 static SOURCE_TYPES: OnceLock<ignore::types::Types> = OnceLock::new();
1324 static SOURCE_AND_CONFIG_TYPES: OnceLock<ignore::types::Types> = OnceLock::new();
1325
1326 let cache = if capture_config {
1327 &SOURCE_AND_CONFIG_TYPES
1328 } else {
1329 &SOURCE_TYPES
1330 };
1331 cache
1332 .get_or_init(|| {
1333 let mut types_builder = ignore::types::TypesBuilder::new();
1334 let source_glob = format!("*.{{{}}}", SOURCE_EXTENSIONS.join(","));
1335 types_builder
1336 .add("source", &source_glob)
1337 .expect("valid glob");
1338 types_builder.select("source");
1339 if capture_config {
1340 for glob in config_candidate_basename_globs() {
1341 let _ = types_builder.add("config", glob);
1345 }
1346 types_builder.select("config");
1347 }
1348 types_builder.build().expect("valid types")
1349 })
1350 .clone()
1351}
1352
1353fn build_source_walk_builder(
1357 config: &ResolvedConfig,
1358 additional_hidden_dir_scopes: &[HiddenDirScope],
1359 capture_config: bool,
1360 skipped_dotdirs: &SkippedDotdirSink,
1361) -> WalkBuilder {
1362 let mut walk_builder = fallow_config::source_walk_builder(&config.root);
1363 walk_builder
1364 .types(build_walk_types(capture_config))
1365 .threads(config.threads);
1366 let scopes = additional_hidden_dir_scopes.to_vec();
1371 let sink = Arc::clone(skipped_dotdirs);
1372 let root = config.root.clone();
1373 let ignore_patterns = config.ignore_patterns.clone();
1374 walk_builder.filter_entry(move |entry| {
1375 if is_allowed_hidden_with_scopes(entry, &scopes) {
1376 return true;
1377 }
1378 if ignore_patterns.has_exceptions()
1382 && entry.file_type().is_some_and(|ft| ft.is_dir())
1383 && ignore_patterns.admits_hidden_dir(
1384 entry
1385 .path()
1386 .strip_prefix(&root)
1387 .unwrap_or_else(|_| entry.path()),
1388 )
1389 {
1390 return true;
1391 }
1392 if entry.file_type().is_some_and(|ft| ft.is_dir())
1393 && let Ok(mut collected) = sink.lock()
1394 {
1395 collected.push(entry.path().to_path_buf());
1396 }
1397 false
1398 });
1399 walk_builder
1400}
1401
1402pub fn discover_files_with_additional_hidden_dirs(
1408 config: &ResolvedConfig,
1409 additional_hidden_dir_scopes: &[HiddenDirScope],
1410) -> Vec<DiscoveredFile> {
1411 discover_files_and_config_candidates(config, additional_hidden_dir_scopes).0
1412}
1413
1414pub fn discover_files_and_config_candidates(
1430 config: &ResolvedConfig,
1431 additional_hidden_dir_scopes: &[HiddenDirScope],
1432) -> (Vec<DiscoveredFile>, Vec<PathBuf>) {
1433 let discovered =
1434 discover_files_config_candidates_and_diagnostics(config, additional_hidden_dir_scopes);
1435 (discovered.files, discovered.config_candidates)
1436}
1437
1438pub struct DiscoveredSources {
1447 pub files: Vec<DiscoveredFile>,
1449 pub config_candidates: Vec<PathBuf>,
1451 pub diagnostics: Vec<WorkspaceDiagnostic>,
1454}
1455
1456#[expect(
1464 clippy::cast_possible_truncation,
1465 reason = "file count is bounded by project size, well under u32::MAX"
1466)]
1467#[expect(clippy::expect_used, reason = "the collector lock must remain usable")]
1468pub fn discover_files_config_candidates_and_diagnostics(
1469 config: &ResolvedConfig,
1470 additional_hidden_dir_scopes: &[HiddenDirScope],
1471) -> DiscoveredSources {
1472 let _span = tracing::info_span!("discover_files").entered();
1473
1474 let capture_config = !config.production;
1475 let skipped_dotdirs: SkippedDotdirSink = Arc::new(Mutex::new(Vec::new()));
1476 let walk_builder = build_source_walk_builder(
1477 config,
1478 additional_hidden_dir_scopes,
1479 capture_config,
1480 &skipped_dotdirs,
1481 );
1482 let production_excludes = super::production_excludes::build_production_excludes(config);
1483 let canonical_root = config.root.canonicalize().ok();
1484
1485 let collected: Mutex<Vec<(std::path::PathBuf, u64)>> = Mutex::new(Vec::new());
1486 let config_collected: Mutex<Vec<std::path::PathBuf>> = Mutex::new(Vec::new());
1487 let excluded_collected: Mutex<ExclusionTally> = Mutex::new(ExclusionTally::default());
1488 let mut visitor_builder = FileVisitorBuilder {
1489 root: &config.root,
1490 canonical_root: canonical_root.as_deref(),
1491 ignore_patterns: &config.ignore_patterns,
1492 user_ignore_pattern_count: config.user_ignore_pattern_count,
1493 hidden_dir_scopes: additional_hidden_dir_scopes,
1494 production_excludes: &production_excludes,
1495 shared: &collected,
1496 config_shared: capture_config.then_some(&config_collected),
1497 excluded_shared: &excluded_collected,
1498 };
1499 walk_builder.build_parallel().visit(&mut visitor_builder);
1500 config.ignore_patterns.mark_walked();
1501
1502 let mut raw = collected
1503 .into_inner()
1504 .expect("walk collector lock poisoned");
1505 sort_permuted(&mut raw, PermutationSite::Files, |a, b| a.0.cmp(&b.0));
1513
1514 let mut config_candidates = config_collected
1515 .into_inner()
1516 .expect("walk config collector lock poisoned");
1517 sort_permuted(&mut config_candidates, PermutationSite::Configs, Ord::cmp);
1518
1519 let excluded_by_default_ignore = excluded_collected
1520 .into_inner()
1521 .expect("walk exclusion collector lock poisoned");
1522
1523 let mut dotdir_candidates = skipped_dotdirs
1527 .lock()
1528 .map_or_else(|_| Vec::new(), |mut guard| std::mem::take(&mut *guard));
1529 sort_permuted(&mut dotdir_candidates, PermutationSite::Dotdirs, Ord::cmp);
1530 dotdir_candidates.dedup();
1531
1532 let (kept, skipped) = partition_by_size(raw, config.max_file_size_bytes);
1533 let (kept, skipped_minified) =
1534 partition_minified_generated_js(kept, config.max_file_size_bytes);
1535 let diagnostics = fallow_config::replace_source_discovery_diagnostics(
1540 &config.root,
1541 report_skipped_large_files(config, &skipped)
1542 .into_iter()
1543 .chain(report_skipped_minified_files(config, &skipped_minified))
1544 .chain(report_skipped_source_dotdirs(
1545 config,
1546 production_excludes.as_ref(),
1547 &dotdir_candidates,
1548 ))
1549 .chain(report_default_ignore_exclusions(
1550 config,
1551 &excluded_by_default_ignore,
1552 ))
1553 .chain(report_missing_node_modules(config))
1554 .chain(report_no_source_files_analyzed(
1555 config,
1556 kept.len(),
1557 &excluded_by_default_ignore,
1558 ))
1559 .collect(),
1560 );
1561
1562 let files: Vec<DiscoveredFile> = kept
1563 .into_iter()
1564 .enumerate()
1565 .map(|(idx, (path, size_bytes))| DiscoveredFile {
1566 id: FileId(idx as u32),
1567 path,
1568 size_bytes,
1569 })
1570 .collect();
1571
1572 note_largest_files(config, &files);
1573 super::legacy_glob::warn_legacy_glob_narrowings(config, &files);
1574
1575 DiscoveredSources {
1576 files,
1577 config_candidates,
1578 diagnostics,
1579 }
1580}
1581
1582#[cfg(test)]
1583mod tests {
1584 use std::ffi::OsStr;
1585 use std::path::MAIN_SEPARATOR;
1586
1587 use super::*;
1588
1589 #[test]
1593 fn exclusion_scope_stops_at_the_patterns_literal_directory_segment() {
1594 assert_eq!(
1595 exclusion_scope(
1596 Path::new("projects/app/build/static/js/main.js"),
1597 "**/build/**"
1598 ),
1599 PathBuf::from("projects/app/build")
1600 );
1601 assert_eq!(
1602 exclusion_scope(Path::new("dist/a.ts"), "**/dist/**"),
1603 PathBuf::from("dist")
1604 );
1605 assert_eq!(
1606 exclusion_scope(
1607 Path::new("node_modules/react/index.js"),
1608 "**/node_modules/**"
1609 ),
1610 PathBuf::from("node_modules"),
1611 "the helper is shape-only: `**/node_modules/**` is never tallied, \
1612 but a directory-shaped pattern still collapses to its literal segment"
1613 );
1614 }
1615
1616 #[test]
1622 fn exclusion_scope_takes_the_deepest_matching_segment() {
1623 assert_eq!(
1624 exclusion_scope(Path::new("build/tools/build/a.ts"), "**/build/**"),
1625 PathBuf::from("build/tools/build")
1626 );
1627 assert_eq!(
1628 exclusion_scope(Path::new("dist/pkg/dist/inner/a.ts"), "**/dist/**"),
1629 PathBuf::from("dist/pkg/dist")
1630 );
1631 }
1632
1633 #[test]
1636 fn exclusion_scope_falls_back_to_the_parent_for_a_file_shaped_pattern() {
1637 assert_eq!(
1638 exclusion_scope(Path::new("vendor/a.min.js"), "**/*.min.js"),
1639 PathBuf::from("vendor")
1640 );
1641 assert_eq!(
1642 exclusion_scope(Path::new("a.min.js"), "**/*.min.js"),
1643 PathBuf::new(),
1644 "a root-level match anchors at the root itself"
1645 );
1646 }
1647
1648 #[test]
1652 fn exclusion_scope_falls_back_when_the_literal_segment_is_absent() {
1653 assert_eq!(
1654 exclusion_scope(Path::new("src/nested/a.ts"), "**/build/**"),
1655 PathBuf::from("src/nested")
1656 );
1657 }
1658
1659 #[test]
1663 fn the_directory_count_is_the_number_of_distinct_scopes() {
1664 let mut one_tree = ExcludedByPattern::default();
1665 one_tree.record(PathBuf::from("packages/web/build"));
1666 one_tree.record(PathBuf::from("packages/web/build"));
1667 assert_eq!(one_tree.file_count, 2);
1668 assert_eq!(one_tree.directory_count(), 1);
1669
1670 let mut scattered = ExcludedByPattern::default();
1671 scattered.record(PathBuf::from("packages/a/dist"));
1672 scattered.record(PathBuf::from("packages/b/dist"));
1673 assert_eq!(scattered.directory_count(), 2);
1674 }
1675
1676 #[test]
1680 fn the_anchor_is_the_largest_group_with_ties_broken_by_path() {
1681 let mut tally = ExcludedByPattern::default();
1682 for _ in 0..3 {
1683 tally.record(PathBuf::from("packages/web/build"));
1684 }
1685 tally.record(PathBuf::from("packages/api/build"));
1686 assert_eq!(tally.file_count, 4);
1687 assert_eq!(tally.anchor(), PathBuf::from("packages/web/build"));
1688
1689 let mut tied = ExcludedByPattern::default();
1690 tied.record(PathBuf::from("z/build"));
1691 tied.record(PathBuf::from("a/build"));
1692 assert_eq!(tied.anchor(), PathBuf::from("a/build"));
1693 }
1694
1695 #[test]
1698 fn merging_two_thread_tallies_keeps_the_count_exact() {
1699 let mut left = ExcludedByPattern::default();
1700 left.record(PathBuf::from("dist"));
1701 left.record(PathBuf::from("dist"));
1702 let mut right = ExcludedByPattern::default();
1703 right.record(PathBuf::from("dist"));
1704 right.record(PathBuf::from("packages/ui/dist"));
1705
1706 left.merge(right);
1707 assert_eq!(left.file_count, 4);
1708 assert_eq!(left.scopes.get(Path::new("dist")), Some(&3));
1709 assert_eq!(left.anchor(), PathBuf::from("dist"));
1710 }
1711
1712 #[test]
1713 fn skipped_dotdirs_note_names_the_directory_when_there_is_one() {
1714 let root = Path::new("/repo");
1715 let only = PathBuf::from("/repo/.tooling");
1716 let note = build_skipped_dotdirs_note(root, &[&only], false);
1717 assert!(note.contains("skipped 1 hidden directory that contains source files"));
1718 assert!(note.contains("add it to entry, ignoreExports or ignoreDependencies"));
1719 assert!(note.contains("fallow --root .tooling analyzes it on its own"));
1720 assert!(note.contains("does not fix this run"));
1721 assert!(note.contains("add '.tooling/**' to"));
1722 assert!(note.contains("add '!.tooling/**' to ignorePatterns to analyze it"));
1723 assert!(
1724 !note.contains("<dir>"),
1725 "the single-directory remedy must be copy-pasteable: {note}"
1726 );
1727 }
1728
1729 #[test]
1730 fn skipped_dotdirs_note_pluralizes_and_keeps_the_placeholder() {
1731 let root = Path::new("/repo");
1732 let a = PathBuf::from("/repo/.a");
1733 let b = PathBuf::from("/repo/.b");
1734 let note = build_skipped_dotdirs_note(root, &[&a, &b], false);
1735 assert!(note.contains("skipped 2 hidden directories that contain source files"));
1736 assert!(note.contains("fallow --root <dir> analyzes one on its own"));
1737 }
1738
1739 #[test]
1740 fn skipped_dotdirs_note_drops_the_tail_count_when_truncated() {
1741 let root = Path::new("/repo");
1742 let owned: Vec<PathBuf> = (0..8)
1743 .map(|i| PathBuf::from(format!("/repo/.d{i}")))
1744 .collect();
1745 let reportable: Vec<&PathBuf> = owned.iter().collect();
1746
1747 let bounded = build_skipped_dotdirs_note(root, &reportable, true);
1748 assert!(bounded.contains("skipped at least 8 hidden directories"));
1749 assert!(
1750 bounded.contains("and more") && !bounded.contains("and 3 more"),
1751 "an inexact total must not carry an exact remainder: {bounded}"
1752 );
1753
1754 let complete = build_skipped_dotdirs_note(root, &reportable, false);
1755 assert!(!complete.contains("at least"));
1756 assert!(complete.contains("and 3 more"));
1757 }
1758
1759 #[test]
1760 fn dotdir_noise_path_components_stay_sorted_and_lowercase() {
1761 let mut sorted = DOTDIR_NOISE_PATH_COMPONENTS.to_vec();
1762 sorted.sort_unstable();
1763 assert_eq!(sorted, DOTDIR_NOISE_PATH_COMPONENTS);
1764 for component in DOTDIR_NOISE_PATH_COMPONENTS {
1765 assert!(!component.starts_with('.'), "'{component}' is not hidden");
1766 assert_eq!(
1767 *component,
1768 component.to_lowercase(),
1769 "'{component}' is matched verbatim against a path component"
1770 );
1771 }
1772 }
1773
1774 #[test]
1775 fn script_scope_denylist_stays_disjoint_and_sorted() {
1776 let mut sorted = SCRIPT_SCOPE_DENYLIST.to_vec();
1777 sorted.sort_unstable();
1778 assert_eq!(
1779 sorted, SCRIPT_SCOPE_DENYLIST,
1780 "keep the list sorted so additions stay reviewable"
1781 );
1782 for dir in SCRIPT_SCOPE_DENYLIST {
1783 assert!(dir.starts_with('.'), "'{dir}' is not a hidden directory");
1784 assert!(
1785 !ALLOWED_HIDDEN_DIRS.contains(dir),
1786 "'{dir}' is traversed, so it can never be a skipped candidate"
1787 );
1788 }
1789 }
1790
1791 #[test]
1792 fn dotdir_module_extensions_are_a_subset_of_source_extensions() {
1793 for ext in DOTDIR_MODULE_EXTENSIONS {
1794 assert!(
1795 SOURCE_EXTENSIONS.contains(ext),
1796 "'{ext}' is not discovered as source, so it cannot be a trigger"
1797 );
1798 }
1799 for ext in ["css", "scss", "sass", "less", "html", "graphql", "gql"] {
1800 assert!(
1801 !DOTDIR_MODULE_EXTENSIONS.contains(&ext),
1802 "'{ext}' carries no imports or exports for the message to be about"
1803 );
1804 }
1805 }
1806
1807 fn assign_file_ids(mut raw: Vec<(std::path::PathBuf, u64)>) -> Vec<DiscoveredFile> {
1810 raw.sort_unstable_by(|a, b| a.0.cmp(&b.0));
1811 raw.into_iter()
1812 .enumerate()
1813 .map(|(idx, (path, size_bytes))| DiscoveredFile {
1814 id: FileId(idx as u32),
1815 path,
1816 size_bytes,
1817 })
1818 .collect()
1819 }
1820
1821 #[test]
1827 fn file_id_assignment_is_deterministic_for_identical_file_set() {
1828 let paths = [
1829 "/project/src/z.ts",
1830 "/project/src/a.ts",
1831 "/project/src/components/Button.tsx",
1832 "/project/src/components/Button.module.css",
1833 "/project/index.ts",
1834 ];
1835
1836 let walk_one: Vec<(std::path::PathBuf, u64)> = paths
1838 .iter()
1839 .map(|p| (std::path::PathBuf::from(p), 10))
1840 .collect();
1841 let mut walk_two = walk_one.clone();
1842 walk_two.reverse();
1843
1844 let files_one = assign_file_ids(walk_one);
1845 let files_two = assign_file_ids(walk_two);
1846
1847 assert_eq!(files_one.len(), files_two.len());
1849 for (a, b) in files_one.iter().zip(files_two.iter()) {
1850 assert_eq!(a.id, b.id);
1851 assert_eq!(a.path, b.path);
1852 }
1853
1854 for (idx, file) in files_one.iter().enumerate() {
1857 assert_eq!(file.id, FileId(idx as u32));
1858 }
1859 assert_eq!(
1860 files_one[0].path,
1861 std::path::PathBuf::from("/project/index.ts")
1862 );
1863 }
1864
1865 #[test]
1866 fn file_id_assignment_recomputes_after_rename_or_delete() {
1867 let before = assign_file_ids(vec![
1868 (std::path::PathBuf::from("/project/src/a.ts"), 10),
1869 (std::path::PathBuf::from("/project/src/b.ts"), 10),
1870 (std::path::PathBuf::from("/project/src/c.ts"), 10),
1871 ]);
1872 let after_delete = assign_file_ids(vec![
1873 (std::path::PathBuf::from("/project/src/a.ts"), 10),
1874 (std::path::PathBuf::from("/project/src/c.ts"), 10),
1875 ]);
1876 let after_rename = assign_file_ids(vec![
1877 (std::path::PathBuf::from("/project/src/a.ts"), 10),
1878 (std::path::PathBuf::from("/project/src/c.ts"), 10),
1879 (std::path::PathBuf::from("/project/src/d.ts"), 10),
1880 ]);
1881
1882 assert_eq!(before[0].id, FileId(0));
1883 assert_eq!(before[1].id, FileId(1));
1884 assert_eq!(before[2].id, FileId(2));
1885 assert_eq!(after_delete[0].id, FileId(0));
1886 assert_eq!(after_delete[1].id, FileId(1));
1887 assert_eq!(
1888 after_delete[1].path,
1889 std::path::PathBuf::from("/project/src/c.ts")
1890 );
1891 assert_eq!(after_rename[0].id, FileId(0));
1892 assert_eq!(after_rename[1].id, FileId(1));
1893 assert_eq!(
1894 after_rename[1].path,
1895 std::path::PathBuf::from("/project/src/c.ts")
1896 );
1897 assert_eq!(after_rename[2].id, FileId(2));
1898 assert_eq!(
1899 after_rename[2].path,
1900 std::path::PathBuf::from("/project/src/d.ts")
1901 );
1902 }
1903
1904 #[test]
1905 fn allowed_hidden_dirs() {
1906 assert!(is_allowed_hidden_dir(OsStr::new(".storybook")));
1907 assert!(is_allowed_hidden_dir(OsStr::new(".vitepress")));
1908 assert!(is_allowed_hidden_dir(OsStr::new(".well-known")));
1909 assert!(is_allowed_hidden_dir(OsStr::new(".changeset")));
1910 assert!(is_allowed_hidden_dir(OsStr::new(".github")));
1911 }
1912
1913 #[test]
1914 fn disallowed_hidden_dirs() {
1915 assert!(!is_allowed_hidden_dir(OsStr::new(".git")));
1916 assert!(!is_allowed_hidden_dir(OsStr::new(".cache")));
1917 assert!(!is_allowed_hidden_dir(OsStr::new(".vscode")));
1918 assert!(!is_allowed_hidden_dir(OsStr::new(".fallow")));
1919 assert!(!is_allowed_hidden_dir(OsStr::new(".next")));
1920 }
1921
1922 #[test]
1923 fn non_hidden_dirs_not_in_allowlist() {
1924 assert!(!is_allowed_hidden_dir(OsStr::new("src")));
1925 assert!(!is_allowed_hidden_dir(OsStr::new("node_modules")));
1926 }
1927
1928 #[test]
1929 fn walk_types_match_every_supported_source_extension() {
1930 for capture_config in [false, true] {
1931 let types = build_walk_types(capture_config);
1932 for extension in SOURCE_EXTENSIONS {
1933 let path = format!("packages/ui/src/nested/component.{extension}");
1934 assert!(
1935 types.matched(&path, false).is_whitelist(),
1936 "expected source match for {path} with capture_config={capture_config}"
1937 );
1938 }
1939 }
1940 }
1941
1942 #[test]
1943 fn walk_types_match_typescript_declaration_files() {
1944 let types = build_walk_types(true);
1945 for path in [
1946 "src/env.d.ts",
1947 "packages/app/types/generated.d.mts",
1948 "packages/app/types/compat.d.cts",
1949 ] {
1950 assert!(
1951 types.matched(path, false).is_whitelist(),
1952 "expected declaration source match for {path}"
1953 );
1954 }
1955 }
1956
1957 #[test]
1958 fn walk_types_reject_source_extension_near_misses() {
1959 for capture_config in [false, true] {
1960 let types = build_walk_types(capture_config);
1961 for path in [
1962 "src/component.tsx.bak",
1963 "src/component.tsxmap",
1964 "src/component.TS",
1965 "src/component.gqlx",
1966 "src/component.htm",
1967 "src/component",
1968 "assets/component.png",
1969 ] {
1970 assert!(
1971 types.matched(path, false).is_ignore(),
1972 "expected non-source rejection for {path} with capture_config={capture_config}"
1973 );
1974 }
1975 }
1976 }
1977
1978 #[test]
1979 fn walk_types_keep_config_candidate_selection_separate() {
1980 assert!(
1981 build_walk_types(true)
1982 .matched("packages/app/tsconfig.json", false)
1983 .is_whitelist()
1984 );
1985 assert!(
1986 build_walk_types(false)
1987 .matched("packages/app/tsconfig.json", false)
1988 .is_ignore()
1989 );
1990 }
1991
1992 #[test]
1993 fn source_extensions_are_exactly_the_supported_set() {
1994 let mut actual = SOURCE_EXTENSIONS.to_vec();
1995 actual.sort_unstable();
1996 let mut expected = vec![
1997 "ts", "tsx", "mts", "cts", "gts", "js", "jsx", "mjs", "cjs", "gjs", "vue", "svelte",
1998 "astro", "mdx", "css", "scss", "sass", "less", "html", "graphql", "gql",
1999 ];
2000 expected.sort_unstable();
2001 assert_eq!(actual, expected);
2002 }
2003
2004 fn production_excludes() -> globset::GlobSet {
2006 let config = fallow_config::FallowConfig {
2007 production: true.into(),
2008 ..Default::default()
2009 }
2010 .resolve(
2011 std::path::PathBuf::from("/project"),
2012 fallow_config::OutputFormat::Human,
2013 1,
2014 true,
2015 true,
2016 None,
2017 );
2018 super::super::production_excludes::build_production_excludes(&config)
2019 .expect("production mode builds an exclude set")
2020 }
2021
2022 #[test]
2025 fn production_exclude_patterns_all_compile() {
2026 for pattern in PRODUCTION_EXCLUDE_PATTERNS {
2027 assert!(
2028 globset::GlobBuilder::new(pattern)
2029 .literal_separator(true)
2030 .build()
2031 .is_ok(),
2032 "production exclude pattern does not compile: {pattern}"
2033 );
2034 }
2035 }
2036
2037 #[test]
2038 fn production_excludes_test_files() {
2039 let set = production_excludes();
2040 assert!(set.is_match("src/Button.test.ts"));
2041 assert!(set.is_match("src/utils.spec.tsx"));
2042 assert!(set.is_match("src/__tests__/helper.ts"));
2043 assert!(!set.is_match("src/Button.ts"));
2044 assert!(!set.is_match("src/utils.tsx"));
2045 }
2046
2047 #[test]
2048 fn production_excludes_story_files() {
2049 let set = production_excludes();
2050 assert!(set.is_match("src/Button.stories.tsx"));
2051 assert!(set.is_match("src/Card.story.ts"));
2052 assert!(!set.is_match("src/Button.tsx"));
2053 }
2054
2055 #[test]
2056 fn production_excludes_config_files_at_root_only() {
2057 let set = production_excludes();
2058 assert!(set.is_match("vitest.config.ts"));
2059 assert!(set.is_match("jest.config.js"));
2060 assert!(!set.is_match("src/app/app.config.ts"));
2061 assert!(!set.is_match("src/app/app.config.server.ts"));
2062 assert!(!set.is_match("packages/foo/vitest.config.ts"));
2063 assert!(!set.is_match("src/config.ts"));
2064 }
2065
2066 #[test]
2067 fn disallowed_hidden_dirs_idea() {
2068 assert!(!is_allowed_hidden_dir(OsStr::new(".idea")));
2069 }
2070
2071 #[test]
2072 fn is_declaration_file_matches_dts_variants() {
2073 assert!(is_declaration_file(Path::new("env.d.ts")));
2074 assert!(is_declaration_file(Path::new("src/auto-imports.d.ts")));
2075 assert!(is_declaration_file(Path::new("mod.d.mts")));
2076 assert!(is_declaration_file(Path::new("compat.d.cts")));
2077 assert!(!is_declaration_file(Path::new("index.ts")));
2078 assert!(!is_declaration_file(Path::new("component.tsx")));
2079 assert!(!is_declaration_file(Path::new("notes.d.txt")));
2080 }
2081
2082 #[test]
2083 fn format_size_mb_renders_one_decimal() {
2084 assert_eq!(format_size_mb(5 * 1024 * 1024), "5.0 MB");
2085 assert_eq!(format_size_mb(1024 * 1024 + 512 * 1024), "1.5 MB");
2086 assert_eq!(format_size_mb(0), "0.0 MB");
2087 }
2088
2089 #[test]
2090 fn partition_by_size_no_limit_keeps_all() {
2091 let raw = vec![(PathBuf::from("a.ts"), 10), (PathBuf::from("b.ts"), 10_000)];
2092 let (kept, skipped) = partition_by_size(raw, None);
2093 assert_eq!(kept.len(), 2);
2094 assert!(skipped.is_empty());
2095 }
2096
2097 #[test]
2098 fn partition_by_size_skips_strictly_over_limit() {
2099 let raw = vec![
2100 (PathBuf::from("under.ts"), 99),
2101 (PathBuf::from("exact.ts"), 100),
2102 (PathBuf::from("over.ts"), 101),
2103 ];
2104 let (kept, skipped) = partition_by_size(raw, Some(100));
2105 let kept_has = |name: &str| kept.iter().any(|(p, _)| p.as_path() == Path::new(name));
2106 assert!(kept_has("under.ts"));
2107 assert!(
2108 kept_has("exact.ts"),
2109 "a file exactly at the limit is kept (skip is strictly-greater)"
2110 );
2111 assert_eq!(skipped.len(), 1);
2112 assert_eq!(skipped[0].0, PathBuf::from("over.ts"));
2113 }
2114
2115 #[test]
2116 fn partition_by_size_exempts_declaration_files() {
2117 let raw = vec![
2118 (PathBuf::from("huge.ts"), 10_000),
2119 (PathBuf::from("auto-imports.d.ts"), 10_000),
2120 ];
2121 let (kept, skipped) = partition_by_size(raw, Some(100));
2122 assert!(
2123 kept.iter()
2124 .any(|(p, _)| p.as_path() == Path::new("auto-imports.d.ts")),
2125 "declaration files are exempt from the size skip regardless of size"
2126 );
2127 assert_eq!(skipped.len(), 1);
2128 assert_eq!(skipped[0].0, PathBuf::from("huge.ts"));
2129 }
2130
2131 fn disco(path: &str, size_bytes: u64) -> DiscoveredFile {
2132 DiscoveredFile {
2133 id: FileId(0),
2134 path: PathBuf::from(path),
2135 size_bytes,
2136 }
2137 }
2138
2139 #[test]
2140 fn largest_files_note_below_threshold_is_none() {
2141 let files = [disco("a.ts", 100), disco("b.ts", 200)];
2142 assert!(build_largest_files_note(Path::new("/p"), &files).is_none());
2143 }
2144
2145 #[test]
2146 fn largest_files_note_single_file_uses_singular() {
2147 let files = [disco("big.ts", 5 * 1024 * 1024)];
2148 let note = build_largest_files_note(Path::new("/p"), &files).expect("note fires");
2149 assert!(
2150 note.contains("discovered 1 file;"),
2151 "singular noun on the single-big-file path (issue #1086 regression): {note}"
2152 );
2153 assert!(!note.contains("discovered 1 files"));
2154 assert!(note.contains("big.ts (5.0 MB)"));
2155 }
2156
2157 #[test]
2158 fn largest_files_note_filters_sub_floor_files() {
2159 let files = [disco("big.ts", 5 * 1024 * 1024), disco("tiny.ts", 10)];
2160 let note = build_largest_files_note(Path::new("/p"), &files).expect("note fires");
2161 assert!(note.contains("discovered 2 files;"));
2162 assert!(note.contains("big.ts (5.0 MB)"));
2163 assert!(
2164 !note.contains("tiny.ts"),
2165 "sub-floor files are not listed as `0.0 MB` chaff: {note}"
2166 );
2167 }
2168
2169 #[test]
2170 fn largest_files_note_large_set_no_big_file_omits_list() {
2171 let files: Vec<DiscoveredFile> = (0..=LARGE_SET_THRESHOLD)
2172 .map(|i| disco(&format!("f{i}.ts"), 100))
2173 .collect();
2174 let note = build_largest_files_note(Path::new("/p"), &files).expect("large set fires");
2175 assert!(note.contains(&format!("discovered {} files", LARGE_SET_THRESHOLD + 1)));
2176 assert!(
2177 !note.contains("largest:"),
2178 "no sub-floor `largest:` list when no file clears the floor: {note}"
2179 );
2180 }
2181
2182 mod discover_files_integration {
2183 use std::path::PathBuf;
2184
2185 use fallow_config::{
2186 DuplicatesConfig, FallowConfig, FlagsConfig, HealthConfig, OutputFormat, ResolveConfig,
2187 RulesConfig,
2188 };
2189
2190 use super::*;
2191
2192 fn make_config(root: PathBuf, production: bool) -> ResolvedConfig {
2194 FallowConfig {
2195 production: production.into(),
2196 ..Default::default()
2197 }
2198 .resolve(root, OutputFormat::Human, 1, true, true, None)
2199 }
2200
2201 fn file_names(files: &[DiscoveredFile], root: &std::path::Path) -> Vec<String> {
2204 files
2205 .iter()
2206 .map(|f| {
2207 f.path
2208 .strip_prefix(root)
2209 .unwrap_or(&f.path)
2210 .to_string_lossy()
2211 .replace('\\', "/")
2212 })
2213 .collect()
2214 }
2215
2216 #[cfg(unix)]
2217 fn symlink_file(target: &Path, link: &Path) {
2218 std::os::unix::fs::symlink(target, link).expect("create file symlink");
2219 }
2220
2221 #[cfg(windows)]
2222 fn symlink_file(target: &Path, link: &Path) {
2223 std::os::windows::fs::symlink_file(target, link).expect("create file symlink");
2224 }
2225
2226 #[cfg(unix)]
2227 fn symlink_dir(target: &Path, link: &Path) {
2228 std::os::unix::fs::symlink(target, link).expect("create directory symlink");
2229 }
2230
2231 #[cfg(windows)]
2232 fn symlink_dir(target: &Path, link: &Path) {
2233 std::os::windows::fs::symlink_dir(target, link).expect("create directory symlink");
2234 }
2235
2236 #[test]
2237 fn source_symlinks_must_target_regular_files_inside_root() {
2238 let dir = tempfile::tempdir().expect("create project");
2239 let outside = tempfile::tempdir().expect("create outside dir");
2240 let src = dir.path().join("src");
2241 std::fs::create_dir_all(&src).unwrap();
2242 std::fs::write(src.join("regular.ts"), "export const regular = 1;").unwrap();
2243 std::fs::write(src.join("inside-target.ts"), "export const inside = 1;").unwrap();
2244 std::fs::write(
2245 outside.path().join("outside-target.ts"),
2246 "export const outside = 1;",
2247 )
2248 .unwrap();
2249 std::fs::create_dir_all(src.join("directory-target")).unwrap();
2250
2251 symlink_file(&src.join("inside-target.ts"), &src.join("inside-link.ts"));
2252 symlink_file(
2253 &outside.path().join("outside-target.ts"),
2254 &src.join("outside-link.ts"),
2255 );
2256 symlink_file(&src.join("missing-target.ts"), &src.join("broken-link.ts"));
2257 symlink_dir(
2258 &src.join("directory-target"),
2259 &src.join("directory-link.ts"),
2260 );
2261
2262 let config = make_config(dir.path().to_path_buf(), false);
2263 let names = file_names(&discover_files(&config), dir.path());
2264
2265 assert!(names.contains(&"src/regular.ts".to_string()));
2266 assert!(names.contains(&"src/inside-target.ts".to_string()));
2267 assert!(names.contains(&"src/inside-link.ts".to_string()));
2268 assert!(!names.contains(&"src/outside-link.ts".to_string()));
2269 assert!(!names.contains(&"src/broken-link.ts".to_string()));
2270 assert!(!names.contains(&"src/directory-link.ts".to_string()));
2271 }
2272
2273 #[test]
2277 fn skips_yarn_pnp_generated_files() {
2278 let dir = tempfile::tempdir().expect("create temp dir");
2279 std::fs::write(dir.path().join(".pnp.cjs"), "module.exports = {};").unwrap();
2280 std::fs::write(dir.path().join(".pnp.loader.mjs"), "export {};").unwrap();
2281 std::fs::write(dir.path().join("index.ts"), "export const a = 1;").unwrap();
2282
2283 let config = make_config(dir.path().to_path_buf(), false);
2284 let names = file_names(&discover_files(&config), dir.path());
2285
2286 assert_eq!(names, vec!["index.ts".to_string()]);
2287 }
2288
2289 #[test]
2290 fn discovers_source_files_with_valid_extensions() {
2291 let dir = tempfile::tempdir().expect("create temp dir");
2292 let src = dir.path().join("src");
2293 std::fs::create_dir_all(&src).unwrap();
2294
2295 std::fs::write(src.join("app.ts"), "export const a = 1;").unwrap();
2296 std::fs::write(src.join("component.tsx"), "export default () => {};").unwrap();
2297 std::fs::write(src.join("utils.js"), "module.exports = {};").unwrap();
2298 std::fs::write(src.join("helper.jsx"), "export const h = 1;").unwrap();
2299 std::fs::write(src.join("config.mjs"), "export default {};").unwrap();
2300 std::fs::write(src.join("legacy.cjs"), "module.exports = {};").unwrap();
2301 std::fs::write(src.join("types.mts"), "export type T = string;").unwrap();
2302 std::fs::write(src.join("compat.cts"), "module.exports = {};").unwrap();
2303
2304 let config = make_config(dir.path().to_path_buf(), false);
2305 let files = discover_files(&config);
2306 let names = file_names(&files, dir.path());
2307
2308 assert!(names.contains(&"src/app.ts".to_string()));
2309 assert!(names.contains(&"src/component.tsx".to_string()));
2310 assert!(names.contains(&"src/utils.js".to_string()));
2311 assert!(names.contains(&"src/helper.jsx".to_string()));
2312 assert!(names.contains(&"src/config.mjs".to_string()));
2313 assert!(names.contains(&"src/legacy.cjs".to_string()));
2314 assert!(names.contains(&"src/types.mts".to_string()));
2315 assert!(names.contains(&"src/compat.cts".to_string()));
2316 }
2317
2318 #[test]
2319 fn compact_source_glob_preserves_discovered_file_inventory() {
2320 let dir = tempfile::tempdir().expect("create temp dir");
2321 let nested = dir.path().join("packages/ui/src/nested");
2322 std::fs::create_dir_all(&nested).unwrap();
2323
2324 let mut expected = Vec::new();
2325 for (index, extension) in SOURCE_EXTENSIONS.iter().enumerate() {
2326 let relative = format!("packages/ui/src/nested/source-{index}.{extension}");
2327 std::fs::write(dir.path().join(&relative), "export const value = 1;").unwrap();
2328 expected.push(relative);
2329 }
2330 for relative in [
2331 "packages/ui/src/nested/env.d.ts",
2332 "packages/ui/src/nested/generated.d.mts",
2333 "packages/ui/src/nested/compat.d.cts",
2334 ] {
2335 std::fs::write(dir.path().join(relative), "export type Value = string;").unwrap();
2336 expected.push(relative.to_string());
2337 }
2338 let rejected = [
2339 "packages/ui/src/nested/component.tsx.bak",
2340 "packages/ui/src/nested/component.tsxmap",
2341 "packages/ui/src/nested/component.TS",
2342 "packages/ui/src/nested/component.gqlx",
2343 "packages/ui/src/nested/component.htm",
2344 "packages/ui/src/nested/component",
2345 "packages/ui/src/nested/component.png",
2346 ];
2347 for relative in rejected {
2348 std::fs::write(dir.path().join(relative), "not source").unwrap();
2349 }
2350
2351 let config = make_config(dir.path().to_path_buf(), false);
2352 let names = file_names(&discover_files(&config), dir.path());
2353
2354 for relative in expected {
2355 assert!(
2356 names.contains(&relative),
2357 "missing supported source {relative}"
2358 );
2359 }
2360 for relative in rejected {
2361 assert!(
2362 !names.iter().any(|name| name == relative),
2363 "unexpected near-miss source {relative}"
2364 );
2365 }
2366 }
2367
2368 #[test]
2369 fn excludes_non_source_extensions() {
2370 let dir = tempfile::tempdir().expect("create temp dir");
2371 let src = dir.path().join("src");
2372 std::fs::create_dir_all(&src).unwrap();
2373
2374 std::fs::write(src.join("app.ts"), "export const a = 1;").unwrap();
2375
2376 std::fs::write(src.join("data.json"), "{}").unwrap();
2377 std::fs::write(src.join("readme.md"), "# Hello").unwrap();
2378 std::fs::write(src.join("notes.txt"), "notes").unwrap();
2379 std::fs::write(src.join("logo.png"), [0u8; 8]).unwrap();
2380
2381 let config = make_config(dir.path().to_path_buf(), false);
2382 let files = discover_files(&config);
2383 let names = file_names(&files, dir.path());
2384
2385 assert_eq!(names.len(), 1, "only the .ts file should be discovered");
2386 assert!(names.contains(&"src/app.ts".to_string()));
2387 }
2388
2389 #[test]
2390 fn excludes_disallowed_hidden_directories() {
2391 let dir = tempfile::tempdir().expect("create temp dir");
2392
2393 let git_dir = dir.path().join(".git");
2394 std::fs::create_dir_all(&git_dir).unwrap();
2395 std::fs::write(git_dir.join("hooks.ts"), "// git hook").unwrap();
2396
2397 let idea_dir = dir.path().join(".idea");
2398 std::fs::create_dir_all(&idea_dir).unwrap();
2399 std::fs::write(idea_dir.join("workspace.ts"), "// idea").unwrap();
2400
2401 let cache_dir = dir.path().join(".cache");
2402 std::fs::create_dir_all(&cache_dir).unwrap();
2403 std::fs::write(cache_dir.join("cached.js"), "// cached").unwrap();
2404
2405 let src = dir.path().join("src");
2406 std::fs::create_dir_all(&src).unwrap();
2407 std::fs::write(src.join("app.ts"), "export const a = 1;").unwrap();
2408
2409 let config = make_config(dir.path().to_path_buf(), false);
2410 let files = discover_files(&config);
2411 let names = file_names(&files, dir.path());
2412
2413 assert_eq!(names.len(), 1, "only src/app.ts should be discovered");
2414 assert!(names.contains(&"src/app.ts".to_string()));
2415 }
2416
2417 #[test]
2418 fn includes_allowed_hidden_directories() {
2419 let dir = tempfile::tempdir().expect("create temp dir");
2420
2421 let storybook = dir.path().join(".storybook");
2422 std::fs::create_dir_all(&storybook).unwrap();
2423 std::fs::write(storybook.join("main.ts"), "export default {};").unwrap();
2424
2425 let github = dir.path().join(".github");
2426 std::fs::create_dir_all(&github).unwrap();
2427 std::fs::write(github.join("actions.js"), "module.exports = {};").unwrap();
2428
2429 let changeset = dir.path().join(".changeset");
2430 std::fs::create_dir_all(&changeset).unwrap();
2431 std::fs::write(changeset.join("config.js"), "module.exports = {};").unwrap();
2432
2433 let config = make_config(dir.path().to_path_buf(), false);
2434 let files = discover_files(&config);
2435 let names = file_names(&files, dir.path());
2436
2437 assert!(
2438 names.contains(&".storybook/main.ts".to_string()),
2439 "files in .storybook should be discovered"
2440 );
2441 assert!(
2442 names.contains(&".github/actions.js".to_string()),
2443 "files in .github should be discovered"
2444 );
2445 assert!(
2446 names.contains(&".changeset/config.js".to_string()),
2447 "files in .changeset should be discovered"
2448 );
2449 }
2450
2451 #[test]
2452 fn default_discovery_excludes_client_and_server_hidden_directories() {
2453 let dir = tempfile::tempdir().expect("create temp dir");
2454 let app = dir.path().join("app");
2455 std::fs::create_dir_all(app.join(".client")).unwrap();
2456 std::fs::create_dir_all(app.join(".server")).unwrap();
2457 std::fs::write(app.join(".client/analytics.ts"), "export const a = 1;").unwrap();
2458 std::fs::write(app.join(".server/db.ts"), "export const db = {};").unwrap();
2459 std::fs::write(app.join("root.tsx"), "export default function Root() {}").unwrap();
2460
2461 let config = make_config(dir.path().to_path_buf(), false);
2462 let files = discover_files(&config);
2463 let names = file_names(&files, dir.path());
2464
2465 assert!(names.contains(&"app/root.tsx".to_string()));
2466 assert!(!names.contains(&"app/.client/analytics.ts".to_string()));
2467 assert!(!names.contains(&"app/.server/db.ts".to_string()));
2468 }
2469
2470 #[test]
2471 fn scoped_hidden_dirs_include_client_and_server_under_package_root() {
2472 let dir = tempfile::tempdir().expect("create temp dir");
2473 let package = dir.path().join("packages/app");
2474 std::fs::create_dir_all(package.join("app/.client")).unwrap();
2475 std::fs::create_dir_all(package.join("app/.server")).unwrap();
2476 std::fs::write(
2477 package.join("app/.client/analytics.ts"),
2478 "export const track = () => {};",
2479 )
2480 .unwrap();
2481 std::fs::write(package.join("app/.server/db.ts"), "export const db = {};").unwrap();
2482
2483 let config = make_config(dir.path().to_path_buf(), false);
2484 let scopes = [HiddenDirScope::new(
2485 package,
2486 vec![".client".to_string(), ".server".to_string()],
2487 )];
2488 let files = discover_files_with_additional_hidden_dirs(&config, &scopes);
2489 let names = file_names(&files, dir.path());
2490
2491 assert!(names.contains(&"packages/app/app/.client/analytics.ts".to_string()));
2492 assert!(names.contains(&"packages/app/app/.server/db.ts".to_string()));
2493 }
2494
2495 #[test]
2496 fn scoped_hidden_dirs_do_not_include_unscoped_packages() {
2497 let dir = tempfile::tempdir().expect("create temp dir");
2498 let active = dir.path().join("packages/active");
2499 let inactive = dir.path().join("packages/inactive");
2500 std::fs::create_dir_all(active.join("app/.server")).unwrap();
2501 std::fs::create_dir_all(inactive.join("app/.server")).unwrap();
2502 std::fs::write(active.join("app/.server/db.ts"), "export const db = {};").unwrap();
2503 std::fs::write(inactive.join("app/.server/db.ts"), "export const db = {};").unwrap();
2504
2505 let config = make_config(dir.path().to_path_buf(), false);
2506 let scopes = [HiddenDirScope::new(active, vec![".server".to_string()])];
2507 let files = discover_files_with_additional_hidden_dirs(&config, &scopes);
2508 let names = file_names(&files, dir.path());
2509
2510 assert!(names.contains(&"packages/active/app/.server/db.ts".to_string()));
2511 assert!(!names.contains(&"packages/inactive/app/.server/db.ts".to_string()));
2512 }
2513
2514 #[test]
2515 fn exact_path_scope_does_not_admit_the_same_name_elsewhere() {
2516 let dir = tempfile::tempdir().expect("create temp dir");
2520 std::fs::create_dir_all(dir.path().join(".a/.b")).unwrap();
2521 std::fs::create_dir_all(dir.path().join("elsewhere/.b")).unwrap();
2522 std::fs::create_dir_all(dir.path().join("unrelated/.a")).unwrap();
2523 std::fs::write(dir.path().join(".a/.b/deep.mjs"), "export const a = 1;").unwrap();
2524 std::fs::write(dir.path().join("elsewhere/.b/y.mjs"), "export const b = 1;").unwrap();
2525 std::fs::write(dir.path().join("unrelated/.a/u.mjs"), "export const c = 1;").unwrap();
2526
2527 let config = make_config(dir.path().to_path_buf(), false);
2528 let scopes = [HiddenDirScope::new_exact_paths(
2529 dir.path().to_path_buf(),
2530 vec![".a".to_string(), format!(".a{MAIN_SEPARATOR}.b")],
2531 )];
2532 let files = discover_files_with_additional_hidden_dirs(&config, &scopes);
2533 let names = file_names(&files, dir.path());
2534
2535 assert!(names.contains(&".a/.b/deep.mjs".to_string()));
2536 assert!(!names.contains(&"elsewhere/.b/y.mjs".to_string()));
2537 assert!(!names.contains(&"unrelated/.a/u.mjs".to_string()));
2538 }
2539
2540 #[test]
2541 fn exact_path_scope_admits_a_hidden_dir_under_a_visible_parent() {
2542 let dir = tempfile::tempdir().expect("create temp dir");
2543 std::fs::create_dir_all(dir.path().join("tools/.config")).unwrap();
2544 std::fs::create_dir_all(dir.path().join("other/.config")).unwrap();
2545 std::fs::write(
2546 dir.path().join("tools/.config/eslint.config.js"),
2547 "export default [];",
2548 )
2549 .unwrap();
2550 std::fs::write(
2551 dir.path().join("other/.config/eslint.config.js"),
2552 "export default [];",
2553 )
2554 .unwrap();
2555
2556 let config = make_config(dir.path().to_path_buf(), false);
2557 let scopes = [HiddenDirScope::new_exact_paths(
2558 dir.path().to_path_buf(),
2559 vec![format!("tools{MAIN_SEPARATOR}.config")],
2560 )];
2561 let files = discover_files_with_additional_hidden_dirs(&config, &scopes);
2562 let names = file_names(&files, dir.path());
2563
2564 assert!(names.contains(&"tools/.config/eslint.config.js".to_string()));
2565 assert!(!names.contains(&"other/.config/eslint.config.js".to_string()));
2566 }
2567
2568 #[test]
2569 fn any_depth_scope_keeps_matching_by_name_for_plugins() {
2570 let dir = tempfile::tempdir().expect("create temp dir");
2574 std::fs::create_dir_all(dir.path().join("app/routes/deep/.server")).unwrap();
2575 std::fs::write(
2576 dir.path().join("app/routes/deep/.server/db.ts"),
2577 "export const db = {};",
2578 )
2579 .unwrap();
2580
2581 let config = make_config(dir.path().to_path_buf(), false);
2582 let scopes = [HiddenDirScope::new(
2583 dir.path().to_path_buf(),
2584 vec![".server".to_string()],
2585 )];
2586 let files = discover_files_with_additional_hidden_dirs(&config, &scopes);
2587 let names = file_names(&files, dir.path());
2588
2589 assert!(names.contains(&"app/routes/deep/.server/db.ts".to_string()));
2590 }
2591
2592 #[test]
2593 fn excludes_root_build_directory() {
2594 let dir = tempfile::tempdir().expect("create temp dir");
2595
2596 std::fs::write(dir.path().join(".ignore"), "/build/\n").unwrap();
2597
2598 let build_dir = dir.path().join("build");
2599 std::fs::create_dir_all(&build_dir).unwrap();
2600 std::fs::write(build_dir.join("output.js"), "// build output").unwrap();
2601
2602 let src = dir.path().join("src");
2603 std::fs::create_dir_all(&src).unwrap();
2604 std::fs::write(src.join("app.ts"), "export const a = 1;").unwrap();
2605
2606 let config = make_config(dir.path().to_path_buf(), false);
2607 let files = discover_files(&config);
2608 let names = file_names(&files, dir.path());
2609
2610 assert_eq!(names.len(), 1, "root build/ should be excluded via .ignore");
2611 assert!(names.contains(&"src/app.ts".to_string()));
2612 }
2613
2614 #[test]
2615 fn excludes_nested_build_directory() {
2616 let dir = tempfile::tempdir().expect("create temp dir");
2617
2618 let nested_build = dir.path().join("src").join("build");
2619 std::fs::create_dir_all(&nested_build).unwrap();
2620 std::fs::write(nested_build.join("helper.ts"), "export const h = 1;").unwrap();
2621
2622 let config = make_config(dir.path().to_path_buf(), false);
2623 let files = discover_files(&config);
2624 let names = file_names(&files, dir.path());
2625
2626 assert!(
2627 !names.contains(&"src/build/helper.ts".to_string()),
2628 "build/ is treated as generated output at any depth: {names:?}"
2629 );
2630 }
2631
2632 #[test]
2633 #[expect(
2634 clippy::cast_possible_truncation,
2635 reason = "test file counts are trivially small"
2636 )]
2637 fn file_ids_are_sequential_after_sorting() {
2638 let dir = tempfile::tempdir().expect("create temp dir");
2639 let src = dir.path().join("src");
2640 std::fs::create_dir_all(&src).unwrap();
2641
2642 std::fs::write(src.join("z_last.ts"), "export const z = 1;").unwrap();
2643 std::fs::write(src.join("a_first.ts"), "export const a = 1;").unwrap();
2644 std::fs::write(src.join("m_middle.ts"), "export const m = 1;").unwrap();
2645
2646 let config = make_config(dir.path().to_path_buf(), false);
2647 let files = discover_files(&config);
2648
2649 for (idx, file) in files.iter().enumerate() {
2650 assert_eq!(file.id, FileId(idx as u32), "FileId should be sequential");
2651 }
2652
2653 for pair in files.windows(2) {
2654 assert!(
2655 pair[0].path < pair[1].path,
2656 "files should be sorted by path"
2657 );
2658 }
2659 }
2660
2661 #[test]
2662 fn production_mode_excludes_test_files() {
2663 let dir = tempfile::tempdir().expect("create temp dir");
2664 let src = dir.path().join("src");
2665 std::fs::create_dir_all(&src).unwrap();
2666
2667 std::fs::write(src.join("app.ts"), "export const a = 1;").unwrap();
2668 std::fs::write(src.join("app.test.ts"), "test('a', () => {});").unwrap();
2669 std::fs::write(src.join("app.spec.ts"), "describe('a', () => {});").unwrap();
2670 std::fs::write(src.join("app.stories.tsx"), "export default {};").unwrap();
2671
2672 let config = make_config(dir.path().to_path_buf(), true);
2673 let files = discover_files(&config);
2674 let names = file_names(&files, dir.path());
2675
2676 assert!(
2677 names.contains(&"src/app.ts".to_string()),
2678 "source files should be included in production mode"
2679 );
2680 assert!(
2681 !names.contains(&"src/app.test.ts".to_string()),
2682 "test files should be excluded in production mode"
2683 );
2684 assert!(
2685 !names.contains(&"src/app.spec.ts".to_string()),
2686 "spec files should be excluded in production mode"
2687 );
2688 assert!(
2689 !names.contains(&"src/app.stories.tsx".to_string()),
2690 "story files should be excluded in production mode"
2691 );
2692 }
2693
2694 #[test]
2695 fn non_production_mode_includes_test_files() {
2696 let dir = tempfile::tempdir().expect("create temp dir");
2697 let src = dir.path().join("src");
2698 std::fs::create_dir_all(&src).unwrap();
2699
2700 std::fs::write(src.join("app.ts"), "export const a = 1;").unwrap();
2701 std::fs::write(src.join("app.test.ts"), "test('a', () => {});").unwrap();
2702
2703 let config = make_config(dir.path().to_path_buf(), false);
2704 let files = discover_files(&config);
2705 let names = file_names(&files, dir.path());
2706
2707 assert!(names.contains(&"src/app.ts".to_string()));
2708 assert!(
2709 names.contains(&"src/app.test.ts".to_string()),
2710 "test files should be included in non-production mode"
2711 );
2712 }
2713
2714 #[test]
2715 fn empty_directory_returns_no_files() {
2716 let dir = tempfile::tempdir().expect("create temp dir");
2717 let config = make_config(dir.path().to_path_buf(), false);
2718 let files = discover_files(&config);
2719 assert!(files.is_empty(), "empty project should discover no files");
2720 }
2721
2722 #[test]
2723 fn hidden_files_not_discovered_as_source() {
2724 let dir = tempfile::tempdir().expect("create temp dir");
2725
2726 std::fs::write(dir.path().join(".env"), "SECRET=abc").unwrap();
2727 std::fs::write(dir.path().join(".gitignore"), "node_modules").unwrap();
2728 std::fs::write(dir.path().join(".eslintrc.js"), "module.exports = {};").unwrap();
2729
2730 let src = dir.path().join("src");
2731 std::fs::create_dir_all(&src).unwrap();
2732 std::fs::write(src.join("app.ts"), "export const a = 1;").unwrap();
2733
2734 let config = make_config(dir.path().to_path_buf(), false);
2735 let files = discover_files(&config);
2736 let names = file_names(&files, dir.path());
2737
2738 assert!(
2739 !names.contains(&".env".to_string()),
2740 ".env should not be discovered"
2741 );
2742 assert!(
2743 !names.contains(&".gitignore".to_string()),
2744 ".gitignore should not be discovered"
2745 );
2746 }
2747
2748 fn make_config_with_ignores(root: PathBuf, ignores: Vec<String>) -> ResolvedConfig {
2750 FallowConfig {
2751 type_aware: fallow_config::TypeAwareConfig::default(),
2752 schema: None,
2753 minimum_version: None,
2754 extends: vec![],
2755 entry: vec![],
2756 ignore_patterns: ignores,
2757 ignore_findings: vec![],
2758 framework: vec![],
2759 workspaces: None,
2760 ignore_dependencies: vec![],
2761 ignore_command_entries: vec![],
2762 ignore_unresolved_imports: vec![],
2763 ignore_exports: vec![],
2764 ignore_catalog_references: vec![],
2765 ignore_dependency_overrides: vec![],
2766 ignore_exports_used_in_file: fallow_config::IgnoreExportsUsedInFileConfig::default(
2767 ),
2768 used_class_members: vec![],
2769 ignore_decorators: vec![],
2770 unused_component_props: fallow_config::UnusedComponentPropsConfig::default(),
2771 circular_dependencies: fallow_config::CircularDependenciesConfig::default(),
2772 duplicates: DuplicatesConfig::default(),
2773 similar_code: fallow_config::SimilarCodeConfig::default(),
2774 mutation: fallow_config::MutationConfig::default(),
2775 health: HealthConfig::default(),
2776 rules: RulesConfig::default(),
2777 boundaries: fallow_config::BoundaryConfig::default(),
2778 production: false.into(),
2779 plugins: vec![],
2780 rule_packs: vec![],
2781 overrides: vec![],
2782 regression: None,
2783 audit: fallow_config::AuditConfig::default(),
2784 codeowners: None,
2785 public_packages: vec![],
2786 flags: FlagsConfig::default(),
2787 security: fallow_config::SecurityConfig::default(),
2788 fix: fallow_config::FixConfig::default(),
2789 resolve: ResolveConfig::default(),
2790 sealed: false,
2791 include_entry_exports: false,
2792 show_cascade: false,
2793 auto_imports: false,
2794 fail_on_parse_error: false,
2795 cache: fallow_config::CacheConfig::default(),
2796 ..Default::default()
2797 }
2798 .resolve(root, OutputFormat::Human, 1, true, true, None)
2799 }
2800
2801 #[test]
2802 fn custom_ignore_patterns_exclude_matching_files() {
2803 let dir = tempfile::tempdir().expect("create temp dir");
2804
2805 let generated = dir.path().join("src").join("api").join("generated");
2806 std::fs::create_dir_all(&generated).unwrap();
2807 std::fs::write(generated.join("client.ts"), "export const api = {};").unwrap();
2808
2809 let client = dir.path().join("src").join("api").join("client");
2810 std::fs::create_dir_all(&client).unwrap();
2811 std::fs::write(client.join("fetch.ts"), "export const fetch = {};").unwrap();
2812
2813 let src = dir.path().join("src");
2814 std::fs::write(src.join("index.ts"), "export const x = 1;").unwrap();
2815
2816 let config = make_config_with_ignores(
2817 dir.path().to_path_buf(),
2818 vec![
2819 "src/api/generated/**".to_string(),
2820 "src/api/client/**".to_string(),
2821 ],
2822 );
2823 let files = discover_files(&config);
2824 let names = file_names(&files, dir.path());
2825
2826 assert_eq!(names.len(), 1, "only non-ignored files: {names:?}");
2827 assert!(names.contains(&"src/index.ts".to_string()));
2828 }
2829
2830 #[test]
2831 fn leading_dot_ignore_patterns_exclude_matching_files() {
2832 let dir = tempfile::tempdir().expect("create temp dir");
2833
2834 let generated = dir.path().join("src").join("generated");
2835 std::fs::create_dir_all(&generated).unwrap();
2836 std::fs::write(generated.join("client.ts"), "export const api = {};").unwrap();
2837
2838 let src = dir.path().join("src");
2839 std::fs::write(src.join("index.ts"), "export const x = 1;").unwrap();
2840
2841 let config = make_config_with_ignores(
2842 dir.path().to_path_buf(),
2843 vec!["./src/generated/**".to_string()],
2844 );
2845 let files = discover_files(&config);
2846 let names = file_names(&files, dir.path());
2847
2848 assert_eq!(names, vec!["src/index.ts"]);
2849 }
2850
2851 #[test]
2852 fn default_ignore_patterns_exclude_node_modules_and_dist() {
2853 let dir = tempfile::tempdir().expect("create temp dir");
2854
2855 let nm = dir.path().join("node_modules").join("lodash");
2856 std::fs::create_dir_all(&nm).unwrap();
2857 std::fs::write(nm.join("lodash.js"), "module.exports = {};").unwrap();
2858
2859 let dist = dir.path().join("dist");
2860 std::fs::create_dir_all(&dist).unwrap();
2861 std::fs::write(dist.join("bundle.js"), "// bundled").unwrap();
2862
2863 let src = dir.path().join("src");
2864 std::fs::create_dir_all(&src).unwrap();
2865 std::fs::write(src.join("index.ts"), "export const x = 1;").unwrap();
2866
2867 let config = make_config(dir.path().to_path_buf(), false);
2868 let files = discover_files(&config);
2869 let names = file_names(&files, dir.path());
2870
2871 assert_eq!(names.len(), 1);
2872 assert!(names.contains(&"src/index.ts".to_string()));
2873 }
2874
2875 #[test]
2876 fn default_ignore_patterns_exclude_build_at_any_depth() {
2877 let dir = tempfile::tempdir().expect("create temp dir");
2878
2879 let build = dir.path().join("build");
2880 std::fs::create_dir_all(&build).unwrap();
2881 std::fs::write(build.join("output.js"), "// built").unwrap();
2882
2883 let nested_build = dir.path().join("src").join("build");
2884 std::fs::create_dir_all(&nested_build).unwrap();
2885 std::fs::write(nested_build.join("helper.ts"), "export const h = 1;").unwrap();
2886
2887 let src = dir.path().join("src");
2888 std::fs::write(src.join("index.ts"), "export const x = 1;").unwrap();
2889
2890 let config = make_config(dir.path().to_path_buf(), false);
2891 let files = discover_files(&config);
2892 let names = file_names(&files, dir.path());
2893
2894 assert_eq!(names, vec!["src/index.ts".to_string()]);
2895 }
2896
2897 #[test]
2900 fn default_ignore_patterns_exclude_nested_build() {
2901 let dir = tempfile::tempdir().expect("create temp dir");
2902
2903 let build = dir.path().join("build");
2904 std::fs::create_dir_all(&build).unwrap();
2905 std::fs::write(build.join("output.js"), "// built").unwrap();
2906
2907 let package_build = dir.path().join("projects").join("app").join("build");
2908 std::fs::create_dir_all(&package_build).unwrap();
2909 std::fs::write(package_build.join("index.js"), "// built").unwrap();
2910
2911 let src = dir.path().join("src");
2912 std::fs::create_dir_all(&src).unwrap();
2913 std::fs::write(src.join("index.ts"), "export const x = 1;").unwrap();
2914
2915 let config = make_config(dir.path().to_path_buf(), false);
2916 let files = discover_files(&config);
2917 let names = file_names(&files, dir.path());
2918
2919 assert_eq!(names, vec!["src/index.ts".to_string()]);
2920 }
2921
2922 #[test]
2924 fn default_ignore_patterns_keep_paths_that_merely_contain_build() {
2925 let dir = tempfile::tempdir().expect("create temp dir");
2926
2927 let src = dir.path().join("src");
2928 std::fs::create_dir_all(src.join("rebuild")).unwrap();
2929 std::fs::create_dir_all(src.join("buildings")).unwrap();
2930 std::fs::write(src.join("build.ts"), "export const a = 1;").unwrap();
2931 std::fs::write(src.join("rebuild").join("helper.ts"), "export const b = 1;").unwrap();
2932 std::fs::write(src.join("buildings").join("a.ts"), "export const c = 1;").unwrap();
2933
2934 let config = make_config(dir.path().to_path_buf(), false);
2935 let files = discover_files(&config);
2936 let mut names = file_names(&files, dir.path());
2937 names.sort();
2938
2939 assert_eq!(
2940 names,
2941 vec![
2942 "src/build.ts".to_string(),
2943 "src/buildings/a.ts".to_string(),
2944 "src/rebuild/helper.ts".to_string(),
2945 ]
2946 );
2947 }
2948
2949 fn make_config_with_max_file_size(
2951 root: PathBuf,
2952 max_file_size_bytes: Option<u64>,
2953 ) -> ResolvedConfig {
2954 let mut config = make_config(root, false);
2955 config.max_file_size_bytes = max_file_size_bytes;
2956 config
2957 }
2958
2959 #[test]
2960 fn skips_files_over_max_file_size() {
2961 let dir = tempfile::tempdir().expect("create temp dir");
2962 let src = dir.path().join("src");
2963 std::fs::create_dir_all(&src).unwrap();
2964 std::fs::write(src.join("small.ts"), "export const a = 1;").unwrap();
2965 std::fs::write(src.join("huge.ts"), "x".repeat(5_000)).unwrap();
2966
2967 let config = make_config_with_max_file_size(dir.path().to_path_buf(), Some(1_000));
2968 let files = discover_files(&config);
2969 let names = file_names(&files, dir.path());
2970
2971 assert!(names.contains(&"src/small.ts".to_string()));
2972 assert!(
2973 !names.contains(&"src/huge.ts".to_string()),
2974 "a file over the size limit must not be discovered"
2975 );
2976 }
2977
2978 #[test]
2979 fn declaration_files_exempt_from_size_skip() {
2980 let dir = tempfile::tempdir().expect("create temp dir");
2981 let src = dir.path().join("src");
2982 std::fs::create_dir_all(&src).unwrap();
2983 std::fs::write(src.join("auto-imports.d.ts"), "x".repeat(5_000)).unwrap();
2984 std::fs::write(src.join("huge.ts"), "x".repeat(5_000)).unwrap();
2985
2986 let config = make_config_with_max_file_size(dir.path().to_path_buf(), Some(1_000));
2987 let files = discover_files(&config);
2988 let names = file_names(&files, dir.path());
2989
2990 assert!(
2991 names.contains(&"src/auto-imports.d.ts".to_string()),
2992 "a large .d.ts is exempt from the skip (reachability root for global types)"
2993 );
2994 assert!(!names.contains(&"src/huge.ts".to_string()));
2995 }
2996
2997 #[test]
2998 fn unlimited_size_keeps_large_files() {
2999 let dir = tempfile::tempdir().expect("create temp dir");
3000 let src = dir.path().join("src");
3001 std::fs::create_dir_all(&src).unwrap();
3002 std::fs::write(src.join("huge.ts"), "x".repeat(5_000)).unwrap();
3003
3004 let config = make_config_with_max_file_size(dir.path().to_path_buf(), None);
3005 let files = discover_files(&config);
3006 let names = file_names(&files, dir.path());
3007
3008 assert!(
3009 names.contains(&"src/huge.ts".to_string()),
3010 "no limit keeps every file"
3011 );
3012 }
3013
3014 #[test]
3015 fn skipped_file_recorded_in_workspace_diagnostics() {
3016 let dir = tempfile::tempdir().expect("create temp dir");
3017 let src = dir.path().join("src");
3018 std::fs::create_dir_all(&src).unwrap();
3019 std::fs::write(src.join("huge.ts"), "x".repeat(5_000)).unwrap();
3020
3021 let config = make_config_with_max_file_size(dir.path().to_path_buf(), Some(1_000));
3022 let _ = discover_files(&config);
3023
3024 let diagnostics = fallow_config::workspace_diagnostics_for(dir.path());
3025 let skipped: Vec<_> = diagnostics
3026 .iter()
3027 .filter(|d| {
3028 matches!(
3029 d.kind,
3030 fallow_config::WorkspaceDiagnosticKind::SkippedLargeFile { .. }
3031 )
3032 })
3033 .collect();
3034 assert_eq!(
3035 skipped.len(),
3036 1,
3037 "the skipped file is recorded in workspace diagnostics for JSON output"
3038 );
3039 assert!(skipped[0].path.ends_with("src/huge.ts"));
3040 assert!(
3041 matches!(
3042 skipped[0].kind,
3043 fallow_config::WorkspaceDiagnosticKind::SkippedLargeFile { size_bytes }
3044 if size_bytes == 5_000
3045 ),
3046 "the recorded diagnostic carries the on-disk byte size"
3047 );
3048 }
3049
3050 fn dotdir_diagnostics(root: &Path) -> Vec<fallow_config::WorkspaceDiagnostic> {
3052 fallow_config::workspace_diagnostics_for(root)
3053 .into_iter()
3054 .filter(|d| {
3055 matches!(
3056 d.kind,
3057 fallow_config::WorkspaceDiagnosticKind::SkippedSourceDotdir
3058 )
3059 })
3060 .collect()
3061 }
3062
3063 fn write_at(root: &Path, relative: &str, contents: &str) {
3064 let path = root.join(relative);
3065 std::fs::create_dir_all(path.parent().expect("has a parent")).unwrap();
3066 std::fs::write(path, contents).unwrap();
3067 }
3068
3069 #[test]
3070 fn skipped_source_dotdir_recorded_in_workspace_diagnostics() {
3071 let dir = tempfile::tempdir().expect("create temp dir");
3072 write_at(
3073 dir.path(),
3074 ".claude/hooks/probe.mjs",
3075 "export const a = 1;\n",
3076 );
3077 write_at(dir.path(), "src/app.ts", "export const b = 2;\n");
3078
3079 let config = make_config(dir.path().to_path_buf(), false);
3080 let files = discover_files(&config);
3081 let names = file_names(&files, dir.path());
3082
3083 let reported = dotdir_diagnostics(dir.path());
3084 assert_eq!(reported.len(), 1, "one skipped dotdir holds source files");
3085 assert!(reported[0].path.ends_with(".claude"));
3086 assert_eq!(reported[0].kind.id(), "skipped-source-dotdir");
3087 assert!(
3088 reported[0].message.contains("--root"),
3089 "message names the real remedy: {}",
3090 reported[0].message
3091 );
3092 assert!(
3093 names.contains(&"src/app.ts".to_string()),
3094 "traversal is unchanged for ordinary directories"
3095 );
3096 assert!(
3097 !names.contains(&".claude/hooks/probe.mjs".to_string()),
3098 "the diagnostic reports the skip, it does not change traversal"
3099 );
3100 }
3101
3102 #[test]
3103 fn allowlisted_dotdir_is_not_reported() {
3104 let dir = tempfile::tempdir().expect("create temp dir");
3105 write_at(dir.path(), ".storybook/main.ts", "export const a = 1;\n");
3106
3107 let config = make_config(dir.path().to_path_buf(), false);
3108 let files = discover_files(&config);
3109 let names = file_names(&files, dir.path());
3110
3111 assert!(dotdir_diagnostics(dir.path()).is_empty());
3112 assert!(
3113 names.contains(&".storybook/main.ts".to_string()),
3114 "an allowlisted dotdir is still traversed"
3115 );
3116 }
3117
3118 #[test]
3119 fn denylisted_dotdir_is_not_reported() {
3120 let dir = tempfile::tempdir().expect("create temp dir");
3121 write_at(dir.path(), ".idea/workspace.ts", "export const a = 1;\n");
3122 write_at(dir.path(), ".husky/hook.js", "export const b = 2;\n");
3123 write_at(dir.path(), ".next/page.js", "export const c = 3;\n");
3124 write_at(dir.path(), ".pnpm/x.js", "export const d = 4;\n");
3125
3126 let config = make_config(dir.path().to_path_buf(), false);
3127 let _ = discover_files(&config);
3128
3129 assert!(
3130 dotdir_diagnostics(dir.path()).is_empty(),
3131 "build caches, VCS and package-manager state never advise"
3132 );
3133 }
3134
3135 #[test]
3136 fn scoped_dotdir_is_traversed_and_not_reported() {
3137 let dir = tempfile::tempdir().expect("create temp dir");
3138 write_at(
3139 dir.path(),
3140 ".claude/hooks/probe.mjs",
3141 "export const a = 1;\n",
3142 );
3143
3144 let config = make_config(dir.path().to_path_buf(), false);
3145 let scopes = [HiddenDirScope::new(
3146 dir.path().to_path_buf(),
3147 vec![".claude".to_owned()],
3148 )];
3149 let files = discover_files_with_additional_hidden_dirs(&config, &scopes);
3150 let names = file_names(&files, dir.path());
3151
3152 assert!(
3153 dotdir_diagnostics(dir.path()).is_empty(),
3154 "a plugin- or script-contributed scope is admitted, so nothing was skipped"
3155 );
3156 assert!(names.contains(&".claude/hooks/probe.mjs".to_string()));
3157 }
3158
3159 #[test]
3160 fn ignore_patterns_silence_the_skipped_source_dotdir() {
3161 let dir = tempfile::tempdir().expect("create temp dir");
3162 write_at(
3163 dir.path(),
3164 ".claude/hooks/probe.mjs",
3165 "export const a = 1;\n",
3166 );
3167
3168 let config =
3169 make_config_with_ignores(dir.path().to_path_buf(), vec![".claude/**".to_owned()]);
3170 let _ = discover_files(&config);
3171
3172 assert!(
3173 dotdir_diagnostics(dir.path()).is_empty(),
3174 "the documented silencing route works"
3175 );
3176 }
3177
3178 #[test]
3179 fn dotdir_without_source_files_is_not_reported() {
3180 let dir = tempfile::tempdir().expect("create temp dir");
3181 write_at(dir.path(), ".claude/settings.json", "{}\n");
3182 write_at(dir.path(), ".claude/README.md", "# notes\n");
3183
3184 let config = make_config(dir.path().to_path_buf(), false);
3185 let _ = discover_files(&config);
3186
3187 assert!(dotdir_diagnostics(dir.path()).is_empty());
3188 }
3189
3190 #[test]
3191 fn dotdir_source_at_scan_depth_limit_is_reported() {
3192 let dir = tempfile::tempdir().expect("create temp dir");
3193 write_at(dir.path(), ".claude/a/b/deep.ts", "export const a = 1;\n");
3194
3195 let config = make_config(dir.path().to_path_buf(), false);
3196 let _ = discover_files(&config);
3197
3198 assert_eq!(dotdir_diagnostics(dir.path()).len(), 1);
3199 }
3200
3201 #[test]
3202 fn dotdir_source_below_scan_depth_limit_is_not_reported() {
3203 let dir = tempfile::tempdir().expect("create temp dir");
3204 write_at(
3205 dir.path(),
3206 ".claude/a/b/c/deeper.ts",
3207 "export const a = 1;\n",
3208 );
3209
3210 let config = make_config(dir.path().to_path_buf(), false);
3211 let _ = discover_files(&config);
3212
3213 assert!(
3214 dotdir_diagnostics(dir.path()).is_empty(),
3215 "the depth cap is real, so widening it stays a deliberate act"
3216 );
3217 }
3218
3219 fn mark_as_git_repo(root: &Path) {
3223 std::fs::create_dir_all(root.join(".git")).expect("create .git marker");
3224 }
3225
3226 #[test]
3227 fn gitignored_dotdir_contents_are_not_reported() {
3228 for pattern in [".tooling/**", ".tooling/*", "**/.tooling/**", "*.ts"] {
3232 let dir = tempfile::tempdir().expect("create temp dir");
3233 mark_as_git_repo(dir.path());
3234 write_at(dir.path(), ".gitignore", &format!("{pattern}\n"));
3235 write_at(dir.path(), ".tooling/mod.ts", "export const a = 1;\n");
3236
3237 let config = make_config(dir.path().to_path_buf(), false);
3238 let _ = discover_files(&config);
3239
3240 assert!(
3241 dotdir_diagnostics(dir.path()).is_empty(),
3242 "gitignore pattern '{pattern}' excludes the contents, so neither \
3243 advertised remedy would find anything there"
3244 );
3245 }
3246 }
3247
3248 #[test]
3249 fn self_ignoring_dotdir_is_not_reported() {
3250 let dir = tempfile::tempdir().expect("create temp dir");
3251 mark_as_git_repo(dir.path());
3252 write_at(dir.path(), ".toolcache/.gitignore", "*\n");
3253 write_at(dir.path(), ".toolcache/mod.ts", "export const a = 1;\n");
3254
3255 let config = make_config(dir.path().to_path_buf(), false);
3256 let _ = discover_files(&config);
3257
3258 assert!(
3259 dotdir_diagnostics(dir.path()).is_empty(),
3260 "a cache directory that ignores itself has excluded its own contents"
3261 );
3262 }
3263
3264 #[test]
3265 fn ungitignored_dotdir_in_a_git_repo_is_still_reported() {
3266 let dir = tempfile::tempdir().expect("create temp dir");
3267 mark_as_git_repo(dir.path());
3268 write_at(dir.path(), ".gitignore", "dist/\n");
3269 write_at(dir.path(), ".tooling/mod.ts", "export const a = 1;\n");
3270
3271 let config = make_config(dir.path().to_path_buf(), false);
3272 let _ = discover_files(&config);
3273
3274 assert_eq!(
3275 dotdir_diagnostics(dir.path()).len(),
3276 1,
3277 "the gitignore check must not swallow the case the diagnostic exists for"
3278 );
3279 }
3280
3281 #[test]
3282 fn production_run_does_not_report_a_test_only_dotdir() {
3283 let dir = tempfile::tempdir().expect("create temp dir");
3284 write_at(dir.path(), ".qa/thing.test.ts", "export const a = 1;\n");
3285 write_at(dir.path(), ".qa/thing.stories.tsx", "export const b = 2;\n");
3286
3287 let config = make_config(dir.path().to_path_buf(), true);
3288 let _ = discover_files(&config);
3289
3290 assert!(
3291 dotdir_diagnostics(dir.path()).is_empty(),
3292 "a --production run would analyze none of those files, so the \
3293 --root remedy would return nothing"
3294 );
3295 }
3296
3297 #[test]
3298 fn production_run_still_reports_a_dotdir_with_production_source() {
3299 let dir = tempfile::tempdir().expect("create temp dir");
3300 write_at(dir.path(), ".qa/thing.test.ts", "export const a = 1;\n");
3301 write_at(dir.path(), ".qa/helper.ts", "export const b = 2;\n");
3302
3303 let config = make_config(dir.path().to_path_buf(), true);
3304 let _ = discover_files(&config);
3305
3306 assert_eq!(dotdir_diagnostics(dir.path()).len(), 1);
3307 }
3308
3309 #[test]
3310 fn dotdir_with_only_generated_markup_is_not_reported() {
3311 let dir = tempfile::tempdir().expect("create temp dir");
3312 write_at(dir.path(), ".lighthouseci/lhr-1.html", "<html></html>\n");
3313 write_at(dir.path(), ".styles/theme.css", ":root { color: red; }\n");
3314 write_at(dir.path(), ".gql/schema.graphql", "type Query { a: Int }\n");
3315
3316 let config = make_config(dir.path().to_path_buf(), false);
3317 let _ = discover_files(&config);
3318
3319 assert!(
3320 dotdir_diagnostics(dir.path()).is_empty(),
3321 "the message claims imports and exports are lost, and these have none"
3322 );
3323 }
3324
3325 #[test]
3326 fn generated_tool_and_foreign_vcs_dotdirs_are_not_reported() {
3327 let dir = tempfile::tempdir().expect("create temp dir");
3328 write_at(dir.path(), ".astro/types.d.ts", "export {};\n");
3329 write_at(dir.path(), ".wxt/types/imports.d.ts", "export {};\n");
3330 write_at(dir.path(), ".yalc/pkg/index.js", "export const a = 1;\n");
3331 write_at(dir.path(), ".jj/repo/config.js", "export const b = 2;\n");
3332 write_at(dir.path(), ".svn/pristine/y.js", "export const c = 3;\n");
3333
3334 let config = make_config(dir.path().to_path_buf(), false);
3335 let _ = discover_files(&config);
3336
3337 assert!(
3338 dotdir_diagnostics(dir.path()).is_empty(),
3339 "generated output and foreign VCS metadata are not first-party source"
3340 );
3341 }
3342
3343 #[test]
3344 fn denylisted_dotdirs_do_not_consume_the_candidate_ceiling() {
3345 let dir = tempfile::tempdir().expect("create temp dir");
3346 for index in 0..(DOTDIR_SCAN_MAX_CANDIDATES + 8) {
3349 write_at(
3350 dir.path(),
3351 &format!("packages/pkg{index:03}/.turbo/blob.js"),
3352 "export const a = 1;\n",
3353 );
3354 }
3355 write_at(dir.path(), "zz/.tooling/mod.ts", "export const b = 2;\n");
3356
3357 let config = make_config(dir.path().to_path_buf(), false);
3358 let _ = discover_files(&config);
3359
3360 let reported = dotdir_diagnostics(dir.path());
3361 assert_eq!(reported.len(), 1, "{reported:?}");
3362 assert!(reported[0].path.ends_with(".tooling"));
3363 }
3364
3365 #[test]
3366 fn one_pathological_dotdir_cannot_starve_the_rest() {
3367 let dir = tempfile::tempdir().expect("create temp dir");
3368 for index in 0..(DOTDIR_SCAN_MAX_ENTRIES * 2) {
3370 write_at(dir.path(), &format!(".aaa-noise/f{index}.bin"), "x");
3371 }
3372 write_at(dir.path(), ".zzz-real/mod.ts", "export const a = 1;\n");
3373
3374 let config = make_config(dir.path().to_path_buf(), false);
3375 let _ = discover_files(&config);
3376
3377 let reported = dotdir_diagnostics(dir.path());
3378 assert_eq!(reported.len(), 1, "{reported:?}");
3379 assert!(reported[0].path.ends_with(".zzz-real"));
3380 }
3381
3382 #[test]
3383 fn repeat_walks_do_not_stack_skipped_source_dotdirs() {
3384 let dir = tempfile::tempdir().expect("create temp dir");
3385 write_at(
3386 dir.path(),
3387 ".claude/hooks/probe.mjs",
3388 "export const a = 1;\n",
3389 );
3390
3391 let config = make_config(dir.path().to_path_buf(), false);
3392 let _ = discover_files(&config);
3393 let _ = discover_files(&config);
3394
3395 assert_eq!(
3396 dotdir_diagnostics(dir.path()).len(),
3397 1,
3398 "each walk replaces its own root's source-discovery set"
3399 );
3400 }
3401
3402 #[test]
3403 fn skips_large_one_line_js_as_minified_generated_output() {
3404 let dir = tempfile::tempdir().expect("create temp dir");
3405 let src = dir.path().join("src");
3406 std::fs::create_dir_all(&src).unwrap();
3407 let asset = src.join("index-abc123.js");
3408 std::fs::write(&asset, "x".repeat(MINIFIED_FILE_SKIP_BYTES as usize + 1)).unwrap();
3409
3410 let config = make_config(dir.path().to_path_buf(), false);
3411 let files = discover_files(&config);
3412 let names = file_names(&files, dir.path());
3413
3414 assert!(
3415 !names.contains(&"src/index-abc123.js".to_string()),
3416 "large one-line JS assets should be skipped before parsing"
3417 );
3418
3419 let diagnostics = fallow_config::workspace_diagnostics_for(dir.path());
3420 assert!(
3421 diagnostics.iter().any(|diag| {
3422 diag.path.ends_with("src/index-abc123.js")
3423 && matches!(
3424 diag.kind,
3425 fallow_config::WorkspaceDiagnosticKind::SkippedMinifiedFile { .. }
3426 )
3427 }),
3428 "the skipped minified asset is recorded for JSON output: {diagnostics:?}"
3429 );
3430 }
3431
3432 #[test]
3433 fn unlimited_size_keeps_large_one_line_js() {
3434 let dir = tempfile::tempdir().expect("create temp dir");
3435 let src = dir.path().join("src");
3436 std::fs::create_dir_all(&src).unwrap();
3437 let asset = src.join("index-abc123.js");
3438 std::fs::write(&asset, "x".repeat(MINIFIED_FILE_SKIP_BYTES as usize + 1)).unwrap();
3439
3440 let config = make_config_with_max_file_size(dir.path().to_path_buf(), None);
3441 let files = discover_files(&config);
3442 let names = file_names(&files, dir.path());
3443
3444 assert!(
3445 names.contains(&"src/index-abc123.js".to_string()),
3446 "--max-file-size 0 should opt out of generated JS skipping"
3447 );
3448 }
3449
3450 #[test]
3451 fn keeps_large_multiline_js() {
3452 let dir = tempfile::tempdir().expect("create temp dir");
3453 let src = dir.path().join("src");
3454 std::fs::create_dir_all(&src).unwrap();
3455 let asset = src.join("handwritten.js");
3456 let mut content = String::new();
3457 while content.len() <= MINIFIED_FILE_SKIP_BYTES as usize + 1 {
3458 content.push_str("export const value = 1;\n");
3459 }
3460 std::fs::write(&asset, content).unwrap();
3461
3462 let config = make_config(dir.path().to_path_buf(), false);
3463 let files = discover_files(&config);
3464 let names = file_names(&files, dir.path());
3465
3466 assert!(
3467 names.contains(&"src/handwritten.js".to_string()),
3468 "large multiline JS should not be treated as a generated minified asset"
3469 );
3470 }
3471 }
3472}