fallow-engine 3.45.0

Typed analysis engine facade for fallow consumers
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
//! Project config resolution owned by the engine boundary.

use std::path::{Path, PathBuf};

use fallow_config::{
    ConfigLoadOptions, EnvSnapshot, FallowConfig, ProductionAnalysis, ResolvedConfig,
    WorkspaceDiagnostic, WorkspaceInfo,
};
use fallow_types::output_format::OutputFormat;
use fallow_types::sourced::{Given, Origin};

use crate::EngineResult;
use crate::resolved_run::steps::{
    GivenSettings, ModeEntry, flatten_production, mode_entry, mode_entry_lossy,
    validate_boundaries_and_rule_packs, validate_config,
};
pub(crate) use crate::resolved_run::steps::{collect_workspace_metadata, resolve_with_env_from};
use crate::resolved_run::{PerAnalysis, ProductionInputs, decide_production};

mod loader;

#[cfg(any(test, feature = "testing"))]
pub use loader::load_count;
pub use loader::{
    ConfigChainSnapshot, ConfigFile, ConfigFileOptions, ConfigLoadError, find_config_file,
    load_config_file,
};

/// The production flags of one run: the global `--production` override and one
/// override per analysis (`--production-dead-code`, `--production-health`,
/// `--production-dupes`).
///
/// Every command and surface that runs more than one analysis reads the
/// production mode of each analysis from here, so the precedence has one
/// implementation: the flag of the analysis, then the global override, then
/// the config.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct ProductionFlags {
    /// The global override. The CLI sets `Some(true)` for `--production` and
    /// `None` without it. The programmatic API can also pass `Some(false)`.
    pub global: Option<bool>,
    /// Override for the dead-code analysis.
    pub dead_code: Option<bool>,
    /// Override for the health analysis.
    pub health: Option<bool>,
    /// Override for the duplication analysis.
    pub dupes: Option<bool>,
}

impl ProductionFlags {
    /// The flags of a CLI run, where `--production` can only switch the mode
    /// on.
    #[must_use]
    pub const fn from_cli(
        production: bool,
        dead_code: Option<bool>,
        health: Option<bool>,
        dupes: Option<bool>,
    ) -> Self {
        Self {
            global: if production { Some(true) } else { None },
            dead_code,
            health,
            dupes,
        }
    }

    /// The production override of a command that runs one analysis
    /// (`dead-code`, `dupes`, `health`): its own override, else
    /// `--production`. `None` means that the config decides.
    #[must_use]
    pub const fn single_analysis_override(production: bool, own: Option<bool>) -> Option<bool> {
        Self::from_cli(production, own, None, None).override_for(ProductionAnalysis::DeadCode)
    }

    /// The override flag of one analysis, without the global override.
    #[must_use]
    const fn own(self, analysis: ProductionAnalysis) -> Option<bool> {
        match analysis {
            ProductionAnalysis::DeadCode => self.dead_code,
            ProductionAnalysis::Health => self.health,
            ProductionAnalysis::Dupes => self.dupes,
        }
    }

    /// The production override of one analysis: its own flag, else the global
    /// override. `None` means that the config decides.
    #[must_use]
    pub const fn override_for(self, analysis: ProductionAnalysis) -> Option<bool> {
        match self.own(analysis) {
            Some(value) => Some(value),
            None => self.global,
        }
    }

    /// The production mode of one analysis as far as the flags decide, before
    /// the config is loaded. A missing flag reads as `false`.
    #[must_use]
    pub const fn mode(self, analysis: ProductionAnalysis) -> bool {
        match self.override_for(analysis) {
            Some(value) => value,
            None => false,
        }
    }

    /// The production mode of each analysis as far as the flags decide.
    #[must_use]
    pub const fn modes(self) -> ProductionModes {
        ProductionModes {
            dead_code: self.mode(ProductionAnalysis::DeadCode),
            health: self.mode(ProductionAnalysis::Health),
            dupes: self.mode(ProductionAnalysis::Dupes),
        }
    }

    /// The effective production mode of one analysis: the flags first, then
    /// the `production` setting of the config.
    #[must_use]
    pub fn effective(
        self,
        analysis: ProductionAnalysis,
        config: &fallow_config::ProductionConfig,
    ) -> bool {
        *decide_production(&self.as_inputs(), config, None)
            .get(analysis)
            .value()
    }

