qk-cache 0.12.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
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
//! `includeIgnored` filesets: globs read from disk, so they select files
//! `.gitignore` and `.nxignore` leave out of every other fileset, as Nx 24's
//! `disk_expansion` reads them.
//!
//! The filesets a project resolves together form one group, and a group's
//! `!` negations filter all of its positives. Each positive is read from the
//! literal directory it starts with. Without a pattern after that directory
//! it names a file, or a directory and everything under it; with one, the
//! pattern is matched below the directory. A positive read from inside a
//! project matches only that project's files, not those of a project nested
//! below it.

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

use anyhow::{Result, bail};

use crate::glob::{Pattern, names_a_path};

/// Directories a walk never enters unless it starts inside one, as Nx's
/// walker; `.qk` holds qk's state outside Git.
const SKIPPED_DIRECTORIES: &[&str] = &[
    "node_modules",
    ".git",
    ".nx/cache",
    ".nx/workspace-data",
    ".yarn/cache",
    ".qk",
];

/// One group's resolved globs, workspace-relative, `!` for a negation.
pub(crate) struct Group {
    positives: Vec<Positive>,
    negations: Vec<Negation>,
    /// Project roots a positive's ownership is decided by: every root but
    /// the workspace root's own.
    roots: BTreeSet<String>,
}

/// A positive entry: the directory it is read from and the pattern below it.
struct Positive {
    root: String,
    remainder: Option<Pattern>,
    /// What every file it matches ends in, see [`extension`].
    extension: Option<String>,
}

/// A negation: what it excludes below its directory, or all of it.
struct Negation {
    root: String,
    remainder: Option<Pattern>,
    /// Whether it excludes the directory whole, so a walk need not enter it.
    whole: bool,
}

