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