    /// The effective production mode of each analysis.
    #[cfg(test)]
    #[must_use]
    pub(crate) fn effective_modes(
        self,
        config: &fallow_config::ProductionConfig,
    ) -> ProductionModes {
        let decided = decide_production(&self.as_inputs(), config, None);
        ProductionModes {
            dead_code: decided.dead_code.into_value(),
            health: decided.health.into_value(),
            dupes: decided.dupes.into_value(),
        }
    }

    /// The override of each analysis from `self`, else from `fallback`. The
    /// parity tests use it to put the flags before the environment
    /// variables. The config decides only when neither source sets a
    /// value. `fallback` has the rank of the environment variables in
    /// `decide_production`.
    #[cfg(test)]
    #[must_use]
    pub(crate) fn with_fallback(self, fallback: Self) -> Self {
        use fallow_types::sourced::{Layer, Sourced};

        let inputs = ProductionInputs {
            global_env: fallback.global,
            own_env: PerAnalysis {
                dead_code: fallback.dead_code,
                health: fallback.health,
                dupes: fallback.dupes,
            },
            ..self.as_inputs()
        };
        let decided = decide_production(&inputs, &fallow_config::ProductionConfig::default(), None);
        let given = |decision: &Sourced<bool>| {
            (decision.source().layer() != Layer::Default).then_some(*decision.value())
        };
        Self {
            global: None,
            dead_code: given(&decided.dead_code),
            health: given(&decided.health),
            dupes: given(&decided.dupes),
        }
    }

    /// The flags as caller-given production inputs, without environment
    /// variables.
    fn as_inputs(self) -> ProductionInputs {
        let given = |value: Option<bool>, name| {
            value.map(|value| Given {
                value,
                origin: Origin::ApiOption(name),
            })
        };
        ProductionInputs {
            global_flag: given(self.global, "ProductionFlags.global"),
            own_flag: PerAnalysis {
                dead_code: given(self.dead_code, "ProductionFlags.dead_code"),
                health: given(self.health, "ProductionFlags.health"),
                dupes: given(self.dupes, "ProductionFlags.dupes"),
            },
            global_env: None,
            own_env: PerAnalysis::default(),
        }
    }
}

/// The production mode of each analysis of one run.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct ProductionModes {
    /// Production mode of the dead-code analysis.
    pub dead_code: bool,
    /// Production mode of the health analysis.
    pub health: bool,
    /// Production mode of the duplication analysis.
    pub dupes: bool,
}

impl ProductionModes {
    /// The production mode of one analysis.
    #[must_use]
    pub const fn for_analysis(self, analysis: ProductionAnalysis) -> bool {
        match analysis {
            ProductionAnalysis::DeadCode => self.dead_code,
            ProductionAnalysis::Health => self.health,
            ProductionAnalysis::Dupes => self.dupes,
        }
    }

    /// Whether health can reuse the dead-code parse: both run in the same mode.
    #[must_use]
    pub const fn dead_code_matches_health(self) -> bool {
        self.dead_code == self.health
    }

    /// Whether duplication can reuse the dead-code files: both run in the same
    /// mode.
    #[must_use]
    pub const fn dead_code_matches_dupes(self) -> bool {
        self.dead_code == self.dupes
    }

    /// Whether all three analyses run in the same mode.
    #[must_use]
    pub const fn all_match(self) -> bool {
        self.dead_code_matches_health() && self.dead_code_matches_dupes()
    }
}

/// Resolved project config plus the config file path when one was loaded.
#[derive(Debug)]
pub struct ProjectConfig {
    /// Fully resolved config for the project.
    pub config: ResolvedConfig,
    /// Path of the loaded config file; `None` when defaults were used.
    pub path: Option<PathBuf>,
    /// Workspace metadata discovered under the project root.
    pub workspaces: Vec<WorkspaceInfo>,
    /// Diagnostics from workspace discovery (undeclared or invalid members).
    pub workspace_diagnostics: Vec<WorkspaceDiagnostic>,
    /// Workspace discovery wall time in milliseconds, when measured.
    pub(crate) workspace_discovery_ms: Option<f64>,
    /// The plugin files, rule packs and `autoDiscover` directories that
    /// config resolution read.
    pub inputs: fallow_config::ConfigInputs,
    /// The content of [`Self::inputs`] just before resolution read them.
    pub(crate) inputs_before_resolve: fallow_config::ConfigInputsSnapshot,
    /// The files that duplication analyzes.
    pub(crate) duplication: DuplicationScope,
}