impl Group {
    /// The group of `globs` that `project` resolves, checked as Nx checks
    /// one before reading anything: no glob is absolute or has a `.` or
    /// `..` segment, a negation excludes something, and not all of them are
    /// negations.
    pub(crate) fn new<'a>(
        project: &str,
        globs: &[String],
        roots: impl IntoIterator<Item = &'a str>,
    ) -> Result<Self> {
        if globs.iter().all(|glob| glob.starts_with('!')) {
            bail!(
                "the includeIgnored fileset {:?} applied to {project:?} is a negation with no positive includeIgnored fileset to filter",
                globs.first().map(String::as_str).unwrap_or_default()
            );
        }
        let mut positives = Vec::new();
        let mut negations = Vec::new();
        for glob in globs {
            let (negated, body) = match glob.strip_prefix('!') {
                Some(body) => (true, body),
                None => (false, glob.as_str()),
            };
            for entry in literal_braces(&normalize(body)) {
                validate(glob, &entry)?;
                let (root, remainder) = partition(&entry);
                let remainder = remainder
                    .map(|rest| Pattern::new(rest, false))
                    .transpose()?;
                if negated {
                    if root.is_empty() && remainder.is_none() {
                        bail!("the includeIgnored fileset {glob:?} names nothing to exclude");
                    }
                    let rest = partition(&entry).1;
                    negations.push(Negation {
                        whole: matches!(rest, None | Some("**" | "**/*")),
                        root,
                        remainder,
                    });
                } else {
                    positives.push(Positive {
                        root,
                        remainder,
                        extension: extension(&entry),
                    });
                }
            }
        }
        Ok(Self {
            positives,
            negations,
            roots: roots
                .into_iter()
                .filter(|root| !matches!(*root, "" | "."))
                .map(|root| root.trim_end_matches('/').to_owned())
                .collect(),
        })
    }

    /// Each positive's directory, and the extension every file it matches
    /// ends in, if one.
    pub(crate) fn reads(&self) -> impl Iterator<Item = (&str, Option<&str>)> {
        self.positives
            .iter()
            .map(|entry| (entry.root.as_str(), entry.extension.as_deref()))
    }

    /// The project roots a positive read from `root` cannot match, as Nx's
    /// `nested_below`: those below it, when a project holds it.
    pub(crate) fn skipped_below(&self, root: &str) -> Vec<&str> {
        if self.owner_of_directory(root).is_none() {
            return Vec::new();
        }
        self.roots
            .iter()
            .filter(|project| is_below(project, root))
            .map(String::as_str)
            .collect()
    }

    /// The files on disk the group selects under `workspace`, sorted.
    pub(crate) fn expand(&self, workspace: &Path) -> Result<BTreeSet<String>> {
        let mut found = BTreeSet::new();
        for entry in &self.positives {
            let start = workspace.join(&entry.root);
            let Ok(metadata) = std::fs::metadata(&start) else {
                continue;
            };
            if metadata.is_file() {
                // An exact path is read as written, whatever holds it.
                if entry.remainder.is_none() && !self.excluded(&entry.root) {
                    found.insert(entry.root.clone());
                }
                continue;
            }
            let mut skip: Vec<&str> = self.skipped_below(&entry.root);
            skip.extend(
                self.negations
                    .iter()
                    .filter(|negation| negation.whole && is_below(&negation.root, &entry.root))
                    .map(|negation| negation.root.as_str()),
            );
            for path in walk(workspace, &entry.root, &skip)? {
                if self.admits(entry, &path) {
                    found.insert(path);
                }
            }
        }
        Ok(found)
    }

    /// Whether the group selects `path`, for a path that need not exist,
    /// such as one a change set names.
    pub(crate) fn matches(&self, path: &str) -> bool {
        self.positives.iter().any(|entry| {
            if entry.remainder.is_none() && entry.root == path {
                return !self.excluded(path);
            }
            self.admits(entry, path)
        })
    }

    /// Whether `entry` matches `path` below its directory, `path` is not
    /// excluded, and `entry` may read it.
    fn admits(&self, entry: &Positive, path: &str) -> bool {
        let rest = if entry.root.is_empty() {
            Some(path)
        } else {
            path.strip_prefix(&entry.root)
                .and_then(|rest| rest.strip_prefix('/'))
        };
        let matched = match (rest, &entry.remainder) {
            (None, _) => false,
            (Some(rest), Some(pattern)) => !rest.is_empty() && pattern.is_match(rest),
            (Some(_), None) => true,
        };
        matched && !self.excluded(path) && self.owns(entry, path)
    }

    fn excluded(&self, path: &str) -> bool {
        self.negations.iter().any(|negation| {
            let rest = if negation.root.is_empty() {
                Some(path)
            } else {
                path.strip_prefix(&negation.root)
                    .and_then(|rest| match rest {
                        "" => Some(rest),
                        rest => rest.strip_prefix('/'),
                    })
            };
            match (rest, &negation.remainder) {
                (None, _) => false,
                (Some(_), None) => true,
                (Some(rest), Some(pattern)) => pattern.is_match(rest),
            }
        })
    }

    /// Whether a positive read from `entry`'s directory may match `path`:
    /// read from inside a project, only that project's files.
    fn owns(&self, entry: &Positive, path: &str) -> bool {
        match self.owner_of_directory(&entry.root) {
            None => true,
            owner => parents(path).find(|parent| self.roots.contains(*parent)) == owner,
        }
    }

    /// The deepest project root that is `directory` or holds it.
    fn owner_of_directory<'a>(&self, directory: &'a str) -> Option<&'a str> {
        std::iter::once(directory)
            .chain(parents(directory))
            .find(|parent| self.roots.contains(*parent))
    }
}

/// `path`'s parent directories, nearest first, without the workspace root.
fn parents(path: &str) -> impl Iterator<Item = &str> {
    std::iter::successors(Some(path), |path| {
        path.rsplit_once('/').map(|(parent, _)| parent)
    })
    .skip(1)
}

/// Whether `path` lies strictly below `directory`; everything lies below the
/// workspace root, written empty.
fn is_below(path: &str, directory: &str) -> bool {
    if directory.is_empty() {
        return !path.is_empty();
    }
    path.strip_prefix(directory)
        .is_some_and(|rest| rest.starts_with('/'))
}

/// Repeated and trailing slashes collapsed, as Nx's `normalize_glob`.
fn normalize(glob: &str) -> String {
    let mut result = String::new();
    for part in glob.split('/') {
        if part.is_empty() {
            continue;
        }
        if !result.is_empty() {
            result.push('/');
        }
        result.push_str(part);
    }
    if glob.starts_with('/') {
        result.insert(0, '/');
    }
    result
}

