slopguard-core 0.1.36

Core analysis engine for slopguard
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
//! Top-level scan orchestration: collect files, fan out per-file scans (cached
//! or not), assemble cross-file findings, and shape the [`ScanResult`].

use std::collections::{HashMap, HashSet};
use std::fs;
use std::path::{Path, PathBuf};

use ast_grep_language::SupportLang;
use log::debug;
use rayon::prelude::*;

use crate::cache::{file_content_hash, rules_hash, CacheEntry, CacheKey, CacheStore};
use crate::config::Config;
use crate::cross_file::FileSymbols;
use crate::disable::unused_disable_findings;
use crate::finding::{CacheStats, Finding, ScanResult, ScanStats};
use crate::resolution::FileImports;
use crate::rule::{Rule, Severity};

use super::project::{append_cross_file, index_only_files};
use super::{
    append_resolution, build_glob_set, compile_rules, explicit_files, scan_file, walk_files,
    CompiledRules, FileScan, ScanError,
};

/// Count errors and warnings in one pass.
pub fn count_severities(findings: &[Finding]) -> (usize, usize) {
    let mut errors = 0;
    let mut warnings = 0;
    for finding in findings {
        match finding.severity {
            Severity::Error => errors += 1,
            Severity::Warning => warnings += 1,
        }
    }
    (errors, warnings)
}

/// Sort findings by location and drop duplicates from overlapping rules.
fn normalize_findings(findings: &mut Vec<Finding>) {
    findings.sort_by(|a, b| (&a.file, a.line, a.column).cmp(&(&b.file, b.line, b.column)));
    findings.dedup_by(|a, b| {
        a.rule_id == b.rule_id && a.file == b.file && a.line == b.line && a.column == b.column
    });
}

/// The files a scan reports on, plus the ones it only reads to complete the
/// cross-file index (see [`index_only_files`]).
struct ScanSet {
    files: Vec<(PathBuf, SupportLang)>,
    index_only: Vec<(PathBuf, SupportLang)>,
}

impl ScanSet {
    /// A directory walk of `paths`. File targets among them widen the index to
    /// their project.
    fn walk(
        paths: &[PathBuf],
        compiled: &CompiledRules,
        config: &Config,
    ) -> Result<Self, ScanError> {
        let ignores = build_glob_set(&config.scan.ignores)?;
        let files = walk_files(paths, &ignores);
        let index_only = index_only_files(compiled, paths, &files, &ignores);
        Ok(Self { files, index_only })
    }

    /// An explicit file list (`--diff`). Every file widens the index to its
    /// project.
    fn explicit(
        targets: &[PathBuf],
        compiled: &CompiledRules,
        config: &Config,
    ) -> Result<Self, ScanError> {
        let ignores = build_glob_set(&config.scan.ignores)?;
        let files = explicit_files(targets, &ignores);
        let index_only = index_only_files(compiled, targets, &files, &ignores);
        Ok(Self { files, index_only })
    }

    /// The paths whose findings are reported.
    fn reported(&self) -> HashSet<&Path> {
        self.files.iter().map(|(path, _)| path.as_path()).collect()
    }
}