/// The files that duplication analyzes.
#[derive(Debug, Clone)]
pub(crate) enum DuplicationScope {
    /// The production mode of duplication equals the mode of the session
    /// config, so duplication analyzes the files of the session.
    SameAsSession,
    /// The production mode of duplication differs from the mode of the
    /// session config. The session resolves this config, which has the
    /// `production` setting of duplication, and walks its files on the first
    /// duplication call.
    Own {
        config: Box<FallowConfig>,
        options: ResolveOptions,
        env: Box<EnvSnapshot>,
        /// The settings that win over `env`. The session writes them on the
        /// config after it resolves the config with `env`.
        given: Box<GivenSettings>,
    },
}

impl DuplicationScope {
    /// The scope of a session config that is flattened for dead code. When
    /// the flags and the config give duplication another production mode,
    /// duplication gets a copy of the config flattened for its own mode.
    fn for_dead_code_session(
        file: &ConfigFile,
        flags: ProductionFlags,
        options: ResolveOptions,
        env: &EnvSnapshot,
    ) -> Self {
        let mode = |analysis| {
            if file.path.is_some() {
                flags.effective(analysis, &file.config.production)
            } else {
                flags.mode(analysis)
            }
        };
        if mode(ProductionAnalysis::Dupes) == mode(ProductionAnalysis::DeadCode) {
            return Self::SameAsSession;
        }
        let dupes = ConfigFile {
            config: file.config.clone(),
            path: file.path.clone(),
            chain: file.chain.clone(),
        }
        .for_analysis(ProductionAnalysis::Dupes, flags);
        Self::Own {
            config: Box::new(dupes.config),
            options,
            env: Box::new(env.clone()),
            given: Box::default(),
        }
    }
}

/// The run settings that resolution records on the config.
#[derive(Debug, Clone, Copy)]
pub struct ResolveOptions {
    /// Output format the resolved config carries into rendering.
    pub output: OutputFormat,
    /// Worker thread count recorded on the resolved config.
    pub threads: usize,
    /// Bypass the parse cache for this run.
    pub no_cache: bool,
    /// Suppress progress notes on stderr.
    pub quiet: bool,
}

impl ResolveOptions {
    /// The settings of a host that resolves a config without a CLI run.
    fn host_default() -> Self {
        Self {
            output: OutputFormat::Human,
            threads: std::thread::available_parallelism().map_or(1, std::num::NonZeroUsize::get),
            no_cache: false,
            quiet: true,
        }
    }
}

impl ConfigFile {
    /// Flatten the `production` setting of the config for one analysis: the
    /// flags decide first, then the config. A project without a config file
    /// gets the mode of the flags.
    #[must_use]
    pub fn for_analysis(mut self, analysis: ProductionAnalysis, flags: ProductionFlags) -> Self {
        let from_file = self.path.is_some();
        let on = if from_file {
            flags.effective(analysis, &self.config.production)
        } else {
            flags.mode(analysis)
        };
        flatten_production(&mut self.config, from_file, on);
        self
    }

    /// Validate the external plugins, the boundaries and the rule packs of
    /// the config.
    ///
    /// # Errors
    ///
    /// Returns an error that names the first invalid part and lists each of
    /// its problems.
    pub fn validate(&self, root: &Path) -> EngineResult<()> {
        validate_config(root, &self.config)
    }

    /// Resolve the config for `root`, then apply the environment variables
    /// that every host honors.
    #[must_use]
    pub fn resolve(self, root: &Path, options: ResolveOptions) -> ResolvedConfig {
        resolve_with_env(self.config, root, options)
    }
}

fn resolve_with_env(config: FallowConfig, root: &Path, options: ResolveOptions) -> ResolvedConfig {
    resolve_with_env_from(config, root, options, &EnvSnapshot::capture())
}

