Skip to main content

fallow_core/
lib.rs

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