/// Nx's `validate_shape`: a glob reads inside the workspace.
fn validate(glob: &str, entry: &str) -> Result<()> {
    if entry.starts_with('/') || Path::new(entry).is_absolute() {
        bail!(
            "the includeIgnored fileset {glob:?} is an absolute path; globs are workspace-relative"
        );
    }
    if entry.split('/').any(|part| matches!(part, "." | "..")) {
        bail!(
            "the includeIgnored fileset {glob:?} has a `.` or `..` segment; write it relative to the workspace root"
        );
    }
    Ok(())
}

/// A brace group of literal names expanded into each path it names, as
/// Nx's `expand_literal_braces`: `{nx,tsconfig.base}.json` is `nx.json` and
/// `tsconfig.base.json`. A group with a pattern in it stays a pattern.
fn literal_braces(glob: &str) -> Vec<String> {
    let Some(open) = glob.find('{') else {
        return vec![glob.to_owned()];
    };
    let Some(close) = glob[open..].find('}').map(|index| open + index) else {
        return vec![glob.to_owned()];
    };
    let alternatives: Vec<&str> = glob[open + 1..close].split(',').collect();
    let literal = alternatives.len() > 1
        && alternatives.iter().all(|alternative| {
            !alternative.is_empty()
                && !alternative.contains(['*', '?', '[', ']', '{', '}', '/', '\\'])
        });
    if !literal {
        return vec![glob.to_owned()];
    }
    alternatives
        .iter()
        .flat_map(|alternative| {
            literal_braces(&format!(
                "{}{alternative}{}",
                &glob[..open],
                &glob[close + 1..]
            ))
        })
        .collect()
}

/// A glob split, as Nx's `partition_glob`, into the directory it is read
/// from, its leading literal segments with their escapes read, and the
/// pattern below it, `None` when it is literal to its end.
pub(crate) fn partition(glob: &str) -> (String, Option<&str>) {
    let mut directory: Vec<String> = Vec::new();
    let mut consumed = 0;
    for segment in glob.split('/') {
        if !names_a_path(segment) || segment.contains('\\') && unescape(segment).is_none() {
            return (directory.join("/"), Some(&glob[consumed..]));
        }
        directory.push(unescape(segment).unwrap_or_else(|| segment.to_owned()));
        consumed += segment.len() + 1;
    }
    (directory.join("/"), None)
}

/// The extension every match of `glob` ends in, as Nx's
/// `extension_after_wildcard`: `js` for `dist/**/*.js`. None unless the last
/// segment is a pattern ending in a literal `.ext`, since a literal segment
/// may name a directory, such as `dist/lib.v2`.
pub(crate) fn extension(glob: &str) -> Option<String> {
    let last = glob.rsplit('/').next()?;
    if names_a_path(last) {
        return None;
    }
    let (_, extension) = last.rsplit_once('.')?;
    (!extension.is_empty() && names_a_path(extension) && !extension.contains(['@', '+']))
        .then(|| extension.to_owned())
}

/// A segment with its escapes read, or `None` when they would make it `.`
/// or `..`, which Nx reads as a pattern rather than a name.
fn unescape(segment: &str) -> Option<String> {
    let mut name = String::new();
    let mut chars = segment.chars();
    while let Some(c) = chars.next() {
        name.push(if c == '\\' {
            chars.next().unwrap_or('\\')
        } else {
            c
        });
    }
    (segment == name || !matches!(name.as_str(), "." | "..")).then_some(name)
}