/// Scan a collected file set without touching the cache.
///
/// Index-only files are parsed for their symbols alone: nothing of theirs is
/// reported, and nothing is stored.
fn scan_collected(set: &ScanSet, compiled: &CompiledRules, apply_disable: bool) -> ScanResult {
    let (per_file, project) = compiled.engines(apply_disable);
    let collect_symbols = project.iter().any(|e| e.needs_symbols());
    let collect_imports = !compiled.resolution.is_empty();
    let scans: Vec<(PathBuf, FileScan)> = set
        .files
        .par_iter()
        .map(|(path, lang)| {
            let scan = scan_file(
                path,
                *lang,
                &per_file,
                &compiled.test_paths,
                collect_symbols,
                collect_imports,
                apply_disable,
            );
            (path.clone(), scan)
        })
        .collect();

    let mut findings: Vec<Finding> = Vec::new();
    let mut contributions: Vec<(PathBuf, FileSymbols)> = Vec::new();
    let mut import_contributions: Vec<(PathBuf, FileImports)> = Vec::new();
    for (path, scan) in scans {
        findings.extend(scan.findings);
        if collect_symbols {
            contributions.push((path.clone(), scan.symbols));
        }
        if collect_imports {
            import_contributions.push((path, scan.imports));
        }
    }
    if collect_symbols {
        let test_paths = &compiled.test_paths;
        let extra: Vec<(PathBuf, FileSymbols)> = set
            .index_only
            .par_iter()
            .map(|(path, lang)| {
                let scan = scan_file(path, *lang, &[], test_paths, true, false, apply_disable);
                (path.clone(), scan.symbols)
            })
            .collect();
        contributions.extend(extra);
    }
    append_cross_file(&mut findings, &project, &contributions, &set.reported());
    append_resolution(
        &mut findings,
        compiled,
        &import_contributions,
        apply_disable,
    );
    normalize_findings(&mut findings);

    let (errors, warnings) = count_severities(&findings);

    ScanResult {
        findings,
        stats: ScanStats {
            errors,
            warnings,
            total: errors + warnings,
            files_scanned: set.files.len(),
            baseline_filtered: 0,
            diff_base: None,
            files_changed: None,
        },
        cache_stats: None,
    }
}

/// Scan a collected file set, reusing cached findings for files whose content
/// hash is unchanged.
///
/// Index-only files go through the cache like the others (a miss is scanned in
/// full and stored, which warms the cache for the next run), but only their
/// symbols are used and they are left out of the stats.
///
/// `prune` drops cache entries that no longer correspond to a scanned file. A
/// partial scan must pass `false`: it never saw the other files, so their
/// entries are still valid.
fn scan_collected_cached(
    set: &ScanSet,
    compiled: &CompiledRules,
    rules: &[Rule],
    cache_dir: &Path,
    key: CacheKey,
    prune: bool,
) -> ScanResult {
    // The cached path always applies disable comments: the raw pass used by
    // `--report-unused-disable` runs uncached, so the cache only ever stores
    // suppressed findings.
    let (per_file, project) = compiled.engines(true);
    let collect_symbols = project.iter().any(|e| e.needs_symbols());
    let collect_imports = !compiled.resolution.is_empty();
    let current_rules_hash = rules_hash(rules);
    let store =
        CacheStore::with_dir(cache_dir.to_path_buf(), key).scoped_to_rules(&current_rules_hash);
    let rules_changed = store
        .check_rules_changed(&current_rules_hash)
        .unwrap_or(true);

    struct FileWork<'a> {
        path: &'a Path,
        lang: SupportLang,
        hash: String,
        /// False for an index-only file: its findings are not reported.
        reported: bool,
    }

    let reported = set.files.iter().map(|file| (file, true));
    let index_only = set.index_only.iter().map(|file| (file, false));
    let work: Vec<FileWork> = reported
        .chain(index_only)
        .filter_map(|((path, lang), reported)| {
            let content = fs::read(path).ok()?;
            Some(FileWork {
                path,
                lang: *lang,
                hash: file_content_hash(&content),
                reported,
            })
        })
        .collect();

    let mut cached_count = 0usize;
    let mut changed_count = 0usize;
    let mut all_findings: Vec<Finding> = Vec::new();
    let mut contributions: Vec<(PathBuf, FileSymbols)> = Vec::new();
    let mut import_contributions: Vec<(PathBuf, FileImports)> = Vec::new();
    let mut take = |file: &FileWork, entry: CacheEntry| {
        if file.reported {
            all_findings.extend(entry.findings);
            if collect_imports {
                import_contributions.push((file.path.to_path_buf(), entry.imports));
            }
        }
        if collect_symbols {
            contributions.push((file.path.to_path_buf(), entry.symbols));
        }
    };

    let mut to_scan: Vec<&FileWork> = Vec::new();
    for file in &work {
        match (!rules_changed).then(|| store.get(&file.hash)).flatten() {
            Some(entry) => {
                cached_count += usize::from(file.reported);
                take(file, entry);
            }
            None => {
                changed_count += usize::from(file.reported);
                to_scan.push(file);
            }
        }
    }

    let scanned: Vec<(&FileWork, FileScan)> = to_scan
        .into_par_iter()
        .map(|file| {
            let scan = scan_file(
                file.path,
                file.lang,
                &per_file,
                &compiled.test_paths,
                collect_symbols,
                collect_imports,
                true,
            );
            (file, scan)
        })
        .collect();

    for (file, scan) in scanned {
        let entry = CacheEntry {
            findings: scan.findings,
            symbols: scan.symbols,
            imports: scan.imports,
        };
        store.put(&file.hash, &entry).ok();
        take(file, entry);
    }

    // Cross-file and resolution evaluation are never cached: they re-run on every
    // scan (cross-file from the assembled index, resolution from the manifests on
    // disk, so a manifest edit is reflected even for an unchanged file).
    append_cross_file(&mut all_findings, &project, &contributions, &set.reported());
    append_resolution(&mut all_findings, compiled, &import_contributions, true);

    if prune {
        let current_hashes: Vec<String> = work.iter().map(|file| file.hash.clone()).collect();
        store.cleanup(&current_hashes).ok();
    }

    normalize_findings(&mut all_findings);

    let (errors, warnings) = count_severities(&all_findings);

    let files_scanned = cached_count + changed_count;

    ScanResult {
        findings: all_findings,
        stats: ScanStats {
            errors,
            warnings,
            total: errors + warnings,
            files_scanned,
            baseline_filtered: 0,
            diff_base: None,
            files_changed: None,
        },
        cache_stats: Some(CacheStats {
            cached: cached_count,
            changed: changed_count,
        }),
    }
}

