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