/// Every file below `directory`, workspace-relative, as Nx's walker lists
/// them: the directory itself is read wherever it points, a linked file
/// counts and a linked directory below it is not entered, nor are `skip`,
/// [`SKIPPED_DIRECTORIES`] below the start, or the files tools write and
/// remove moments later.
fn walk(workspace: &Path, directory: &str, skip: &[&str]) -> Result<Vec<String>> {
    let start = workspace.join(directory);
    let prefix = if directory.is_empty() {
        String::new()
    } else {
        format!("{directory}/")
    };
    let mut files = Vec::new();
    let mut walker = walkdir::WalkDir::new(&start)
        .follow_links(false)
        .follow_root_links(true)
        .into_iter();
    while let Some(entry) = walker.next() {
        let entry = match entry {
            Ok(entry) => entry,
            Err(error)
                if error
                    .io_error()
                    .is_some_and(|io| io.kind() == std::io::ErrorKind::NotFound) =>
            {
                continue;
            }
            Err(error) => return Err(error.into()),
        };
        if entry.depth() == 0 {
            continue;
        }
        let Ok(relative) = entry.path().strip_prefix(&start) else {
            continue;
        };
        let Some(relative) = relative.to_str() else {
            continue;
        };
        let path = format!("{prefix}{}", relative.replace('\\', "/"));
        let file_type = entry.file_type();
        if file_type.is_dir() {
            let skipped = skip.contains(&path.as_str())
                || SKIPPED_DIRECTORIES
                    .iter()
                    .any(|name| path == *name || path.ends_with(&format!("/{name}")));
            if skipped {
                walker.skip_current_dir();
            }
            continue;
        }
        if file_type.is_symlink()
            && std::fs::metadata(entry.path()).is_ok_and(|target| target.is_dir())
        {
            continue;
        }
        if transient(entry.file_name().to_str().unwrap_or_default()) {
            continue;
        }
        files.push(path);
    }
    Ok(files)
}

