skif-cli 0.9.0

Command-line interface for skif, a dependency and package-manager linter for JS/TS monorepos.
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
//! `skif --watch`: lint, then lint again whenever a file below the root changes (ADR 0013).
//!
//! The watcher polls: it lists the files below the root with their size and modification time and compares the
//! list with the previous one. A run starts once the tree has been quiet for [`QUIET`], so a checkout or a
//! formatter that writes many files causes one run.
//!
//! Polling costs one walk of the tree, one `stat` per file, every interval. The walk runs on at most [`SCAN_THREADS`]
//! threads, and the interval grows to twice the scan's own duration, so a scan runs at most about a third of the
//! time: on a large repository the watcher spends up to about a third of one core per walker thread scanning, at most
//! about four thirds of one core.
//!
//! The reports the run writes (`--output`, `--format FORMAT=FILE`) and the cache location are left out of the scan:
//! each run rewrites them, and seeing that write would start the next run, forever.
//!
//! The rounds share one cache (ADR 0007), pruned after each round to what it used: the cache store, read once and
//! written after each round, or with the cache off a module cache in memory.

use std::collections::{BTreeMap, BTreeSet};
use std::path::{Path, PathBuf};
use std::sync::{Mutex, PoisonError};
use std::time::{Duration, Instant, SystemTime};

use skif::{CachedFs, OsFs};

use crate::args::{Format, LintArgs};
use crate::cache::{CacheEnv, CacheHost, DiskHost};
use crate::env::Env;
use crate::lint::{self, canonical};
use crate::output::{stderr_line, write_stdout};
use crate::roots;

/// The shortest time between two scans; a slower scan stretches it to twice its own duration.
const POLL: Duration = Duration::from_millis(200);
/// How long the tree must stay unchanged after a change before the next run.
const QUIET: Duration = Duration::from_millis(100);
/// Directories that are never entered, whatever the ignore files say.
const SKIPPED: [&str; 3] = ["node_modules", ".git", "target"];
/// The most threads a scan walks the tree on; fewer when the machine has fewer cores.
const SCAN_THREADS: usize = 4;

/// Lints, then re-lints on every change, until the process is interrupted. Returns only when the root cannot be
/// resolved at all.
pub fn run(args: &LintArgs, env: Env) -> u8 {
    let dir = match watched_dir(args) {
        Ok(dir) => dir,
        Err(code) => return code,
    };
    let targets = match args.targets() {
        Ok(targets) => targets,
        Err(error) => return lint::run_failed(args, &error),
    };
    let clear = env.stdout_tty && targets.contains(&(Format::Human, None));
    let cache_env = CacheEnv::detect(env.ci);
    let mut written = args.written_files();
    written.extend(crate::cache::written_dirs(&args.cache_flags(), &cache_env));
    // One cache for every round, so a round parses only what changed: the cache store, read once and written after
    // each round, or with the cache off a module cache in memory.
    let modules = DiskHost::new(args.cache_flags(), cache_env, args.debug).keeping_memory();
    let mut snapshot = scan(&dir, &written);
    lint_round(args, env, clear, &dir, &modules);
    let mut debouncer = Debouncer::new(QUIET);
    let mut last_scan = Duration::ZERO;
    loop {
        std::thread::sleep(POLL.max(last_scan * 2));
        let started = Instant::now();
        let next = scan(&dir, &written);
        last_scan = started.elapsed();
        let changed = next != snapshot;
        snapshot = next;
        if debouncer.observe(changed, Instant::now()) {
            lint_round(args, env, clear, &dir, &modules);
        }
    }
}

fn lint_round(args: &LintArgs, env: Env, clear: bool, dir: &Path, modules: &dyn CacheHost) {
    if clear {
        let _ = write_stdout(b"\x1b[2J\x1b[H");
    }
    // The watcher keeps running between rounds, so every round frees what it built.
    lint::run_with_cache(args, env, lint::Teardown::Free, Some(modules));
    // The watcher only stops when interrupted, so the timings of each round are written as it ends.
    skif_core::timing::flush_stderr();
    stderr_line(&format!(
        "skif: watching {} for changes; press Ctrl-C to stop",
        roots::display(dir)
    ));
}

