Skip to main content

fallow_core/discover/
walk.rs

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
17/// Process-wide dedupe of the size-skip / largest-files stderr notes, keyed by a
18/// content-derived string, so combined-mode (`fallow` runs check + dupes +
19/// health, each of which can trigger a source walk) emits each note at most once
20/// per distinct content. Mirrors the workspace-diagnostics `should_emit`
21/// pattern (issue #1086).
22pub(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
30/// A discovered file path paired with its on-disk size in bytes, as collected
31/// by the parallel walker before [`DiscoveredFile`] ids are assigned.
32type SizedFile = (PathBuf, u64);
33
34/// Dot-prefixed directories the walk dropped, collected inside the parallel
35/// walker's `filter_entry` predicate.
36///
37/// `Arc<Mutex<..>>` and not a borrow: `WalkBuilder::filter_entry` requires
38/// `Fn(&DirEntry) -> bool + Send + Sync + 'static`, so the closure can neither
39/// borrow a local nor mutate captured state directly, and the predicate runs
40/// on every walker thread.
41type SkippedDotdirSink = Arc<Mutex<Vec<PathBuf>>>;
42
43/// Candidate source files ONE built-in ignore pattern removed from a walk,
44/// with the directories they sat in (issue #2638).
45///
46/// The scope map is deliberately uncapped, and its size is not the risk it
47/// looks like. For a directory-shaped pattern every file under one matched
48/// directory collapses to a single key, the one built-in that could span a
49/// whole dependency tree is never tallied at all (see
50/// [`UNREPORTED_DEFAULT_IGNORES`]), and only the file-shaped patterns
51/// (`**/*.min.js` and friends) key on a file's parent, on files that are rare
52/// by construction. The map is therefore strictly smaller than the file list
53/// the same walk already holds. A ceiling would buy nothing and would cost
54/// both determinism and honesty: the parallel walk fills per-thread maps in
55/// nondeterministic order, so "the first N directories" is not a stable set,
56/// the anchor picked from a truncated set would differ between runs of the
57/// same command, and `directory_count` would become a floor rather than a
58/// count.
59#[derive(Default)]
60struct ExcludedByPattern {
61    /// Candidate source files this pattern excluded. Exact.
62    file_count: u32,
63    /// Excluded files per scope directory, project-root-relative.
64    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    /// Distinct directories this pattern MATCHED at, which for a
81    /// directory-shaped pattern is not the number of directories that held
82    /// the files: everything under one matched `dist/` collapses to that one
83    /// scope. Exact, and the number the message needs to say whether
84    /// [`Self::anchor`] names the whole exclusion or one matched location of
85    /// several.
86    fn directory_count(&self) -> u32 {
87        u32::try_from(self.scopes.len()).unwrap_or(u32::MAX)
88    }
89
90    /// The matched directory this pattern excluded the most files from, ties
91    /// broken by the lexicographically first path so two runs on one tree
92    /// report the same anchor.
93    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
106/// Per-built-in-pattern exclusion tallies, keyed by index into
107/// [`DEFAULT_IGNORE_PATTERNS`].
108type ExclusionTally = FxHashMap<usize, ExcludedByPattern>;
109
110/// Built-in ignore patterns whose exclusions are never reported (issue #2638).
111///
112/// The diagnostic exists to say "first-party source of yours was dropped".
113/// `**/node_modules/**` drops installed dependencies, which are nobody's
114/// first-party source: on a project whose `node_modules` is not gitignored it
115/// would report a five-figure count whose only honest remedy is "that is your
116/// dependency tree", and it would be the one built-in able to dominate the
117/// walk's memory with per-package scope directories. `**/.git/**` cannot fire
118/// at all, because hidden directories are not traversed. `VIZ_MAP` is fallow's own output.
119const UNREPORTED_DEFAULT_IGNORES: &[&str] = &["**/node_modules/**", "**/.git/**", VIZ_MAP];
120const VIZ_MAP: &str = "fallow-viz.html";
121
122/// The directory a built-in pattern excluded a file "at": the LONGEST prefix
123/// of the file's project-relative parent directory ending in the pattern's
124/// literal segment, or the parent itself when the pattern has no literal
125/// segment.
126///
127/// `**/build/**` with `projects/app/build/static/js/main.js` gives
128/// `projects/app/build`, which is the directory a reader can act on and the
129/// one `fallow --root` takes. The deepest match is what makes that remedy
130/// true: the glob is tested against the path relative to the run root, so on
131/// `build/tools/build/a.ts` an anchor at the outer `build` leaves the inner
132/// one in the relative path and the built-in matches again. `**/*.min.js` has
133/// no literal segment, so `vendor/a.min.js` gives `vendor`. A root-level match
134/// returns the empty path, which the diagnostic renders as the root itself.
135fn 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
151/// Number of example file paths named in the aggregated skipped-large-file and
152/// largest-files stderr notes before the tail collapses to "and N more". Keeps
153/// the notes to one bounded line on a monorepo that skips many files.
154const NOTE_EXAMPLE_CAP: usize = 5;
155
156/// Directory levels below a skipped dotdir the bounded scan descends. The
157/// dotdir itself is level 0. Two levels reach the conventional
158/// `<dotdir>/<group>/<file>` layout (`.claude/hooks/probe.mjs`) with one level
159/// of headroom, and stop well above a vendored toolchain tree.
160const DOTDIR_SCAN_MAX_DEPTH: usize = 2;
161
162/// Directory entries the bounded scan reads across all levels of ONE skipped
163/// dotdir. The ceiling this buys is a SYSCALL count, not a wall-clock figure:
164/// a directory-heavy dotdir spends budget on subdirectories that each cost an
165/// opendir of their own, so 256 entries can still mean 257 directory reads and
166/// several milliseconds. State the bound in syscalls, never in milliseconds.
167const DOTDIR_SCAN_MAX_ENTRIES: usize = 256;
168
169/// Directory entries the bounded scan reads across ALL skipped dotdirs in one
170/// walk. [`DOTDIR_SCAN_MAX_ENTRIES`] bounds a single directory and nothing
171/// bounded the sum, so the added cost was linear in the candidate count: a
172/// synthetic tree of 1000 directory-heavy dotdirs turned a 191 ms run into
173/// 6.6 s. Candidates are scanned in sorted order and share this budget, so
174/// exhausting it drops the advisory for the remaining candidates
175/// deterministically instead of paying an unbounded cost. With this ceiling
176/// and [`DOTDIR_SCAN_MAX_CANDIDATES`] the same synthetic trees measure about
177/// 30 ms of added work whether they hold 300 or 1000 candidates, and a real
178/// repository stays inside run-to-run noise.
179const DOTDIR_SCAN_TOTAL_ENTRIES: usize = 1024;
180
181/// Skipped dotdirs the bounded scan OPENS in one walk. The entry budget does
182/// not bound the per-candidate setup cost, since each scan builds its own
183/// gitignore matcher chain (about 0.2 ms) before it reads a single entry, so
184/// the candidate count needs a ceiling of its own. Counted after the name
185/// checks, so a monorepo full of `.turbo` and `.next` directories cannot spend
186/// the ceiling on directories that were never going to be scanned.
187const DOTDIR_SCAN_MAX_CANDIDATES: usize = 64;
188
189/// File extensions that put a file in the module graph the advisory talks
190/// about. Narrower than [`SOURCE_EXTENSIONS`] on purpose: the message states
191/// that the directory's imports and exports are not analyzed, and that is only
192/// true of code. A dotdir holding nothing but a generated Lighthouse
193/// `report.html`, a Sanity runtime page, a `schema.graphql`, or a stylesheet
194/// has no imports or exports to lose, so it does not earn the advisory.
195const DOTDIR_MODULE_EXTENSIONS: &[&str] = &[
196    "ts", "tsx", "mts", "cts", "gts", "js", "jsx", "mjs", "cjs", "gjs", "vue", "svelte", "astro",
197    "mdx",
198];
199
200/// Discovered-file-count threshold above which the pre-parse largest-files note
201/// fires, so an out-of-memory hang at the parse stage has a visible suspect
202/// list (issue #1086).
203const LARGE_SET_THRESHOLD: usize = 20_000;
204
205/// Single-file byte threshold above which the pre-parse largest-files note
206/// fires even on a small project. Set just under the default 5 MB skip so the
207/// note fires for kept files that are approaching the skip limit (the genuine
208/// out-of-memory suspects), not for ordinary large-but-benign files.
209const LARGE_FILE_NOTE_BYTES: u64 = 4 * 1024 * 1024;
210
211/// Minimum size for a file to appear in the largest-files note. Filters out the
212/// `0.0 MB` entries that would otherwise pad the list once it fires, keeping the
213/// named files to plausible memory contributors.
214const NOTE_FILE_FLOOR_BYTES: u64 = 256 * 1024;
215
216/// Minimum size for content-shape based minified-bundle skipping. Smaller
217/// one-line files can be hand-written utilities, while multi-MB one-line JS is
218/// generated output in practice.
219const MINIFIED_FILE_SKIP_BYTES: u64 = 1024 * 1024;
220
221/// Number of bytes inspected when deciding whether a large JS file is minified.
222const MINIFIED_SAMPLE_BYTES: usize = 256 * 1024;
223
224/// A single line this long in a multi-MB JS file is treated as generated
225/// minified output. This avoids parsing assets that can expand to huge ASTs.
226const MINIFIED_LONG_LINE_BYTES: usize = 128 * 1024;
227
228/// Whether a path is a TypeScript declaration file (`.d.ts`/`.d.mts`/`.d.cts`).
229/// Declaration files are exempt from the per-file size skip because they are
230/// reachability roots for global types: skipping a large `auto-imports.d.ts`
231/// would false-flag the files whose types it provides.
232fn 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
280/// Render a byte count as a megabyte figure with one decimal place.
281fn 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
290/// Join up to [`NOTE_EXAMPLE_CAP`] `path (size)` examples (already ordered) into
291/// one comma-separated string, collapsing the tail to "and N more".
292fn 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
309/// Split discovered `(path, size)` pairs into the kept set and the set skipped
310/// for exceeding `max_file_size_bytes`. Declaration files are never skipped.
311fn 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
322/// Split discovered `(path, size)` pairs into files kept for parsing and files
323/// skipped because they look like generated minified JavaScript.
324fn 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
335/// Include the missing dependency tree in the walk's source-discovery diagnostics.
336/// The walk replaces the root's full set, so a separate write can be lost.
337fn 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
349/// Return diagnostics for skipped large files and emit at most one warning for the batch.
350fn 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
390/// Build the typed diagnostics for generated minified JS files skipped before
391/// parsing. The caller writes the returned list to the registry.
392fn 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
433/// Join up to [`NOTE_EXAMPLE_CAP`] root-relative paths (already ordered) into
434/// one comma-separated string, collapsing the tail to "and N more". The
435/// size-bearing sibling is [`summarize_examples`].
436fn 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
450/// Like [`summarize_paths`], but for a list already known to be incomplete: the
451/// tail reads "and more" rather than naming a count the run cannot vouch for.
452fn 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
465/// Whether a candidate file inside a skipped dotdir is one this run had
466/// already excluded from analysis, matching what [`FileVisitor`] applies to
467/// every discovered file: the compiled `ignorePatterns` set (user entries plus
468/// the built-in defaults) against the ROOT-RELATIVE path, plus the production
469/// excludes when the run is a `--production` run.
470///
471/// The root-relative path is what matters. Matching the directory path instead
472/// would miss the pattern a user actually writes, because `.claude/**` does not
473/// match `.claude`.
474///
475/// Production excludes ARE applied, even though the skip the diagnostic reports
476/// is a traversal decision that `--production` does not change. A `--production`
477/// run that named a dotdir holding only `thing.test.ts` would print a remedy
478/// (`fallow --root .qa --production`) that returns nothing, so the advisory has
479/// to agree with the file set the run would actually analyze. Combined mode's
480/// two walks can therefore disagree, which the documented union semantics of
481/// the combined root already cover.
482fn 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
492/// True when `path` carries an extension that puts it in the module graph the
493/// advisory describes. See [`DOTDIR_MODULE_EXTENSIONS`] for why this is
494/// narrower than [`has_source_extension`].
495fn 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
501/// Depth- and entry-capped search for one reportable source file, stopping at
502/// the first hit. Never a recursive walk of an unbounded tree: the ceiling is
503/// `1 + DOTDIR_SCAN_MAX_ENTRIES` directory reads for one dotdir, and
504/// [`DOTDIR_SCAN_TOTAL_ENTRIES`] across the whole walk.
505///
506/// Runs on `fallow_config::source_walk_builder`, the git settings of the
507/// source walk, rather than a bare `read_dir`, because "the project has not excluded it" has
508/// to mean what git means. Only the DIRECTORY form of a gitignore rule
509/// (`.build-tools/`) prunes a dotdir before the walk's own filter sees it: the
510/// `dir/**`, `dir/*`, `**/dir/**` and file-level (`*.ts`) forms all leave the
511/// directory reaching this scan with every file inside it ignored, and a cache
512/// directory that ignores itself through its own nested `.gitignore` does the
513/// same. Reporting those would advertise two remedies that both do nothing,
514/// since a re-rooted `fallow --root <dir>` still reads the parent repository's
515/// gitignore and would find no files either.
516///
517/// Accepted tradeoff: for a dotdir with more than [`DOTDIR_SCAN_MAX_ENTRIES`]
518/// entries whose only source file sits past the budget, the verdict is
519/// readdir-order dependent, so the advisory can flap between runs. The
520/// alternative is unbounded I/O, and the consequence of a flap is a missing
521/// advisory line, never a changed analysis. No test may depend on that
522/// boundary.
523fn 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        // Regular files only. A symlink is never followed out of the scan, and
555        // a fifo or a socket named `pipe.ts` is not source either.
556        #[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
575/// Path components whose subtrees never earn the skipped-source-dotdir
576/// advisory. A hidden directory under one of these is test scaffolding rather
577/// than first-party source the project meant to analyze.
578const 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
592/// Whether a dropped dotdir is worth opening at all. Decided from the PATH
593/// alone, so it costs no I/O and runs before the scan budget is touched:
594/// `.git` in a large repository, a `.jj` object store, and `node_modules/.pnpm`
595/// are never opened. `ALLOWED_HIDDEN_DIRS` and every plugin- or
596/// script-contributed scope are already excluded by construction, since a
597/// directory they admit is never dropped and so never reaches this list.
598fn 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    // Pure cost saving, not a further condition: the built-in `**/node_modules/**`
607    // ignore default makes every file under a `node_modules` component ignored,
608    // so the scan could only ever return false.
609    if relative
610        .components()
611        .any(|component| component.as_os_str() == OsStr::new("node_modules"))
612    {
613        return false;
614    }
615    // Precision, and the one place this check is deliberately less complete
616    // than it could be. A hidden directory under a test, fixture, or playground
617    // tree is usually there BECAUSE it is hidden: some of these exist purely to
618    // exercise hidden-directory handling, so an advisory about them is wrong
619    // about the project every time it fires. Measured on a ten-repository
620    // corpus, this component filter removes every false positive one framework
621    // contributed and half of another's while keeping the true positives, which
622    // sit at a repository root rather than under a test tree.
623    !relative.components().any(|component| {
624        DOTDIR_NOISE_PATH_COMPONENTS.contains(&component.as_os_str().to_string_lossy().as_ref())
625    })
626}
627
628/// Build the typed diagnostics for the built-in ignore patterns that removed
629/// candidate source files from this walk (issue #2638).
630///
631/// One entry per pattern, in [`DEFAULT_IGNORE_PATTERNS`] order, so the array
632/// grows by at most the number of built-in patterns on a project of any size
633/// and its order does not depend on the parallel walk. No stderr output: the
634/// kind answers `warns_on_stderr()` with `false`, and the CLI prints a note
635/// only under `--explain-skipped`.
636fn 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                // A file-shaped built-in that matched a file directly at the
658                // analysis root anchors at the root, and `root.join("")` is the
659                // root itself. The analysis envelopes' post-serialisation strip
660                // only removes a `root + separator` prefix, so without this the
661                // entry would carry the absolute host path into every JSON
662                // surface while its siblings render `.`.
663                .into_root_relative(&config.root),
664            )
665        })
666        .collect()
667}
668
669/// Report that the walk finished with nothing to analyze (issue #2686).
670///
671/// The condition is the file list being empty, not any particular exclusion,
672/// so it also covers a docs-only repository, a workspace member with no
673/// TypeScript and a path filter that matched nothing. The built-in-ignore
674/// tally rides along as `excluded_file_count` so the common cause stays
675/// attributable without making it the trigger.
676///
677/// Recorded by discovery rather than by the CLI's human note, so every envelope
678/// built from a diagnostics snapshot carries it: the MCP tools and the
679/// programmatic routes share this list, and a kind that existed on the CLI path
680/// alone would break that.
681fn 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
702/// Build the typed diagnostics for the dot-prefixed directories this walk
703/// dropped that hold source files the project has not excluded, and emit one
704/// aggregated `tracing::warn!` so the otherwise silent skip is visible on
705/// stderr too (issue #461). The caller writes the returned list to the
706/// registry.
707fn 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    // The caller sorted and deduped, so both caps truncate deterministically:
716    // the same tree reports the same prefix on every run.
717    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    // Either ceiling can stop the scan with candidates left unexamined, so the
729    // count is a floor rather than a total whenever one of them binds.
730    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
764/// Build the skipped-source-dotdir note. Pure so the singular and plural forms,
765/// the truncated prefix, and the single-directory remedy substitution are
766/// unit-testable without a tracing subscriber, mirroring
767/// [`build_largest_files_note`].
768///
769/// With exactly one directory the remedy names it instead of printing a `<dir>`
770/// placeholder: the path is already known and was printed a few words earlier,
771/// so a placeholder would make the one case a user can act on directly the one
772/// case they have to retype.
773fn build_skipped_dotdirs_note(root: &Path, reportable: &[&PathBuf], truncated: bool) -> String {
774    let count = reportable.len();
775    // An exact remainder inside an explicitly inexact total reads as a
776    // contradiction ("at least 64 ... and 59 more"), so a truncated run drops
777    // the tail count.
778    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
804/// Build the pre-parse largest-files note, or `None` when the discovered set is
805/// neither unusually large nor contains an unusually large file. Pure so the
806/// pluralization, floor filtering, and count-only fallback are unit-testable
807/// without a tracing subscriber. See issue #1086.
808fn 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        // Large file SET with no individually large file: report the count only,
826        // omitting a "largest:" list that would otherwise be all sub-floor noise.
827        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
839/// Emit a pre-parse note listing the largest kept files when the discovered set
840/// is unusually large or contains an unusually large file, so an out-of-memory
841/// hang at the parse stage is diagnosable (issue #1086). Visible before the
842/// expensive parse begins, so it survives a subsequent crash.
843fn 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/// How a [`HiddenDirScope`] matches a hidden directory during the walk.
855#[derive(Debug, Clone, Copy, PartialEq, Eq)]
856pub enum HiddenDirMatch {
857    /// Match by directory NAME at any depth beneath the scope root.
858    ///
859    /// Framework plugins declare bundle-boundary conventions like `.client`
860    /// and `.server` that a project may place under any route directory, so
861    /// the name is the whole rule and the depth is not knowable in advance.
862    AnyDepth,
863    /// Match the exact root-relative directory PATH.
864    ///
865    /// A `package.json` script naming `.a/.b/build.mjs` states where the file
866    /// it needs actually lives, so the scope admits `<root>/.a` and
867    /// `<root>/.a/.b` and nothing else. An unrelated `packages/x/.b` stays
868    /// untraversed (issue #461).
869    ExactPath,
870}
871
872/// Package-scoped hidden directories that source discovery should traverse.
873#[derive(Debug, Clone, PartialEq, Eq)]
874pub struct HiddenDirScope {
875    root: PathBuf,
876    dirs: Vec<String>,
877    match_mode: HiddenDirMatch,
878}
879
880impl HiddenDirScope {
881    /// Build a scope rooted at a package directory that admits the given
882    /// hidden directory names at any depth beneath it.
883    ///
884    /// This is the plugin-contributed shape. For a scope inferred from a
885    /// concrete path, use [`HiddenDirScope::new_exact_paths`], which does not
886    /// admit the same name elsewhere in the tree.
887    #[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    /// Build a scope rooted at a package directory that admits exactly the
897    /// given root-relative directory paths.
898    #[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    /// Rebuild a scope with an explicit match mode.
908    ///
909    /// Used when a scope crosses a crate boundary and must arrive with the
910    /// same semantics it left with.
911    #[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                // `Path` compares component-wise, so a `/`-separated entry
942                // from a script string matches on every platform.
943                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
952/// Per-thread file collector for the parallel walker.
953///
954/// Source files (by extension) flow to `shared`; when `config_shared` is set,
955/// non-source files admitted by the config-candidate type group flow to it
956/// instead. The two channels are disjoint and the source channel is byte-for-byte
957/// identical to the config-capture-disabled walk.
958struct FileVisitor<'a> {
959    root: &'a Path,
960    canonical_root: Option<&'a Path>,
961    ignore_patterns: &'a IgnorePatternSet,
962    /// Globs at the front of `ignore_patterns` that came from the project's
963    /// own `ignorePatterns`; the built-in defaults follow them.
964    user_ignore_pattern_count: usize,
965    /// Plugin- and script-contributed hidden directory scopes. Read only when
966    /// the project wrote a `!` exception, to tell a hidden directory that the
967    /// exception opened from one the walk always opens.
968    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    /// Reused across every excluded candidate so attribution allocates once
977    /// per walker thread rather than once per file.
978    match_buf: Vec<usize>,
979}
980
981impl FileVisitor<'_> {
982    /// Attribute one excluded candidate source file to the built-in pattern
983    /// that removed it (issue #2638), and credit the `ignorePatterns` globs
984    /// that removed any file that no built-in matches (issue #3824).
985    ///
986    /// Runs only on files `is_match` already rejected, so the kept-file path
987    /// still pays a single boolean match.
988    fn record_ignore_exclusion(&mut self, relative: &Path, is_source: bool) {
989        // Cheap pre-filter, not a second rule: `**/node_modules/**` is in
990        // UNREPORTED_DEFAULT_IGNORES, and it is also the one built-in that
991        // fires on an entire dependency tree. Testing a path component beats
992        // running the whole glob union over tens of thousands of files whose
993        // attribution the report would then discard.
994        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        // globset returns ascending indices, so the first match is both the
1005        // cheapest user-pattern test and the lowest-index built-in.
1006        let Some(&first) = self.match_buf.first().filter(|_| is_source) else {
1007            return;
1008        };
1009        if first < self.user_ignore_pattern_count {
1010            // An `ignorePatterns` entry also matched. The project chose this
1011            // exclusion, so reporting it as a surprise would be wrong.
1012            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            // A non-source file admitted by the config-candidate type group. No
1075            // size metadata is needed; these are pattern-matched, never parsed.
1076            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
1124/// Builder that creates per-thread `FileVisitor` instances for the parallel walker.
1125struct 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
1157/// File extensions discovery treats as analyzable source files.
1158pub 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
1163/// Glob patterns for test/dev/story files excluded in production mode.
1164pub 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
1187/// Check if a hidden directory name is on the allowlist.
1188pub 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
1202/// Files Yarn Plug'n'Play writes at the workspace root. They carry source
1203/// extensions (`.pnp.cjs` is the multi-megabyte generated loader with the
1204/// install state inlined, `.pnp.loader.mjs` its ESM shim) but are install
1205/// artifacts, not project source, so the walker drops them by name.
1206const 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
1214/// Check if a hidden directory entry should be allowed through the filter.
1215///
1216/// Returns `true` if the entry is not hidden or is on the allowlist.
1217/// Hidden files (not directories) are allowed through since the type filter
1218/// handles them, except for the generated Yarn PnP files.
1219fn 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
1238/// True when a parent directory of `path` is hidden and only a `!` exception
1239/// in `ignorePatterns` opened it: the allowlist and the plugin and script
1240/// scopes do not admit it. A file there is kept only when an exception
1241/// matches the file itself.
1242fn 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
1259/// Discover all source files in the project.
1260///
1261/// # Panics
1262///
1263/// Panics if the file type glob or progress template is invalid (compile-time constants).
1264pub fn discover_files(config: &ResolvedConfig) -> Vec<DiscoveredFile> {
1265    discover_files_with_additional_hidden_dirs(config, &[])
1266}
1267
1268/// The set of config-file basenames (last path component of every built-in
1269/// plugin `config_patterns()` entry, brace forms preserved) that the walk should
1270/// additionally admit so non-source configs (`tsconfig.json`, `bunfig.toml`,
1271/// `.eslintrc.json`, ...) can be captured in one traversal instead of being
1272/// re-discovered by a filesystem re-walk in `discover_config_files`.
1273///
1274/// Derived live from the built-in plugin list, so it can never drift behind a
1275/// new plugin's config patterns. Source-extension config basenames
1276/// (`vite.config.{ts,js}`) are admitted too, but the walk visitor routes them
1277/// back to the source channel by extension, so the config channel only ever
1278/// collects genuinely non-source files.
1279fn 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            // Tooling evidence resolves always-used files, such as `.c8rc.json`,
1289            // in workspace directories against the candidate index. A wildcard
1290            // basename such as `*` would capture every file, so it stays out.
1291            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
1307/// True when `path`'s extension is one of the known source extensions, i.e. the
1308/// file belongs in the source channel rather than the config-candidate channel.
1309fn 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/// Build the file-type filter. Always selects known source extensions; when
1316/// `capture_config` is set, also selects config-candidate basenames so the
1317/// walker yields them for the second collection channel.
1318#[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                    // Ignore individually-invalid plugin patterns rather than panicking;
1342                    // a malformed pattern simply fails to admit its config file (the
1343                    // pre-existing filesystem fallback still covers production mode).
1344                    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
1353/// Construct the parallel walker, applying the appropriate hidden-dir filter.
1354/// When `capture_config` is set the walk also yields config-candidate files for
1355/// the secondary collection channel.
1356fn 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    // One filter, not two: `filter_entry` replaces rather than chains, and the
1367    // dropped-dotdir record has to happen on the same false path that decides
1368    // the skip so the allowlist and every plugin- or script-contributed scope
1369    // are excluded by construction (issue #461).
1370    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        // A `!` entry in `ignorePatterns` that can match a file in this hidden
1379        // directory opens it (issue #2452). The visitor then keeps only the
1380        // files an exception matches.
1381        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
1402/// Discover all source files in the project, with package-scoped hidden dirs.
1403///
1404/// # Panics
1405///
1406/// Panics if the file type glob or progress template is invalid (compile-time constants).
1407pub 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
1414/// Discover source files AND, in one traversal, the non-source config-candidate
1415/// files (`tsconfig.json`, `bunfig.toml`, `.eslintrc.json`, ...) used by
1416/// `discover_config_files` to resolve plugin config patterns in-memory instead of
1417/// re-walking the filesystem.
1418///
1419/// The returned `Vec<DiscoveredFile>` is byte-for-byte identical to the
1420/// config-capture-disabled walk: config candidates are routed to the second
1421/// return value by extension and never enter the source channel. Config capture
1422/// is skipped in production mode (where the walk applies `PRODUCTION_EXCLUDE_PATTERNS`
1423/// and `discover_config_files` keeps its filesystem path), so the second vector is
1424/// empty there.
1425///
1426/// # Panics
1427///
1428/// Panics if the file type glob or progress template is invalid (compile-time constants).
1429pub 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
1438/// Source files, config candidates, and the source-discovery diagnostics one
1439/// walk produced.
1440///
1441/// `diagnostics` is the walk's OWN skip list, not a read of the process-wide
1442/// registry: combined mode can run two walks on the same root concurrently, and
1443/// each walk replaces the registry's source-discovery entries, so only the
1444/// by-value list is a stable answer to "what did THIS analysis skip" (issue
1445/// #2366).
1446pub struct DiscoveredSources {
1447    /// Source files with stable path-sorted [`FileId`]s.
1448    pub files: Vec<DiscoveredFile>,
1449    /// Non-source config-candidate paths captured in the same traversal.
1450    pub config_candidates: Vec<PathBuf>,
1451    /// Skipped-large-file, skipped-minified-file, and skipped-source-dotdir
1452    /// diagnostics from this walk.
1453    pub diagnostics: Vec<WorkspaceDiagnostic>,
1454}
1455
1456/// [`discover_files_and_config_candidates`] plus the source-discovery
1457/// diagnostics this walk recorded, for callers that must carry a per-analysis
1458/// snapshot instead of reading the shared registry back (issue #2366).
1459///
1460/// # Panics
1461///
1462/// Panics if the file type glob or progress template is invalid (compile-time constants).
1463#[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    // ADR-004 (path-sorted FileIds): the parallel walk visits files in
1506    // nondeterministic order, so we sort by absolute path BEFORE the
1507    // `.enumerate()` FileId assignment below. This is the stable-cross-run
1508    // identity invariant the persisted graph cache depends on: an identical
1509    // file set yields identical FileIds, so a cache hit (same paths +
1510    // fingerprints) can trust graph data persisted by FileId. Do not replace
1511    // this with insertion-order assignment.
1512    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    // The parallel walk records dotdirs in nondeterministic thread order, and
1524    // the diagnostic array order is part of the JSON contract, so sort and
1525    // dedupe before the predicate runs (issue #2366).
1526    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    // One registry write replaces this root's whole source-discovery set, so a
1536    // stale entry from a previous pass drops out (issue #1086) without a window
1537    // in which a concurrent walk on the same root can observe or clobber a
1538    // half-written set (issue #2366).
1539    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    /// Issue #2638: the anchor a directory-shaped built-in reports is the
1590    /// directory a reader can act on and the one `fallow --root` takes, not
1591    /// the deepest directory that happens to hold the file.
1592    #[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    /// The DEEPEST matching segment wins, because the anchor is the directory
1617    /// the `--root` remedy names. Anchoring at the outermost `build` would
1618    /// leave the inner one in the path relative to the new root, so the
1619    /// built-in would match again and the advertised remedy would recover
1620    /// nothing.
1621    #[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    /// A file-shaped pattern has no literal segment to stop at, so the file's
1634    /// own directory is the most specific honest answer.
1635    #[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    /// A pattern whose literal segment is absent from the path (only reachable
1649    /// through a future pattern shape) degrades to the parent rather than
1650    /// returning the whole path.
1651    #[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    /// Issue #2638: `directory_count` distinguishes one contained tree from an
1660    /// exclusion scattered over sibling packages, which is what keeps the
1661    /// rendered message from claiming a majority it does not have.
1662    #[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    /// Issue #2638 (AC2): the anchor is the directory with the most excluded
1677    /// files, and a tie resolves to the lexicographically first path so two
1678    /// runs on one tree report the same location.
1679    #[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    /// Per-thread tallies merge into one exact total, which is what makes
1696    /// `file_count` trustworthy on a parallel walk.
1697    #[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    /// Reproduce the FileId-assignment rule used by `walk_source_files`: sort by
1808    /// absolute path, then assign `FileId(idx)` in that order.
1809    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    /// ADR-004: an identical file set must yield identical FileIds regardless of
1822    /// the (nondeterministic, parallel) discovery order. The persisted graph
1823    /// cache keys persisted graph data by FileId, so a cache HIT (same paths +
1824    /// fingerprints) must reproduce the exact same FileId-to-path mapping the
1825    /// graph was built against. This guards the cache's soundness prerequisite.
1826    #[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        // Two independent walks that observe the same paths in DIFFERENT orders.
1837        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        // Identical (FileId -> path) mapping despite the different walk orders.
1848        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        // The mapping is the path-sorted order, and each FileId equals its index
1855        // (the density invariant `project.rs` asserts and the graph relies on).
1856        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    /// The production exclude set that discovery uses in production mode.
2005    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    /// `build_production_excludes` drops a pattern that does not compile, so a
2023    /// broken pattern would silently stop excluding its files.
2024    #[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        /// Create a minimal ResolvedConfig pointing at the given root directory.
2193        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        /// Helper to collect discovered file names (relative to root) for assertions.
2202        /// Normalizes path separators to `/` for cross-platform test consistency.
2203        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        /// Yarn PnP writes `.pnp.cjs` and `.pnp.loader.mjs` at the workspace
2274        /// root. They match the source extension filter but are generated
2275        /// install state, not code to analyze.
2276        #[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            // A script naming `.a/.b/deep.mjs` says where the file it needs
2517            // lives. Before issue #461 the scope stored the bare names, so an
2518            // unrelated `elsewhere/.b` and `unrelated/.a` were pulled in too.
2519            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            // Framework plugins declare `.client` / `.server` conventions that
2571            // may sit under any route directory, so the plugin shape must keep
2572            // matching at any depth.
2573            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        /// Create a config with custom ignore patterns.
2749        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        /// A monorepo keeps its generated output inside each package, so the
2898        /// built-in exclusion has to survive the workspace prefix.
2899        #[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        /// `build` only counts as output when it is a whole path segment.
2923        #[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        /// Resolve a config then override the per-file size limit in bytes.
2950        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        /// The skipped-source-dotdir entries the last walk on `root` recorded.
3051        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        /// Mark `root` as a git worktree so the `ignore` crate applies the
3220        /// gitignore files below it. `require_git` is on by default, and it
3221        /// tests for the presence of `.git`, not for a valid object store.
3222        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            // The directory FORM (`.tooling/`) prunes the dotdir upstream of the
3229            // predicate, so these are the forms that reach it with every file
3230            // inside already ignored.
3231            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            // Sorted before the real candidate, and more of them than the
3347            // ceiling, so a cap applied before the name checks would hide it.
3348            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            // Wide and shallow, no source: exhausts this candidate's own budget.
3369            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}