Skip to main content

fallow_core/
lib.rs

1pub mod analyze;
2pub mod cache;
3pub mod churn;
4pub mod cross_reference;
5pub mod discover;
6pub mod duplicates;
7pub(crate) mod errors;
8mod external_style_usage;
9pub mod extract;
10pub mod plugins;
11pub(crate) mod progress;
12pub mod results;
13pub(crate) mod scripts;
14pub mod suppress;
15pub mod trace;
16
17// Re-export from fallow-graph for backwards compatibility
18pub use fallow_graph::graph;
19pub use fallow_graph::project;
20pub use fallow_graph::resolve;
21
22use std::path::Path;
23use std::time::Instant;
24
25use errors::FallowError;
26use fallow_config::{
27    EntryPointRole, PackageJson, ResolvedConfig, discover_workspaces, find_undeclared_workspaces,
28};
29use rayon::prelude::*;
30use results::AnalysisResults;
31use trace::PipelineTimings;
32
33const UNDECLARED_WORKSPACE_WARNING_PREVIEW: usize = 5;
34type LoadedWorkspacePackage<'a> = (&'a fallow_config::WorkspaceInfo, PackageJson);
35
36/// Result of the full analysis pipeline, including optional performance timings.
37pub struct AnalysisOutput {
38    pub results: AnalysisResults,
39    pub timings: Option<PipelineTimings>,
40    pub graph: Option<graph::ModuleGraph>,
41    /// Parsed modules from the pipeline, available when `retain_modules` is true.
42    /// Used by the combined command to share a single parse across dead-code and health.
43    pub modules: Option<Vec<extract::ModuleInfo>>,
44    /// Discovered files from the pipeline, available when `retain_modules` is true.
45    pub files: Option<Vec<discover::DiscoveredFile>>,
46}
47
48/// Update cache: write freshly parsed modules and refresh stale mtime/size entries.
49fn update_cache(
50    store: &mut cache::CacheStore,
51    modules: &[extract::ModuleInfo],
52    files: &[discover::DiscoveredFile],
53) {
54    for module in modules {
55        if let Some(file) = files.get(module.file_id.0 as usize) {
56            let (mt, sz) = file_mtime_and_size(&file.path);
57            // If content hash matches, just refresh mtime/size if stale (e.g. `touch`ed file)
58            if let Some(cached) = store.get_by_path_only(&file.path)
59                && cached.content_hash == module.content_hash
60            {
61                if cached.mtime_secs != mt || cached.file_size != sz {
62                    store.insert(&file.path, cache::module_to_cached(module, mt, sz));
63                }
64                continue;
65            }
66            store.insert(&file.path, cache::module_to_cached(module, mt, sz));
67        }
68    }
69    store.retain_paths(files);
70}
71
72/// Extract mtime (seconds since epoch) and file size from a path.
73fn file_mtime_and_size(path: &std::path::Path) -> (u64, u64) {
74    std::fs::metadata(path).map_or((0, 0), |m| {
75        let mt = m
76            .modified()
77            .ok()
78            .and_then(|t| t.duration_since(std::time::SystemTime::UNIX_EPOCH).ok())
79            .map_or(0, |d| d.as_secs());
80        (mt, m.len())
81    })
82}
83
84fn format_undeclared_workspace_warning(
85    root: &Path,
86    undeclared: &[fallow_config::WorkspaceDiagnostic],
87) -> Option<String> {
88    if undeclared.is_empty() {
89        return None;
90    }
91
92    let preview = undeclared
93        .iter()
94        .take(UNDECLARED_WORKSPACE_WARNING_PREVIEW)
95        .map(|diag| {
96            diag.path
97                .strip_prefix(root)
98                .unwrap_or(&diag.path)
99                .display()
100                .to_string()
101                .replace('\\', "/")
102        })
103        .collect::<Vec<_>>();
104    let remaining = undeclared
105        .len()
106        .saturating_sub(UNDECLARED_WORKSPACE_WARNING_PREVIEW);
107    let tail = if remaining > 0 {
108        format!(" (and {remaining} more)")
109    } else {
110        String::new()
111    };
112    let noun = if undeclared.len() == 1 {
113        "directory with package.json is"
114    } else {
115        "directories with package.json are"
116    };
117    let guidance = if undeclared.len() == 1 {
118        "Add that path to package.json workspaces or pnpm-workspace.yaml if it should be analyzed as a workspace."
119    } else {
120        "Add those paths to package.json workspaces or pnpm-workspace.yaml if they should be analyzed as workspaces."
121    };
122
123    Some(format!(
124        "{} {} not declared as {}: {}{}. {}",
125        undeclared.len(),
126        noun,
127        if undeclared.len() == 1 {
128            "a workspace"
129        } else {
130            "workspaces"
131        },
132        preview.join(", "),
133        tail,
134        guidance
135    ))
136}
137
138fn warn_undeclared_workspaces(
139    root: &Path,
140    workspaces_vec: &[fallow_config::WorkspaceInfo],
141    quiet: bool,
142) {
143    if quiet {
144        return;
145    }
146
147    let undeclared = find_undeclared_workspaces(root, workspaces_vec);
148    if let Some(message) = format_undeclared_workspace_warning(root, &undeclared) {
149        tracing::warn!("{message}");
150    }
151}
152
153/// Run the full analysis pipeline.
154///
155/// # Errors
156///
157/// Returns an error if file discovery, parsing, or analysis fails.
158pub fn analyze(config: &ResolvedConfig) -> Result<AnalysisResults, FallowError> {
159    let output = analyze_full(config, false, false, false, false)?;
160    Ok(output.results)
161}
162
163/// Run the full analysis pipeline with export usage collection (for LSP Code Lens).
164///
165/// # Errors
166///
167/// Returns an error if file discovery, parsing, or analysis fails.
168pub fn analyze_with_usages(config: &ResolvedConfig) -> Result<AnalysisResults, FallowError> {
169    let output = analyze_full(config, false, true, false, false)?;
170    Ok(output.results)
171}
172
173/// Run the full analysis pipeline with optional performance timings and graph retention.
174///
175/// # Errors
176///
177/// Returns an error if file discovery, parsing, or analysis fails.
178pub fn analyze_with_trace(config: &ResolvedConfig) -> Result<AnalysisOutput, FallowError> {
179    analyze_full(config, true, false, false, false)
180}
181
182/// Run the full analysis pipeline, retaining parsed modules and discovered files.
183///
184/// Used by the combined command to share a single parse across dead-code and health.
185/// When `need_complexity` is true, the `ComplexityVisitor` runs during parsing so
186/// the returned modules contain per-function complexity data.
187///
188/// # Errors
189///
190/// Returns an error if file discovery, parsing, or analysis fails.
191pub fn analyze_retaining_modules(
192    config: &ResolvedConfig,
193    need_complexity: bool,
194    retain_graph: bool,
195) -> Result<AnalysisOutput, FallowError> {
196    analyze_full(config, retain_graph, false, need_complexity, true)
197}
198
199/// Run the analysis pipeline using pre-parsed modules, skipping the parsing stage.
200///
201/// This avoids re-parsing files when the caller already has a `ParseResult` (e.g., from
202/// `fallow_core::extract::parse_all_files`). Discovery, plugins, scripts, entry points,
203/// import resolution, graph construction, and dead code detection still run normally.
204/// The graph is always retained (needed for file scores).
205///
206/// # Errors
207///
208/// Returns an error if discovery, graph construction, or analysis fails.
209#[allow(
210    clippy::too_many_lines,
211    reason = "pipeline orchestration stays easier to audit in one place"
212)]
213pub fn analyze_with_parse_result(
214    config: &ResolvedConfig,
215    modules: &[extract::ModuleInfo],
216) -> Result<AnalysisOutput, FallowError> {
217    let _span = tracing::info_span!("fallow_analyze_with_parse_result").entered();
218    let pipeline_start = Instant::now();
219
220    let show_progress = !config.quiet
221        && std::io::IsTerminal::is_terminal(&std::io::stderr())
222        && matches!(
223            config.output,
224            fallow_config::OutputFormat::Human
225                | fallow_config::OutputFormat::Compact
226                | fallow_config::OutputFormat::Markdown
227        );
228    let progress = progress::AnalysisProgress::new(show_progress);
229
230    if !config.root.join("node_modules").is_dir() {
231        tracing::warn!(
232            "node_modules directory not found. Run `npm install` / `pnpm install` first for accurate results."
233        );
234    }
235
236    // Discover workspaces
237    let t = Instant::now();
238    let workspaces_vec = discover_workspaces(&config.root);
239    let workspaces_ms = t.elapsed().as_secs_f64() * 1000.0;
240    if !workspaces_vec.is_empty() {
241        tracing::info!(count = workspaces_vec.len(), "workspaces discovered");
242    }
243
244    // Warn about directories with package.json not declared as workspaces
245    warn_undeclared_workspaces(&config.root, &workspaces_vec, config.quiet);
246
247    // Stage 1: Discover files (cheap — needed for file registry and resolution)
248    let t = Instant::now();
249    let pb = progress.stage_spinner("Discovering files...");
250    let discovered_files = discover::discover_files(config);
251    let discover_ms = t.elapsed().as_secs_f64() * 1000.0;
252    pb.finish_and_clear();
253
254    let project = project::ProjectState::new(discovered_files, workspaces_vec);
255    let files = project.files();
256    let workspaces = project.workspaces();
257    let root_pkg = load_root_package_json(config);
258    let workspace_pkgs = load_workspace_packages(workspaces);
259
260    // Stage 1.5: Run plugin system
261    let t = Instant::now();
262    let pb = progress.stage_spinner("Detecting plugins...");
263    let mut plugin_result = run_plugins(
264        config,
265        files,
266        workspaces,
267        root_pkg.as_ref(),
268        &workspace_pkgs,
269    );
270    let plugins_ms = t.elapsed().as_secs_f64() * 1000.0;
271    pb.finish_and_clear();
272
273    // Stage 1.6: Analyze package.json scripts
274    let t = Instant::now();
275    analyze_all_scripts(
276        config,
277        workspaces,
278        root_pkg.as_ref(),
279        &workspace_pkgs,
280        &mut plugin_result,
281    );
282    let scripts_ms = t.elapsed().as_secs_f64() * 1000.0;
283
284    // Stage 2: SKIPPED — using pre-parsed modules from caller
285
286    // Stage 3: Discover entry points
287    let t = Instant::now();
288    let entry_points = discover_all_entry_points(
289        config,
290        files,
291        workspaces,
292        root_pkg.as_ref(),
293        &workspace_pkgs,
294        &plugin_result,
295    );
296    let entry_points_ms = t.elapsed().as_secs_f64() * 1000.0;
297
298    // Compute entry-point summary before the graph consumes the entry_points vec
299    let ep_summary = summarize_entry_points(&entry_points.all);
300
301    // Stage 4: Resolve imports to file IDs
302    let t = Instant::now();
303    let pb = progress.stage_spinner("Resolving imports...");
304    let mut resolved = resolve::resolve_all_imports(
305        modules,
306        files,
307        workspaces,
308        &plugin_result.active_plugins,
309        &plugin_result.path_aliases,
310        &plugin_result.scss_include_paths,
311        &config.root,
312        &config.resolve.conditions,
313    );
314    external_style_usage::augment_external_style_package_usage(
315        &mut resolved,
316        config,
317        workspaces,
318        &plugin_result,
319    );
320    let resolve_ms = t.elapsed().as_secs_f64() * 1000.0;
321    pb.finish_and_clear();
322
323    // Stage 5: Build module graph
324    let t = Instant::now();
325    let pb = progress.stage_spinner("Building module graph...");
326    let graph = graph::ModuleGraph::build_with_reachability_roots(
327        &resolved,
328        &entry_points.all,
329        &entry_points.runtime,
330        &entry_points.test,
331        files,
332    );
333    let graph_ms = t.elapsed().as_secs_f64() * 1000.0;
334    pb.finish_and_clear();
335
336    // Stage 6: Analyze for dead code
337    let t = Instant::now();
338    let pb = progress.stage_spinner("Analyzing...");
339    let mut result = analyze::find_dead_code_full(
340        &graph,
341        config,
342        &resolved,
343        Some(&plugin_result),
344        workspaces,
345        modules,
346        false,
347    );
348    let analyze_ms = t.elapsed().as_secs_f64() * 1000.0;
349    pb.finish_and_clear();
350    progress.finish();
351
352    result.entry_point_summary = Some(ep_summary);
353
354    let total_ms = pipeline_start.elapsed().as_secs_f64() * 1000.0;
355
356    tracing::debug!(
357        "\n┌─ Pipeline Profile (reuse) ─────────────────────\n\
358         │  discover files:   {:>8.1}ms  ({} files)\n\
359         │  workspaces:       {:>8.1}ms\n\
360         │  plugins:          {:>8.1}ms\n\
361         │  script analysis:  {:>8.1}ms\n\
362         │  parse/extract:    SKIPPED (reused {} modules)\n\
363         │  entry points:     {:>8.1}ms  ({} entries)\n\
364         │  resolve imports:  {:>8.1}ms\n\
365         │  build graph:      {:>8.1}ms\n\
366         │  analyze:          {:>8.1}ms\n\
367         │  ────────────────────────────────────────────\n\
368         │  TOTAL:            {:>8.1}ms\n\
369         └─────────────────────────────────────────────────",
370        discover_ms,
371        files.len(),
372        workspaces_ms,
373        plugins_ms,
374        scripts_ms,
375        modules.len(),
376        entry_points_ms,
377        entry_points.all.len(),
378        resolve_ms,
379        graph_ms,
380        analyze_ms,
381        total_ms,
382    );
383
384    let timings = Some(PipelineTimings {
385        discover_files_ms: discover_ms,
386        file_count: files.len(),
387        workspaces_ms,
388        workspace_count: workspaces.len(),
389        plugins_ms,
390        script_analysis_ms: scripts_ms,
391        parse_extract_ms: 0.0, // Skipped — modules were reused
392        module_count: modules.len(),
393        cache_hits: 0,
394        cache_misses: 0,
395        cache_update_ms: 0.0,
396        entry_points_ms,
397        entry_point_count: entry_points.all.len(),
398        resolve_imports_ms: resolve_ms,
399        build_graph_ms: graph_ms,
400        analyze_ms,
401        total_ms,
402    });
403
404    Ok(AnalysisOutput {
405        results: result,
406        timings,
407        graph: Some(graph),
408        modules: None,
409        files: None,
410    })
411}
412
413#[expect(
414    clippy::unnecessary_wraps,
415    reason = "Result kept for future error handling"
416)]
417#[expect(
418    clippy::too_many_lines,
419    reason = "main pipeline function; split candidate for sig-audit-loop"
420)]
421fn analyze_full(
422    config: &ResolvedConfig,
423    retain: bool,
424    collect_usages: bool,
425    need_complexity: bool,
426    retain_modules: bool,
427) -> Result<AnalysisOutput, FallowError> {
428    let _span = tracing::info_span!("fallow_analyze").entered();
429    let pipeline_start = Instant::now();
430
431    // Progress bars: enabled when not quiet, stderr is a terminal, and output is human-readable.
432    // Structured formats (JSON, SARIF) suppress spinners even on TTY — users piping structured
433    // output don't expect progress noise on stderr.
434    let show_progress = !config.quiet
435        && std::io::IsTerminal::is_terminal(&std::io::stderr())
436        && matches!(
437            config.output,
438            fallow_config::OutputFormat::Human
439                | fallow_config::OutputFormat::Compact
440                | fallow_config::OutputFormat::Markdown
441        );
442    let progress = progress::AnalysisProgress::new(show_progress);
443
444    // Warn if node_modules is missing — resolution will be severely degraded
445    if !config.root.join("node_modules").is_dir() {
446        tracing::warn!(
447            "node_modules directory not found. Run `npm install` / `pnpm install` first for accurate results."
448        );
449    }
450
451    // Discover workspaces if in a monorepo
452    let t = Instant::now();
453    let workspaces_vec = discover_workspaces(&config.root);
454    let workspaces_ms = t.elapsed().as_secs_f64() * 1000.0;
455    if !workspaces_vec.is_empty() {
456        tracing::info!(count = workspaces_vec.len(), "workspaces discovered");
457    }
458
459    // Warn about directories with package.json not declared as workspaces
460    warn_undeclared_workspaces(&config.root, &workspaces_vec, config.quiet);
461
462    // Stage 1: Discover all source files
463    let t = Instant::now();
464    let pb = progress.stage_spinner("Discovering files...");
465    let discovered_files = discover::discover_files(config);
466    let discover_ms = t.elapsed().as_secs_f64() * 1000.0;
467    pb.finish_and_clear();
468
469    // Build ProjectState: owns the file registry with stable FileIds and workspace metadata.
470    // This is the foundation for cross-workspace resolution and future incremental analysis.
471    let project = project::ProjectState::new(discovered_files, workspaces_vec);
472    let files = project.files();
473    let workspaces = project.workspaces();
474    let root_pkg = load_root_package_json(config);
475    let workspace_pkgs = load_workspace_packages(workspaces);
476
477    // Stage 1.5: Run plugin system — parse config files, discover dynamic entries
478    let t = Instant::now();
479    let pb = progress.stage_spinner("Detecting plugins...");
480    let mut plugin_result = run_plugins(
481        config,
482        files,
483        workspaces,
484        root_pkg.as_ref(),
485        &workspace_pkgs,
486    );
487    let plugins_ms = t.elapsed().as_secs_f64() * 1000.0;
488    pb.finish_and_clear();
489
490    // Stage 1.6: Analyze package.json scripts for binary usage and config file refs
491    let t = Instant::now();
492    analyze_all_scripts(
493        config,
494        workspaces,
495        root_pkg.as_ref(),
496        &workspace_pkgs,
497        &mut plugin_result,
498    );
499    let scripts_ms = t.elapsed().as_secs_f64() * 1000.0;
500
501    // Stage 2: Parse all files in parallel and extract imports/exports
502    let t = Instant::now();
503    let pb = progress.stage_spinner(&format!("Parsing {} files...", files.len()));
504    let mut cache_store = if config.no_cache {
505        None
506    } else {
507        cache::CacheStore::load(&config.cache_dir)
508    };
509
510    let parse_result = extract::parse_all_files(files, cache_store.as_ref(), need_complexity);
511    let modules = parse_result.modules;
512    let cache_hits = parse_result.cache_hits;
513    let cache_misses = parse_result.cache_misses;
514    let parse_ms = t.elapsed().as_secs_f64() * 1000.0;
515    pb.finish_and_clear();
516
517    // Update cache with freshly parsed modules and refresh stale mtime/size entries.
518    let t = Instant::now();
519    if !config.no_cache {
520        let store = cache_store.get_or_insert_with(cache::CacheStore::new);
521        update_cache(store, &modules, files);
522        if let Err(e) = store.save(&config.cache_dir) {
523            tracing::warn!("Failed to save cache: {e}");
524        }
525    }
526    let cache_ms = t.elapsed().as_secs_f64() * 1000.0;
527
528    // Stage 3: Discover entry points (static patterns + plugin-discovered patterns)
529    let t = Instant::now();
530    let entry_points = discover_all_entry_points(
531        config,
532        files,
533        workspaces,
534        root_pkg.as_ref(),
535        &workspace_pkgs,
536        &plugin_result,
537    );
538    let entry_points_ms = t.elapsed().as_secs_f64() * 1000.0;
539
540    // Stage 4: Resolve imports to file IDs
541    let t = Instant::now();
542    let pb = progress.stage_spinner("Resolving imports...");
543    let mut resolved = resolve::resolve_all_imports(
544        &modules,
545        files,
546        workspaces,
547        &plugin_result.active_plugins,
548        &plugin_result.path_aliases,
549        &plugin_result.scss_include_paths,
550        &config.root,
551        &config.resolve.conditions,
552    );
553    external_style_usage::augment_external_style_package_usage(
554        &mut resolved,
555        config,
556        workspaces,
557        &plugin_result,
558    );
559    let resolve_ms = t.elapsed().as_secs_f64() * 1000.0;
560    pb.finish_and_clear();
561
562    // Stage 5: Build module graph
563    let t = Instant::now();
564    let pb = progress.stage_spinner("Building module graph...");
565    let graph = graph::ModuleGraph::build_with_reachability_roots(
566        &resolved,
567        &entry_points.all,
568        &entry_points.runtime,
569        &entry_points.test,
570        files,
571    );
572    let graph_ms = t.elapsed().as_secs_f64() * 1000.0;
573    pb.finish_and_clear();
574
575    // Compute entry-point summary before the graph consumes the entry_points vec
576    let ep_summary = summarize_entry_points(&entry_points.all);
577
578    // Stage 6: Analyze for dead code (with plugin context and workspace info)
579    let t = Instant::now();
580    let pb = progress.stage_spinner("Analyzing...");
581    let mut result = analyze::find_dead_code_full(
582        &graph,
583        config,
584        &resolved,
585        Some(&plugin_result),
586        workspaces,
587        &modules,
588        collect_usages,
589    );
590    let analyze_ms = t.elapsed().as_secs_f64() * 1000.0;
591    pb.finish_and_clear();
592    progress.finish();
593
594    result.entry_point_summary = Some(ep_summary);
595
596    let total_ms = pipeline_start.elapsed().as_secs_f64() * 1000.0;
597
598    let cache_summary = if cache_hits > 0 {
599        format!(" ({cache_hits} cached, {cache_misses} parsed)")
600    } else {
601        String::new()
602    };
603
604    tracing::debug!(
605        "\n┌─ Pipeline Profile ─────────────────────────────\n\
606         │  discover files:   {:>8.1}ms  ({} files)\n\
607         │  workspaces:       {:>8.1}ms\n\
608         │  plugins:          {:>8.1}ms\n\
609         │  script analysis:  {:>8.1}ms\n\
610         │  parse/extract:    {:>8.1}ms  ({} modules{})\n\
611         │  cache update:     {:>8.1}ms\n\
612         │  entry points:     {:>8.1}ms  ({} entries)\n\
613         │  resolve imports:  {:>8.1}ms\n\
614         │  build graph:      {:>8.1}ms\n\
615         │  analyze:          {:>8.1}ms\n\
616         │  ────────────────────────────────────────────\n\
617         │  TOTAL:            {:>8.1}ms\n\
618         └─────────────────────────────────────────────────",
619        discover_ms,
620        files.len(),
621        workspaces_ms,
622        plugins_ms,
623        scripts_ms,
624        parse_ms,
625        modules.len(),
626        cache_summary,
627        cache_ms,
628        entry_points_ms,
629        entry_points.all.len(),
630        resolve_ms,
631        graph_ms,
632        analyze_ms,
633        total_ms,
634    );
635
636    let timings = if retain {
637        Some(PipelineTimings {
638            discover_files_ms: discover_ms,
639            file_count: files.len(),
640            workspaces_ms,
641            workspace_count: workspaces.len(),
642            plugins_ms,
643            script_analysis_ms: scripts_ms,
644            parse_extract_ms: parse_ms,
645            module_count: modules.len(),
646            cache_hits,
647            cache_misses,
648            cache_update_ms: cache_ms,
649            entry_points_ms,
650            entry_point_count: entry_points.all.len(),
651            resolve_imports_ms: resolve_ms,
652            build_graph_ms: graph_ms,
653            analyze_ms,
654            total_ms,
655        })
656    } else {
657        None
658    };
659
660    Ok(AnalysisOutput {
661        results: result,
662        timings,
663        graph: if retain { Some(graph) } else { None },
664        modules: if retain_modules { Some(modules) } else { None },
665        files: if retain_modules {
666            Some(files.to_vec())
667        } else {
668            None
669        },
670    })
671}
672
673/// Analyze package.json scripts from root and all workspace packages.
674///
675/// Populates the plugin result with script-used packages and config file
676/// entry patterns. Also scans CI config files for binary invocations.
677fn load_root_package_json(config: &ResolvedConfig) -> Option<PackageJson> {
678    PackageJson::load(&config.root.join("package.json")).ok()
679}
680
681fn load_workspace_packages(
682    workspaces: &[fallow_config::WorkspaceInfo],
683) -> Vec<LoadedWorkspacePackage<'_>> {
684    workspaces
685        .iter()
686        .filter_map(|ws| {
687            PackageJson::load(&ws.root.join("package.json"))
688                .ok()
689                .map(|pkg| (ws, pkg))
690        })
691        .collect()
692}
693
694fn analyze_all_scripts(
695    config: &ResolvedConfig,
696    workspaces: &[fallow_config::WorkspaceInfo],
697    root_pkg: Option<&PackageJson>,
698    workspace_pkgs: &[LoadedWorkspacePackage<'_>],
699    plugin_result: &mut plugins::AggregatedPluginResult,
700) {
701    // Collect all dependency names to build the bin-name → package-name reverse map.
702    // This resolves binaries like "attw" to "@arethetypeswrong/cli" even without
703    // node_modules/.bin symlinks.
704    let mut all_dep_names: Vec<String> = Vec::new();
705    if let Some(pkg) = root_pkg {
706        all_dep_names.extend(pkg.all_dependency_names());
707    }
708    for (_, ws_pkg) in workspace_pkgs {
709        all_dep_names.extend(ws_pkg.all_dependency_names());
710    }
711    all_dep_names.sort_unstable();
712    all_dep_names.dedup();
713
714    // Probe node_modules/ at project root and each workspace root so non-hoisted
715    // deps (pnpm strict, Yarn workspaces) are also discovered.
716    let mut nm_roots: Vec<&std::path::Path> = Vec::new();
717    if config.root.join("node_modules").is_dir() {
718        nm_roots.push(&config.root);
719    }
720    for ws in workspaces {
721        if ws.root.join("node_modules").is_dir() {
722            nm_roots.push(&ws.root);
723        }
724    }
725    let bin_map = scripts::build_bin_to_package_map(&nm_roots, &all_dep_names);
726
727    if let Some(pkg) = root_pkg
728        && let Some(ref pkg_scripts) = pkg.scripts
729    {
730        let scripts_to_analyze = if config.production {
731            scripts::filter_production_scripts(pkg_scripts)
732        } else {
733            pkg_scripts.clone()
734        };
735        let script_analysis = scripts::analyze_scripts(&scripts_to_analyze, &config.root, &bin_map);
736        plugin_result.script_used_packages = script_analysis.used_packages;
737
738        for config_file in &script_analysis.config_files {
739            plugin_result
740                .discovered_always_used
741                .push((config_file.clone(), "scripts".to_string()));
742        }
743    }
744    for (ws, ws_pkg) in workspace_pkgs {
745        if let Some(ref ws_scripts) = ws_pkg.scripts {
746            let scripts_to_analyze = if config.production {
747                scripts::filter_production_scripts(ws_scripts)
748            } else {
749                ws_scripts.clone()
750            };
751            let ws_analysis = scripts::analyze_scripts(&scripts_to_analyze, &ws.root, &bin_map);
752            plugin_result
753                .script_used_packages
754                .extend(ws_analysis.used_packages);
755
756            let ws_prefix = ws
757                .root
758                .strip_prefix(&config.root)
759                .unwrap_or(&ws.root)
760                .to_string_lossy();
761            for config_file in &ws_analysis.config_files {
762                plugin_result
763                    .discovered_always_used
764                    .push((format!("{ws_prefix}/{config_file}"), "scripts".to_string()));
765            }
766        }
767    }
768
769    // Scan CI config files for binary invocations
770    let ci_packages = scripts::ci::analyze_ci_files(&config.root, &bin_map);
771    plugin_result.script_used_packages.extend(ci_packages);
772    plugin_result
773        .entry_point_roles
774        .entry("scripts".to_string())
775        .or_insert(EntryPointRole::Support);
776}
777
778/// Discover all entry points from static patterns, workspaces, plugins, and infrastructure.
779fn discover_all_entry_points(
780    config: &ResolvedConfig,
781    files: &[discover::DiscoveredFile],
782    workspaces: &[fallow_config::WorkspaceInfo],
783    root_pkg: Option<&PackageJson>,
784    workspace_pkgs: &[LoadedWorkspacePackage<'_>],
785    plugin_result: &plugins::AggregatedPluginResult,
786) -> discover::CategorizedEntryPoints {
787    let mut entry_points = discover::CategorizedEntryPoints::default();
788    let root_discovery = discover::discover_entry_points_with_warnings_from_pkg(
789        config,
790        files,
791        root_pkg,
792        workspaces.is_empty(),
793    );
794
795    let workspace_pkg_by_root: rustc_hash::FxHashMap<std::path::PathBuf, &PackageJson> =
796        workspace_pkgs
797            .iter()
798            .map(|(ws, pkg)| (ws.root.clone(), pkg))
799            .collect();
800
801    let workspace_discovery: Vec<discover::EntryPointDiscovery> = workspaces
802        .par_iter()
803        .map(|ws| {
804            let pkg = workspace_pkg_by_root.get(&ws.root).copied();
805            discover::discover_workspace_entry_points_with_warnings_from_pkg(&ws.root, files, pkg)
806        })
807        .collect();
808    let mut skipped_entries = rustc_hash::FxHashMap::default();
809    entry_points.extend_runtime(root_discovery.entries);
810    for (path, count) in root_discovery.skipped_entries {
811        *skipped_entries.entry(path).or_insert(0) += count;
812    }
813    let mut ws_entries = Vec::new();
814    for workspace in workspace_discovery {
815        ws_entries.extend(workspace.entries);
816        for (path, count) in workspace.skipped_entries {
817            *skipped_entries.entry(path).or_insert(0) += count;
818        }
819    }
820    discover::warn_skipped_entry_summary(&skipped_entries);
821    entry_points.extend_runtime(ws_entries);
822
823    let plugin_entries = discover::discover_plugin_entry_point_sets(plugin_result, config, files);
824    entry_points.extend(plugin_entries);
825
826    let infra_entries = discover::discover_infrastructure_entry_points(&config.root);
827    entry_points.extend_runtime(infra_entries);
828
829    // Add dynamically loaded files from config as entry points
830    if !config.dynamically_loaded.is_empty() {
831        let dynamic_entries = discover::discover_dynamically_loaded_entry_points(config, files);
832        entry_points.extend_runtime(dynamic_entries);
833    }
834
835    entry_points.dedup()
836}
837
838/// Summarize entry points by source category for user-facing output.
839fn summarize_entry_points(entry_points: &[discover::EntryPoint]) -> results::EntryPointSummary {
840    let mut counts: rustc_hash::FxHashMap<String, usize> = rustc_hash::FxHashMap::default();
841    for ep in entry_points {
842        let category = match &ep.source {
843            discover::EntryPointSource::PackageJsonMain
844            | discover::EntryPointSource::PackageJsonModule
845            | discover::EntryPointSource::PackageJsonExports
846            | discover::EntryPointSource::PackageJsonBin
847            | discover::EntryPointSource::PackageJsonScript => "package.json",
848            discover::EntryPointSource::Plugin { .. } => "plugin",
849            discover::EntryPointSource::TestFile => "test file",
850            discover::EntryPointSource::DefaultIndex => "default index",
851            discover::EntryPointSource::ManualEntry => "manual entry",
852            discover::EntryPointSource::InfrastructureConfig => "config",
853            discover::EntryPointSource::DynamicallyLoaded => "dynamically loaded",
854        };
855        *counts.entry(category.to_string()).or_insert(0) += 1;
856    }
857    let mut by_source: Vec<(String, usize)> = counts.into_iter().collect();
858    by_source.sort_by(|a, b| b.1.cmp(&a.1).then_with(|| a.0.cmp(&b.0)));
859    results::EntryPointSummary {
860        total: entry_points.len(),
861        by_source,
862    }
863}
864
865/// Run plugins for root project and all workspace packages.
866fn run_plugins(
867    config: &ResolvedConfig,
868    files: &[discover::DiscoveredFile],
869    workspaces: &[fallow_config::WorkspaceInfo],
870    root_pkg: Option<&PackageJson>,
871    workspace_pkgs: &[LoadedWorkspacePackage<'_>],
872) -> plugins::AggregatedPluginResult {
873    let registry = plugins::PluginRegistry::new(config.external_plugins.clone());
874    let file_paths: Vec<std::path::PathBuf> = files.iter().map(|f| f.path.clone()).collect();
875    let root_config_search_roots = collect_config_search_roots(&config.root, &file_paths);
876    let root_config_search_root_refs: Vec<&Path> = root_config_search_roots
877        .iter()
878        .map(std::path::PathBuf::as_path)
879        .collect();
880
881    // Run plugins for root project (full run with external plugins, inline config, etc.)
882    let mut result = root_pkg.map_or_else(plugins::AggregatedPluginResult::default, |pkg| {
883        registry.run_with_search_roots(
884            pkg,
885            &config.root,
886            &file_paths,
887            &root_config_search_root_refs,
888        )
889    });
890
891    if workspaces.is_empty() {
892        return result;
893    }
894
895    let root_active_plugins: rustc_hash::FxHashSet<&str> =
896        result.active_plugins.iter().map(String::as_str).collect();
897
898    // Pre-compile config matchers and relative files once for all workspace runs.
899    // This avoids re-compiling glob patterns and re-computing relative paths per workspace
900    // (previously O(workspaces × plugins × files) glob compilations).
901    let precompiled_matchers = registry.precompile_config_matchers();
902    let relative_files: Vec<(&std::path::PathBuf, String)> = file_paths
903        .iter()
904        .map(|f| {
905            let rel = f
906                .strip_prefix(&config.root)
907                .unwrap_or(f)
908                .to_string_lossy()
909                .into_owned();
910            (f, rel)
911        })
912        .collect();
913
914    // Run plugins for each workspace package in parallel, then merge results.
915    let ws_results: Vec<_> = workspace_pkgs
916        .par_iter()
917        .filter_map(|(ws, ws_pkg)| {
918            let ws_result = registry.run_workspace_fast(
919                ws_pkg,
920                &ws.root,
921                &config.root,
922                &precompiled_matchers,
923                &relative_files,
924                &root_active_plugins,
925            );
926            if ws_result.active_plugins.is_empty() {
927                return None;
928            }
929            let ws_prefix = ws
930                .root
931                .strip_prefix(&config.root)
932                .unwrap_or(&ws.root)
933                .to_string_lossy()
934                .into_owned();
935            Some((ws_result, ws_prefix))
936        })
937        .collect();
938
939    // Merge workspace results sequentially (deterministic order via par_iter index stability)
940    // Track seen names for O(1) dedup instead of O(n) Vec::contains
941    let mut seen_plugins: rustc_hash::FxHashSet<String> =
942        result.active_plugins.iter().cloned().collect();
943    let mut seen_prefixes: rustc_hash::FxHashSet<String> =
944        result.virtual_module_prefixes.iter().cloned().collect();
945    let mut seen_generated: rustc_hash::FxHashSet<String> =
946        result.generated_import_patterns.iter().cloned().collect();
947    for (ws_result, ws_prefix) in ws_results {
948        // Prefix helper: workspace-relative patterns need the workspace prefix
949        // to be matchable from the monorepo root. But patterns that are already
950        // project-root-relative (e.g., from angular.json which uses absolute paths
951        // like "apps/client/src/styles.css") should not be double-prefixed.
952        let prefix_if_needed = |pat: &str| -> String {
953            if pat.starts_with(ws_prefix.as_str()) || pat.starts_with('/') {
954                pat.to_string()
955            } else {
956                format!("{ws_prefix}/{pat}")
957            }
958        };
959
960        for (rule, pname) in &ws_result.entry_patterns {
961            result
962                .entry_patterns
963                .push((rule.prefixed(&ws_prefix), pname.clone()));
964        }
965        for (plugin_name, role) in ws_result.entry_point_roles {
966            result.entry_point_roles.entry(plugin_name).or_insert(role);
967        }
968        for (pat, pname) in &ws_result.always_used {
969            result
970                .always_used
971                .push((prefix_if_needed(pat), pname.clone()));
972        }
973        for (pat, pname) in &ws_result.discovered_always_used {
974            result
975                .discovered_always_used
976                .push((prefix_if_needed(pat), pname.clone()));
977        }
978        for (pat, pname) in &ws_result.fixture_patterns {
979            result
980                .fixture_patterns
981                .push((prefix_if_needed(pat), pname.clone()));
982        }
983        for rule in &ws_result.used_exports {
984            result.used_exports.push(rule.prefixed(&ws_prefix));
985        }
986        // Merge active plugin names (deduplicated via HashSet)
987        for plugin_name in ws_result.active_plugins {
988            if !seen_plugins.contains(&plugin_name) {
989                seen_plugins.insert(plugin_name.clone());
990                result.active_plugins.push(plugin_name);
991            }
992        }
993        // These don't need prefixing (absolute paths / package names)
994        result
995            .referenced_dependencies
996            .extend(ws_result.referenced_dependencies);
997        result.setup_files.extend(ws_result.setup_files);
998        result
999            .tooling_dependencies
1000            .extend(ws_result.tooling_dependencies);
1001        // Virtual module prefixes (e.g., Docusaurus @theme/, @site/) are
1002        // package-name prefixes, not file paths — no workspace prefix needed.
1003        for prefix in ws_result.virtual_module_prefixes {
1004            if !seen_prefixes.contains(&prefix) {
1005                seen_prefixes.insert(prefix.clone());
1006                result.virtual_module_prefixes.push(prefix);
1007            }
1008        }
1009        // Generated import patterns (e.g., SvelteKit /$types) are suffix
1010        // matches on specifiers, not file paths — no workspace prefix needed.
1011        for pattern in ws_result.generated_import_patterns {
1012            if !seen_generated.contains(&pattern) {
1013                seen_generated.insert(pattern.clone());
1014                result.generated_import_patterns.push(pattern);
1015            }
1016        }
1017        // Path aliases from workspace plugins (e.g., SvelteKit $lib/ → src/lib).
1018        // Prefix the replacement directory so it resolves from the monorepo root.
1019        for (prefix, replacement) in ws_result.path_aliases {
1020            result
1021                .path_aliases
1022                .push((prefix, format!("{ws_prefix}/{replacement}")));
1023        }
1024    }
1025
1026    result
1027}
1028
1029fn collect_config_search_roots(
1030    root: &Path,
1031    file_paths: &[std::path::PathBuf],
1032) -> Vec<std::path::PathBuf> {
1033    let mut roots: rustc_hash::FxHashSet<std::path::PathBuf> = rustc_hash::FxHashSet::default();
1034    roots.insert(root.to_path_buf());
1035
1036    for file_path in file_paths {
1037        let mut current = file_path.parent();
1038        while let Some(dir) = current {
1039            if !dir.starts_with(root) {
1040                break;
1041            }
1042            roots.insert(dir.to_path_buf());
1043            if dir == root {
1044                break;
1045            }
1046            current = dir.parent();
1047        }
1048    }
1049
1050    let mut roots_vec: Vec<_> = roots.into_iter().collect();
1051    roots_vec.sort();
1052    roots_vec
1053}
1054
1055/// Run analysis on a project directory (with export usages for LSP Code Lens).
1056///
1057/// # Errors
1058///
1059/// Returns an error if config loading, file discovery, parsing, or analysis fails.
1060pub fn analyze_project(root: &Path) -> Result<AnalysisResults, FallowError> {
1061    let config = default_config(root);
1062    analyze_with_usages(&config)
1063}
1064
1065/// Create a default config for a project root.
1066pub(crate) fn default_config(root: &Path) -> ResolvedConfig {
1067    let user_config = fallow_config::FallowConfig::find_and_load(root)
1068        .ok()
1069        .flatten();
1070    match user_config {
1071        Some((config, _path)) => config.resolve(
1072            root.to_path_buf(),
1073            fallow_config::OutputFormat::Human,
1074            num_cpus(),
1075            false,
1076            true, // quiet: LSP/programmatic callers don't need progress bars
1077        ),
1078        None => fallow_config::FallowConfig::default().resolve(
1079            root.to_path_buf(),
1080            fallow_config::OutputFormat::Human,
1081            num_cpus(),
1082            false,
1083            true,
1084        ),
1085    }
1086}
1087
1088fn num_cpus() -> usize {
1089    std::thread::available_parallelism().map_or(4, std::num::NonZeroUsize::get)
1090}
1091
1092#[cfg(test)]
1093mod tests {
1094    use super::{collect_config_search_roots, format_undeclared_workspace_warning};
1095    use std::path::{Path, PathBuf};
1096
1097    use fallow_config::WorkspaceDiagnostic;
1098
1099    fn diag(root: &Path, relative: &str) -> WorkspaceDiagnostic {
1100        WorkspaceDiagnostic {
1101            path: root.join(relative),
1102            message: String::new(),
1103        }
1104    }
1105
1106    #[test]
1107    fn undeclared_workspace_warning_is_singular_for_one_path() {
1108        let root = Path::new("/repo");
1109        let warning = format_undeclared_workspace_warning(root, &[diag(root, "packages/api")])
1110            .expect("warning should be rendered");
1111
1112        assert_eq!(
1113            warning,
1114            "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."
1115        );
1116    }
1117
1118    #[test]
1119    fn undeclared_workspace_warning_summarizes_many_paths() {
1120        let root = PathBuf::from("/repo");
1121        let diagnostics = [
1122            "examples/a",
1123            "examples/b",
1124            "examples/c",
1125            "examples/d",
1126            "examples/e",
1127            "examples/f",
1128        ]
1129        .into_iter()
1130        .map(|path| diag(&root, path))
1131        .collect::<Vec<_>>();
1132
1133        let warning = format_undeclared_workspace_warning(&root, &diagnostics)
1134            .expect("warning should be rendered");
1135
1136        assert_eq!(
1137            warning,
1138            "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."
1139        );
1140    }
1141
1142    #[test]
1143    fn collect_config_search_roots_includes_file_ancestors_once() {
1144        let root = PathBuf::from("/repo");
1145        let search_roots = collect_config_search_roots(
1146            &root,
1147            &[
1148                root.join("apps/query/src/main.ts"),
1149                root.join("packages/shared/lib/index.ts"),
1150            ],
1151        );
1152
1153        assert_eq!(
1154            search_roots,
1155            vec![
1156                root.clone(),
1157                root.join("apps"),
1158                root.join("apps/query"),
1159                root.join("apps/query/src"),
1160                root.join("packages"),
1161                root.join("packages/shared"),
1162                root.join("packages/shared/lib"),
1163            ]
1164        );
1165    }
1166}