/// The directory the report's paths are relative to: the workspace root, or the invocation directory of a
/// multi-root run. When the root cannot be loaded yet (an invalid `skif.toml`), the path itself is watched, so the
/// user can fix the file and see the next run.
///
/// The directory is what [`roots::resolve`] gives as the base, found without discovering the roots of a multi-root
/// run: its base is the invocation directory whatever roots it finds, and the first round discovers them.
fn watched_dir(args: &LintArgs) -> Result<PathBuf, u8> {
    let start = match canonical(&args.path) {
        Ok(start) => start,
        Err(error) => {
            // The first run reports the failure in the requested format.
            return Err(lint::run_failed(args, &error));
        }
    };
    let fallback = if start.is_dir() {
        start.clone()
    } else {
        start.parent().map_or_else(|| start.clone(), Path::to_path_buf)
    };
    if args.recursive {
        return Ok(fallback);
    }
    Ok(skif::find_target(&start, &CachedFs::new(OsFs), &lint::overrides(args)).map_or(fallback, |target| target.root))
}

/// What a scan saw of one file.
type Stamp = (Option<SystemTime>, u64);

/// Every file below `dir` with its modification time and size. `.gitignore` files are honoured, the [`SKIPPED`]
/// directories are never entered, and the `written` files and everything below a `written` directory (canonical
/// paths, as [`LintArgs::written_files`] gives them) are left out; so is a file that vanishes during the scan.
///
/// The walk runs on up to [`SCAN_THREADS`] threads, each collecting what it sees; the map is ordered, so the result
/// does not depend on which thread saw which file.
fn scan(dir: &Path, written: &BTreeSet<PathBuf>) -> BTreeMap<PathBuf, Stamp> {
    let collected = Mutex::new(Vec::new());
    walker(dir).build_parallel().visit(&mut Collector {
        written,
        collected: &collected,
    });
    collected
        .into_inner()
        .unwrap_or_else(PoisonError::into_inner)
        .into_iter()
        .flatten()
        .collect()
}

/// The walk [`scan`] runs: hidden files included, `.gitignore` honoured without a repository or the global ignore
/// file, and the [`SKIPPED`] directories never entered; a parallel walk runs on [`scan_threads`] threads.
fn walker(dir: &Path) -> ignore::WalkBuilder {
    let mut builder = ignore::WalkBuilder::new(dir);
    builder
        .hidden(false)
        .require_git(false)
        .git_global(false)
        .threads(scan_threads())
        .filter_entry(|entry| {
            !(entry.file_type().is_some_and(|kind| kind.is_dir())
                && entry.file_name().to_str().is_some_and(|name| SKIPPED.contains(&name)))
        });
    builder
}

/// The threads a scan walks on: one per core, at most [`SCAN_THREADS`].
fn scan_threads() -> usize {
    std::thread::available_parallelism()
        .map_or(1, std::num::NonZero::get)
        .min(SCAN_THREADS)
}

/// What one entry of the walk adds to the scan: a file that is not `written`, with its stamp.
fn stamp(entry: ignore::DirEntry, written: &BTreeSet<PathBuf>) -> Option<(PathBuf, Stamp)> {
    // `written` holds a handful of paths: files, and the directory of a cache location.
    if !entry.file_type().is_some_and(|kind| kind.is_file())
        || written.iter().any(|skip| entry.path().starts_with(skip))
    {
        return None;
    }
    let metadata = entry.metadata().ok()?;
    Some((entry.into_path(), (metadata.modified().ok(), metadata.len())))
}

/// Hands each walker thread a [`Visitor`] that adds its files to `collected` when the thread finishes.
struct Collector<'a> {
    written: &'a BTreeSet<PathBuf>,
    collected: &'a Mutex<Vec<Vec<(PathBuf, Stamp)>>>,
}

impl<'s> ignore::ParallelVisitorBuilder<'s> for Collector<'s> {
    fn build(&mut self) -> Box<dyn ignore::ParallelVisitor + 's> {
        Box::new(Visitor {
            written: self.written,
            collected: self.collected,
            files: Vec::new(),
        })
    }
}

/// The files one walker thread saw.
struct Visitor<'a> {
    written: &'a BTreeSet<PathBuf>,
    collected: &'a Mutex<Vec<Vec<(PathBuf, Stamp)>>>,
    files: Vec<(PathBuf, Stamp)>,
}