impl ModeEntry {
    /// The project config of this mode entry.
    pub(crate) fn into_project_config(
        self,
        path: Option<PathBuf>,
        duplication: DuplicationScope,
    ) -> ProjectConfig {
        ProjectConfig {
            config: self.config,
            path,
            workspaces: self.workspaces,
            workspace_diagnostics: self.workspace_diagnostics,
            workspace_discovery_ms: Some(self.workspace_discovery_ms),
            inputs: self.inputs,
            inputs_before_resolve: self.inputs_before_resolve,
            duplication,
        }
    }
}

/// Scalar config-loading knobs for one analysis family.
#[derive(Debug, Clone, Copy)]
pub struct ProjectConfigOptions {
    /// Output format the resolved config carries into rendering.
    pub output: OutputFormat,
    /// Bypass the parse cache for this run.
    pub no_cache: bool,
    /// Worker thread count recorded on the resolved config.
    pub threads: usize,
    /// Tri-state production override: `Some` forces production-only analysis
    /// on or off regardless of config, `None` defers to the config value.
    pub production_override: Option<bool>,
    /// Suppress progress notes on stderr.
    pub quiet: bool,
    /// Which analysis family's per-analysis production config to flatten.
    pub analysis: ProductionAnalysis,
    /// Permit `extends` config inheritance from remote URLs.
    pub allow_remote_extends: bool,
}

/// Resolved project configuration plus doctor-specific plugin diagnostics.
#[derive(Debug)]
#[non_exhaustive]
pub struct ProjectConfigReadiness {
    /// The same resolved configuration returned by [`config_for_project_analysis`].
    pub project: ProjectConfig,
    /// Unresolved resources named explicitly by the `plugins` config field.
    pub configured_plugin_diagnostics: Vec<fallow_config::ConfiguredPluginDiagnostic>,
}

/// Resolve the analysis config for a project.
///
/// # Errors
///
/// Returns an error when an explicit config cannot be loaded or automatic
/// config discovery finds an invalid config.
pub fn config_for_project(root: &Path, config_path: Option<&Path>) -> EngineResult<ProjectConfig> {
    config_for_project_with_load_options(root, config_path, ConfigLoadOptions::default())
}

/// Resolve project config with an explicit inheritance trust policy.
///
/// # Errors
///
/// Returns an error when config loading or validation fails.
pub fn config_for_project_with_load_options(
    root: &Path,
    config_path: Option<&Path>,
    load_options: ConfigLoadOptions,
) -> EngineResult<ProjectConfig> {
    config_for_project_with_production(root, config_path, load_options, ProductionFlags::default())
}

/// Resolve project config with an explicit inheritance trust policy and the
/// production flags of the run. The flags decide the production mode before
/// resolution, so the rules that production mode turns off are off, as with
/// the CLI `--production` flag. The config holds the production mode of dead
/// code. Duplication in a session of this config uses its own production mode,
/// as `fallow dupes` does.
///
/// # Errors
///
/// Returns an error when config loading or validation fails.
pub fn config_for_project_with_production(
    root: &Path,
    config_path: Option<&Path>,
    load_options: ConfigLoadOptions,
    production: ProductionFlags,
) -> EngineResult<ProjectConfig> {
    load_project_config(
        root,
        config_path,
        load_options,
        production,
        &EnvSnapshot::capture(),
    )
}

/// Load a project config as [`config_for_project_with_production`] does, with
/// the environment variables of `env` in place of the process environment.
#[cfg(test)]
pub(crate) fn config_for_project_with_env(
    root: &Path,
    config_path: Option<&Path>,
    env: &EnvSnapshot,
) -> EngineResult<ProjectConfig> {
    load_project_config(
        root,
        config_path,
        ConfigLoadOptions::default(),
        ProductionFlags::default(),
        env,
    )
}

fn load_project_config(
    root: &Path,
    config_path: Option<&Path>,
    load_options: ConfigLoadOptions,
    production: ProductionFlags,
    env: &EnvSnapshot,
) -> EngineResult<ProjectConfig> {
    let file = load_config_file(
        root,
        config_path,
        ConfigFileOptions {
            allow_remote_extends: load_options.allow_remote_extends,
        },
    )?;
    let options = ResolveOptions::host_default();
    let duplication = DuplicationScope::for_dead_code_session(&file, production, options, env);
    let ConfigFile { config, path, .. } =
        file.for_analysis(ProductionAnalysis::DeadCode, production);
    if path.is_some() {
        validate_boundaries_and_rule_packs(root, &config)?;
    }
    Ok(mode_entry(root, config, options, env)?.into_project_config(path, duplication))
}

