qk-cache 0.17.0

Task input hashing and caching for the qk task runner
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
//! `qk:ultracache`: key a task by what it read when it last ran, rather
//! than by its declared file inputs.
//!
//! A run with `--record` traces each opted-in task it executes and keeps a
//! path set: the files the task read, the directories it listed and the
//! paths it looked up. A later run keys the task by how those paths are now,
//! so a change to a file the task never read is still a hit, and a new file
//! in a directory it listed is a miss. See [`crate::recorded`].
//!
//! - `mode`: `"on"`, the default once the field is set, `"record"` or
//!   `"off"`. With `"record"`, a task is recorded and its recordings kept and
//!   shared as with `"on"`, but it is keyed by its declared inputs, and the
//!   run reports what its recordings would have done. Nx's `"warn"` and
//!   `"error"` are not supported yet.
//! - `ignoreReads`: workspace globs left out of the path set, for files whose
//!   changes cannot change the result. One starting `^` names files in the
//!   projects the task's project depends on, directly or not: see
//!   [`IgnoredReads`].

use std::collections::BTreeSet;
use std::path::Path;

use anyhow::{Context, Result, bail};
use qk_config::Workspace;
use qk_graph::ProjectGraph;
use qk_taskgraph::Task;
use serde_json::Value;

use crate::glob::Pattern;

/// The fields a `qk:ultracache` object can set. Others are ignored, as Nx
/// ignores keys it does not know; `qk doctor` reports them.
pub const FIELDS: &[&str] = &["mode", "ignoreReads"];

#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct Settings {
    pub mode: Mode,
    pub ignore_reads: Vec<String>,
}

#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum Mode {
    /// Keyed by its recordings once it has any.
    #[default]
    On,
    /// Recorded, but keyed by its declared inputs.
    Record,
}

impl Settings {
    /// The target's settings when it is recorded, `None` when it is not:
    /// without the field, or with `mode` `"off"`.
    pub fn of(task: &Task) -> Result<Option<Self>> {
        let Some(value) = task.definition.extra.get("qk:ultracache") else {
            return Ok(None);
        };
        let Value::Object(object) = value else {
            bail!("qk:ultracache must be an object");
        };
        let mode = match object.get("mode").map(Value::as_str) {
            None | Some(Some("on")) => Mode::On,
            Some(Some("record")) => Mode::Record,
            Some(Some("off")) => return Ok(None),
            Some(Some(mode @ ("warn" | "error"))) => {
                bail!(
                    "qk:ultracache.mode {mode:?} is not supported yet; use \"on\", \"record\" or \"off\""
                )
            }
            Some(_) => bail!("qk:ultracache.mode must be \"on\", \"record\" or \"off\""),
        };
        let ignore_reads = ignored_reads(task)?;
        Ok(Some(Self { mode, ignore_reads }))
    }
}

/// The target's `qk:ultracache.ignoreReads`, whatever its mode: the reads
/// that change nothing the task does, which the sandbox leaves out of its
/// findings and allows whether or not the task is recorded.
pub fn ignored_reads(task: &Task) -> Result<Vec<String>> {
    let Some(Value::Object(object)) = task.definition.extra.get("qk:ultracache") else {
        return Ok(Vec::new());
    };
    let ignore_reads: Vec<String> = match object.get("ignoreReads") {
        None => Vec::new(),
        Some(value) => serde_json::from_value(value.clone())
            .context("qk:ultracache.ignoreReads must be an array of globs")?,
    };
    // Checked before the task runs, rather than after it was traced.
    for glob in &ignore_reads {
        Pattern::new(glob.strip_prefix('^').unwrap_or(glob), false)
            .with_context(|| format!("qk:ultracache.ignoreReads has an invalid glob {glob:?}"))?;
    }
    Ok(ignore_reads)
}

