mod audit_base;
mod env_policy;
mod notices;
#[cfg(any(test, feature = "testing"))]
pub mod parity;
mod pick;
mod resolve;
pub mod steps;
#[cfg(any(test, feature = "testing"))]
pub mod testing;
mod type_aware;
mod view;
pub use audit_base::{AuditBaseDecision, decide_audit_base};
pub use resolve::{
BaseConfig, BaseSide, EmptyCause, InputField, InputProblem, ResolveError, resolve_base_run,
resolve_base_run_with_head_decisions, resolve_run,
};
pub use type_aware::{TypeAwareConfigInput, TypeAwareSettings, decide_type_aware};
use std::path::{Path, PathBuf};
use fallow_config::{
AnalysisSelection, CommandKind, ConfigKey, EnvSnapshot, EnvVar, FallowConfig, PlannedAnalysis,
ResolvedConfig, SkipReason, Surface, WorkspaceInfo,
};
use fallow_types::output_format::OutputFormat;
use fallow_types::sourced::Sourced;
pub use pick::{PerAnalysis, ProductionInputs, decide_production};
use crate::project_config::{
ConfigChainSnapshot, DuplicationScope, ProductionModes, ResolveOptions,
};
use crate::session::AnalysisSession;
use crate::{EngineError, EngineResult};
use steps::{GivenSettings, ModeEntry};
#[derive(Debug)]
pub struct ResolvedRun {
surface: Surface,
command: CommandKind,
root: Sourced<PathBuf>,
config_file: Sourced<Option<PathBuf>>,
config_snapshot: ConfigChainSnapshot,
file: FallowConfig,
dead_code: AnalysisDecision,
duplication: AnalysisDecision,
health: AnalysisDecision,
off: Option<ModeEntry>,
on: Option<ModeEntry>,
settings: RunSettings,
type_aware: type_aware::TypeAwareDecision,
given: GivenSettings,
env: EnvSnapshot,
env_read: Vec<EnvVar>,
env_ignored: Vec<EnvVar>,
fallback: Option<ResolveError>,
notices: Vec<fallow_config::WorkspaceDiagnostic>,
inputs: Box<fallow_config::RunInputs>,
}
#[derive(Debug)]
struct AnalysisDecision {
selection: Sourced<AnalysisSelection>,
production: Sourced<bool>,
}
#[derive(Debug)]
pub struct RunSettings {
pub threads: Sourced<usize>,
pub no_cache: Sourced<bool>,
pub allow_remote_extends: Sourced<bool>,
pub max_file_size_bytes: Sourced<Option<u64>>,
pub cache_dir: Sourced<PathBuf>,
pub cache_max_size_mb: Sourced<Option<u32>>,
pub package_baselines: Sourced<bool>,
pub include_entry_exports: Sourced<bool>,
pub output: OutputFormat,
pub quiet: bool,
}
impl RunSettings {
const fn resolve_options(&self) -> ResolveOptions {
ResolveOptions {
output: self.output,
threads: *self.threads.value(),
no_cache: *self.no_cache.value(),
quiet: self.quiet,
}
}
}
pub enum AnalysisSlot<'run> {
Runs(Runnable<'run>),
Skipped {
reason: &'run Sourced<AnalysisSelection>,
},
}
#[derive(Clone, Copy)]
pub struct Runnable<'run> {
run: &'run ResolvedRun,
analysis: PlannedAnalysis,
}
const fn config_key_name(key: ConfigKey) -> &'static str {
match key {
ConfigKey::AuditDeadCode => "audit.deadCode",
ConfigKey::AuditHealth => "audit.health",
ConfigKey::AuditDupes => "audit.dupes",
ConfigKey::DuplicatesEnabled => "duplicates.enabled",
}
}
fn skip_key(selection: AnalysisSelection) -> Option<ConfigKey> {
match selection {
AnalysisSelection::Skipped(SkipReason::Config(key)) => Some(key),
AnalysisSelection::Run | AnalysisSelection::Skipped(SkipReason::NotRequested) => None,
}
}
impl ResolvedRun {
pub const fn surface(&self) -> Surface {
self.surface
}
pub const fn command(&self) -> CommandKind {
self.command
}
pub const fn root(&self) -> &Sourced<PathBuf> {
&self.root
}
pub const fn config_file(&self) -> &Sourced<Option<PathBuf>> {
&self.config_file
}
pub const fn config(&self) -> &FallowConfig {
&self.file
}
const fn decision(&self, analysis: PlannedAnalysis) -> &AnalysisDecision {
match analysis {
PlannedAnalysis::DeadCode => &self.dead_code,
PlannedAnalysis::Duplication => &self.duplication,
PlannedAnalysis::Health => &self.health,
}
}
pub fn analysis(&self, analysis: PlannedAnalysis) -> AnalysisSlot<'_> {
let decision = self.decision(analysis);
if decision.selection.value().runs() {
AnalysisSlot::Runs(Runnable {
run: self,
analysis,
})
} else {
AnalysisSlot::Skipped {
reason: &decision.selection,
}
}
}
pub const fn production(&self, analysis: PlannedAnalysis) -> &Sourced<bool> {
&self.decision(analysis).production
}
pub fn production_modes(&self) -> ProductionModes {
ProductionModes {
dead_code: *self.dead_code.production.value(),
health: *self.health.production.value(),
dupes: *self.duplication.production.value(),
}
}
pub const fn settings(&self) -> &RunSettings {
&self.settings
}
pub fn type_aware(&self) -> Result<&TypeAwareSettings, ResolveError> {
self.type_aware.result()
}
#[must_use]
fn audit_base(&self) -> AuditBaseDecision {
decide_audit_base(&self.inputs)
}
pub const fn env(&self) -> &EnvSnapshot {
&self.env
}
#[cfg(test)]
fn env_read(&self) -> &[EnvVar] {
&self.env_read
}
pub fn env_ignored(&self) -> &[EnvVar] {
&self.env_ignored
}
#[cfg(test)]
const fn fell_back(&self) -> bool {
self.fallback.is_some()
}
pub const fn fallback_error(&self) -> Option<&ResolveError> {
self.fallback.as_ref()
}
pub fn notices(&self) -> &[fallow_config::WorkspaceDiagnostic] {
&self.notices
}
pub fn sources_changed(&self) -> bool {
self.config_snapshot.changed()
|| [self.off.as_ref(), self.on.as_ref()]
.into_iter()
.flatten()
.any(|entry| entry.inputs.snapshot() != entry.inputs_before_resolve)
}
pub fn plugin_readiness(&self) -> Vec<fallow_config::ConfiguredPluginDiagnostic> {
fallow_config::diagnose_configured_external_plugins(self.root.value(), &self.file.plugins)
}
fn entry(&self, production: bool) -> Option<&ModeEntry> {
if production {
self.on.as_ref()
} else {
self.off.as_ref()
}
}
}
impl<'run> Runnable<'run> {
pub const fn analysis(&self) -> PlannedAnalysis {
self.analysis
}
pub const fn production(&self) -> &'run Sourced<bool> {
self.run.production(self.analysis)
}
pub const fn run(&self) -> &'run ResolvedRun {
self.run
}
fn entry(&self) -> &'run ModeEntry {
let Some(entry) = self.run.entry(*self.production().value()) else {
unreachable!("the resolver makes a mode entry for each analysis that runs");
};
entry
}
pub fn config(&self) -> &'run ResolvedConfig {
&self.entry().config
}
pub fn workspaces(&self) -> &'run [WorkspaceInfo] {
&self.entry().workspaces
}
pub fn workspace_diagnostics(&self) -> &'run [fallow_config::WorkspaceDiagnostic] {
&self.entry().workspace_diagnostics
}
#[cfg(test)]
fn shares_files_with(&self, other: Runnable<'_>) -> bool {
self.production().value() == other.production().value()
}
pub fn open_session(&self) -> AnalysisSession {
self.session_from(self.entry().config.clone())
}
pub fn open_session_adjusted(
&self,
adjust: impl FnOnce(&mut ResolvedConfig),
) -> EngineResult<AnalysisSession> {
let decided = &self.entry().config;
let mut config = decided.clone();
adjust(&mut config);
if let Some(field) = changed_decided_field(decided, &config) {
return Err(EngineError::new(format!(
"a command adjustment changed the resolved field `{field}`"
)));
}
Ok(self.session_from(config))
}
fn session_from(&self, config: ResolvedConfig) -> AnalysisSession {
let entry = self.entry();
let project = ModeEntry {
config,
inputs: entry.inputs.clone(),
inputs_before_resolve: entry.inputs_before_resolve.clone(),
workspaces: entry.workspaces.clone(),
workspace_diagnostics: entry.workspace_diagnostics.clone(),
workspace_discovery_ms: entry.workspace_discovery_ms,
}
.into_project_config(
self.run.config_file.value().clone(),
self.duplication_scope(),
);
AnalysisSession::from_project_config(project)
}
fn duplication_scope(&self) -> DuplicationScope {
let dupes = *self.run.duplication.production.value();
if dupes == *self.production().value() {
return DuplicationScope::SameAsSession;
}
let mut config = self.run.file.clone();
steps::flatten_production(&mut config, self.run.config_file.value().is_some(), dupes);
DuplicationScope::Own {
config: Box::new(config),
options: self.run.settings.resolve_options(),
env: Box::new(self.run.given.env_for_steps(&self.run.env)),
given: Box::new(self.run.given.clone()),
}
}
}
fn changed_decided_field(
decided: &ResolvedConfig,
adjusted: &ResolvedConfig,
) -> Option<&'static str> {
let checks: [(&'static str, bool); 12] = [
("root", decided.root == adjusted.root),
("production", decided.production == adjusted.production),
(
"production_ignore",
decided.production_ignore == adjusted.production_ignore,
),
(
"ignore_patterns",
decided
.ignore_patterns
.same_globs(&adjusted.ignore_patterns),
),
(
"workspace_patterns",
decided.workspace_patterns == adjusted.workspace_patterns,
),
(
"max_file_size_bytes",
decided.max_file_size_bytes == adjusted.max_file_size_bytes,
),
("cache_dir", decided.cache_dir == adjusted.cache_dir),
(
"cache_max_size_mb",
decided.cache_max_size_mb == adjusted.cache_max_size_mb,
),
("no_cache", decided.no_cache == adjusted.no_cache),
("threads", decided.threads == adjusted.threads),
(
"include_entry_exports",
decided.include_entry_exports == adjusted.include_entry_exports,
),
(
"workspace_changed_since",
decided.workspace_changed_since == adjusted.workspace_changed_since,
),
];
checks
.into_iter()
.find_map(|(field, same)| (!same).then_some(field))
}
#[must_use]
pub fn available_threads() -> usize {
threads_or_one(std::thread::available_parallelism())
}
fn threads_or_one(parallelism: std::io::Result<std::num::NonZeroUsize>) -> usize {
parallelism.map_or(1, std::num::NonZeroUsize::get)
}
fn join_cwd(path: &Path, cwd: Option<&Path>) -> Option<PathBuf> {
if path.is_absolute() {
Some(path.to_path_buf())
} else {
cwd.map(|cwd| cwd.join(path))
}
}
#[cfg(test)]
#[path = "resolve_tests.rs"]
mod resolve_tests;
#[cfg(test)]
#[path = "run_tests.rs"]
mod run_tests;
#[cfg(test)]
#[path = "base_tests.rs"]
mod base_tests;