impl ignore::ParallelVisitor for Visitor<'_> {
    fn visit(&mut self, entry: Result<ignore::DirEntry, ignore::Error>) -> ignore::WalkState {
        if let Some(file) = entry.ok().and_then(|entry| stamp(entry, self.written)) {
            self.files.push(file);
        }
        ignore::WalkState::Continue
    }
}

impl Drop for Visitor<'_> {
    fn drop(&mut self) {
        let mut collected = self.collected.lock().unwrap_or_else(PoisonError::into_inner);
        collected.push(std::mem::take(&mut self.files));
    }
}

/// Decides when a series of scans should start a run: after a change, once a scan finds the tree unchanged and
/// the quiet period has passed since the last change.
#[derive(Debug)]
struct Debouncer {
    quiet: Duration,
    /// When the last change not yet followed by a run was seen.
    pending: Option<Instant>,
}

impl Debouncer {
    const fn new(quiet: Duration) -> Self {
        Self { quiet, pending: None }
    }

    /// Records one scan at `now`; true when a run should start.
    fn observe(&mut self, changed: bool, now: Instant) -> bool {
        if changed {
            self.pending = Some(now);
            return false;
        }
        match self.pending {
            Some(last) if now.saturating_duration_since(last) >= self.quiet => {
                self.pending = None;
                true
            }
            _ => false,
        }
    }
}

#[cfg(test)]
mod tests {
    use std::fs;

    use super::*;

    const MS: Duration = Duration::from_millis(1);

    #[test]
    fn an_unchanged_tree_never_fires() {
        let start = Instant::now();
        let mut debouncer = Debouncer::new(100 * MS);
        assert!((0..20).all(|step| !debouncer.observe(false, start + step * 200 * MS)));
    }

    #[test]
    fn a_burst_fires_once_after_the_quiet_period() {
        let start = Instant::now();
        let mut debouncer = Debouncer::new(100 * MS);
        // Changes on three consecutive scans: nothing runs while the tree keeps changing.
        assert!(!debouncer.observe(true, start));
        assert!(!debouncer.observe(true, start + 20 * MS));
        assert!(!debouncer.observe(true, start + 40 * MS));
        // Quiet, but not for long enough yet.
        assert!(!debouncer.observe(false, start + 90 * MS));
        // Quiet for the whole period: one run.
        assert!(debouncer.observe(false, start + 140 * MS));
        assert!(!debouncer.observe(false, start + 400 * MS));
    }

    #[test]
    fn a_change_after_a_run_fires_again() {
        let start = Instant::now();
        let mut debouncer = Debouncer::new(100 * MS);
        assert!(!debouncer.observe(true, start));
        assert!(debouncer.observe(false, start + 200 * MS));
        assert!(!debouncer.observe(true, start + 400 * MS));
        assert!(debouncer.observe(false, start + 600 * MS));
    }