/// What a task's `ignoreReads` leaves out, matched against workspace-relative
/// paths written with `/`.
///
/// An entry is a workspace glob, or, starting `^`, a glob within the projects
/// the task's project depends on, directly or not: it matches a path whose
/// innermost project is one of them, the glob matched against the path from
/// that project's root. A file of the task's own project, or of a project
/// nested in a dependency that it does not depend on, is never matched so.
/// What a tool reads following imports into a dependency, as an import cycle
/// rule does, is named that way without naming the task's own files.
#[derive(Clone, Default)]
pub struct IgnoredReads {
    plain: Vec<(String, Pattern)>,
    dependency: Vec<(String, Pattern)>,
    /// Matched as plain entries, but not walked: the task's scratch
    /// directories, which its recordings leave out too.
    extra: Vec<Pattern>,
    /// Every project's root, the deepest first, and whether the task's
    /// project depends on it; only kept with a `^` entry.
    owners: Vec<(String, bool)>,
}

impl IgnoredReads {
    /// `written` as [`ignored_reads`] returns it, for a task of `project`.
    /// Without `projects`, `^` entries match nothing.
    pub fn new(
        written: &[String],
        workspace: &Workspace,
        projects: Option<&ProjectGraph>,
        project: &str,
    ) -> Result<Self> {
        let mut ignored = Self::of_globs(written)?;
        if ignored.dependency.is_empty() {
            return Ok(ignored);
        }
        let depended = projects
            .map(|projects| depended(projects, project))
            .unwrap_or_default();
        ignored.owners = workspace
            .projects
            .iter()
            .map(|(name, config)| (root_of(&config.root), depended.contains(name.as_str())))
            .collect();
        // Deepest first; of two projects written with the same root, as `a`
        // and `./a`, the one not depended on, so neither is ignored, as the
        // sandbox's report counts neither as a dependency's.
        ignored.owners.sort_by(|a, b| {
            b.0.len()
                .cmp(&a.0.len())
                .then_with(|| a.0.cmp(&b.0))
                .then_with(|| a.1.cmp(&b.1))
        });
        Ok(ignored)
    }

    /// Nothing left out.
    pub const fn none() -> Self {
        Self {
            plain: Vec::new(),
            dependency: Vec::new(),
            extra: Vec::new(),
            owners: Vec::new(),
        }
    }

    /// Workspace globs alone, as a task outside any project graph has them:
    /// `^` entries match nothing.
    pub fn of_globs(written: &[String]) -> Result<Self> {
        let mut ignored = Self::default();
        for glob in written {
            match glob.strip_prefix('^') {
                Some(within) => ignored
                    .dependency
                    .push((within.to_owned(), Pattern::new(within, false)?)),
                None => ignored
                    .plain
                    .push((glob.clone(), Pattern::new(glob, false)?)),
            }
        }
        Ok(ignored)
    }

    /// These, and `extra` matched as plain entries are.
    pub fn with_patterns(mut self, extra: impl IntoIterator<Item = Pattern>) -> Self {
        self.extra.extend(extra);
        self
    }

    pub fn is_empty(&self) -> bool {
        self.plain.is_empty() && self.dependency.is_empty() && self.extra.is_empty()
    }

    pub fn is_match(&self, path: impl AsRef<Path>) -> bool {
        let path = path.as_ref();
        if self.plain.iter().any(|(_, glob)| glob.is_match(path))
            || self.extra.iter().any(|glob| glob.is_match(path))
        {
            return true;
        }
        if self.dependency.is_empty() {
            return false;
        }
        let Some(text) = path.to_str() else {
            return false;
        };
        match self.owner(text) {
            Some((root, true)) => {
                let within = within(root, text);
                self.dependency
                    .iter()
                    .any(|(_, glob)| glob.is_match(within))
            }
            _ => false,
        }
    }