/// Scan the given paths for rule violations.
///
/// A file among `paths` (as the pre-commit hook passes them) is reported on its
/// own, but the cross-file index still covers its whole project.
pub fn scan(paths: &[PathBuf], rules: &[Rule], config: &Config) -> Result<ScanResult, ScanError> {
    let compiled = compile_rules(rules, config)?;
    let set = ScanSet::walk(paths, &compiled, config)?;
    Ok(scan_collected(&set, &compiled, true))
}

/// Scan with file-level caching. Files whose content hash matches a cached
/// entry (and whose rules have not changed) return cached findings without
/// reparsing.
///
/// `cache_dir` is the directory where cache files are stored, used as is (the
/// CLI defaults it to a per-project directory under the user cache, see
/// [`crate::cache::project_cache_dir`]). Entries are signed with the user's
/// [`CacheKey`]; when that key cannot be loaded or created, the scan runs
/// uncached rather than trusting unsigned entries.
///
/// Stale entries are pruned only when every path is a directory: a file target
/// (the pre-commit hook) is a partial scan and must keep the other entries.
pub fn scan_cached(
    paths: &[PathBuf],
    rules: &[Rule],
    config: &Config,
    cache_dir: &Path,
) -> Result<ScanResult, ScanError> {
    match CacheKey::load_or_create_default() {
        Ok(key) => scan_cached_with_key(paths, rules, config, cache_dir, key),
        Err(err) => {
            debug!("cache key unavailable ({err}), scanning without cache");
            scan(paths, rules, config)
        }
    }
}

/// [`scan_cached`] with an explicit signing key instead of the user's.
pub fn scan_cached_with_key(
    paths: &[PathBuf],
    rules: &[Rule],
    config: &Config,
    cache_dir: &Path,
    key: CacheKey,
) -> Result<ScanResult, ScanError> {
    let compiled = compile_rules(rules, config)?;
    let set = ScanSet::walk(paths, &compiled, config)?;
    let prune = paths.iter().all(|path| path.is_dir());
    Ok(scan_collected_cached(
        &set, &compiled, rules, cache_dir, key, prune,
    ))
}

