fallow-engine 3.48.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
//! Project config resolution owned by the engine boundary.

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

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

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

#[cfg(any(test, feature = "testing"))]
mod direct_load;
mod loader;

#[cfg(any(test, feature = "testing"))]
pub(crate) use direct_load::default_project_config;
#[cfg(test)]
pub(crate) use direct_load::{
    ProjectConfigOptions, config_for_project_analysis, config_for_project_analysis_with_env,
    config_for_project_with_env,
};
#[cfg(any(test, feature = "testing"))]
pub use direct_load::{
    config_for_project, config_for_project_with_load_options, config_for_project_with_production,
};

#[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 a config load without a resolved run: the global
/// `--production` override and one override per analysis
/// (`--production-dead-code`, `--production-health`, `--production-dupes`).
///
/// The precedence is the precedence of the resolver without environment
/// variables: the flag of the analysis, then the global override, then the
/// config. A run of a surface decides the production mode in
/// [`crate::resolved_run`]; these flags serve the loads of
/// [`ConfigFile::for_analysis`] that have no run.
#[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()
    }
}

/// The resolved config of one mode entry, with the config file path and the
/// workspace metadata of the load. A session holds it. Outside this crate
/// only tests and benchmarks (feature `testing`) see the type.
mod project {
    use std::path::PathBuf;

    use fallow_config::{ResolvedConfig, WorkspaceDiagnostic, WorkspaceInfo};

    use super::DuplicationScope;

    /// 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,
    }
}

#[cfg(any(test, feature = "testing"))]
pub use project::ProjectConfig;
#[cfg(not(any(test, feature = "testing")))]
pub(crate) use project::ProjectConfig;

/// 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>,
    },
}

/// 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 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,
        }
    }
}

/// 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)
        })
}

#[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:?}");
        }
    }
}