    /// The globs a walk for what this matches starts from, as workspace
    /// globs: the plain entries, and each `^` entry under the root of each
    /// project it can match in.
    pub fn walks(&self) -> (Vec<String>, Vec<String>) {
        let plain = self.plain.iter().map(|(glob, _)| glob.clone()).collect();
        let mut dependency = Vec::new();
        for (root, depended) in &self.owners {
            if !depended {
                continue;
            }
            for (glob, _) in &self.dependency {
                dependency.push(if root.is_empty() {
                    glob.clone()
                } else {
                    format!("{}/{glob}", crate::glob::escape(root))
                });
            }
        }
        (plain, dependency)
    }

    /// Whether a project has its root strictly inside `directory`: a `^`
    /// entry then does not match all that `directory` holds, since it
    /// matches nothing in a project not depended on and matches from its own
    /// root in one that is.
    pub fn project_within(&self, directory: &str) -> bool {
        self.owners.iter().any(|(root, _)| {
            root.len() > directory.len()
                && (directory.is_empty()
                    || root
                        .strip_prefix(directory)
                        .is_some_and(|rest| rest.starts_with('/')))
        })
    }

    /// The roots of the projects `^` entries match in, and of those nested
    /// in them, or written with the same root, that they do not, each with
    /// whether it is depended on, for keys that change with what the entries
    /// match.
    pub fn scope(&self) -> Vec<(&str, bool)> {
        self.owners
            .iter()
            .filter(|(root, depended)| {
                *depended
                    || self.owners.iter().any(|(outer, outer_depended)| {
                        *outer_depended
                            && (root == outer
                                || outer.is_empty()
                                || root
                                    .strip_prefix(outer.as_str())
                                    .is_some_and(|rest| rest.starts_with('/')))
                    })
            })
            .map(|(root, depended)| (root.as_str(), *depended))
            .collect()
    }

    /// The innermost project holding `path`, with whether it is depended on.
    fn owner(&self, path: &str) -> Option<(&str, bool)> {
        self.owners
            .iter()
            .find(|(root, _)| root.is_empty() || within(root, path) != path)
            .map(|(root, depended)| (root.as_str(), *depended))
    }
}

/// `path` from `root`, or `path` itself when `root` does not hold it.
fn within<'a>(root: &str, path: &'a str) -> &'a str {
    if root.is_empty() {
        return path;
    }
    path.strip_prefix(root)
        .and_then(|rest| rest.strip_prefix('/'))
        .unwrap_or(path)
}

/// A project root as paths are named: `./libs/lib/` is `libs/lib`, and the
/// workspace's root project is empty.
fn root_of(root: &str) -> String {
    root.split('/')
        .filter(|part| !part.is_empty() && *part != ".")
        .collect::<Vec<_>>()
        .join("/")
}

