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    let root_pkg = load_root_package_json(config);
334    let discovery_hidden_dir_scopes =
335        discover::collect_plugin_hidden_dir_scopes(config, root_pkg.as_ref(), &workspaces_vec);
336
337    // Stage 1: Discover files (cheap — needed for file registry and resolution)
338    let t = Instant::now();
339    let pb = progress.stage_spinner("Discovering files...");
340    let discovered_files =
341        discover::discover_files_with_additional_hidden_dirs(config, &discovery_hidden_dir_scopes);
342    let discover_ms = t.elapsed().as_secs_f64() * 1000.0;
343    pb.finish_and_clear();
344
345    let project = project::ProjectState::new(discovered_files, workspaces_vec);
346    let files = project.files();
347    let workspaces = project.workspaces();
348    let workspace_pkgs = load_workspace_packages(workspaces);
349
350    // Stage 1.5: Run plugin system
351    let t = Instant::now();
352    let pb = progress.stage_spinner("Detecting plugins...");
353    let mut plugin_result = run_plugins(
354        config,
355        files,
356        workspaces,
357        root_pkg.as_ref(),
358        &workspace_pkgs,
359    );
360    let plugins_ms = t.elapsed().as_secs_f64() * 1000.0;
361    pb.finish_and_clear();
362
363    // Stage 1.6: Analyze package.json scripts
364    let t = Instant::now();
365    analyze_all_scripts(
366        config,
367        workspaces,
368        root_pkg.as_ref(),
369        &workspace_pkgs,
370        &mut plugin_result,
371    );
372    let scripts_ms = t.elapsed().as_secs_f64() * 1000.0;
373
374    // Stage 2: SKIPPED — using pre-parsed modules from caller
375
376    // Stage 3: Discover entry points
377    let t = Instant::now();
378    let entry_points = discover_all_entry_points(
379        config,
380        files,
381        workspaces,
382        root_pkg.as_ref(),
383        &workspace_pkgs,
384        &plugin_result,
385    );
386    let entry_points_ms = t.elapsed().as_secs_f64() * 1000.0;
387
388    // Compute entry-point summary before the graph consumes the entry_points vec
389    let ep_summary = summarize_entry_points(&entry_points.all);
390
391    // Stage 4: Resolve imports to file IDs
392    let t = Instant::now();
393    let pb = progress.stage_spinner("Resolving imports...");
394    let mut resolved = resolve::resolve_all_imports(
395        modules,
396        files,
397        workspaces,
398        &plugin_result.active_plugins,
399        &plugin_result.path_aliases,
400        &plugin_result.scss_include_paths,
401        &config.root,
402        &config.resolve.conditions,
403    );
404    external_style_usage::augment_external_style_package_usage(
405        &mut resolved,
406        config,
407        workspaces,
408        &plugin_result,
409    );
410    let resolve_ms = t.elapsed().as_secs_f64() * 1000.0;
411    pb.finish_and_clear();
412
413    // Stage 5: Build module graph
414    let t = Instant::now();
415    let pb = progress.stage_spinner("Building module graph...");
416    let mut graph = graph::ModuleGraph::build_with_reachability_roots(
417        &resolved,
418        &entry_points.all,
419        &entry_points.runtime,
420        &entry_points.test,
421        files,
422    );
423    credit_workspace_package_usage(&mut graph, &resolved, workspaces);
424    let graph_ms = t.elapsed().as_secs_f64() * 1000.0;
425    pb.finish_and_clear();
426
427    // Stage 6: Analyze for dead code
428    let t = Instant::now();
429    let pb = progress.stage_spinner("Analyzing...");
430    let mut result = analyze::find_dead_code_full(
431        &graph,
432        config,
433        &resolved,
434        Some(&plugin_result),
435        workspaces,
436        modules,
437        false,
438    );
439    let analyze_ms = t.elapsed().as_secs_f64() * 1000.0;
440    pb.finish_and_clear();
441    progress.finish();
442
443    result.entry_point_summary = Some(ep_summary);
444
445    let total_ms = pipeline_start.elapsed().as_secs_f64() * 1000.0;
446
447    tracing::debug!(
448        "\n┌─ Pipeline Profile (reuse) ─────────────────────\n\
449         │  discover files:   {:>8.1}ms  ({} files)\n\
450         │  workspaces:       {:>8.1}ms\n\
451         │  plugins:          {:>8.1}ms\n\
452         │  script analysis:  {:>8.1}ms\n\
453         │  parse/extract:    SKIPPED (reused {} modules)\n\
454         │  entry points:     {:>8.1}ms  ({} entries)\n\
455         │  resolve imports:  {:>8.1}ms\n\
456         │  build graph:      {:>8.1}ms\n\
457         │  analyze:          {:>8.1}ms\n\
458         │  ────────────────────────────────────────────\n\
459         │  TOTAL:            {:>8.1}ms\n\
460         └─────────────────────────────────────────────────",
461        discover_ms,
462        files.len(),
463        workspaces_ms,
464        plugins_ms,
465        scripts_ms,
466        modules.len(),
467        entry_points_ms,
468        entry_points.all.len(),
469        resolve_ms,
470        graph_ms,
471        analyze_ms,
472        total_ms,
473    );
474
475    let timings = Some(PipelineTimings {
476        discover_files_ms: discover_ms,
477        file_count: files.len(),
478        workspaces_ms,
479        workspace_count: workspaces.len(),
480        plugins_ms,
481        script_analysis_ms: scripts_ms,
482        parse_extract_ms: 0.0, // Skipped — modules were reused
483        module_count: modules.len(),
484        cache_hits: 0,
485        cache_misses: 0,
486        cache_update_ms: 0.0,
487        entry_points_ms,
488        entry_point_count: entry_points.all.len(),
489        resolve_imports_ms: resolve_ms,
490        build_graph_ms: graph_ms,
491        analyze_ms,
492        duplication_ms: None,
493        total_ms,
494    });
495
496    Ok(AnalysisOutput {
497        results: result,
498        timings,
499        graph: Some(graph),
500        modules: None,
501        files: None,
502        script_used_packages: plugin_result.script_used_packages.clone(),
503    })
504}
505
506#[expect(
507    clippy::unnecessary_wraps,
508    reason = "Result kept for future error handling"
509)]
510#[expect(
511    clippy::too_many_lines,
512    reason = "main pipeline function; sequential phases are held together for clarity"
513)]
514fn analyze_full(
515    config: &ResolvedConfig,
516    retain: bool,
517    collect_usages: bool,
518    need_complexity: bool,
519    retain_modules: bool,
520) -> Result<AnalysisOutput, FallowError> {
521    let _span = tracing::info_span!("fallow_analyze").entered();
522    let pipeline_start = Instant::now();
523
524    // Progress bars: enabled when not quiet, stderr is a terminal, and output is human-readable.
525    // Structured formats (JSON, SARIF) suppress spinners even on TTY — users piping structured
526    // output don't expect progress noise on stderr.
527    let show_progress = !config.quiet
528        && std::io::IsTerminal::is_terminal(&std::io::stderr())
529        && matches!(
530            config.output,
531            fallow_config::OutputFormat::Human
532                | fallow_config::OutputFormat::Compact
533                | fallow_config::OutputFormat::Markdown
534        );
535    let progress = progress::AnalysisProgress::new(show_progress);
536
537    // Warn if node_modules is missing — resolution will be severely degraded
538    if !config.root.join("node_modules").is_dir() {
539        tracing::warn!(
540            "node_modules directory not found. Run `npm install` / `pnpm install` first for accurate results."
541        );
542    }
543
544    // Discover workspaces if in a monorepo
545    let t = Instant::now();
546    let workspaces_vec = discover_workspaces(&config.root);
547    let workspaces_ms = t.elapsed().as_secs_f64() * 1000.0;
548    if !workspaces_vec.is_empty() {
549        tracing::info!(count = workspaces_vec.len(), "workspaces discovered");
550    }
551
552    // Warn about directories with package.json not declared as workspaces
553    warn_undeclared_workspaces(
554        &config.root,
555        &workspaces_vec,
556        &config.ignore_patterns,
557        config.quiet,
558    );
559    let root_pkg = load_root_package_json(config);
560    let discovery_hidden_dir_scopes =
561        discover::collect_plugin_hidden_dir_scopes(config, root_pkg.as_ref(), &workspaces_vec);
562
563    // Stage 1: Discover all source files
564    let t = Instant::now();
565    let pb = progress.stage_spinner("Discovering files...");
566    let discovered_files =
567        discover::discover_files_with_additional_hidden_dirs(config, &discovery_hidden_dir_scopes);
568    let discover_ms = t.elapsed().as_secs_f64() * 1000.0;
569    pb.finish_and_clear();
570
571    // Build ProjectState: owns the file registry with stable FileIds and workspace metadata.
572    // This is the foundation for cross-workspace resolution and future incremental analysis.
573    let project = project::ProjectState::new(discovered_files, workspaces_vec);
574    let files = project.files();
575    let workspaces = project.workspaces();
576    let workspace_pkgs = load_workspace_packages(workspaces);
577
578    // Stage 1.5: Run plugin system — parse config files, discover dynamic entries
579    let t = Instant::now();
580    let pb = progress.stage_spinner("Detecting plugins...");
581    let mut plugin_result = run_plugins(
582        config,
583        files,
584        workspaces,
585        root_pkg.as_ref(),
586        &workspace_pkgs,
587    );
588    let plugins_ms = t.elapsed().as_secs_f64() * 1000.0;
589    pb.finish_and_clear();
590
591    // Stage 1.6: Analyze package.json scripts for binary usage and config file refs
592    let t = Instant::now();
593    analyze_all_scripts(
594        config,
595        workspaces,
596        root_pkg.as_ref(),
597        &workspace_pkgs,
598        &mut plugin_result,
599    );
600    let scripts_ms = t.elapsed().as_secs_f64() * 1000.0;
601
602    // Stage 2: Parse all files in parallel and extract imports/exports
603    let t = Instant::now();
604    let pb = progress.stage_spinner(&format!("Parsing {} files...", files.len()));
605    let mut cache_store = if config.no_cache {
606        None
607    } else {
608        cache::CacheStore::load(&config.cache_dir)
609    };
610
611    let parse_result = extract::parse_all_files(files, cache_store.as_ref(), need_complexity);
612    let modules = parse_result.modules;
613    let cache_hits = parse_result.cache_hits;
614    let cache_misses = parse_result.cache_misses;
615    let parse_ms = t.elapsed().as_secs_f64() * 1000.0;
616    pb.finish_and_clear();
617
618    // Update cache with freshly parsed modules and refresh stale mtime/size entries.
619    let t = Instant::now();
620    if !config.no_cache {
621        let store = cache_store.get_or_insert_with(cache::CacheStore::new);
622        update_cache(store, &modules, files);
623        if let Err(e) = store.save(&config.cache_dir) {
624            tracing::warn!("Failed to save cache: {e}");
625        }
626    }
627    let cache_ms = t.elapsed().as_secs_f64() * 1000.0;
628
629    // Stage 3: Discover entry points (static patterns + plugin-discovered patterns)
630    let t = Instant::now();
631    let entry_points = discover_all_entry_points(
632        config,
633        files,
634        workspaces,
635        root_pkg.as_ref(),
636        &workspace_pkgs,
637        &plugin_result,
638    );
639    let entry_points_ms = t.elapsed().as_secs_f64() * 1000.0;
640
641    // Stage 4: Resolve imports to file IDs
642    let t = Instant::now();
643    let pb = progress.stage_spinner("Resolving imports...");
644    let mut resolved = resolve::resolve_all_imports(
645        &modules,
646        files,
647        workspaces,
648        &plugin_result.active_plugins,
649        &plugin_result.path_aliases,
650        &plugin_result.scss_include_paths,
651        &config.root,
652        &config.resolve.conditions,
653    );
654    external_style_usage::augment_external_style_package_usage(
655        &mut resolved,
656        config,
657        workspaces,
658        &plugin_result,
659    );
660    let resolve_ms = t.elapsed().as_secs_f64() * 1000.0;
661    pb.finish_and_clear();
662
663    // Stage 5: Build module graph
664    let t = Instant::now();
665    let pb = progress.stage_spinner("Building module graph...");
666    let mut graph = graph::ModuleGraph::build_with_reachability_roots(
667        &resolved,
668        &entry_points.all,
669        &entry_points.runtime,
670        &entry_points.test,
671        files,
672    );
673    credit_workspace_package_usage(&mut graph, &resolved, workspaces);
674    let graph_ms = t.elapsed().as_secs_f64() * 1000.0;
675    pb.finish_and_clear();
676
677    // Compute entry-point summary before the graph consumes the entry_points vec
678    let ep_summary = summarize_entry_points(&entry_points.all);
679
680    // Stage 6: Analyze for dead code (with plugin context and workspace info)
681    let t = Instant::now();
682    let pb = progress.stage_spinner("Analyzing...");
683    let mut result = analyze::find_dead_code_full(
684        &graph,
685        config,
686        &resolved,
687        Some(&plugin_result),
688        workspaces,
689        &modules,
690        collect_usages,
691    );
692    let analyze_ms = t.elapsed().as_secs_f64() * 1000.0;
693    pb.finish_and_clear();
694    progress.finish();
695
696    result.entry_point_summary = Some(ep_summary);
697
698    let total_ms = pipeline_start.elapsed().as_secs_f64() * 1000.0;
699
700    let cache_summary = if cache_hits > 0 {
701        format!(" ({cache_hits} cached, {cache_misses} parsed)")
702    } else {
703        String::new()
704    };
705
706    tracing::debug!(
707        "\n┌─ Pipeline Profile ─────────────────────────────\n\
708         │  discover files:   {:>8.1}ms  ({} files)\n\
709         │  workspaces:       {:>8.1}ms\n\
710         │  plugins:          {:>8.1}ms\n\
711         │  script analysis:  {:>8.1}ms\n\
712         │  parse/extract:    {:>8.1}ms  ({} modules{})\n\
713         │  cache update:     {:>8.1}ms\n\
714         │  entry points:     {:>8.1}ms  ({} entries)\n\
715         │  resolve imports:  {:>8.1}ms\n\
716         │  build graph:      {:>8.1}ms\n\
717         │  analyze:          {:>8.1}ms\n\
718         │  ────────────────────────────────────────────\n\
719         │  TOTAL:            {:>8.1}ms\n\
720         └─────────────────────────────────────────────────",
721        discover_ms,
722        files.len(),
723        workspaces_ms,
724        plugins_ms,
725        scripts_ms,
726        parse_ms,
727        modules.len(),
728        cache_summary,
729        cache_ms,
730        entry_points_ms,
731        entry_points.all.len(),
732        resolve_ms,
733        graph_ms,
734        analyze_ms,
735        total_ms,
736    );
737
738    let timings = if retain {
739        Some(PipelineTimings {
740            discover_files_ms: discover_ms,
741            file_count: files.len(),
742            workspaces_ms,
743            workspace_count: workspaces.len(),
744            plugins_ms,
745            script_analysis_ms: scripts_ms,
746            parse_extract_ms: parse_ms,
747            module_count: modules.len(),
748            cache_hits,
749            cache_misses,
750            cache_update_ms: cache_ms,
751            entry_points_ms,
752            entry_point_count: entry_points.all.len(),
753            resolve_imports_ms: resolve_ms,
754            build_graph_ms: graph_ms,
755            analyze_ms,
756            duplication_ms: None,
757            total_ms,
758        })
759    } else {
760        None
761    };
762
763    Ok(AnalysisOutput {
764        results: result,
765        timings,
766        graph: if retain { Some(graph) } else { None },
767        modules: if retain_modules { Some(modules) } else { None },
768        files: if retain_modules {
769            Some(files.to_vec())
770        } else {
771            None
772        },
773        script_used_packages: plugin_result.script_used_packages,
774    })
775}
776
777/// Analyze package.json scripts from root and all workspace packages.
778///
779/// Populates the plugin result with script-used packages and config file
780/// entry patterns. Also scans CI config files for binary invocations.
781fn load_root_package_json(config: &ResolvedConfig) -> Option<PackageJson> {
782    PackageJson::load(&config.root.join("package.json")).ok()
783}
784
785fn load_workspace_packages(
786    workspaces: &[fallow_config::WorkspaceInfo],
787) -> Vec<LoadedWorkspacePackage<'_>> {
788    workspaces
789        .iter()
790        .filter_map(|ws| {
791            PackageJson::load(&ws.root.join("package.json"))
792                .ok()
793                .map(|pkg| (ws, pkg))
794        })
795        .collect()
796}
797
798fn analyze_all_scripts(
799    config: &ResolvedConfig,
800    workspaces: &[fallow_config::WorkspaceInfo],
801    root_pkg: Option<&PackageJson>,
802    workspace_pkgs: &[LoadedWorkspacePackage<'_>],
803    plugin_result: &mut plugins::AggregatedPluginResult,
804) {
805    // Collect all dependency names to build the bin-name → package-name reverse map.
806    // This resolves binaries like "attw" to "@arethetypeswrong/cli" even without
807    // node_modules/.bin symlinks.
808    let mut all_dep_names: Vec<String> = Vec::new();
809    if let Some(pkg) = root_pkg {
810        all_dep_names.extend(pkg.all_dependency_names());
811    }
812    for (_, ws_pkg) in workspace_pkgs {
813        all_dep_names.extend(ws_pkg.all_dependency_names());
814    }
815    all_dep_names.sort_unstable();
816    all_dep_names.dedup();
817
818    // Probe node_modules/ at project root and each workspace root so non-hoisted
819    // deps (pnpm strict, Yarn workspaces) are also discovered.
820    let mut nm_roots: Vec<&std::path::Path> = Vec::new();
821    if config.root.join("node_modules").is_dir() {
822        nm_roots.push(&config.root);
823    }
824    for ws in workspaces {
825        if ws.root.join("node_modules").is_dir() {
826            nm_roots.push(&ws.root);
827        }
828    }
829    let bin_map = scripts::build_bin_to_package_map(&nm_roots, &all_dep_names);
830
831    if let Some(pkg) = root_pkg
832        && let Some(ref pkg_scripts) = pkg.scripts
833    {
834        let scripts_to_analyze = if config.production {
835            scripts::filter_production_scripts(pkg_scripts)
836        } else {
837            pkg_scripts.clone()
838        };
839        let script_analysis = scripts::analyze_scripts(&scripts_to_analyze, &config.root, &bin_map);
840        plugin_result.script_used_packages = script_analysis.used_packages;
841
842        for config_file in &script_analysis.config_files {
843            plugin_result
844                .discovered_always_used
845                .push((config_file.clone(), "scripts".to_string()));
846        }
847        for entry in &script_analysis.entry_files {
848            if let Some(pat) = scripts::normalize_script_entry_pattern("", entry) {
849                plugin_result
850                    .entry_patterns
851                    .push((plugins::PathRule::new(pat), "scripts".to_string()));
852            }
853        }
854    }
855    for (ws, ws_pkg) in workspace_pkgs {
856        if let Some(ref ws_scripts) = ws_pkg.scripts {
857            let scripts_to_analyze = if config.production {
858                scripts::filter_production_scripts(ws_scripts)
859            } else {
860                ws_scripts.clone()
861            };
862            let ws_analysis = scripts::analyze_scripts(&scripts_to_analyze, &ws.root, &bin_map);
863            plugin_result
864                .script_used_packages
865                .extend(ws_analysis.used_packages);
866
867            let ws_prefix = ws
868                .root
869                .strip_prefix(&config.root)
870                .unwrap_or(&ws.root)
871                .to_string_lossy();
872            for config_file in &ws_analysis.config_files {
873                plugin_result
874                    .discovered_always_used
875                    .push((format!("{ws_prefix}/{config_file}"), "scripts".to_string()));
876            }
877            for entry in &ws_analysis.entry_files {
878                if let Some(pat) = scripts::normalize_script_entry_pattern(&ws_prefix, entry) {
879                    plugin_result
880                        .entry_patterns
881                        .push((plugins::PathRule::new(pat), "scripts".to_string()));
882                }
883            }
884        }
885    }
886
887    // Scan CI config files for binary invocations and positional file references.
888    // Returns both packages used by CI tooling AND project-relative file paths
889    // referenced as command-line arguments (e.g., `node scripts/deploy.ts` in a
890    // GitHub Actions `run:` block) so the referenced files become reachable
891    // entry points. CI files always live at the project root, so file paths
892    // need no workspace-prefix transformation. See issue #195 (Case D).
893    let ci_analysis = scripts::ci::analyze_ci_files(&config.root, &bin_map);
894    plugin_result
895        .script_used_packages
896        .extend(ci_analysis.used_packages);
897    for entry in &ci_analysis.entry_files {
898        if let Some(pat) = scripts::normalize_script_entry_pattern("", entry) {
899            plugin_result
900                .entry_patterns
901                .push((plugins::PathRule::new(pat), "scripts".to_string()));
902        }
903    }
904    plugin_result
905        .entry_point_roles
906        .entry("scripts".to_string())
907        .or_insert(EntryPointRole::Support);
908}
909
910/// Discover all entry points from static patterns, workspaces, plugins, and infrastructure.
911fn discover_all_entry_points(
912    config: &ResolvedConfig,
913    files: &[discover::DiscoveredFile],
914    workspaces: &[fallow_config::WorkspaceInfo],
915    root_pkg: Option<&PackageJson>,
916    workspace_pkgs: &[LoadedWorkspacePackage<'_>],
917    plugin_result: &plugins::AggregatedPluginResult,
918) -> discover::CategorizedEntryPoints {
919    let mut entry_points = discover::CategorizedEntryPoints::default();
920    let root_discovery = discover::discover_entry_points_with_warnings_from_pkg(
921        config,
922        files,
923        root_pkg,
924        workspaces.is_empty(),
925    );
926
927    let workspace_pkg_by_root: rustc_hash::FxHashMap<std::path::PathBuf, &PackageJson> =
928        workspace_pkgs
929            .iter()
930            .map(|(ws, pkg)| (ws.root.clone(), pkg))
931            .collect();
932
933    let workspace_discovery: Vec<discover::EntryPointDiscovery> = workspaces
934        .par_iter()
935        .map(|ws| {
936            let pkg = workspace_pkg_by_root.get(&ws.root).copied();
937            discover::discover_workspace_entry_points_with_warnings_from_pkg(&ws.root, files, pkg)
938        })
939        .collect();
940    let mut skipped_entries = rustc_hash::FxHashMap::default();
941    entry_points.extend_runtime(root_discovery.entries);
942    for (path, count) in root_discovery.skipped_entries {
943        *skipped_entries.entry(path).or_insert(0) += count;
944    }
945    let mut ws_entries = Vec::new();
946    for workspace in workspace_discovery {
947        ws_entries.extend(workspace.entries);
948        for (path, count) in workspace.skipped_entries {
949            *skipped_entries.entry(path).or_insert(0) += count;
950        }
951    }
952    discover::warn_skipped_entry_summary(&skipped_entries);
953    entry_points.extend_runtime(ws_entries);
954
955    let plugin_entries = discover::discover_plugin_entry_point_sets(plugin_result, config, files);
956    entry_points.extend(plugin_entries);
957
958    let infra_entries = discover::discover_infrastructure_entry_points(&config.root);
959    entry_points.extend_runtime(infra_entries);
960
961    // Add dynamically loaded files from config as entry points
962    if !config.dynamically_loaded.is_empty() {
963        let dynamic_entries = discover::discover_dynamically_loaded_entry_points(config, files);
964        entry_points.extend_runtime(dynamic_entries);
965    }
966
967    entry_points.dedup()
968}
969
970/// Summarize entry points by source category for user-facing output.
971fn summarize_entry_points(entry_points: &[discover::EntryPoint]) -> results::EntryPointSummary {
972    let mut counts: rustc_hash::FxHashMap<String, usize> = rustc_hash::FxHashMap::default();
973    for ep in entry_points {
974        let category = match &ep.source {
975            discover::EntryPointSource::PackageJsonMain
976            | discover::EntryPointSource::PackageJsonModule
977            | discover::EntryPointSource::PackageJsonExports
978            | discover::EntryPointSource::PackageJsonBin
979            | discover::EntryPointSource::PackageJsonScript => "package.json",
980            discover::EntryPointSource::Plugin { .. } => "plugin",
981            discover::EntryPointSource::TestFile => "test file",
982            discover::EntryPointSource::DefaultIndex => "default index",
983            discover::EntryPointSource::ManualEntry => "manual entry",
984            discover::EntryPointSource::InfrastructureConfig => "config",
985            discover::EntryPointSource::DynamicallyLoaded => "dynamically loaded",
986        };
987        *counts.entry(category.to_string()).or_insert(0) += 1;
988    }
989    let mut by_source: Vec<(String, usize)> = counts.into_iter().collect();
990    by_source.sort_by(|a, b| b.1.cmp(&a.1).then_with(|| a.0.cmp(&b.0)));
991    results::EntryPointSummary {
992        total: entry_points.len(),
993        by_source,
994    }
995}
996
997/// Run plugins for root project and all workspace packages.
998fn run_plugins(
999    config: &ResolvedConfig,
1000    files: &[discover::DiscoveredFile],
1001    workspaces: &[fallow_config::WorkspaceInfo],
1002    root_pkg: Option<&PackageJson>,
1003    workspace_pkgs: &[LoadedWorkspacePackage<'_>],
1004) -> plugins::AggregatedPluginResult {
1005    let registry = plugins::PluginRegistry::new(config.external_plugins.clone());
1006    let file_paths: Vec<std::path::PathBuf> = files.iter().map(|f| f.path.clone()).collect();
1007    let root_config_search_roots = collect_config_search_roots(&config.root, &file_paths);
1008    let root_config_search_root_refs: Vec<&Path> = root_config_search_roots
1009        .iter()
1010        .map(std::path::PathBuf::as_path)
1011        .collect();
1012
1013    // Run plugins for root project (full run with external plugins, inline config, etc.)
1014    let mut result = root_pkg.map_or_else(plugins::AggregatedPluginResult::default, |pkg| {
1015        registry.run_with_search_roots(
1016            pkg,
1017            &config.root,
1018            &file_paths,
1019            &root_config_search_root_refs,
1020            config.production,
1021        )
1022    });
1023
1024    if workspaces.is_empty() {
1025        return result;
1026    }
1027
1028    let root_active_plugins: rustc_hash::FxHashSet<&str> =
1029        result.active_plugins.iter().map(String::as_str).collect();
1030
1031    // Pre-compile config matchers once and bucket source files by workspace.
1032    // Workspace config matching can then scan only files below that workspace
1033    // instead of every project file for every active matcher.
1034    let precompiled_matchers = registry.precompile_config_matchers();
1035    let workspace_relative_files = bucket_files_by_workspace(workspace_pkgs, &file_paths);
1036
1037    // Run plugins for each workspace package in parallel, then merge results.
1038    let ws_results: Vec<_> = workspace_pkgs
1039        .par_iter()
1040        .zip(workspace_relative_files.par_iter())
1041        .filter_map(|((ws, ws_pkg), relative_files)| {
1042            let ws_result = registry.run_workspace_fast(
1043                ws_pkg,
1044                &ws.root,
1045                &config.root,
1046                &precompiled_matchers,
1047                relative_files,
1048                &root_active_plugins,
1049                config.production,
1050            );
1051            if ws_result.active_plugins.is_empty() {
1052                return None;
1053            }
1054            let ws_prefix = ws
1055                .root
1056                .strip_prefix(&config.root)
1057                .unwrap_or(&ws.root)
1058                .to_string_lossy()
1059                .into_owned();
1060            Some((ws_result, ws_prefix))
1061        })
1062        .collect();
1063
1064    // Merge workspace results sequentially (deterministic order via par_iter index stability)
1065    // Track seen names for O(1) dedup instead of O(n) Vec::contains
1066    let mut seen_plugins: rustc_hash::FxHashSet<String> =
1067        result.active_plugins.iter().cloned().collect();
1068    let mut seen_prefixes: rustc_hash::FxHashSet<String> =
1069        result.virtual_module_prefixes.iter().cloned().collect();
1070    let mut seen_generated: rustc_hash::FxHashSet<String> =
1071        result.generated_import_patterns.iter().cloned().collect();
1072    let mut seen_suffixes: rustc_hash::FxHashSet<String> =
1073        result.virtual_package_suffixes.iter().cloned().collect();
1074
1075    fn extend_unique(
1076        target: &mut Vec<String>,
1077        seen: &mut rustc_hash::FxHashSet<String>,
1078        items: Vec<String>,
1079    ) {
1080        for item in items {
1081            if seen.insert(item.clone()) {
1082                target.push(item);
1083            }
1084        }
1085    }
1086    for (ws_result, ws_prefix) in ws_results {
1087        // Prefix helper: workspace-relative patterns need the workspace prefix
1088        // to be matchable from the monorepo root. But patterns that are already
1089        // project-root-relative (e.g., from angular.json which uses absolute paths
1090        // like "apps/client/src/styles.css") should not be double-prefixed.
1091        let prefix_if_needed = |pat: &str| -> String {
1092            if pat.starts_with(ws_prefix.as_str()) || pat.starts_with('/') {
1093                pat.to_string()
1094            } else {
1095                format!("{ws_prefix}/{pat}")
1096            }
1097        };
1098
1099        for (rule, pname) in &ws_result.entry_patterns {
1100            result
1101                .entry_patterns
1102                .push((rule.prefixed(&ws_prefix), pname.clone()));
1103        }
1104        for (plugin_name, role) in ws_result.entry_point_roles {
1105            result.entry_point_roles.entry(plugin_name).or_insert(role);
1106        }
1107        for (pat, pname) in &ws_result.always_used {
1108            result
1109                .always_used
1110                .push((prefix_if_needed(pat), pname.clone()));
1111        }
1112        for (pat, pname) in &ws_result.discovered_always_used {
1113            result
1114                .discovered_always_used
1115                .push((prefix_if_needed(pat), pname.clone()));
1116        }
1117        for (pat, pname) in &ws_result.fixture_patterns {
1118            result
1119                .fixture_patterns
1120                .push((prefix_if_needed(pat), pname.clone()));
1121        }
1122        for rule in &ws_result.used_exports {
1123            result.used_exports.push(rule.prefixed(&ws_prefix));
1124        }
1125        // Merge active plugin names (deduplicated via HashSet)
1126        for plugin_name in ws_result.active_plugins {
1127            if !seen_plugins.contains(&plugin_name) {
1128                seen_plugins.insert(plugin_name.clone());
1129                result.active_plugins.push(plugin_name);
1130            }
1131        }
1132        // These don't need prefixing (absolute paths / package names)
1133        result
1134            .referenced_dependencies
1135            .extend(ws_result.referenced_dependencies);
1136        result.setup_files.extend(ws_result.setup_files);
1137        result
1138            .tooling_dependencies
1139            .extend(ws_result.tooling_dependencies);
1140        // Virtual import boundaries — prefixes (e.g., Docusaurus `@theme/`),
1141        // generated import patterns (e.g., SvelteKit `/$types`), and package-name
1142        // suffixes (e.g., Vitest `/__mocks__`) — match against import specifiers
1143        // or package names, never file paths, so no workspace prefix is applied.
1144        extend_unique(
1145            &mut result.virtual_module_prefixes,
1146            &mut seen_prefixes,
1147            ws_result.virtual_module_prefixes,
1148        );
1149        extend_unique(
1150            &mut result.generated_import_patterns,
1151            &mut seen_generated,
1152            ws_result.generated_import_patterns,
1153        );
1154        extend_unique(
1155            &mut result.virtual_package_suffixes,
1156            &mut seen_suffixes,
1157            ws_result.virtual_package_suffixes,
1158        );
1159        // Path aliases from workspace plugins (e.g., SvelteKit $lib/ → src/lib).
1160        // Prefix the replacement directory so it resolves from the monorepo root.
1161        for (prefix, replacement) in ws_result.path_aliases {
1162            result
1163                .path_aliases
1164                .push((prefix, format!("{ws_prefix}/{replacement}")));
1165        }
1166    }
1167
1168    result
1169}
1170
1171fn bucket_files_by_workspace(
1172    workspace_pkgs: &[LoadedWorkspacePackage<'_>],
1173    file_paths: &[std::path::PathBuf],
1174) -> Vec<Vec<(std::path::PathBuf, String)>> {
1175    let mut buckets = vec![Vec::new(); workspace_pkgs.len()];
1176
1177    for file_path in file_paths {
1178        for (idx, (ws, _)) in workspace_pkgs.iter().enumerate() {
1179            if let Ok(relative) = file_path.strip_prefix(&ws.root) {
1180                buckets[idx].push((file_path.clone(), relative.to_string_lossy().into_owned()));
1181                break;
1182            }
1183        }
1184    }
1185
1186    buckets
1187}
1188
1189fn collect_config_search_roots(
1190    root: &Path,
1191    file_paths: &[std::path::PathBuf],
1192) -> Vec<std::path::PathBuf> {
1193    let mut roots: rustc_hash::FxHashSet<std::path::PathBuf> = rustc_hash::FxHashSet::default();
1194    roots.insert(root.to_path_buf());
1195
1196    for file_path in file_paths {
1197        let mut current = file_path.parent();
1198        while let Some(dir) = current {
1199            if !dir.starts_with(root) {
1200                break;
1201            }
1202            roots.insert(dir.to_path_buf());
1203            if dir == root {
1204                break;
1205            }
1206            current = dir.parent();
1207        }
1208    }
1209
1210    let mut roots_vec: Vec<_> = roots.into_iter().collect();
1211    roots_vec.sort();
1212    roots_vec
1213}
1214
1215/// Run analysis on a project directory (with export usages for LSP Code Lens).
1216///
1217/// # Errors
1218///
1219/// Returns an error if config loading, file discovery, parsing, or analysis fails.
1220pub fn analyze_project(root: &Path) -> Result<AnalysisResults, FallowError> {
1221    let config = default_config(root);
1222    analyze_with_usages(&config)
1223}
1224
1225/// Create a default config for a project root.
1226///
1227/// `analyze_project` is the dead-code entry point used by the LSP and other
1228/// programmatic embedders. When the loaded config uses the per-analysis
1229/// production form (`production: { deadCode: true, ... }`), the production
1230/// flag must be flattened to the dead-code analysis here. Otherwise
1231/// `ResolvedConfig::resolve` calls `.global()` which returns false for the
1232/// per-analysis variant and the production-mode rule overrides
1233/// (`unused_dev_dependencies: off`, etc.) plus `resolved.production = true`
1234/// are silently dropped.
1235pub(crate) fn default_config(root: &Path) -> ResolvedConfig {
1236    let user_config = fallow_config::FallowConfig::find_and_load(root)
1237        .ok()
1238        .flatten();
1239    match user_config {
1240        Some((mut config, _path)) => {
1241            let dead_code_production = config
1242                .production
1243                .for_analysis(fallow_config::ProductionAnalysis::DeadCode);
1244            config.production = dead_code_production.into();
1245            config.resolve(
1246                root.to_path_buf(),
1247                fallow_config::OutputFormat::Human,
1248                num_cpus(),
1249                false,
1250                true, // quiet: LSP/programmatic callers don't need progress bars
1251            )
1252        }
1253        None => fallow_config::FallowConfig::default().resolve(
1254            root.to_path_buf(),
1255            fallow_config::OutputFormat::Human,
1256            num_cpus(),
1257            false,
1258            true,
1259        ),
1260    }
1261}
1262
1263fn num_cpus() -> usize {
1264    std::thread::available_parallelism().map_or(4, std::num::NonZeroUsize::get)
1265}
1266
1267#[cfg(test)]
1268mod tests {
1269    use super::{
1270        bucket_files_by_workspace, collect_config_search_roots, format_undeclared_workspace_warning,
1271    };
1272    use std::path::{Path, PathBuf};
1273
1274    use fallow_config::WorkspaceDiagnostic;
1275
1276    fn diag(root: &Path, relative: &str) -> WorkspaceDiagnostic {
1277        WorkspaceDiagnostic {
1278            path: root.join(relative),
1279            message: String::new(),
1280        }
1281    }
1282
1283    #[test]
1284    fn undeclared_workspace_warning_is_singular_for_one_path() {
1285        let root = Path::new("/repo");
1286        let warning = format_undeclared_workspace_warning(root, &[diag(root, "packages/api")])
1287            .expect("warning should be rendered");
1288
1289        assert_eq!(
1290            warning,
1291            "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."
1292        );
1293    }
1294
1295    #[test]
1296    fn undeclared_workspace_warning_summarizes_many_paths() {
1297        let root = PathBuf::from("/repo");
1298        let diagnostics = [
1299            "examples/a",
1300            "examples/b",
1301            "examples/c",
1302            "examples/d",
1303            "examples/e",
1304            "examples/f",
1305        ]
1306        .into_iter()
1307        .map(|path| diag(&root, path))
1308        .collect::<Vec<_>>();
1309
1310        let warning = format_undeclared_workspace_warning(&root, &diagnostics)
1311            .expect("warning should be rendered");
1312
1313        assert_eq!(
1314            warning,
1315            "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."
1316        );
1317    }
1318
1319    #[test]
1320    fn collect_config_search_roots_includes_file_ancestors_once() {
1321        let root = PathBuf::from("/repo");
1322        let search_roots = collect_config_search_roots(
1323            &root,
1324            &[
1325                root.join("apps/query/src/main.ts"),
1326                root.join("packages/shared/lib/index.ts"),
1327            ],
1328        );
1329
1330        assert_eq!(
1331            search_roots,
1332            vec![
1333                root.clone(),
1334                root.join("apps"),
1335                root.join("apps/query"),
1336                root.join("apps/query/src"),
1337                root.join("packages"),
1338                root.join("packages/shared"),
1339                root.join("packages/shared/lib"),
1340            ]
1341        );
1342    }
1343
1344    #[test]
1345    fn bucket_files_by_workspace_uses_workspace_relative_paths() {
1346        let root = PathBuf::from("/repo");
1347        let ui = fallow_config::WorkspaceInfo {
1348            root: root.join("apps/ui"),
1349            name: "ui".to_string(),
1350            is_internal_dependency: false,
1351        };
1352        let api = fallow_config::WorkspaceInfo {
1353            root: root.join("apps/api"),
1354            name: "api".to_string(),
1355            is_internal_dependency: false,
1356        };
1357        let workspace_pkgs = vec![
1358            (
1359                &ui,
1360                fallow_config::PackageJson {
1361                    name: Some("ui".to_string()),
1362                    ..Default::default()
1363                },
1364            ),
1365            (
1366                &api,
1367                fallow_config::PackageJson {
1368                    name: Some("api".to_string()),
1369                    ..Default::default()
1370                },
1371            ),
1372        ];
1373        let files = vec![
1374            root.join("apps/ui/vite.config.ts"),
1375            root.join("apps/ui/src/main.ts"),
1376            root.join("apps/api/src/server.ts"),
1377            root.join("tools/build.ts"),
1378        ];
1379
1380        let buckets = bucket_files_by_workspace(&workspace_pkgs, &files);
1381
1382        assert_eq!(
1383            buckets[0],
1384            vec![
1385                (
1386                    root.join("apps/ui/vite.config.ts"),
1387                    "vite.config.ts".to_string()
1388                ),
1389                (root.join("apps/ui/src/main.ts"), "src/main.ts".to_string()),
1390            ]
1391        );
1392        assert_eq!(
1393            buckets[1],
1394            vec![(
1395                root.join("apps/api/src/server.ts"),
1396                "src/server.ts".to_string()
1397            )]
1398        );
1399    }
1400}