impl ProjectConfig {
    /// Drop the workspace metadata of the load, so that a session built from
    /// this config discovers the workspaces again. A caller that changes the
    /// resolved config after the load uses this.
    #[must_use]
    pub fn without_workspace_metadata(mut self) -> Self {
        self.workspaces.clear();
        self.workspace_diagnostics.clear();
        self.workspace_discovery_ms = None;
        self
    }
}

/// Resolve the parse-cache size limit for a resolved config.
#[must_use]
pub(crate) fn resolve_cache_max_size_bytes(config: &ResolvedConfig) -> usize {
    config
        .cache_max_size_mb
        .map_or(fallow_extract::cache::DEFAULT_CACHE_MAX_SIZE, |mb| {
            (mb as usize).saturating_mul(1024 * 1024)
        })
}

pub(crate) fn default_project_config(root: &Path) -> ProjectConfig {
    mode_entry_lossy(
        root,
        FallowConfig::default(),
        ResolveOptions::host_default(),
        &EnvSnapshot::capture(),
    )
    .into_project_config(None, DuplicationScope::SameAsSession)
}

/// Resolve config for a specific analysis without depending on the CLI crate.
///
/// This mirrors the CLI's core config semantics: explicit production overrides
/// are applied before resolution, per-analysis production config is flattened
/// for the requested analysis, and boundary / external plugin / rule-pack
/// validation happens before the resolved config reaches the engine.
///
/// # Errors
///
/// Returns an engine error when config loading or validation fails.
pub fn config_for_project_analysis(
    root: &Path,
    config_path: Option<&Path>,
    options: ProjectConfigOptions,
) -> EngineResult<ProjectConfig> {
    resolve_project_config_analysis(root, config_path, options, &EnvSnapshot::capture())
        .map(|(project, _)| project)
}

/// [`config_for_project_analysis`] with the environment variables of `env`
/// in place of the process environment.
#[cfg(test)]
pub(crate) fn config_for_project_analysis_with_env(
    root: &Path,
    config_path: Option<&Path>,
    options: ProjectConfigOptions,
    env: &EnvSnapshot,
) -> EngineResult<ProjectConfig> {
    resolve_project_config_analysis(root, config_path, options, env).map(|(project, _)| project)
}

/// Resolve project configuration and collect typed readiness diagnostics for
/// plugin resources named explicitly by the user configuration.
///
/// # Errors
///
/// Returns an engine error when config loading or validation fails.
pub fn config_for_project_readiness(
    root: &Path,
    config_path: Option<&Path>,
    options: ProjectConfigOptions,
) -> EngineResult<ProjectConfigReadiness> {
    let (project, configured_plugin_paths) =
        resolve_project_config_analysis(root, config_path, options, &EnvSnapshot::capture())?;
    let configured_plugin_diagnostics =
        fallow_config::diagnose_configured_external_plugins(root, &configured_plugin_paths);
    Ok(ProjectConfigReadiness {
        project,
        configured_plugin_diagnostics,
    })
}

fn resolve_project_config_analysis(
    root: &Path,
    config_path: Option<&Path>,
    options: ProjectConfigOptions,
    env: &EnvSnapshot,
) -> EngineResult<(ProjectConfig, Vec<String>)> {
    let file = load_config_file(
        root,
        config_path,
        ConfigFileOptions {
            allow_remote_extends: options.allow_remote_extends,
        },
    )?
    .for_analysis(
        options.analysis,
        ProductionFlags {
            global: options.production_override,
            ..ProductionFlags::default()
        },
    );
    file.validate(root)?;
    let ConfigFile { config, path, .. } = file;
    let configured_plugin_paths = config.plugins.clone();
    let options = ResolveOptions {
        output: options.output,
        threads: options.threads,
        no_cache: options.no_cache,
        quiet: options.quiet,
    };
    let entry = mode_entry(root, config, options, env)?;
    // The caller names one analysis and owns the production mode of each
    // other analysis, so a session of this config runs duplication only when
    // the caller found that the modes match.
    let project = entry.into_project_config(path, DuplicationScope::SameAsSession);
    Ok((project, configured_plugin_paths))
}