    /// The watched directory is the base the first round resolves: the workspace root of a member, a project's own
    /// directory, and the invocation directory of a multi-root run, recursive or not.
    #[test]
    fn the_watched_directory_is_the_base_the_run_resolves() {
        let dir = tempfile::tempdir().unwrap();
        let top = canonical(dir.path()).unwrap();
        for (file, text) in [
            ("ws/package.json", r#"{"name": "ws", "workspaces": ["packages/*"]}"#),
            ("ws/packages/a/package.json", r#"{"name": "a"}"#),
            ("solo/package.json", r#"{"name": "solo"}"#),
        ] {
            let path = top.join(file);
            fs::create_dir_all(path.parent().unwrap()).unwrap();
            fs::write(path, text).unwrap();
        }
        let cases = [
            (top.join("ws/packages/a"), false, top.join("ws")),
            (top.join("solo"), false, top.join("solo")),
            (top.clone(), false, top.clone()),
            (top.join("ws"), true, top.join("ws")),
            (top.clone(), true, top.clone()),
        ];
        for (path, recursive, expected) in cases {
            let args = LintArgs {
                path: path.clone(),
                recursive,
                ..LintArgs::default()
            };
            let watched = watched_dir(&args).unwrap();
            let resolved = roots::resolve(&path, recursive, &CachedFs::new(OsFs), &lint::overrides(&args))
                .map(|resolved| resolved.base)
                .unwrap();
            assert_eq!(watched, expected, "{path:?} recursive={recursive}");
            assert_eq!(watched, resolved, "{path:?} recursive={recursive}");
        }
    }

    #[test]
    fn the_scan_skips_dependencies_vcs_and_ignored_files() {
        let dir = tempfile::tempdir().unwrap();
        for file in [
            "package.json",
            "src/index.ts",
            "node_modules/x/index.js",
            ".git/HEAD",
            "target/out",
            "dist/bundle.js",
        ] {
            let path = dir.path().join(file);
            fs::create_dir_all(path.parent().unwrap()).unwrap();
            fs::write(path, "x").unwrap();
        }
        fs::write(dir.path().join(".gitignore"), "dist/\n").unwrap();
        let files: Vec<PathBuf> = scan(dir.path(), &BTreeSet::new())
            .into_keys()
            .map(|path| path.strip_prefix(dir.path()).unwrap().to_path_buf())
            .collect();
        assert_eq!(
            files,
            [
                PathBuf::from(".gitignore"),
                PathBuf::from("package.json"),
                Path::new("src").join("index.ts"),
            ]
        );
    }

    #[test]
    fn the_scan_leaves_out_the_files_the_run_writes() {
        let dir = tempfile::tempdir().unwrap();
        let root = canonical(dir.path()).unwrap();
        fs::write(root.join("package.json"), "{}").unwrap();
        fs::write(root.join("report.sarif"), "{}").unwrap();
        let written = BTreeSet::from([root.join("report.sarif")]);
        let files: Vec<PathBuf> = scan(&root, &written).into_keys().collect();
        assert_eq!(files, [root.join("package.json")]);
    }

    /// A tree wide enough to spread over the walker's threads: the parallel scan gives exactly the files, sizes and
    /// modification times a single-threaded walk gives, with the skipped directories, the files ignored by nested
    /// `.gitignore` files, the written files and a symbolic link left out.
    #[test]
    fn the_parallel_scan_matches_a_single_threaded_walk() {
        let dir = tempfile::tempdir().unwrap();
        let root = canonical(dir.path()).unwrap();
        let at = |file: &str| file.split('/').fold(root.clone(), |path, part| path.join(part));
        let mut expected = BTreeSet::new();
        let mut write = |file: &str, text: &str, seen: bool| {
            let path = at(file);
            fs::create_dir_all(path.parent().unwrap()).unwrap();
            fs::write(&path, text).unwrap();
            if seen {
                expected.insert(path);
            }
        };
        write(".gitignore", "*.log\n", true);
        write("report.sarif", "{}", false);
        for package in 0..40 {
            let base = format!("packages/p{package}");
            write(&format!("{base}/.gitignore"), "build/\n", true);
            write(&format!("{base}/.env"), "A=1", true);
            write(&format!("{base}/package.json"), &"x".repeat(package), true);
            write(&format!("{base}/debug.log"), "x", false);
            write(&format!("{base}/build/out.js"), "x", false);
            write(&format!("{base}/node_modules/dep/index.js"), "x", false);
            write(&format!("{base}/.git/HEAD"), "x", false);
            write(&format!("{base}/target/out"), "x", false);
            for module in 0..25 {
                write(&format!("{base}/src/m{module}/index.ts"), "export {}", true);
            }
        }
        // A symbolic link is not followed, so neither walk lists it as a file.
        #[cfg(unix)]
        std::os::unix::fs::symlink(at("packages/p0/package.json"), at("packages/p0/linked.json")).unwrap();
        let written = BTreeSet::from([root.join("report.sarif")]);
        let sequential: BTreeMap<PathBuf, Stamp> = walker(&root)
            .build()
            .filter_map(Result::ok)
            .filter_map(|entry| stamp(entry, &written))
            .collect();
        let parallel = scan(&root, &written);
        assert_eq!(parallel.keys().cloned().collect::<BTreeSet<_>>(), expected);
        assert_eq!(parallel, sequential);
        assert_eq!(parallel[&at("packages/p7/package.json")].1, 7);
    }

    #[test]
    fn the_scan_sees_a_changed_size() {
        let dir = tempfile::tempdir().unwrap();
        fs::write(dir.path().join("package.json"), "{}").unwrap();
        let before = scan(dir.path(), &BTreeSet::new());
        fs::write(dir.path().join("package.json"), "{ }").unwrap();
        assert_ne!(scan(dir.path(), &BTreeSet::new()), before);
    }
}