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