/// The projects `project` depends on, directly or not, itself left out.
fn depended(projects: &ProjectGraph, project: &str) -> BTreeSet<String> {
    let mut seen = BTreeSet::new();
    let mut pending = vec![project];
    while let Some(next) = pending.pop() {
        for dependency in projects.dependencies.get(next).into_iter().flatten() {
            if dependency.target != project && seen.insert(dependency.target.clone()) {
                pending.push(&dependency.target);
            }
        }
    }
    seen
}

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

    fn task(value: Value) -> Task {
        let mut definition: qk_config::Target =
            serde_json::from_value(serde_json::json!({})).unwrap();
        definition.extra.insert("qk:ultracache".into(), value);
        Task {
            id: "app:build".into(),
            project: "app".into(),
            target: "build".into(),
            configuration: None,
            args: Vec::new(),
            definition,
            dependencies: Default::default(),
            edges: Default::default(),
        }
    }

    /// `app` at `apps/app` depends on `lib`, which holds `nested`, which it
    /// does not, and on `outer`, whose root holds `app` itself.
    fn workspace() -> (tempfile::TempDir, Workspace, ProjectGraph) {
        let temp = tempfile::tempdir().unwrap();
        let root = temp.path();
        std::fs::write(root.join("nx.json"), "{}").unwrap();
        for (directory, name, depends) in [
            ("apps/app", "app", vec!["lib", "outer", "inner"]),
            ("apps", "outer", vec![]),
            ("packages/lib", "lib", vec![]),
            ("packages/lib/nested", "nested", vec![]),
            ("packages/lib/src/inner", "inner", vec![]),
            ("packages/other", "other", vec![]),
        ] {
            std::fs::create_dir_all(root.join(directory)).unwrap();
            std::fs::write(
                root.join(directory).join("project.json"),
                serde_json::json!({"name": name, "implicitDependencies": depends}).to_string(),
            )
            .unwrap();
        }
        let workspace = Workspace::load(root).unwrap();
        let projects = ProjectGraph::build(&workspace).unwrap();
        (temp, workspace, projects)
    }

    #[test]
    fn a_dependency_entry_matches_only_the_projects_depended_on() {
        let (_temp, workspace, projects) = workspace();
        let written = ["^**/*.ts".to_owned(), "tmp/**".to_owned()];
        let ignored = IgnoredReads::new(&written, &workspace, Some(&projects), "app").unwrap();
        for (path, matched) in [
            ("packages/lib/src/a.ts", true),
            ("packages/lib/src/a.json", false),
            // A project nested in a dependency that is not one itself
            ("packages/lib/nested/src/a.ts", false),
            ("packages/other/a.ts", false),
            // Its own files, though a dependency's root holds them
            ("apps/app/src/a.ts", false),
            ("apps/z.ts", true),
            ("tmp/x", true),
        ] {
            assert_eq!(ignored.is_match(path), matched, "{path}");
        }
        let (plain, dependency) = ignored.walks();
        assert_eq!(plain, ["tmp/**"]);
        assert_eq!(
            dependency,
            [
                "packages/lib/src/inner/**/*.ts",
                "packages/lib/**/*.ts",
                "apps/**/*.ts"
            ]
        );
        assert!(ignored.project_within("packages/lib"));
        // A dependency nested in one, whose files `^` entries match from its
        // own root
        assert!(ignored.project_within("packages/lib/src"));
        assert!(!ignored.project_within("packages/lib/src/inner"));
        assert!(!ignored.project_within("packages/lib/nested/src"));
        assert!(ignored.project_within("apps"));
        assert_eq!(
            ignored.scope(),
            [
                ("packages/lib/src/inner", true),
                ("packages/lib/nested", false),
                ("packages/lib", true),
                ("apps/app", false),
                ("apps", true)
            ]
        );
        // Without the graph, or without a `^` entry, nothing depends on it.
        let alone = IgnoredReads::new(&written, &workspace, None, "app").unwrap();
        assert!(!alone.is_match("packages/lib/src/a.ts"));
        assert!(alone.is_match("tmp/x"));
    }

    #[test]
    fn reads_the_mode_and_ignored_reads() {
        let on = Settings::of(&task(serde_json::json!({"ignoreReads": ["tmp/**"]})))
            .unwrap()
            .unwrap();
        assert_eq!(on.ignore_reads, ["tmp/**"]);
        assert_eq!(on.mode, Mode::On);
        let record = Settings::of(&task(serde_json::json!({"mode": "record"})))
            .unwrap()
            .unwrap();
        assert_eq!(record.mode, Mode::Record);
        assert!(
            Settings::of(&task(serde_json::json!({"mode": "off"})))
                .unwrap()
                .is_none()
        );
        // Unrecorded, its ignored reads still hold for the sandbox.
        let off = task(serde_json::json!({"mode": "off", "ignoreReads": ["tmp/**"]}));
        assert_eq!(ignored_reads(&off).unwrap(), ["tmp/**"]);
        for invalid in [
            serde_json::json!(true),
            serde_json::json!({"mode": "warn"}),
            serde_json::json!({"mode": 1}),
            serde_json::json!({"ignoreReads": "tmp/**"}),
            serde_json::json!({"ignoreReads": ["src/[*.log"]}),
        ] {
            assert!(Settings::of(&task(invalid.clone())).is_err(), "{invalid}");
        }
    }
}