#[cfg(test)]
mod tests {
    use super::*;
    use fallow_config::EnvVar;

    use crate::resolved_run::steps::apply_env_overrides_from;

    fn lookup_from(vars: &[(&str, &str)]) -> EnvSnapshot {
        EnvSnapshot::from_pairs(vars, None)
    }

    #[test]
    fn env_overrides_reach_every_host_config() {
        let root = Path::new("/repo");
        let mut config = default_project_config(root).config;

        apply_env_overrides_from(
            &mut config,
            &lookup_from(&[
                ("FALLOW_MAX_FILE_SIZE", "7"),
                ("FALLOW_CACHE_DIR", ".cache/fallow"),
                ("FALLOW_CACHE_MAX_SIZE", "64"),
            ]),
        );

        assert_eq!(
            config.max_file_size_bytes,
            fallow_config::resolve_max_file_size_bytes(Some(7))
        );
        assert_eq!(config.cache_dir, root.join(".cache/fallow"));
        assert_eq!(config.cache_max_size_mb, Some(64));
    }

    /// `FALLOW_CACHE_MAX_SIZE` gives the same limit when a host passes it to
    /// `resolve` and when the engine applies it after `resolve`.
    #[test]
    fn cache_max_size_env_gives_one_limit_before_and_after_resolve() {
        let root = Path::new("/repo");
        let resolve = |configured: Option<u32>, argument: Option<u32>| {
            FallowConfig {
                cache: fallow_config::CacheConfig {
                    dir: None,
                    max_size_mb: configured,
                },
                ..FallowConfig::default()
            }
            .resolve(
                root.to_path_buf(),
                OutputFormat::Human,
                1,
                false,
                true,
                argument,
            )
        };
        let values: [&[(&str, &str)]; 5] = [
            &[],
            &[("FALLOW_CACHE_MAX_SIZE", "64")],
            &[("FALLOW_CACHE_MAX_SIZE", " 8 ")],
            &[("FALLOW_CACHE_MAX_SIZE", "0")],
            &[("FALLOW_CACHE_MAX_SIZE", "many")],
        ];
        for configured in [None, Some(5)] {
            for vars in values {
                let argument = lookup_from(vars)
                    .get(EnvVar::CacheMaxSize)
                    .and_then(fallow_config::cache_max_size_from_env_value);
                let before = resolve(configured, argument);
                let mut after = resolve(configured, None);
                apply_env_overrides_from(&mut after, &lookup_from(vars));
                assert_eq!(
                    before.cache_max_size_mb, after.cache_max_size_mb,
                    "{configured:?} {vars:?}"
                );
            }
        }
    }

    #[test]
    fn invalid_or_empty_env_values_keep_the_config() {
        let root = Path::new("/repo");
        let mut config = default_project_config(root).config;
        let before_dir = config.cache_dir.clone();
        let before_max = config.cache_max_size_mb;

        apply_env_overrides_from(
            &mut config,
            &lookup_from(&[("FALLOW_CACHE_DIR", ""), ("FALLOW_CACHE_MAX_SIZE", "0")]),
        );

        assert_eq!(config.cache_dir, before_dir);
        assert_eq!(config.cache_max_size_mb, before_max);
    }

    #[test]
    fn package_baselines_env_turns_the_map_off_only_for_a_false_value() {
        let root = Path::new("/repo");
        let with_map = || {
            let mut config = default_project_config(root).config;
            config
                .workspace_changed_since
                .insert("packages/web".to_owned(), "main".to_owned());
            config
        };

        let mut disabled = with_map();
        apply_env_overrides_from(
            &mut disabled,
            &lookup_from(&[("FALLOW_PACKAGE_BASELINES", " Off ")]),
        );
        assert!(disabled.workspace_changed_since.is_empty());

        for value in ["true", "1", "maybe", ""] {
            let mut kept = with_map();
            apply_env_overrides_from(
                &mut kept,
                &lookup_from(&[("FALLOW_PACKAGE_BASELINES", value)]),
            );
            assert_eq!(kept.workspace_changed_since.len(), 1, "{value:?}");
        }
    }
}