fallow_engine/resolved_run/
mod.rs1mod audit_base;
10mod env_policy;
11mod notices;
12#[cfg(any(test, feature = "testing"))]
13pub mod parity;
14mod pick;
15mod resolve;
16pub mod steps;
17#[cfg(any(test, feature = "testing"))]
18pub mod testing;
19mod type_aware;
20mod view;
21
22pub use audit_base::{AuditBaseDecision, decide_audit_base};
23pub use resolve::{
24 BaseConfig, BaseSide, EmptyCause, InputField, InputProblem, ResolveError, resolve_base_run,
25 resolve_base_run_with_head_decisions, resolve_run,
26};
27pub use type_aware::{TypeAwareConfigInput, TypeAwareSettings, decide_type_aware};
28
29use std::path::{Path, PathBuf};
30
31use fallow_config::{
32 AnalysisSelection, CommandKind, ConfigKey, EnvSnapshot, EnvVar, FallowConfig, PlannedAnalysis,
33 ResolvedConfig, SkipReason, Surface, WorkspaceInfo,
34};
35use fallow_types::output_format::OutputFormat;
36use fallow_types::sourced::Sourced;
37
38pub use pick::{PerAnalysis, ProductionInputs, decide_production};
39
40use crate::project_config::{
41 ConfigChainSnapshot, DuplicationScope, ProductionModes, ResolveOptions,
42};
43use crate::session::AnalysisSession;
44use crate::{EngineError, EngineResult};
45use steps::{GivenSettings, ModeEntry};
46
47#[derive(Debug)]
51pub struct ResolvedRun {
52 surface: Surface,
53 command: CommandKind,
54 root: Sourced<PathBuf>,
55 config_file: Sourced<Option<PathBuf>>,
56 config_snapshot: ConfigChainSnapshot,
58 file: FallowConfig,
60 dead_code: AnalysisDecision,
61 duplication: AnalysisDecision,
62 health: AnalysisDecision,
63 off: Option<ModeEntry>,
65 on: Option<ModeEntry>,
67 settings: RunSettings,
68 type_aware: type_aware::TypeAwareDecision,
71 given: GivenSettings,
73 env: EnvSnapshot,
76 env_read: Vec<EnvVar>,
77 env_ignored: Vec<EnvVar>,
78 fallback: Option<ResolveError>,
80 notices: Vec<fallow_config::WorkspaceDiagnostic>,
82 inputs: Box<fallow_config::RunInputs>,
84}
85
86#[derive(Debug)]
88struct AnalysisDecision {
89 selection: Sourced<AnalysisSelection>,
90 production: Sourced<bool>,
91}
92
93#[derive(Debug)]
95pub struct RunSettings {
96 pub threads: Sourced<usize>,
98 pub no_cache: Sourced<bool>,
100 pub allow_remote_extends: Sourced<bool>,
102 pub max_file_size_bytes: Sourced<Option<u64>>,
104 pub cache_dir: Sourced<PathBuf>,
106 pub cache_max_size_mb: Sourced<Option<u32>>,
108 pub package_baselines: Sourced<bool>,
110 pub include_entry_exports: Sourced<bool>,
112 pub output: OutputFormat,
114 pub quiet: bool,
116}
117
118impl RunSettings {
119 const fn resolve_options(&self) -> ResolveOptions {
120 ResolveOptions {
121 output: self.output,
122 threads: *self.threads.value(),
123 no_cache: *self.no_cache.value(),
124 quiet: self.quiet,
125 }
126 }
127}
128
129pub enum AnalysisSlot<'run> {
131 Runs(Runnable<'run>),
133 Skipped {
135 reason: &'run Sourced<AnalysisSelection>,
137 },
138}
139
140#[derive(Clone, Copy)]
143pub struct Runnable<'run> {
144 run: &'run ResolvedRun,
145 analysis: PlannedAnalysis,
146}
147
148const fn config_key_name(key: ConfigKey) -> &'static str {
150 match key {
151 ConfigKey::AuditDeadCode => "audit.deadCode",
152 ConfigKey::AuditHealth => "audit.health",
153 ConfigKey::AuditDupes => "audit.dupes",
154 ConfigKey::DuplicatesEnabled => "duplicates.enabled",
155 }
156}
157
158fn skip_key(selection: AnalysisSelection) -> Option<ConfigKey> {
160 match selection {
161 AnalysisSelection::Skipped(SkipReason::Config(key)) => Some(key),
162 AnalysisSelection::Run | AnalysisSelection::Skipped(SkipReason::NotRequested) => None,
163 }
164}
165
166impl ResolvedRun {
167 pub const fn surface(&self) -> Surface {
169 self.surface
170 }
171
172 pub const fn command(&self) -> CommandKind {
174 self.command
175 }
176
177 pub const fn root(&self) -> &Sourced<PathBuf> {
179 &self.root
180 }
181
182 pub const fn config_file(&self) -> &Sourced<Option<PathBuf>> {
184 &self.config_file
185 }
186
187 pub const fn config(&self) -> &FallowConfig {
189 &self.file
190 }
191
192 const fn decision(&self, analysis: PlannedAnalysis) -> &AnalysisDecision {
193 match analysis {
194 PlannedAnalysis::DeadCode => &self.dead_code,
195 PlannedAnalysis::Duplication => &self.duplication,
196 PlannedAnalysis::Health => &self.health,
197 }
198 }
199
200 pub fn analysis(&self, analysis: PlannedAnalysis) -> AnalysisSlot<'_> {
202 let decision = self.decision(analysis);
203 if decision.selection.value().runs() {
204 AnalysisSlot::Runs(Runnable {
205 run: self,
206 analysis,
207 })
208 } else {
209 AnalysisSlot::Skipped {
210 reason: &decision.selection,
211 }
212 }
213 }
214
215 pub const fn production(&self, analysis: PlannedAnalysis) -> &Sourced<bool> {
217 &self.decision(analysis).production
218 }
219
220 pub fn production_modes(&self) -> ProductionModes {
222 ProductionModes {
223 dead_code: *self.dead_code.production.value(),
224 health: *self.health.production.value(),
225 dupes: *self.duplication.production.value(),
226 }
227 }
228
229 pub const fn settings(&self) -> &RunSettings {
231 &self.settings
232 }
233
234 pub fn type_aware(&self) -> Result<&TypeAwareSettings, ResolveError> {
243 self.type_aware.result()
244 }
245
246 #[must_use]
249 fn audit_base(&self) -> AuditBaseDecision {
250 decide_audit_base(&self.inputs)
251 }
252
253 pub const fn env(&self) -> &EnvSnapshot {
256 &self.env
257 }
258
259 #[cfg(test)]
261 fn env_read(&self) -> &[EnvVar] {
262 &self.env_read
263 }
264
265 pub fn env_ignored(&self) -> &[EnvVar] {
267 &self.env_ignored
268 }
269
270 #[cfg(test)]
273 const fn fell_back(&self) -> bool {
274 self.fallback.is_some()
275 }
276
277 pub const fn fallback_error(&self) -> Option<&ResolveError> {
281 self.fallback.as_ref()
282 }
283
284 pub fn notices(&self) -> &[fallow_config::WorkspaceDiagnostic] {
289 &self.notices
290 }
291
292 pub fn sources_changed(&self) -> bool {
297 self.config_snapshot.changed()
298 || [self.off.as_ref(), self.on.as_ref()]
299 .into_iter()
300 .flatten()
301 .any(|entry| entry.inputs.snapshot() != entry.inputs_before_resolve)
302 }
303
304 pub fn plugin_readiness(&self) -> Vec<fallow_config::ConfiguredPluginDiagnostic> {
307 fallow_config::diagnose_configured_external_plugins(self.root.value(), &self.file.plugins)
308 }
309
310 pub fn declared_external_plugins(&self) -> Vec<fallow_config::ExternalPluginDef> {
315 let mut plugins =
316 fallow_config::discover_external_plugins(self.root.value(), &self.file.plugins);
317 plugins.extend(self.file.framework.iter().cloned());
318 plugins
319 }
320
321 fn entry(&self, production: bool) -> Option<&ModeEntry> {
323 if production {
324 self.on.as_ref()
325 } else {
326 self.off.as_ref()
327 }
328 }
329}
330
331impl<'run> Runnable<'run> {
332 pub const fn analysis(&self) -> PlannedAnalysis {
334 self.analysis
335 }
336
337 pub const fn production(&self) -> &'run Sourced<bool> {
339 self.run.production(self.analysis)
340 }
341
342 pub const fn run(&self) -> &'run ResolvedRun {
344 self.run
345 }
346
347 fn entry(&self) -> &'run ModeEntry {
348 let Some(entry) = self.run.entry(*self.production().value()) else {
349 unreachable!("the resolver makes a mode entry for each analysis that runs");
350 };
351 entry
352 }
353
354 pub fn config(&self) -> &'run ResolvedConfig {
356 &self.entry().config
357 }
358
359 pub fn workspaces(&self) -> &'run [WorkspaceInfo] {
361 &self.entry().workspaces
362 }
363
364 pub fn workspace_diagnostics(&self) -> &'run [fallow_config::WorkspaceDiagnostic] {
366 &self.entry().workspace_diagnostics
367 }
368
369 #[cfg(test)]
372 fn shares_files_with(&self, other: Runnable<'_>) -> bool {
373 self.production().value() == other.production().value()
374 }
375
376 pub fn open_session(&self) -> AnalysisSession {
378 self.session_from(self.entry().config.clone())
379 }
380
381 pub fn open_session_adjusted(
390 &self,
391 adjust: impl FnOnce(&mut ResolvedConfig),
392 ) -> EngineResult<AnalysisSession> {
393 let decided = &self.entry().config;
394 let mut config = decided.clone();
395 adjust(&mut config);
396 if let Some(field) = changed_decided_field(decided, &config) {
397 return Err(EngineError::new(format!(
398 "a command adjustment changed the resolved field `{field}`"
399 )));
400 }
401 Ok(self.session_from(config))
402 }
403
404 fn session_from(&self, config: ResolvedConfig) -> AnalysisSession {
405 let entry = self.entry();
406 let project = ModeEntry {
407 config,
408 inputs: entry.inputs.clone(),
409 inputs_before_resolve: entry.inputs_before_resolve.clone(),
410 workspaces: entry.workspaces.clone(),
411 workspace_diagnostics: entry.workspace_diagnostics.clone(),
412 workspace_discovery_ms: entry.workspace_discovery_ms,
413 }
414 .into_project_config(
415 self.run.config_file.value().clone(),
416 self.duplication_scope(),
417 );
418 AnalysisSession::from_project_config(project)
419 }
420
421 fn duplication_scope(&self) -> DuplicationScope {
425 let dupes = *self.run.duplication.production.value();
426 if dupes == *self.production().value() {
427 return DuplicationScope::SameAsSession;
428 }
429 let mut config = self.run.file.clone();
430 steps::flatten_production(&mut config, self.run.config_file.value().is_some(), dupes);
431 DuplicationScope::Own {
432 config: Box::new(config),
433 options: self.run.settings.resolve_options(),
434 env: Box::new(self.run.given.env_for_steps(&self.run.env)),
435 given: Box::new(self.run.given.clone()),
436 }
437 }
438}
439
440fn changed_decided_field(
442 decided: &ResolvedConfig,
443 adjusted: &ResolvedConfig,
444) -> Option<&'static str> {
445 let checks: [(&'static str, bool); 12] = [
446 ("root", decided.root == adjusted.root),
447 ("production", decided.production == adjusted.production),
448 (
449 "production_ignore",
450 decided.production_ignore == adjusted.production_ignore,
451 ),
452 (
453 "ignore_patterns",
454 decided
455 .ignore_patterns
456 .same_globs(&adjusted.ignore_patterns),
457 ),
458 (
459 "workspace_patterns",
460 decided.workspace_patterns == adjusted.workspace_patterns,
461 ),
462 (
463 "max_file_size_bytes",
464 decided.max_file_size_bytes == adjusted.max_file_size_bytes,
465 ),
466 ("cache_dir", decided.cache_dir == adjusted.cache_dir),
467 (
468 "cache_max_size_mb",
469 decided.cache_max_size_mb == adjusted.cache_max_size_mb,
470 ),
471 ("no_cache", decided.no_cache == adjusted.no_cache),
472 ("threads", decided.threads == adjusted.threads),
473 (
474 "include_entry_exports",
475 decided.include_entry_exports == adjusted.include_entry_exports,
476 ),
477 (
478 "workspace_changed_since",
479 decided.workspace_changed_since == adjusted.workspace_changed_since,
480 ),
481 ];
482 checks
483 .into_iter()
484 .find_map(|(field, same)| (!same).then_some(field))
485}
486
487#[must_use]
490pub fn available_threads() -> usize {
491 threads_or_one(std::thread::available_parallelism())
492}
493
494fn threads_or_one(parallelism: std::io::Result<std::num::NonZeroUsize>) -> usize {
497 parallelism.map_or(1, std::num::NonZeroUsize::get)
498}
499
500fn join_cwd(path: &Path, cwd: Option<&Path>) -> Option<PathBuf> {
502 if path.is_absolute() {
503 Some(path.to_path_buf())
504 } else {
505 cwd.map(|cwd| cwd.join(path))
506 }
507}
508
509#[cfg(test)]
510#[path = "resolve_tests.rs"]
511mod resolve_tests;
512
513#[cfg(test)]
514#[path = "run_tests.rs"]
515mod run_tests;
516
517#[cfg(test)]
518#[path = "base_tests.rs"]
519mod base_tests;