Skip to main content

fallow_core/
lib.rs

1//! fallow-core is the internal implementation crate behind the `fallow`
2//! analyzer. External embedders should consume the curated programmatic
3//! surface at `fallow_cli::programmatic` (e.g. `detect_dead_code`,
4//! `detect_boundary_violations`, `detect_duplication`, `compute_complexity`,
5//! `compute_health`); each returns a `serde_json::Value` matching the CLI's
6//! `--format json` shape plus structured `ProgrammaticError` with the CLI's
7//! exit-code ladder. See `decisions/008-fallow-core-internal-policy.md` for
8//! the policy, and `docs/fallow-core-migration.md` for the function-by-function
9//! migration map. Items in this crate may change in any release, including
10//! patch releases; a subsequent minor will flip `publish = false` so the crate
11//! is no longer fetchable from crates.io.
12
13pub mod analyze;
14pub mod cache;
15pub mod changed_files;
16pub mod churn;
17pub mod cross_reference;
18pub mod discover;
19pub mod duplicates;
20pub(crate) mod errors;
21mod external_style_usage;
22pub mod extract;
23pub mod git_env;
24mod package_assets;
25pub mod plugins;
26pub(crate) mod progress;
27pub mod results;
28pub(crate) mod scripts;
29pub mod suppress;
30pub mod trace;
31
32// Re-export from fallow-graph for backwards compatibility
33pub use fallow_graph::graph;
34pub use fallow_graph::project;
35pub use fallow_graph::resolve;
36
37use std::path::{Path, PathBuf};
38use std::time::Instant;
39
40use errors::FallowError;
41use fallow_config::{
42    EntryPointRole, PackageJson, ResolvedConfig, discover_workspaces,
43    find_undeclared_workspaces_with_ignores,
44};
45use rayon::prelude::*;
46use results::AnalysisResults;
47use rustc_hash::FxHashSet;
48use trace::PipelineTimings;
49
50const UNDECLARED_WORKSPACE_WARNING_PREVIEW: usize = 5;
51type LoadedWorkspacePackage<'a> = (&'a fallow_config::WorkspaceInfo, PackageJson);
52
53fn record_graph_package_usage(
54    graph: &mut graph::ModuleGraph,
55    package_name: &str,
56    file_id: discover::FileId,
57    is_type_only: bool,
58) {
59    graph
60        .package_usage
61        .entry(package_name.to_owned())
62        .or_default()
63        .push(file_id);
64    if is_type_only {
65        graph
66            .type_only_package_usage
67            .entry(package_name.to_owned())
68            .or_default()
69            .push(file_id);
70    }
71}
72
73fn workspace_package_name<'a>(
74    source: &str,
75    workspace_names: &'a FxHashSet<&str>,
76) -> Option<&'a str> {
77    if !resolve::is_bare_specifier(source) {
78        return None;
79    }
80    let package_name = resolve::extract_package_name(source);
81    workspace_names.get(package_name.as_str()).copied()
82}
83
84fn credit_workspace_package_usage(
85    graph: &mut graph::ModuleGraph,
86    resolved: &[resolve::ResolvedModule],
87    workspaces: &[fallow_config::WorkspaceInfo],
88) {
89    if workspaces.is_empty() {
90        return;
91    }
92
93    let workspace_names: FxHashSet<&str> = workspaces.iter().map(|ws| ws.name.as_str()).collect();
94    for module in resolved {
95        for import in module.all_resolved_imports() {
96            if matches!(import.target, resolve::ResolveResult::InternalModule(_))
97                && let Some(package_name) =
98                    workspace_package_name(&import.info.source, &workspace_names)
99            {
100                record_graph_package_usage(
101                    graph,
102                    package_name,
103                    module.file_id,
104                    import.info.is_type_only,
105                );
106            }
107        }
108
109        for re_export in &module.re_exports {
110            if matches!(re_export.target, resolve::ResolveResult::InternalModule(_))
111                && let Some(package_name) =
112                    workspace_package_name(&re_export.info.source, &workspace_names)
113            {
114                record_graph_package_usage(
115                    graph,
116                    package_name,
117                    module.file_id,
118                    re_export.info.is_type_only,
119                );
120            }
121        }
122    }
123}
124
125/// Result of the full analysis pipeline, including optional performance timings.
126pub struct AnalysisOutput {
127    pub results: AnalysisResults,
128    pub timings: Option<PipelineTimings>,
129    pub graph: Option<graph::ModuleGraph>,
130    /// Parsed modules from the pipeline, available when `retain_modules` is true.
131    /// Used by the combined command to share a single parse across dead-code and health.
132    pub modules: Option<Vec<extract::ModuleInfo>>,
133    /// Discovered files from the pipeline, available when `retain_modules` is true.
134    pub files: Option<Vec<discover::DiscoveredFile>>,
135    /// Package names invoked from package.json scripts and CI configs, mirroring
136    /// what the unused-deps detector consults. Populated for every pipeline run;
137    /// trace tooling reads it so `trace_dependency` agrees with `unused-deps` on
138    /// "used vs unused" instead of returning false-negatives for script-only deps.
139    pub script_used_packages: rustc_hash::FxHashSet<String>,
140    /// xxh3 content hash of every parsed source file, keyed by absolute path.
141    /// Used by `fallow fix` to detect on-disk drift between the in-process
142    /// analysis read and the per-file write; if the file's current hash
143    /// differs from the captured value, the fix for that file is skipped
144    /// with a clear diagnostic and exit 2. The hash is the same value
145    /// extract/cache uses for cache invalidation, so a cached parse contributes
146    /// the same hash as a fresh parse. Roughly 8 bytes per file (negligible
147    /// memory cost even on 100k-file projects).
148    pub file_hashes: rustc_hash::FxHashMap<std::path::PathBuf, u64>,
149}
150
151/// Update cache: write freshly parsed modules and refresh stale mtime/size entries.
152fn update_cache(
153    store: &mut cache::CacheStore,
154    modules: &[extract::ModuleInfo],
155    files: &[discover::DiscoveredFile],
156) {
157    for module in modules {
158        if let Some(file) = files.get(module.file_id.0 as usize) {
159            let (mt, sz) = file_mtime_and_size(&file.path);
160            // If content hash matches, just refresh mtime/size if stale
161            // (e.g. `touch`ed file). Critically, preserve the existing
162            // `last_access_secs` instead of rebuilding the entry via
163            // `module_to_cached` (which would stamp the current epoch
164            // second and defeat the LRU). A metadata-only refresh is NOT
165            // a content change, so the entry's recency should not bump.
166            if let Some(cached) = store.get_by_path_only(&file.path)
167                && cached.content_hash == module.content_hash
168            {
169                if cached.mtime_secs != mt || cached.file_size != sz {
170                    let preserved_last_access = cached.last_access_secs;
171                    let mut refreshed = cache::module_to_cached(module, mt, sz);
172                    refreshed.last_access_secs = preserved_last_access;
173                    store.insert(&file.path, refreshed);
174                }
175                continue;
176            }
177            store.insert(&file.path, cache::module_to_cached(module, mt, sz));
178        }
179    }
180    store.retain_paths(files);
181}
182
183/// Resolve `config.cache_max_size_mb` into bytes, falling back to the
184/// extract crate's `DEFAULT_CACHE_MAX_SIZE`. Lives at this layer (not on
185/// `ResolvedConfig`) because `fallow-config` does not depend on
186/// `fallow-extract`; the bytes conversion is owned by the cache callsite.
187/// Public so CLI subcommands that load the cache directly (`flags`,
188/// `health`, `coverage analyze`) can call it without re-deriving the
189/// same fallback policy.
190#[must_use]
191pub fn resolve_cache_max_size_bytes(config: &ResolvedConfig) -> usize {
192    config
193        .cache_max_size_mb
194        .map_or(cache::DEFAULT_CACHE_MAX_SIZE, |mb| {
195            (mb as usize).saturating_mul(1024 * 1024)
196        })
197}
198
199/// Extract mtime (seconds since epoch) and file size from a path.
200fn file_mtime_and_size(path: &std::path::Path) -> (u64, u64) {
201    std::fs::metadata(path).map_or((0, 0), |m| {
202        let mt = m
203            .modified()
204            .ok()
205            .and_then(|t| t.duration_since(std::time::SystemTime::UNIX_EPOCH).ok())
206            .map_or(0, |d| d.as_secs());
207        (mt, m.len())
208    })
209}
210
211fn format_undeclared_workspace_warning(
212    root: &Path,
213    undeclared: &[fallow_config::WorkspaceDiagnostic],
214) -> Option<String> {
215    if undeclared.is_empty() {
216        return None;
217    }
218
219    let preview = undeclared
220        .iter()
221        .take(UNDECLARED_WORKSPACE_WARNING_PREVIEW)
222        .map(|diag| {
223            diag.path
224                .strip_prefix(root)
225                .unwrap_or(&diag.path)
226                .display()
227                .to_string()
228                .replace('\\', "/")
229        })
230        .collect::<Vec<_>>();
231    let remaining = undeclared
232        .len()
233        .saturating_sub(UNDECLARED_WORKSPACE_WARNING_PREVIEW);
234    let tail = if remaining > 0 {
235        format!(" (and {remaining} more)")
236    } else {
237        String::new()
238    };
239    let noun = if undeclared.len() == 1 {
240        "directory with package.json is"
241    } else {
242        "directories with package.json are"
243    };
244    let guidance = if undeclared.len() == 1 {
245        "Add that path to package.json workspaces or pnpm-workspace.yaml if it should be analyzed as a workspace."
246    } else {
247        "Add those paths to package.json workspaces or pnpm-workspace.yaml if they should be analyzed as workspaces."
248    };
249
250    Some(format!(
251        "{} {} not declared as {}: {}{}. {}",
252        undeclared.len(),
253        noun,
254        if undeclared.len() == 1 {
255            "a workspace"
256        } else {
257            "workspaces"
258        },
259        preview.join(", "),
260        tail,
261        guidance
262    ))
263}
264
265fn warn_undeclared_workspaces(
266    root: &Path,
267    workspaces_vec: &[fallow_config::WorkspaceInfo],
268    ignore_patterns: &globset::GlobSet,
269    quiet: bool,
270) {
271    let undeclared = find_undeclared_workspaces_with_ignores(root, workspaces_vec, ignore_patterns);
272    if undeclared.is_empty() {
273        return;
274    }
275
276    // Filter out paths that ALREADY carry a config-load-time diagnostic
277    // (typically `MalformedPackageJson` from issue #473). A directory whose
278    // package.json failed to parse appears "undeclared" from the analyze
279    // pipeline's perspective because `discover_workspaces` silently dropped
280    // it, but the user IS declaring it; the malformed-package-json warning
281    // already names the path and explains the fix, so re-flagging it as
282    // "undeclared" actively misleads.
283    let existing = fallow_config::workspace_diagnostics_for(root);
284    let already_flagged: rustc_hash::FxHashSet<PathBuf> = existing
285        .iter()
286        .map(|d| dunce::canonicalize(&d.path).unwrap_or_else(|_| d.path.clone()))
287        .collect();
288    let undeclared: Vec<_> = undeclared
289        .into_iter()
290        .filter(|diag| {
291            let canonical = dunce::canonicalize(&diag.path).unwrap_or_else(|_| diag.path.clone());
292            !already_flagged.contains(&canonical)
293        })
294        .collect();
295    if undeclared.is_empty() {
296        return;
297    }
298
299    // Fold the surviving undeclared diagnostics into the shared registry so
300    // they appear in `workspace_diagnostics[]` on the JSON envelope and in
301    // `fallow list --workspaces`. Quiet mode still populates the registry
302    // (JSON consumers need the data) but skips the human warning.
303    fallow_config::append_workspace_diagnostics(root, undeclared.clone());
304
305    if !quiet && let Some(message) = format_undeclared_workspace_warning(root, &undeclared) {
306        tracing::warn!("{message}");
307    }
308}
309
310/// Run the full analysis pipeline.
311///
312/// # Errors
313///
314/// Returns an error if file discovery, parsing, or analysis fails.
315#[deprecated(
316    since = "2.76.0",
317    note = "fallow_core is internal; use fallow_cli::programmatic::detect_dead_code instead. NOTE: replacement returns serde_json::Value, not typed AnalysisResults. See docs/fallow-core-migration.md and ADR-008."
318)]
319pub fn analyze(config: &ResolvedConfig) -> Result<AnalysisResults, FallowError> {
320    let output = analyze_full(config, false, false, false, false)?;
321    Ok(output.results)
322}
323
324/// Run the full analysis pipeline with export usage collection (for LSP Code Lens).
325///
326/// # Errors
327///
328/// Returns an error if file discovery, parsing, or analysis fails.
329#[deprecated(
330    since = "2.76.0",
331    note = "fallow_core is internal; use fallow_cli::programmatic::detect_dead_code instead. NOTE: export-usage collection is not exposed in the programmatic surface today. See docs/fallow-core-migration.md and ADR-008."
332)]
333pub fn analyze_with_usages(config: &ResolvedConfig) -> Result<AnalysisResults, FallowError> {
334    let output = analyze_full(config, false, true, false, false)?;
335    Ok(output.results)
336}
337
338/// Run the full analysis pipeline with optional performance timings and graph retention.
339///
340/// # Errors
341///
342/// Returns an error if file discovery, parsing, or analysis fails.
343#[deprecated(
344    since = "2.76.0",
345    note = "fallow_core is internal; use fallow_cli::programmatic::detect_dead_code instead. NOTE: trace timings are not exposed in the programmatic surface today; use `fallow check --performance` for CLI-side timings. See docs/fallow-core-migration.md and ADR-008."
346)]
347pub fn analyze_with_trace(config: &ResolvedConfig) -> Result<AnalysisOutput, FallowError> {
348    analyze_full(config, true, false, false, false)
349}
350
351/// Run the full analysis pipeline and return the full `AnalysisOutput`, including
352/// `file_hashes` (used by `fallow fix` to detect on-disk drift between analysis
353/// and per-file write). Graphs and modules are NOT retained; the only difference
354/// from `analyze` is that the caller can access `AnalysisOutput.file_hashes`.
355///
356/// # Errors
357///
358/// Returns an error if file discovery, parsing, or analysis fails.
359#[deprecated(
360    since = "2.76.0",
361    note = "fallow_core is internal; the CLI fix command uses this via the workspace path dependency. External embedders should use fallow_cli::programmatic::detect_dead_code. See docs/fallow-core-migration.md and ADR-008."
362)]
363pub fn analyze_with_file_hashes(config: &ResolvedConfig) -> Result<AnalysisOutput, FallowError> {
364    analyze_full(config, false, false, false, false)
365}
366
367/// Run the full analysis pipeline, retaining parsed modules and discovered files.
368///
369/// Used by the combined command to share a single parse across dead-code and health.
370/// When `need_complexity` is true, the `ComplexityVisitor` runs during parsing so
371/// the returned modules contain per-function complexity data.
372///
373/// # Errors
374///
375/// Returns an error if file discovery, parsing, or analysis fails.
376#[deprecated(
377    since = "2.76.0",
378    note = "fallow_core is internal; use fallow_cli::programmatic::detect_dead_code instead. NOTE: combined-mode module retention is not exposed in the programmatic surface today. See docs/fallow-core-migration.md and ADR-008."
379)]
380pub fn analyze_retaining_modules(
381    config: &ResolvedConfig,
382    need_complexity: bool,
383    retain_graph: bool,
384) -> Result<AnalysisOutput, FallowError> {
385    analyze_full(config, retain_graph, false, need_complexity, true)
386}
387
388/// Run the analysis pipeline using pre-parsed modules, skipping the parsing stage.
389///
390/// This avoids re-parsing files when the caller already has a `ParseResult` (e.g., from
391/// `fallow_core::extract::parse_all_files`). Discovery, plugins, scripts, entry points,
392/// import resolution, graph construction, and dead code detection still run normally.
393/// The graph is always retained (needed for file scores).
394///
395/// # Errors
396///
397/// Returns an error if discovery, graph construction, or analysis fails.
398#[allow(
399    clippy::too_many_lines,
400    reason = "pipeline orchestration stays easier to audit in one place"
401)]
402#[deprecated(
403    since = "2.76.0",
404    note = "fallow_core is internal; use fallow_cli::programmatic::detect_dead_code instead. NOTE: pre-parsed module reuse is not exposed in the programmatic surface today. See docs/fallow-core-migration.md and ADR-008."
405)]
406pub fn analyze_with_parse_result(
407    config: &ResolvedConfig,
408    modules: &[extract::ModuleInfo],
409) -> Result<AnalysisOutput, FallowError> {
410    let _span = tracing::info_span!("fallow_analyze_with_parse_result").entered();
411    let pipeline_start = Instant::now();
412
413    let show_progress = !config.quiet
414        && std::io::IsTerminal::is_terminal(&std::io::stderr())
415        && matches!(
416            config.output,
417            fallow_config::OutputFormat::Human
418                | fallow_config::OutputFormat::Compact
419                | fallow_config::OutputFormat::Markdown
420        );
421    let progress = progress::AnalysisProgress::new(show_progress);
422
423    if !config.root.join("node_modules").is_dir() {
424        tracing::warn!(
425            "node_modules directory not found. Run `npm install` / `pnpm install` first for accurate results."
426        );
427    }
428
429    // Discover workspaces
430    let t = Instant::now();
431    let workspaces_vec = discover_workspaces(&config.root);
432    let workspaces_ms = t.elapsed().as_secs_f64() * 1000.0;
433    if !workspaces_vec.is_empty() {
434        tracing::info!(count = workspaces_vec.len(), "workspaces discovered");
435    }
436
437    // Warn about directories with package.json not declared as workspaces
438    warn_undeclared_workspaces(
439        &config.root,
440        &workspaces_vec,
441        &config.ignore_patterns,
442        config.quiet,
443    );
444    let root_pkg = load_root_package_json(config);
445    let discovery_hidden_dir_scopes =
446        discover::collect_hidden_dir_scopes(config, root_pkg.as_ref(), &workspaces_vec);
447
448    // Stage 1: Discover files (cheap — needed for file registry and resolution)
449    let t = Instant::now();
450    progress.set_stage("discovering files...");
451    let discovered_files =
452        discover::discover_files_with_additional_hidden_dirs(config, &discovery_hidden_dir_scopes);
453    let discover_ms = t.elapsed().as_secs_f64() * 1000.0;
454
455    let project = project::ProjectState::new(discovered_files, workspaces_vec);
456    let files = project.files();
457    let workspaces = project.workspaces();
458    let workspace_pkgs = load_workspace_packages(workspaces);
459
460    // Stage 1.5: Run plugin system
461    let t = Instant::now();
462    progress.set_stage("detecting plugins...");
463    let mut plugin_result = run_plugins(
464        config,
465        files,
466        workspaces,
467        root_pkg.as_ref(),
468        &workspace_pkgs,
469    );
470    let plugins_ms = t.elapsed().as_secs_f64() * 1000.0;
471
472    // Stage 1.6: Analyze package.json scripts
473    let t = Instant::now();
474    analyze_all_scripts(
475        config,
476        workspaces,
477        root_pkg.as_ref(),
478        &workspace_pkgs,
479        &mut plugin_result,
480    );
481    let scripts_ms = t.elapsed().as_secs_f64() * 1000.0;
482
483    // Stage 2: SKIPPED — using pre-parsed modules from caller
484
485    // Stage 3: Discover entry points
486    let t = Instant::now();
487    let entry_points = discover_all_entry_points(
488        config,
489        files,
490        workspaces,
491        root_pkg.as_ref(),
492        &workspace_pkgs,
493        &plugin_result,
494    );
495    let entry_points_ms = t.elapsed().as_secs_f64() * 1000.0;
496
497    // Compute entry-point summary before the graph consumes the entry_points vec
498    let ep_summary = summarize_entry_points(&entry_points.all);
499
500    // Stage 4: Resolve imports to file IDs
501    let t = Instant::now();
502    progress.set_stage("resolving imports...");
503    let mut resolved = resolve::resolve_all_imports(
504        modules,
505        files,
506        workspaces,
507        &plugin_result.active_plugins,
508        &plugin_result.path_aliases,
509        &plugin_result.auto_imports,
510        &plugin_result.scss_include_paths,
511        &plugin_result.static_dir_mappings,
512        &config.root,
513        &config.resolve.conditions,
514    );
515    external_style_usage::augment_external_style_package_usage(
516        &mut resolved,
517        config,
518        workspaces,
519        &plugin_result,
520    );
521    let resolve_ms = t.elapsed().as_secs_f64() * 1000.0;
522
523    // Stage 5: Build module graph
524    let t = Instant::now();
525    progress.set_stage("building module graph...");
526    let mut graph = graph::ModuleGraph::build_with_reachability_roots(
527        &resolved,
528        &entry_points.all,
529        &entry_points.runtime,
530        &entry_points.test,
531        files,
532    );
533    credit_workspace_package_usage(&mut graph, &resolved, workspaces);
534    let graph_ms = t.elapsed().as_secs_f64() * 1000.0;
535
536    // Stage 6: Analyze for dead code
537    let t = Instant::now();
538    progress.set_stage("analyzing...");
539    #[expect(
540        deprecated,
541        reason = "ADR-008 keeps workspace path-dependency calls while warning external fallow-core consumers"
542    )]
543    let mut result = analyze::find_dead_code_full(
544        &graph,
545        config,
546        &resolved,
547        Some(&plugin_result),
548        workspaces,
549        modules,
550        false,
551    );
552    let analyze_ms = t.elapsed().as_secs_f64() * 1000.0;
553    progress.finish();
554
555    result.entry_point_summary = Some(ep_summary);
556
557    let total_ms = pipeline_start.elapsed().as_secs_f64() * 1000.0;
558
559    tracing::debug!(
560        "\n┌─ Pipeline Profile (reuse) ─────────────────────\n\
561         │  discover files:   {:>8.1}ms  ({} files)\n\
562         │  workspaces:       {:>8.1}ms\n\
563         │  plugins:          {:>8.1}ms\n\
564         │  script analysis:  {:>8.1}ms\n\
565         │  parse/extract:    SKIPPED (reused {} modules)\n\
566         │  entry points:     {:>8.1}ms  ({} entries)\n\
567         │  resolve imports:  {:>8.1}ms\n\
568         │  build graph:      {:>8.1}ms\n\
569         │  analyze:          {:>8.1}ms\n\
570         │  ────────────────────────────────────────────\n\
571         │  TOTAL:            {:>8.1}ms\n\
572         └─────────────────────────────────────────────────",
573        discover_ms,
574        files.len(),
575        workspaces_ms,
576        plugins_ms,
577        scripts_ms,
578        modules.len(),
579        entry_points_ms,
580        entry_points.all.len(),
581        resolve_ms,
582        graph_ms,
583        analyze_ms,
584        total_ms,
585    );
586
587    let timings = Some(PipelineTimings {
588        discover_files_ms: discover_ms,
589        file_count: files.len(),
590        workspaces_ms,
591        workspace_count: workspaces.len(),
592        plugins_ms,
593        script_analysis_ms: scripts_ms,
594        parse_extract_ms: 0.0, // Skipped: modules were reused
595        parse_cpu_ms: 0.0,     // Skipped: modules were reused
596        module_count: modules.len(),
597        cache_hits: 0,
598        cache_misses: 0,
599        cache_update_ms: 0.0,
600        entry_points_ms,
601        entry_point_count: entry_points.all.len(),
602        resolve_imports_ms: resolve_ms,
603        build_graph_ms: graph_ms,
604        analyze_ms,
605        duplication_ms: None,
606        total_ms,
607    });
608
609    let file_hashes: rustc_hash::FxHashMap<std::path::PathBuf, u64> = modules
610        .iter()
611        .filter_map(|module| {
612            files
613                .get(module.file_id.0 as usize)
614                .map(|file| (file.path.clone(), module.content_hash))
615        })
616        .collect();
617
618    Ok(AnalysisOutput {
619        results: result,
620        timings,
621        graph: Some(graph),
622        modules: None,
623        files: None,
624        script_used_packages: plugin_result.script_used_packages.clone(),
625        file_hashes,
626    })
627}
628
629#[expect(
630    clippy::unnecessary_wraps,
631    reason = "Result kept for future error handling"
632)]
633#[expect(
634    clippy::too_many_lines,
635    reason = "main pipeline function; sequential phases are held together for clarity"
636)]
637fn analyze_full(
638    config: &ResolvedConfig,
639    retain: bool,
640    collect_usages: bool,
641    need_complexity: bool,
642    retain_modules: bool,
643) -> Result<AnalysisOutput, FallowError> {
644    let _span = tracing::info_span!("fallow_analyze").entered();
645    let pipeline_start = Instant::now();
646
647    // Progress bars: enabled when not quiet, stderr is a terminal, and output is human-readable.
648    // Structured formats (JSON, SARIF) suppress spinners even on TTY — users piping structured
649    // output don't expect progress noise on stderr.
650    let show_progress = !config.quiet
651        && std::io::IsTerminal::is_terminal(&std::io::stderr())
652        && matches!(
653            config.output,
654            fallow_config::OutputFormat::Human
655                | fallow_config::OutputFormat::Compact
656                | fallow_config::OutputFormat::Markdown
657        );
658    let progress = progress::AnalysisProgress::new(show_progress);
659
660    // Warn if node_modules is missing — resolution will be severely degraded
661    if !config.root.join("node_modules").is_dir() {
662        tracing::warn!(
663            "node_modules directory not found. Run `npm install` / `pnpm install` first for accurate results."
664        );
665    }
666
667    // Discover workspaces if in a monorepo
668    let t = Instant::now();
669    let workspaces_vec = discover_workspaces(&config.root);
670    let workspaces_ms = t.elapsed().as_secs_f64() * 1000.0;
671    if !workspaces_vec.is_empty() {
672        tracing::info!(count = workspaces_vec.len(), "workspaces discovered");
673    }
674
675    // Warn about directories with package.json not declared as workspaces
676    warn_undeclared_workspaces(
677        &config.root,
678        &workspaces_vec,
679        &config.ignore_patterns,
680        config.quiet,
681    );
682    let root_pkg = load_root_package_json(config);
683    let discovery_hidden_dir_scopes =
684        discover::collect_hidden_dir_scopes(config, root_pkg.as_ref(), &workspaces_vec);
685
686    // Stage 1: Discover all source files
687    let t = Instant::now();
688    progress.set_stage("discovering files...");
689    let discovered_files =
690        discover::discover_files_with_additional_hidden_dirs(config, &discovery_hidden_dir_scopes);
691    let discover_ms = t.elapsed().as_secs_f64() * 1000.0;
692
693    // Build ProjectState: owns the file registry with stable FileIds and workspace metadata.
694    // This is the foundation for cross-workspace resolution and future incremental analysis.
695    let project = project::ProjectState::new(discovered_files, workspaces_vec);
696    let files = project.files();
697    let workspaces = project.workspaces();
698    let workspace_pkgs = load_workspace_packages(workspaces);
699
700    // Stage 1.5: Run plugin system — parse config files, discover dynamic entries
701    let t = Instant::now();
702    progress.set_stage("detecting plugins...");
703    let mut plugin_result = run_plugins(
704        config,
705        files,
706        workspaces,
707        root_pkg.as_ref(),
708        &workspace_pkgs,
709    );
710    let plugins_ms = t.elapsed().as_secs_f64() * 1000.0;
711
712    // Stage 1.6: Analyze package.json scripts for binary usage and config file refs
713    let t = Instant::now();
714    analyze_all_scripts(
715        config,
716        workspaces,
717        root_pkg.as_ref(),
718        &workspace_pkgs,
719        &mut plugin_result,
720    );
721    let scripts_ms = t.elapsed().as_secs_f64() * 1000.0;
722
723    // Stage 2: Parse all files in parallel and extract imports/exports
724    let t = Instant::now();
725    progress.set_stage(&format!("parsing {} files...", files.len()));
726    let cache_max_size_bytes = resolve_cache_max_size_bytes(config);
727    let mut cache_store = if config.no_cache {
728        None
729    } else {
730        cache::CacheStore::load(
731            &config.cache_dir,
732            config.cache_config_hash,
733            cache_max_size_bytes,
734        )
735    };
736
737    let parse_result = extract::parse_all_files(files, cache_store.as_ref(), need_complexity);
738    let modules = parse_result.modules;
739    let cache_hits = parse_result.cache_hits;
740    let cache_misses = parse_result.cache_misses;
741    let parse_cpu_ms = parse_result.parse_cpu_ms;
742    let parse_ms = t.elapsed().as_secs_f64() * 1000.0;
743
744    // Update cache with freshly parsed modules and refresh stale mtime/size entries.
745    let t = Instant::now();
746    if !config.no_cache {
747        let store = cache_store.get_or_insert_with(cache::CacheStore::new);
748        update_cache(store, &modules, files);
749        if let Err(e) = store.save(
750            &config.cache_dir,
751            config.cache_config_hash,
752            cache_max_size_bytes,
753        ) {
754            tracing::warn!("Failed to save cache: {e}");
755        }
756    }
757    let cache_ms = t.elapsed().as_secs_f64() * 1000.0;
758
759    // Stage 3: Discover entry points (static patterns + plugin-discovered patterns)
760    let t = Instant::now();
761    let entry_points = discover_all_entry_points(
762        config,
763        files,
764        workspaces,
765        root_pkg.as_ref(),
766        &workspace_pkgs,
767        &plugin_result,
768    );
769    let entry_points_ms = t.elapsed().as_secs_f64() * 1000.0;
770
771    // Stage 4: Resolve imports to file IDs
772    let t = Instant::now();
773    progress.set_stage("resolving imports...");
774    let mut resolved = resolve::resolve_all_imports(
775        &modules,
776        files,
777        workspaces,
778        &plugin_result.active_plugins,
779        &plugin_result.path_aliases,
780        &plugin_result.auto_imports,
781        &plugin_result.scss_include_paths,
782        &plugin_result.static_dir_mappings,
783        &config.root,
784        &config.resolve.conditions,
785    );
786    external_style_usage::augment_external_style_package_usage(
787        &mut resolved,
788        config,
789        workspaces,
790        &plugin_result,
791    );
792    let resolve_ms = t.elapsed().as_secs_f64() * 1000.0;
793
794    // Stage 5: Build module graph
795    let t = Instant::now();
796    progress.set_stage("building module graph...");
797    let mut graph = graph::ModuleGraph::build_with_reachability_roots(
798        &resolved,
799        &entry_points.all,
800        &entry_points.runtime,
801        &entry_points.test,
802        files,
803    );
804    credit_workspace_package_usage(&mut graph, &resolved, workspaces);
805    let graph_ms = t.elapsed().as_secs_f64() * 1000.0;
806
807    // Compute entry-point summary before the graph consumes the entry_points vec
808    let ep_summary = summarize_entry_points(&entry_points.all);
809
810    // Stage 6: Analyze for dead code (with plugin context and workspace info)
811    let t = Instant::now();
812    progress.set_stage("analyzing...");
813    #[expect(
814        deprecated,
815        reason = "ADR-008 keeps workspace path-dependency calls while warning external fallow-core consumers"
816    )]
817    let mut result = analyze::find_dead_code_full(
818        &graph,
819        config,
820        &resolved,
821        Some(&plugin_result),
822        workspaces,
823        &modules,
824        collect_usages,
825    );
826    let analyze_ms = t.elapsed().as_secs_f64() * 1000.0;
827    progress.finish();
828
829    result.entry_point_summary = Some(ep_summary);
830
831    let total_ms = pipeline_start.elapsed().as_secs_f64() * 1000.0;
832
833    let cache_summary = if cache_hits > 0 {
834        format!(" ({cache_hits} cached, {cache_misses} parsed)")
835    } else {
836        String::new()
837    };
838
839    tracing::debug!(
840        "\n┌─ Pipeline Profile ─────────────────────────────\n\
841         │  discover files:   {:>8.1}ms  ({} files)\n\
842         │  workspaces:       {:>8.1}ms\n\
843         │  plugins:          {:>8.1}ms\n\
844         │  script analysis:  {:>8.1}ms\n\
845         │  parse/extract:    {:>8.1}ms  ({} modules{})\n\
846         │  cache update:     {:>8.1}ms\n\
847         │  entry points:     {:>8.1}ms  ({} entries)\n\
848         │  resolve imports:  {:>8.1}ms\n\
849         │  build graph:      {:>8.1}ms\n\
850         │  analyze:          {:>8.1}ms\n\
851         │  ────────────────────────────────────────────\n\
852         │  TOTAL:            {:>8.1}ms\n\
853         └─────────────────────────────────────────────────",
854        discover_ms,
855        files.len(),
856        workspaces_ms,
857        plugins_ms,
858        scripts_ms,
859        parse_ms,
860        modules.len(),
861        cache_summary,
862        cache_ms,
863        entry_points_ms,
864        entry_points.all.len(),
865        resolve_ms,
866        graph_ms,
867        analyze_ms,
868        total_ms,
869    );
870
871    let timings = if retain {
872        Some(PipelineTimings {
873            discover_files_ms: discover_ms,
874            file_count: files.len(),
875            workspaces_ms,
876            workspace_count: workspaces.len(),
877            plugins_ms,
878            script_analysis_ms: scripts_ms,
879            parse_extract_ms: parse_ms,
880            parse_cpu_ms,
881            module_count: modules.len(),
882            cache_hits,
883            cache_misses,
884            cache_update_ms: cache_ms,
885            entry_points_ms,
886            entry_point_count: entry_points.all.len(),
887            resolve_imports_ms: resolve_ms,
888            build_graph_ms: graph_ms,
889            analyze_ms,
890            duplication_ms: None,
891            total_ms,
892        })
893    } else {
894        None
895    };
896
897    let file_hashes: rustc_hash::FxHashMap<std::path::PathBuf, u64> = modules
898        .iter()
899        .filter_map(|module| {
900            files
901                .get(module.file_id.0 as usize)
902                .map(|file| (file.path.clone(), module.content_hash))
903        })
904        .collect();
905
906    Ok(AnalysisOutput {
907        results: result,
908        timings,
909        graph: if retain { Some(graph) } else { None },
910        modules: if retain_modules { Some(modules) } else { None },
911        files: if retain_modules {
912            Some(files.to_vec())
913        } else {
914            None
915        },
916        script_used_packages: plugin_result.script_used_packages,
917        file_hashes,
918    })
919}
920
921/// Analyze package.json scripts from root and all workspace packages.
922///
923/// Populates the plugin result with script-used packages and config file
924/// entry patterns. Also scans CI config files for binary invocations.
925fn load_root_package_json(config: &ResolvedConfig) -> Option<PackageJson> {
926    PackageJson::load(&config.root.join("package.json")).ok()
927}
928
929fn load_workspace_packages(
930    workspaces: &[fallow_config::WorkspaceInfo],
931) -> Vec<LoadedWorkspacePackage<'_>> {
932    workspaces
933        .iter()
934        .filter_map(|ws| {
935            PackageJson::load(&ws.root.join("package.json"))
936                .ok()
937                .map(|pkg| (ws, pkg))
938        })
939        .collect()
940}
941
942fn analyze_all_scripts(
943    config: &ResolvedConfig,
944    workspaces: &[fallow_config::WorkspaceInfo],
945    root_pkg: Option<&PackageJson>,
946    workspace_pkgs: &[LoadedWorkspacePackage<'_>],
947    plugin_result: &mut plugins::AggregatedPluginResult,
948) {
949    // Collect all dependency names to build the bin-name → package-name reverse map.
950    // This resolves binaries like "attw" to "@arethetypeswrong/cli" even without
951    // node_modules/.bin symlinks.
952    let mut all_dep_names: Vec<String> = Vec::new();
953    if let Some(pkg) = root_pkg {
954        all_dep_names.extend(pkg.all_dependency_names());
955    }
956    for (_, ws_pkg) in workspace_pkgs {
957        all_dep_names.extend(ws_pkg.all_dependency_names());
958    }
959    all_dep_names.sort_unstable();
960    all_dep_names.dedup();
961
962    // Probe node_modules/ at project root and each workspace root so non-hoisted
963    // deps (pnpm strict, Yarn workspaces) are also discovered.
964    let mut nm_roots: Vec<&std::path::Path> = Vec::new();
965    if config.root.join("node_modules").is_dir() {
966        nm_roots.push(&config.root);
967    }
968    for ws in workspaces {
969        if ws.root.join("node_modules").is_dir() {
970            nm_roots.push(&ws.root);
971        }
972    }
973    let bin_map = scripts::build_bin_to_package_map(&nm_roots, &all_dep_names);
974
975    if let Some(pkg) = root_pkg
976        && let Some(ref pkg_scripts) = pkg.scripts
977    {
978        let scripts_to_analyze = if config.production {
979            scripts::filter_production_scripts(pkg_scripts)
980        } else {
981            pkg_scripts.clone()
982        };
983        let script_analysis = scripts::analyze_scripts(&scripts_to_analyze, &config.root, &bin_map);
984        plugin_result.script_used_packages = script_analysis.used_packages;
985
986        for config_file in &script_analysis.config_files {
987            plugin_result
988                .discovered_always_used
989                .push((config_file.clone(), "scripts".to_string()));
990        }
991        for entry in &script_analysis.entry_files {
992            if let Some(pat) = scripts::normalize_script_entry_pattern("", entry) {
993                plugin_result
994                    .entry_patterns
995                    .push((plugins::PathRule::new(pat), "scripts".to_string()));
996            }
997        }
998    }
999    use rayon::prelude::*;
1000    type WsScriptOut = (
1001        Vec<String>,
1002        Vec<(String, String)>,
1003        Vec<(plugins::PathRule, String)>,
1004    );
1005    let ws_results: Vec<WsScriptOut> = workspace_pkgs
1006        .par_iter()
1007        .map(|(ws, ws_pkg)| {
1008            let mut used_packages = Vec::new();
1009            let mut discovered_always_used: Vec<(String, String)> = Vec::new();
1010            let mut entry_patterns: Vec<(plugins::PathRule, String)> = Vec::new();
1011            if let Some(ref ws_scripts) = ws_pkg.scripts {
1012                let scripts_to_analyze = if config.production {
1013                    scripts::filter_production_scripts(ws_scripts)
1014                } else {
1015                    ws_scripts.clone()
1016                };
1017                let ws_analysis = scripts::analyze_scripts(&scripts_to_analyze, &ws.root, &bin_map);
1018                used_packages.extend(ws_analysis.used_packages);
1019
1020                let ws_prefix = ws
1021                    .root
1022                    .strip_prefix(&config.root)
1023                    .unwrap_or(&ws.root)
1024                    .to_string_lossy();
1025                for config_file in &ws_analysis.config_files {
1026                    discovered_always_used
1027                        .push((format!("{ws_prefix}/{config_file}"), "scripts".to_string()));
1028                }
1029                for entry in &ws_analysis.entry_files {
1030                    if let Some(pat) = scripts::normalize_script_entry_pattern(&ws_prefix, entry) {
1031                        entry_patterns.push((plugins::PathRule::new(pat), "scripts".to_string()));
1032                    }
1033                }
1034            }
1035            (used_packages, discovered_always_used, entry_patterns)
1036        })
1037        .collect();
1038    for (used_packages, discovered_always_used, entry_patterns) in ws_results {
1039        plugin_result.script_used_packages.extend(used_packages);
1040        plugin_result
1041            .discovered_always_used
1042            .extend(discovered_always_used);
1043        plugin_result.entry_patterns.extend(entry_patterns);
1044    }
1045
1046    // Scan CI config files for binary invocations and positional file references.
1047    // Returns both packages used by CI tooling AND project-relative file paths
1048    // referenced as command-line arguments (e.g., `node scripts/deploy.ts` in a
1049    // GitHub Actions `run:` block) so the referenced files become reachable
1050    // entry points. CI files always live at the project root, so file paths
1051    // need no workspace-prefix transformation. See issue #195 (Case D).
1052    let ci_analysis = scripts::ci::analyze_ci_files(&config.root, &bin_map);
1053    plugin_result
1054        .script_used_packages
1055        .extend(ci_analysis.used_packages);
1056    for entry in &ci_analysis.entry_files {
1057        if let Some(pat) = scripts::normalize_script_entry_pattern("", entry) {
1058            plugin_result
1059                .entry_patterns
1060                .push((plugins::PathRule::new(pat), "scripts".to_string()));
1061        }
1062    }
1063    plugin_result
1064        .entry_point_roles
1065        .entry("scripts".to_string())
1066        .or_insert(EntryPointRole::Support);
1067}
1068
1069/// Discover all entry points from static patterns, workspaces, plugins, and infrastructure.
1070fn discover_all_entry_points(
1071    config: &ResolvedConfig,
1072    files: &[discover::DiscoveredFile],
1073    workspaces: &[fallow_config::WorkspaceInfo],
1074    root_pkg: Option<&PackageJson>,
1075    workspace_pkgs: &[LoadedWorkspacePackage<'_>],
1076    plugin_result: &plugins::AggregatedPluginResult,
1077) -> discover::CategorizedEntryPoints {
1078    let mut entry_points = discover::CategorizedEntryPoints::default();
1079    let root_discovery = discover::discover_entry_points_with_warnings_from_pkg(
1080        config,
1081        files,
1082        root_pkg,
1083        workspaces.is_empty(),
1084    );
1085
1086    let workspace_pkg_by_root: rustc_hash::FxHashMap<std::path::PathBuf, &PackageJson> =
1087        workspace_pkgs
1088            .iter()
1089            .map(|(ws, pkg)| (ws.root.clone(), pkg))
1090            .collect();
1091
1092    let workspace_discovery: Vec<discover::EntryPointDiscovery> = workspaces
1093        .par_iter()
1094        .map(|ws| {
1095            let pkg = workspace_pkg_by_root.get(&ws.root).copied();
1096            discover::discover_workspace_entry_points_with_warnings_from_pkg(&ws.root, files, pkg)
1097        })
1098        .collect();
1099    let mut skipped_entries = rustc_hash::FxHashMap::default();
1100    entry_points.extend_runtime(root_discovery.entries);
1101    for (path, count) in root_discovery.skipped_entries {
1102        *skipped_entries.entry(path).or_insert(0) += count;
1103    }
1104    let mut ws_entries = Vec::new();
1105    for workspace in workspace_discovery {
1106        ws_entries.extend(workspace.entries);
1107        for (path, count) in workspace.skipped_entries {
1108            *skipped_entries.entry(path).or_insert(0) += count;
1109        }
1110    }
1111    discover::warn_skipped_entry_summary(&skipped_entries);
1112    entry_points.extend_runtime(ws_entries);
1113
1114    let plugin_entries = discover::discover_plugin_entry_point_sets(plugin_result, config, files);
1115    entry_points.extend(plugin_entries);
1116
1117    let infra_entries = discover::discover_infrastructure_entry_points(&config.root);
1118    entry_points.extend_runtime(infra_entries);
1119
1120    // Add dynamically loaded files from config as entry points
1121    if !config.dynamically_loaded.is_empty() {
1122        let dynamic_entries = discover::discover_dynamically_loaded_entry_points(config, files);
1123        entry_points.extend_runtime(dynamic_entries);
1124    }
1125
1126    entry_points.dedup()
1127}
1128
1129/// Summarize entry points by source category for user-facing output.
1130fn summarize_entry_points(entry_points: &[discover::EntryPoint]) -> results::EntryPointSummary {
1131    let mut counts: rustc_hash::FxHashMap<String, usize> = rustc_hash::FxHashMap::default();
1132    for ep in entry_points {
1133        let category = match &ep.source {
1134            discover::EntryPointSource::PackageJsonMain
1135            | discover::EntryPointSource::PackageJsonModule
1136            | discover::EntryPointSource::PackageJsonExports
1137            | discover::EntryPointSource::PackageJsonBin
1138            | discover::EntryPointSource::PackageJsonScript => "package.json",
1139            discover::EntryPointSource::Plugin { .. } => "plugin",
1140            discover::EntryPointSource::TestFile => "test file",
1141            discover::EntryPointSource::DefaultIndex => "default index",
1142            discover::EntryPointSource::ManualEntry => "manual entry",
1143            discover::EntryPointSource::InfrastructureConfig => "config",
1144            discover::EntryPointSource::DynamicallyLoaded => "dynamically loaded",
1145        };
1146        *counts.entry(category.to_string()).or_insert(0) += 1;
1147    }
1148    let mut by_source: Vec<(String, usize)> = counts.into_iter().collect();
1149    by_source.sort_by(|a, b| b.1.cmp(&a.1).then_with(|| a.0.cmp(&b.0)));
1150    results::EntryPointSummary {
1151        total: entry_points.len(),
1152        by_source,
1153    }
1154}
1155
1156fn append_package_file_asset_patterns(
1157    result: &mut plugins::AggregatedPluginResult,
1158    prefix: &str,
1159    pkg: &PackageJson,
1160) {
1161    let prefix = prefix.trim_matches('/');
1162    for pattern in package_assets::scaffold_template_asset_patterns(pkg) {
1163        let pattern = if prefix.is_empty() {
1164            pattern
1165        } else {
1166            format!("{prefix}/{pattern}")
1167        };
1168        result
1169            .discovered_always_used
1170            .push((pattern, package_assets::PACKAGE_FILES_SOURCE.to_string()));
1171    }
1172}
1173
1174fn append_workspace_package_file_asset_patterns(
1175    result: &mut plugins::AggregatedPluginResult,
1176    config: &ResolvedConfig,
1177    workspace_pkgs: &[LoadedWorkspacePackage<'_>],
1178) {
1179    for (ws, ws_pkg) in workspace_pkgs {
1180        let ws_prefix = ws
1181            .root
1182            .strip_prefix(&config.root)
1183            .unwrap_or(&ws.root)
1184            .to_string_lossy()
1185            .replace('\\', "/");
1186        append_package_file_asset_patterns(result, &ws_prefix, ws_pkg);
1187    }
1188}
1189
1190/// Run plugins for root project and all workspace packages.
1191#[expect(
1192    clippy::too_many_lines,
1193    reason = "plugin orchestration keeps root and workspace merging in one flow"
1194)]
1195fn run_plugins(
1196    config: &ResolvedConfig,
1197    files: &[discover::DiscoveredFile],
1198    workspaces: &[fallow_config::WorkspaceInfo],
1199    root_pkg: Option<&PackageJson>,
1200    workspace_pkgs: &[LoadedWorkspacePackage<'_>],
1201) -> plugins::AggregatedPluginResult {
1202    let registry = plugins::PluginRegistry::new(config.external_plugins.clone());
1203    let file_paths: Vec<std::path::PathBuf> = files.iter().map(|f| f.path.clone()).collect();
1204    let root_config_search_roots = collect_config_search_roots(&config.root, &file_paths);
1205    let root_config_search_root_refs: Vec<&Path> = root_config_search_roots
1206        .iter()
1207        .map(std::path::PathBuf::as_path)
1208        .collect();
1209
1210    // Run plugins for root project (full run with external plugins, inline config, etc.)
1211    let mut result = root_pkg.map_or_else(plugins::AggregatedPluginResult::default, |pkg| {
1212        registry.run_with_search_roots(
1213            pkg,
1214            &config.root,
1215            &file_paths,
1216            &root_config_search_root_refs,
1217            config.production,
1218        )
1219    });
1220    if let Some(pkg) = root_pkg {
1221        append_package_file_asset_patterns(&mut result, "", pkg);
1222    }
1223
1224    if workspaces.is_empty() {
1225        gate_auto_import_entry_patterns(&mut result, config, workspaces);
1226        return result;
1227    }
1228
1229    append_workspace_package_file_asset_patterns(&mut result, config, workspace_pkgs);
1230
1231    let root_active_plugins: rustc_hash::FxHashSet<&str> =
1232        result.active_plugins.iter().map(String::as_str).collect();
1233
1234    // Pre-compile config matchers once and bucket source files by workspace.
1235    // Workspace config matching can then scan only files below that workspace
1236    // instead of every project file for every active matcher.
1237    let precompiled_matchers = registry.precompile_config_matchers();
1238    let workspace_relative_files = bucket_files_by_workspace(workspace_pkgs, &file_paths);
1239
1240    // Run plugins for each workspace package in parallel, then merge results.
1241    let ws_results: Vec<_> = workspace_pkgs
1242        .par_iter()
1243        .zip(workspace_relative_files.par_iter())
1244        .filter_map(|((ws, ws_pkg), relative_files)| {
1245            let ws_result = registry.run_workspace_fast(
1246                ws_pkg,
1247                &ws.root,
1248                &config.root,
1249                &precompiled_matchers,
1250                relative_files,
1251                &root_active_plugins,
1252                config.production,
1253            );
1254            if ws_result.active_plugins.is_empty() {
1255                return None;
1256            }
1257            let ws_prefix = ws
1258                .root
1259                .strip_prefix(&config.root)
1260                .unwrap_or(&ws.root)
1261                .to_string_lossy()
1262                .into_owned();
1263            Some((ws_result, ws_prefix))
1264        })
1265        .collect();
1266
1267    // Merge workspace results sequentially (deterministic order via par_iter index stability)
1268    // Track seen names for O(1) dedup instead of O(n) Vec::contains
1269    let mut seen_plugins: rustc_hash::FxHashSet<String> =
1270        result.active_plugins.iter().cloned().collect();
1271    let mut seen_prefixes: rustc_hash::FxHashSet<String> =
1272        result.virtual_module_prefixes.iter().cloned().collect();
1273    let mut seen_generated: rustc_hash::FxHashSet<String> =
1274        result.generated_import_patterns.iter().cloned().collect();
1275    let mut seen_generated_type_prefixes: rustc_hash::FxHashSet<String> = result
1276        .generated_type_import_prefixes
1277        .iter()
1278        .cloned()
1279        .collect();
1280    let mut seen_suffixes: rustc_hash::FxHashSet<String> =
1281        result.virtual_package_suffixes.iter().cloned().collect();
1282
1283    fn extend_unique(
1284        target: &mut Vec<String>,
1285        seen: &mut rustc_hash::FxHashSet<String>,
1286        items: Vec<String>,
1287    ) {
1288        for item in items {
1289            if seen.insert(item.clone()) {
1290                target.push(item);
1291            }
1292        }
1293    }
1294    for (ws_result, ws_prefix) in ws_results {
1295        // Prefix helper: workspace-relative patterns need the workspace prefix
1296        // to be matchable from the monorepo root. But patterns that are already
1297        // project-root-relative (e.g., from angular.json which uses absolute paths
1298        // like "apps/client/src/styles.css") should not be double-prefixed.
1299        let prefix_if_needed = |pat: &str| -> String {
1300            if pat.starts_with(ws_prefix.as_str()) || pat.starts_with('/') {
1301                pat.to_string()
1302            } else {
1303                format!("{ws_prefix}/{pat}")
1304            }
1305        };
1306
1307        for (rule, pname) in &ws_result.entry_patterns {
1308            result
1309                .entry_patterns
1310                .push((rule.prefixed(&ws_prefix), pname.clone()));
1311        }
1312        for (plugin_name, role) in ws_result.entry_point_roles {
1313            result.entry_point_roles.entry(plugin_name).or_insert(role);
1314        }
1315        for (pat, pname) in &ws_result.always_used {
1316            result
1317                .always_used
1318                .push((prefix_if_needed(pat), pname.clone()));
1319        }
1320        for (pat, pname) in &ws_result.discovered_always_used {
1321            result
1322                .discovered_always_used
1323                .push((prefix_if_needed(pat), pname.clone()));
1324        }
1325        for (pat, pname) in &ws_result.fixture_patterns {
1326            result
1327                .fixture_patterns
1328                .push((prefix_if_needed(pat), pname.clone()));
1329        }
1330        for rule in &ws_result.used_exports {
1331            result.used_exports.push(rule.prefixed(&ws_prefix));
1332        }
1333        for rule in &ws_result.provided_dependencies {
1334            result.provided_dependencies.push(rule.prefixed(&ws_prefix));
1335        }
1336        // Merge active plugin names (deduplicated via HashSet)
1337        for plugin_name in ws_result.active_plugins {
1338            if !seen_plugins.contains(&plugin_name) {
1339                seen_plugins.insert(plugin_name.clone());
1340                result.active_plugins.push(plugin_name);
1341            }
1342        }
1343        // These don't need prefixing (absolute paths / package names)
1344        result
1345            .referenced_dependencies
1346            .extend(ws_result.referenced_dependencies);
1347        result.setup_files.extend(ws_result.setup_files);
1348        result
1349            .tooling_dependencies
1350            .extend(ws_result.tooling_dependencies);
1351        result
1352            .static_dir_mappings
1353            .extend(ws_result.static_dir_mappings);
1354        // Virtual import boundaries — prefixes (e.g., Docusaurus `@theme/`),
1355        // generated import patterns (e.g., SvelteKit `/$types`), generated type
1356        // prefixes (e.g., React Router `./+types/`), and package-name suffixes
1357        // (e.g., Vitest `/__mocks__`) — match against import specifiers or
1358        // package names, never file paths, so no workspace prefix is applied.
1359        extend_unique(
1360            &mut result.virtual_module_prefixes,
1361            &mut seen_prefixes,
1362            ws_result.virtual_module_prefixes,
1363        );
1364        extend_unique(
1365            &mut result.generated_import_patterns,
1366            &mut seen_generated,
1367            ws_result.generated_import_patterns,
1368        );
1369        extend_unique(
1370            &mut result.generated_type_import_prefixes,
1371            &mut seen_generated_type_prefixes,
1372            ws_result.generated_type_import_prefixes,
1373        );
1374        extend_unique(
1375            &mut result.virtual_package_suffixes,
1376            &mut seen_suffixes,
1377            ws_result.virtual_package_suffixes,
1378        );
1379        // Path aliases from workspace plugins (e.g., SvelteKit $lib/ → src/lib).
1380        // Prefix the replacement directory so it resolves from the monorepo root.
1381        for (prefix, replacement) in ws_result.path_aliases {
1382            result
1383                .path_aliases
1384                .push((prefix, format!("{ws_prefix}/{replacement}")));
1385        }
1386        // Auto-import rules carry absolute `source` paths (built from the
1387        // workspace package root), so they are already correct per-package and
1388        // need no prefixing, just merging. See issue #704.
1389        result.auto_imports.extend(ws_result.auto_imports);
1390    }
1391
1392    gate_auto_import_entry_patterns(&mut result, config, workspaces);
1393
1394    result
1395}
1396
1397/// When `autoImports` is enabled, drop the Nuxt component entry patterns so
1398/// genuinely-unreferenced components are reported as `unused-file` (their
1399/// reachability now comes from synthesized auto-import edges instead). Guarded by
1400/// `config_declares_components`: if the root OR any workspace package's
1401/// `nuxt.config` declares a `components:` key, the patterns are kept, because
1402/// custom `prefix` / `pathPrefix` / `dirs` are not yet modeled and dropping the
1403/// protection would risk false positives. Auto-import edge synthesis itself is
1404/// unconditional; only this entry-pattern removal is flag-gated. See issue #704.
1405fn gate_auto_import_entry_patterns(
1406    result: &mut plugins::AggregatedPluginResult,
1407    config: &ResolvedConfig,
1408    workspaces: &[fallow_config::WorkspaceInfo],
1409) {
1410    if !config.auto_imports {
1411        return;
1412    }
1413    if !result.active_plugins.iter().any(|name| name == "nuxt") {
1414        return;
1415    }
1416    if plugins::nuxt::config_declares_components(&config.root)
1417        || workspaces
1418            .iter()
1419            .any(|ws| plugins::nuxt::config_declares_components(&ws.root))
1420    {
1421        return;
1422    }
1423    result.entry_patterns.retain(|(rule, plugin)| {
1424        !(plugin == "nuxt" && plugins::nuxt::is_component_entry_pattern(&rule.pattern))
1425    });
1426}
1427
1428fn bucket_files_by_workspace(
1429    workspace_pkgs: &[LoadedWorkspacePackage<'_>],
1430    file_paths: &[std::path::PathBuf],
1431) -> Vec<Vec<(std::path::PathBuf, String)>> {
1432    let mut buckets = vec![Vec::new(); workspace_pkgs.len()];
1433
1434    for file_path in file_paths {
1435        for (idx, (ws, _)) in workspace_pkgs.iter().enumerate() {
1436            if let Ok(relative) = file_path.strip_prefix(&ws.root) {
1437                buckets[idx].push((file_path.clone(), relative.to_string_lossy().into_owned()));
1438                break;
1439            }
1440        }
1441    }
1442
1443    buckets
1444}
1445
1446fn collect_config_search_roots(
1447    root: &Path,
1448    file_paths: &[std::path::PathBuf],
1449) -> Vec<std::path::PathBuf> {
1450    let mut roots: rustc_hash::FxHashSet<std::path::PathBuf> = rustc_hash::FxHashSet::default();
1451    roots.insert(root.to_path_buf());
1452
1453    for file_path in file_paths {
1454        let mut current = file_path.parent();
1455        while let Some(dir) = current {
1456            if !dir.starts_with(root) {
1457                break;
1458            }
1459            roots.insert(dir.to_path_buf());
1460            if dir == root {
1461                break;
1462            }
1463            current = dir.parent();
1464        }
1465    }
1466
1467    let mut roots_vec: Vec<_> = roots.into_iter().collect();
1468    roots_vec.sort();
1469    roots_vec
1470}
1471
1472/// Run analysis on a project directory (with export usages for LSP Code Lens).
1473///
1474/// # Errors
1475///
1476/// Returns an error if config loading, file discovery, parsing, or analysis fails.
1477#[deprecated(
1478    since = "2.76.0",
1479    note = "fallow_core is internal; use fallow_cli::programmatic::detect_dead_code instead (build a `DeadCodeOptions { analysis: AnalysisOptions { root, ..default() }, ..default() }`). See docs/fallow-core-migration.md and ADR-008."
1480)]
1481pub fn analyze_project(root: &Path) -> Result<AnalysisResults, FallowError> {
1482    let config = default_config(root);
1483    #[expect(
1484        deprecated,
1485        reason = "ADR-008: thin wrapper, internal call into the same deprecated surface"
1486    )]
1487    analyze_with_usages(&config)
1488}
1489
1490/// Resolve the analysis config for a project, mirroring the CLI's `--config`
1491/// behavior when `config_path` is provided.
1492///
1493/// # Errors
1494///
1495/// Returns an error when an explicit config cannot be loaded or automatic
1496/// config discovery finds an invalid config.
1497pub fn config_for_project(
1498    root: &Path,
1499    config_path: Option<&Path>,
1500) -> Result<(ResolvedConfig, Option<std::path::PathBuf>), FallowError> {
1501    let user_config = if let Some(path) = config_path {
1502        Some((
1503            fallow_config::FallowConfig::load(path)
1504                .map_err(|e| FallowError::config(format!("{e:#}")))?,
1505            path.to_path_buf(),
1506        ))
1507    } else {
1508        fallow_config::FallowConfig::find_and_load(root).map_err(FallowError::config)?
1509    };
1510
1511    let config = match user_config {
1512        Some((mut config, path)) => {
1513            let dead_code_production = config
1514                .production
1515                .for_analysis(fallow_config::ProductionAnalysis::DeadCode);
1516            config.production = dead_code_production.into();
1517            // Issue #468: validate boundary zone references and root-prefix
1518            // conflicts BEFORE resolve(). Mirrors the CLI's runtime_support
1519            // wiring; LSP and programmatic embedders surface the same exit-2
1520            // diagnostic via FallowError::config so editors / API consumers
1521            // get a structured failure instead of analysis-time noise.
1522            config
1523                .validate_resolved_boundaries(root)
1524                .map_err(|errors| {
1525                    let joined = errors
1526                        .iter()
1527                        .map(ToString::to_string)
1528                        .collect::<Vec<_>>()
1529                        .join("\n  - ");
1530                    FallowError::config(format!("invalid boundary configuration:\n  - {joined}"))
1531                })?;
1532            (
1533                config.resolve(
1534                    root.to_path_buf(),
1535                    fallow_config::OutputFormat::Human,
1536                    num_cpus(),
1537                    false,
1538                    true, // quiet: LSP/programmatic callers don't need progress bars
1539                    None, // LSP/programmatic embedders use the default cache cap
1540                ),
1541                Some(path),
1542            )
1543        }
1544        None => (
1545            fallow_config::FallowConfig::default().resolve(
1546                root.to_path_buf(),
1547                fallow_config::OutputFormat::Human,
1548                num_cpus(),
1549                false,
1550                true,
1551                None,
1552            ),
1553            None,
1554        ),
1555    };
1556
1557    Ok(config)
1558}
1559
1560/// Create a default config for a project root.
1561///
1562/// `analyze_project` is the dead-code entry point used by the LSP and other
1563/// programmatic embedders. When the loaded config uses the per-analysis
1564/// production form (`production: { deadCode: true, ... }`), the production
1565/// flag must be flattened to the dead-code analysis here. Otherwise
1566/// `ResolvedConfig::resolve` calls `.global()` which returns false for the
1567/// per-analysis variant and the production-mode rule overrides
1568/// (`unused_dev_dependencies: off`, etc.) plus `resolved.production = true`
1569/// are silently dropped.
1570pub(crate) fn default_config(root: &Path) -> ResolvedConfig {
1571    config_for_project(root, None).map_or_else(
1572        |_| {
1573            fallow_config::FallowConfig::default().resolve(
1574                root.to_path_buf(),
1575                fallow_config::OutputFormat::Human,
1576                num_cpus(),
1577                false,
1578                true,
1579                None,
1580            )
1581        },
1582        |(config, _)| config,
1583    )
1584}
1585
1586fn num_cpus() -> usize {
1587    std::thread::available_parallelism().map_or(4, std::num::NonZeroUsize::get)
1588}
1589
1590#[cfg(test)]
1591mod tests {
1592    use super::{
1593        bucket_files_by_workspace, collect_config_search_roots,
1594        format_undeclared_workspace_warning, warn_undeclared_workspaces,
1595    };
1596    use std::path::{Path, PathBuf};
1597
1598    use fallow_config::{WorkspaceDiagnostic, WorkspaceDiagnosticKind};
1599
1600    fn diag(root: &Path, relative: &str) -> WorkspaceDiagnostic {
1601        WorkspaceDiagnostic::new(
1602            root,
1603            root.join(relative),
1604            WorkspaceDiagnosticKind::UndeclaredWorkspace,
1605        )
1606    }
1607
1608    #[test]
1609    fn undeclared_workspace_warning_is_singular_for_one_path() {
1610        let root = Path::new("/repo");
1611        let warning = format_undeclared_workspace_warning(root, &[diag(root, "packages/api")])
1612            .expect("warning should be rendered");
1613
1614        assert_eq!(
1615            warning,
1616            "1 directory with package.json is not declared as a workspace: packages/api. Add that path to package.json workspaces or pnpm-workspace.yaml if it should be analyzed as a workspace."
1617        );
1618    }
1619
1620    #[test]
1621    fn undeclared_workspace_warning_summarizes_many_paths() {
1622        let root = PathBuf::from("/repo");
1623        let diagnostics = [
1624            "examples/a",
1625            "examples/b",
1626            "examples/c",
1627            "examples/d",
1628            "examples/e",
1629            "examples/f",
1630        ]
1631        .into_iter()
1632        .map(|path| diag(&root, path))
1633        .collect::<Vec<_>>();
1634
1635        let warning = format_undeclared_workspace_warning(&root, &diagnostics)
1636            .expect("warning should be rendered");
1637
1638        assert_eq!(
1639            warning,
1640            "6 directories with package.json are not declared as workspaces: examples/a, examples/b, examples/c, examples/d, examples/e (and 1 more). Add those paths to package.json workspaces or pnpm-workspace.yaml if they should be analyzed as workspaces."
1641        );
1642    }
1643
1644    #[test]
1645    fn collect_config_search_roots_includes_file_ancestors_once() {
1646        let root = PathBuf::from("/repo");
1647        let search_roots = collect_config_search_roots(
1648            &root,
1649            &[
1650                root.join("apps/query/src/main.ts"),
1651                root.join("packages/shared/lib/index.ts"),
1652            ],
1653        );
1654
1655        assert_eq!(
1656            search_roots,
1657            vec![
1658                root.clone(),
1659                root.join("apps"),
1660                root.join("apps/query"),
1661                root.join("apps/query/src"),
1662                root.join("packages"),
1663                root.join("packages/shared"),
1664                root.join("packages/shared/lib"),
1665            ]
1666        );
1667    }
1668
1669    #[test]
1670    fn bucket_files_by_workspace_uses_workspace_relative_paths() {
1671        let root = PathBuf::from("/repo");
1672        let ui = fallow_config::WorkspaceInfo {
1673            root: root.join("apps/ui"),
1674            name: "ui".to_string(),
1675            is_internal_dependency: false,
1676        };
1677        let api = fallow_config::WorkspaceInfo {
1678            root: root.join("apps/api"),
1679            name: "api".to_string(),
1680            is_internal_dependency: false,
1681        };
1682        let workspace_pkgs = vec![
1683            (
1684                &ui,
1685                fallow_config::PackageJson {
1686                    name: Some("ui".to_string()),
1687                    ..Default::default()
1688                },
1689            ),
1690            (
1691                &api,
1692                fallow_config::PackageJson {
1693                    name: Some("api".to_string()),
1694                    ..Default::default()
1695                },
1696            ),
1697        ];
1698        let files = vec![
1699            root.join("apps/ui/vite.config.ts"),
1700            root.join("apps/ui/src/main.ts"),
1701            root.join("apps/api/src/server.ts"),
1702            root.join("tools/build.ts"),
1703        ];
1704
1705        let buckets = bucket_files_by_workspace(&workspace_pkgs, &files);
1706
1707        assert_eq!(
1708            buckets[0],
1709            vec![
1710                (
1711                    root.join("apps/ui/vite.config.ts"),
1712                    "vite.config.ts".to_string()
1713                ),
1714                (root.join("apps/ui/src/main.ts"), "src/main.ts".to_string()),
1715            ]
1716        );
1717        assert_eq!(
1718            buckets[1],
1719            vec![(
1720                root.join("apps/api/src/server.ts"),
1721                "src/server.ts".to_string()
1722            )]
1723        );
1724    }
1725
1726    #[test]
1727    fn warn_undeclared_workspaces_suppresses_paths_already_flagged_as_malformed() {
1728        // Regression test for the load-bearing dedup in
1729        // `warn_undeclared_workspaces`: when a declared workspace's
1730        // package.json is malformed, the discovery pass drops the directory
1731        // and stashes `MalformedPackageJson` in the registry. The later
1732        // undeclared-workspace pass would otherwise re-flag the SAME
1733        // directory as "undeclared" (because it never made it into the
1734        // `declared` Vec), confusing users who think the workspace is not
1735        // declared when it actually is, just typo'd. This test asserts the
1736        // pre-existing MalformedPackageJson entry suppresses the duplicate
1737        // undeclared warning.
1738        let dir = tempfile::tempdir().expect("create temp dir");
1739        let pkg_good = dir.path().join("packages").join("good");
1740        let pkg_bad = dir.path().join("packages").join("bad");
1741        std::fs::create_dir_all(&pkg_good).unwrap();
1742        std::fs::create_dir_all(&pkg_bad).unwrap();
1743        std::fs::write(
1744            dir.path().join("package.json"),
1745            r#"{"workspaces": ["packages/*"]}"#,
1746        )
1747        .unwrap();
1748        std::fs::write(pkg_good.join("package.json"), r#"{"name": "good"}"#).unwrap();
1749        std::fs::write(pkg_bad.join("package.json"), r"{,").unwrap();
1750
1751        // Run discovery; in production `load_config_for_analysis` stashes
1752        // the returned diagnostics into the registry, so this test mirrors
1753        // that pattern by stashing manually.
1754        let (workspaces, diagnostics) = fallow_config::discover_workspaces_with_diagnostics(
1755            dir.path(),
1756            &globset::GlobSet::empty(),
1757        )
1758        .expect("root package.json is valid");
1759        assert_eq!(workspaces.len(), 1, "only the valid workspace discovers");
1760        fallow_config::stash_workspace_diagnostics(dir.path(), diagnostics);
1761
1762        // Now run the undeclared pass via the public entry point. The
1763        // registry should contain the MalformedPackageJson diagnostic but
1764        // NOT an UndeclaredWorkspace for the same path.
1765        warn_undeclared_workspaces(dir.path(), &workspaces, &globset::GlobSet::empty(), false);
1766
1767        let diagnostics = fallow_config::workspace_diagnostics_for(dir.path());
1768        let mut malformed = 0;
1769        let mut undeclared_for_bad = 0;
1770        for diag in &diagnostics {
1771            if matches!(
1772                diag.kind,
1773                WorkspaceDiagnosticKind::MalformedPackageJson { .. }
1774            ) && diag.path.ends_with("bad")
1775            {
1776                malformed += 1;
1777            }
1778            if matches!(diag.kind, WorkspaceDiagnosticKind::UndeclaredWorkspace)
1779                && diag.path.ends_with("bad")
1780            {
1781                undeclared_for_bad += 1;
1782            }
1783        }
1784        assert_eq!(
1785            malformed, 1,
1786            "expected one MalformedPackageJson for packages/bad: {diagnostics:?}"
1787        );
1788        assert_eq!(
1789            undeclared_for_bad, 0,
1790            "warn_undeclared_workspaces must NOT re-flag a path that already \
1791             carries MalformedPackageJson; got duplicates: {diagnostics:?}"
1792        );
1793    }
1794}