/// Files Vite and Vitest write beside their config while loading it, and
/// pnpm's store probes, which Nx's walker leaves out.
fn transient(name: &str) -> bool {
    let timestamp = [
        "vitest.config.ts.timestamp",
        "vite.config.ts.timestamp",
        "vitest.config.mts.timestamp",
        "vite.config.mts.timestamp",
    ]
    .iter()
    .any(|config| name.starts_with(config) && name.ends_with(".mjs"));
    let probe = name
        .strip_prefix("_tmp_")
        .is_some_and(|rest| rest.contains('_'));
    timestamp || probe
}

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

    fn group(globs: &[&str], roots: &[&str]) -> Result<Group> {
        let globs: Vec<String> = globs.iter().map(|glob| (*glob).to_owned()).collect();
        Group::new("web", &globs, roots.iter().copied())
    }

    fn workspace(files: &[&str]) -> tempfile::TempDir {
        let root = tempfile::tempdir().unwrap();
        for file in files {
            let path = root.path().join(file);
            std::fs::create_dir_all(path.parent().unwrap()).unwrap();
            std::fs::write(path, *file).unwrap();
        }
        root
    }

    fn expand(root: &tempfile::TempDir, globs: &[&str], roots: &[&str]) -> Vec<String> {
        group(globs, roots)
            .unwrap()
            .expand(root.path())
            .unwrap()
            .into_iter()
            .collect()
    }

    const FILES: &[&str] = &[
        "dist/gen/a.js",
        "dist/gen/a.js.map",
        "dist/gen/nested/b.js",
        "dist/other/c.js",
        "dist/gen/node_modules/dep/index.js",
        "node_modules/foo/package.json",
    ];

    // Nx 24's `expands_from_the_partitioned_directory_and_applies_negations`
    // and its neighbours.
    #[test]
    fn negations_filter_every_positive_in_any_order() {
        let root = workspace(FILES);
        assert_eq!(
            expand(&root, &["!dist/gen/**/*.map", "dist/gen/**/*"], &[]),
            ["dist/gen/a.js", "dist/gen/nested/b.js"]
        );
        assert_eq!(
            expand(&root, &["dist/**", "!dist/gen/nested"], &[]),
            ["dist/gen/a.js", "dist/gen/a.js.map", "dist/other/c.js"]
        );
        assert_eq!(
            expand(
                &root,
                &["dist/gen/a.js", "dist/other/c.js", "!dist/gen/a.js"],
                &[]
            ),
            ["dist/other/c.js"]
        );
    }

    #[test]
    fn an_exact_path_names_a_file_or_everything_under_a_directory() {
        let root = workspace(FILES);
        assert_eq!(expand(&root, &["dist/other"], &[]), ["dist/other/c.js"]);
        assert_eq!(expand(&root, &["dist/gen/"], &[]).len(), 3);
        assert!(expand(&root, &["dist/missing.js"], &[]).is_empty());
        assert!(
            group(&["dist/missing.js"], &[])
                .unwrap()
                .matches("dist/missing.js")
        );
    }

    #[test]
    fn walks_skip_node_modules_unless_they_start_inside_it() {
        let root = workspace(FILES);
        assert!(
            !expand(&root, &["dist/**"], &[])
                .iter()
                .any(|file| file.contains("node_modules"))
        );
        assert_eq!(
            expand(&root, &["node_modules/foo/package.json"], &[]),
            ["node_modules/foo/package.json"]
        );
        assert_eq!(
            expand(&root, &["node_modules/**/package.json"], &[]),
            ["node_modules/foo/package.json"]
        );
    }

    #[test]
    fn a_positive_skips_projects_nested_below_where_it_reads() {
        let root = workspace(&[
            "packages/gradle/src/a.ts",
            "packages/gradle/build/out.json",
            "packages/gradle/runner/build.gradle.kts",
            "packages/gradle/runner/build/runner.jar",
        ]);
        let roots = ["packages/gradle", "packages/gradle/runner", "."];
        assert_eq!(
            expand(&root, &["packages/gradle/**/*"], &roots),
            ["packages/gradle/build/out.json", "packages/gradle/src/a.ts"]
        );
        assert_eq!(
            expand(&root, &["packages/gradle/runner/build/**"], &roots),
            ["packages/gradle/runner/build/runner.jar"]
        );
        // Read from outside every project, it matches what it names.
        assert_eq!(
            expand(&root, &["packages/**/*.jar"], &roots),
            ["packages/gradle/runner/build/runner.jar"]
        );
        let group = group(&["packages/gradle/**/*"], &roots).unwrap();
        assert!(!group.matches("packages/gradle/runner/build/runner.jar"));
        assert!(group.matches("packages/gradle/build/new.json"));
    }

    #[test]
    fn brackets_braces_and_escapes_read_as_any_fileset() {
        let root = workspace(&[
            "app/(marketing)/page.tsx",
            "app/marketing/page.tsx",
            "app/[id]/page.tsx",
            "app/i/page.tsx",
            "nx.json",
            "tsconfig.base.json",
        ]);
        assert_eq!(
            expand(&root, &["app/[id]/page.tsx"], &[]),
            ["app/i/page.tsx"]
        );
        assert_eq!(
            expand(&root, &[r"app/\[id\]/page.tsx"], &[]),
            ["app/[id]/page.tsx"]
        );
        assert_eq!(
            expand(&root, &[r"app/\(marketing\)"], &[]),
            ["app/(marketing)/page.tsx"]
        );
        assert_eq!(
            expand(&root, &["{nx,tsconfig.base,missing}.json"], &[]),
            ["nx.json", "tsconfig.base.json"]
        );
    }

    #[test]
    fn rejects_what_nx_rejects() {
        for globs in [
            &["!dist/**/*.map"][..],
            &["dist/**", "!"],
            &["../outside/**"],
            &["dist/./gen/**"],
            &["/etc/passwd"],
        ] {
            assert!(group(globs, &[]).is_err(), "{globs:?}");
        }
        assert!(group(&["dist//gen/", "!dist//gen//**/*.map"], &[]).is_ok());
    }

    #[cfg(unix)]
    #[test]
    fn links_are_read_where_they_point_but_not_walked_into() {
        let root = workspace(FILES);
        let elsewhere = workspace(&["linked.js", "tree/deep.js"]);
        std::os::unix::fs::symlink(
            elsewhere.path().join("linked.js"),
            root.path().join("dist/gen/escape.js"),
        )
        .unwrap();
        std::os::unix::fs::symlink(
            elsewhere.path().join("tree"),
            root.path().join("dist/gen/tree"),
        )
        .unwrap();
        assert_eq!(
            expand(&root, &["dist/gen/*.js"], &[]),
            ["dist/gen/a.js", "dist/gen/escape.js"]
        );
        assert_eq!(
            expand(&root, &["dist/gen/tree"], &[]),
            ["dist/gen/tree/deep.js"]
        );
        assert!(
            !expand(&root, &["dist/**"], &[])
                .iter()
                .any(|file| file.contains("tree/"))
        );
    }
}