/// Scan an explicit list of files instead of walking directories.
///
/// Used by `--diff`, where git already produced the exact file set. Paths that
/// no longer exist or that slopguard cannot parse are silently skipped.
///
/// Cross-file rules still see the whole project: the index is completed from
/// the project each file belongs to, and only the findings located in the
/// given files are reported.
pub fn scan_files(
    files: &[PathBuf],
    rules: &[Rule],
    config: &Config,
) -> Result<ScanResult, ScanError> {
    let compiled = compile_rules(rules, config)?;
    let set = ScanSet::explicit(files, &compiled, config)?;
    Ok(scan_collected(&set, &compiled, true))
}

/// Compute `unused-disable` findings for the given walked paths: a full raw scan
/// (disable comments not applied) whose findings reveal which directives would
/// have suppressed something. Cross-file rules run, as in [`scan`].
///
/// This is the walk (non-diff) variant used by `--report-unused-disable`. It is
/// uncached on purpose: the cache stores suppressed findings, which cannot tell
/// which directives were consumed.
pub fn scan_unused_disables(
    paths: &[PathBuf],
    rules: &[Rule],
    config: &Config,
) -> Result<Vec<Finding>, ScanError> {
    let compiled = compile_rules(rules, config)?;
    let set = ScanSet::walk(paths, &compiled, config)?;
    let raw = scan_collected(&set, &compiled, false);
    Ok(unused_from_raw(&set.files, &raw.findings))
}

/// Explicit-file (diff) variant of [`scan_unused_disables`]. Cross-file rules
/// run over the whole project, as in [`scan_files`], and only the changed files
/// are checked for unused directives.
pub fn scan_files_unused_disables(
    files: &[PathBuf],
    rules: &[Rule],
    config: &Config,
) -> Result<Vec<Finding>, ScanError> {
    let compiled = compile_rules(rules, config)?;
    let set = ScanSet::explicit(files, &compiled, config)?;
    let raw = scan_collected(&set, &compiled, false);
    Ok(unused_from_raw(&set.files, &raw.findings))
}

/// Group raw findings by file and, for each collected file, report the disable
/// directives that suppressed nothing. Files that cannot be read are skipped.
fn unused_from_raw(files: &[(PathBuf, SupportLang)], raw: &[Finding]) -> Vec<Finding> {
    let mut by_file: HashMap<&Path, Vec<Finding>> = HashMap::new();
    for finding in raw {
        by_file
            .entry(finding.file.as_path())
            .or_default()
            .push(finding.clone());
    }
    let empty: Vec<Finding> = Vec::new();
    let mut out = Vec::new();
    for (path, _lang) in files {
        let Ok(source) = fs::read_to_string(path) else {
            continue;
        };
        let file_raw = by_file.get(path.as_path()).unwrap_or(&empty);
        out.extend(unused_disable_findings(path, &source, file_raw));
    }
    out
}

/// Cached variant of [`scan_files`]. Cache pruning is skipped: a partial scan
/// must not evict the entries of files it did not look at. Like
/// [`scan_cached`], it runs uncached when the user's [`CacheKey`] is
/// unavailable.
pub fn scan_files_cached(
    files: &[PathBuf],
    rules: &[Rule],
    config: &Config,
    cache_dir: &Path,
) -> Result<ScanResult, ScanError> {
    match CacheKey::load_or_create_default() {
        Ok(key) => scan_files_cached_with_key(files, rules, config, cache_dir, key),
        Err(err) => {
            debug!("cache key unavailable ({err}), scanning without cache");
            scan_files(files, rules, config)
        }
    }
}

/// [`scan_files_cached`] with an explicit signing key instead of the user's.
pub fn scan_files_cached_with_key(
    files: &[PathBuf],
    rules: &[Rule],
    config: &Config,
    cache_dir: &Path,
    key: CacheKey,
) -> Result<ScanResult, ScanError> {
    let compiled = compile_rules(rules, config)?;
    let set = ScanSet::explicit(files, &compiled, config)?;
    Ok(scan_collected_cached(
        &set, &compiled, rules, cache_dir, key, false,
    ))
}