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