Skip to main content

jsonvet_cli/
lib.rs

1//! The jsonvet command line, shared by the `jsonvet` and `check-json5` binaries.
2
3use std::ffi::{OsStr, OsString};
4use std::io::{self, BufWriter, IsTerminal, Read, Write};
5use std::path::{Path, PathBuf};
6use std::process::ExitCode;
7
8use std::sync::atomic::{AtomicUsize, Ordering};
9use std::sync::{Arc, Mutex};
10
11use ignore::Match;
12use ignore::gitignore::{Gitignore, GitignoreBuilder};
13use jsonvet::{Dialect, Problem, Report, Severity};
14
15mod args;
16mod catalog;
17mod editorconfig;
18mod format;
19mod frame;
20mod glob;
21mod json;
22mod lsp;
23mod messages;
24mod output;
25mod rules;
26mod sarif;
27mod schema;
28mod settings;
29mod write;
30
31use args::{Command, STDIN};
32use output::{Format, Look, Printer};
33use schema::Schema;
34use schema::select::{Job, Schemas, check_own_schema, jobs, with_schema};
35use settings::{Resolver, Settings};
36use write::write_atomically;
37
38/// The stack every run uses, for `--schema`: jsonschema validates recursively, and building,
39/// validating and dropping a value [`schema::SCHEMA_MAX_DEPTH`] levels deep against a recursive schema
40/// took up to 2.2 MB in a debug build, more than Windows' 1 MB main thread. Only the pages used
41/// are committed, so a plain check costs no more memory.
42const STACK: usize = 64 << 20;
43
44/// How much memory the files read at once may take before they are checked and printed.
45const CHUNK_BYTES: usize = 64 << 20;
46
47/// How many bytes of memory a byte of file text takes with a schema, which holds the file's value
48/// one document at a time. Kept above the most that docs/usage.md ("JSON Schema limits") gives,
49/// so a chunk never overcommits.
50const SCHEMA_BYTES: usize = 20;
51
52/// From this size, a JSON Lines file is split at newlines and its parts checked on the cores
53/// its chunk leaves free.
54const SPLIT_BYTES: usize = 1 << 20;
55
56/// Run the command with the process's arguments and environment, and return its exit code.
57pub fn run() -> ExitCode {
58    // The release profile aborts on a panic, which ends the process with a signal and no way to
59    // report it. A hook runs first, so a bug exits 101 with a pointer to the issue tracker.
60    std::panic::set_hook(Box::new(|info| {
61        // NOTE: `eprintln!` would panic again here when stderr cannot be written, and abort.
62        let _ = writeln!(
63            io::stderr(),
64            "jsonvet: internal error: {info}\n\nThis is a bug in jsonvet. Please report it at \
65             https://github.com/simonvanlierde/jsonvet/issues with the command you ran and, if \
66             you can share it, the file that caused it."
67        );
68        std::process::exit(101);
69    }));
70    on_big_stack(run_here)
71}
72
73/// Run `f` on a thread with a [`STACK`]-byte stack, or here if no thread can start. A panic in
74/// `f` carries on here.
75fn on_big_stack<T: Send + 'static>(f: fn() -> T) -> T {
76    let thread = std::thread::Builder::new().name("jsonvet".into());
77    match thread.stack_size(STACK).spawn(f) {
78        Ok(thread) => thread
79            .join()
80            .unwrap_or_else(|panic| std::panic::resume_unwind(panic)),
81        Err(_) => f(),
82    }
83}
84
85fn run_here() -> ExitCode {
86    // NOTE: a hidden trigger for tests/cli.rs, so the panic hook runs without a real bug. Debug
87    // builds only, so release binaries cannot be made to panic on purpose.
88    #[cfg(debug_assertions)]
89    assert!(
90        std::env::var_os("JSONVET_TEST_PANIC").is_none(),
91        "test panic"
92    );
93    let annotate = std::env::var_os("GITHUB_ACTIONS").is_some_and(|v| v == "true");
94    // Not locked: `jsonvet lsp` writes to it from a thread of its own.
95    let stdout = io::stdout();
96    let set = |name| std::env::var_os(name).is_some_and(|v| !v.is_empty());
97    // NO_COLOR wins over FORCE_COLOR: asking for no color is the safer wish to honour.
98    // FORCE_COLOR=0 and =false ask for no color, as Node and chalk read them.
99    let color = if set("NO_COLOR") {
100        Some(false)
101    } else if set("FORCE_COLOR") {
102        Some(std::env::var_os("FORCE_COLOR").is_none_or(|v| v != "0" && v != "false"))
103    } else {
104        None
105    };
106    let terminal = Terminal {
107        tty: stdout.is_terminal(),
108        color,
109    };
110    match run_with(
111        std::env::args_os().skip(1),
112        annotate,
113        terminal,
114        BufWriter::new(stdout),
115    ) {
116        Ok(code) => ExitCode::from(code),
117        Err(message) => {
118            // Nowhere to say it when stderr is the failure, but the exit code still tells.
119            let _ = writeln!(io::stderr(), "jsonvet: {message}");
120            ExitCode::from(2)
121        }
122    }
123}
124
125/// The end of a run: the `line` of counts as text on a terminal (`text`), and as an annotation
126/// when `found` problems, then whatever the format closes with.
127fn print_summary(
128    printer: &mut Printer<impl Write>,
129    line: Option<&str>,
130    text: bool,
131    found: bool,
132) -> io::Result<()> {
133    if let Some(line) = line {
134        if text {
135            printer.text_line(line)?;
136        }
137        if found {
138            printer.summary_annotation(line)?;
139        }
140    }
141    printer.finish()
142}
143
144/// Where output goes, as far as text output's look depends on it. Public only for
145/// `benches/codspeed.rs`; not part of the crate's API.
146#[doc(hidden)]
147#[derive(Clone, Copy, Debug, Default)]
148pub struct Terminal {
149    /// Standard output is a terminal. A summary and code frames are for people: hooks and CI
150    /// read standard output from a pipe and see one line per problem.
151    pub tty: bool,
152    /// What the environment asks for when `--color` is `auto`: `Some(false)` for `NO_COLOR`,
153    /// `Some(true)` for `FORCE_COLOR`.
154    pub color: Option<bool>,
155}
156
157/// Whether `text` loads as the config file `path` (a `pyproject.toml` reads its `[tool.jsonvet]`
158/// table), with the error message if not. Public only for the fuzz target `config_file`; not
159/// part of the crate's API.
160#[doc(hidden)]
161pub fn check_config_text(path: &Path, text: &str) -> Result<(), String> {
162    settings::load_str(path, text, false, None).map(drop)
163}
164
165/// The command, with its inputs passed in: 0 when no file has an error, 1 when one does.
166/// On a terminal, text output ends with a count of the problems. A usage error comes back as
167/// `Err`, which `run` turns into exit status 2. Public only for `benches/codspeed.rs`, which
168/// measures `--schema` in-process; not part of the crate's API.
169#[doc(hidden)]
170#[allow(
171    clippy::too_many_lines,
172    reason = "the checking pipeline keeps its stages in execution order"
173)]
174pub fn run_with(
175    args: impl IntoIterator<Item = OsString>,
176    annotate: bool,
177    terminal: Terminal,
178    out: impl Write,
179) -> Result<u8, String> {
180    // Usage errors quote arguments and the schema file, which can hold a newline or an escape.
181    let args = match args::parse(args)
182        .map_err(|e| messages::fill("error.usage", &[("error", &output::printable(&e))]))?
183    {
184        Command::Help => return print(out, messages::text("help")),
185        Command::Version => return print(out, concat!("jsonvet ", env!("CARGO_PKG_VERSION"))),
186        Command::ListRules => return print(out, &rules::list()),
187        Command::Explain(text) => return print(out, &text),
188        Command::Lsp => return lsp::run(),
189        Command::Check(args) => *args,
190    };
191    messages::english_only(args.format != Format::Text);
192    let resolver = Arc::new(Mutex::new(Resolver::new(&args.config, args.settings)?));
193    let settings_for = |path: &Path| lock(&resolver).for_file(path);
194    let threads = std::thread::available_parallelism().map_or(1, std::num::NonZero::get);
195    let files = expand(args.files, &resolver, threads)?;
196    if let Some(check_only) = args.fmt {
197        return format::run(&files, check_only, args.stdin_filename, &resolver, out);
198    }
199    let schemas = Schemas::default();
200    let load = |path: &Path, quiet| {
201        schemas.get(path, quiet, |admit| {
202            Schema::load(path, &*settings_for(path)?, quiet, admit)
203        })
204    };
205    let root = |path: &Path| lock(&resolver).root(path);
206
207    // Standard input goes by its `--stdin-filename`, for settings and in the output.
208    let stdin_name = args.stdin_filename.unwrap_or_else(|| STDIN.into());
209    let shown = |file: &OsStr| -> OsString {
210        if file == STDIN {
211            stdin_name.clone()
212        } else {
213            file.to_owned()
214        }
215    };
216    // Every config file, and every schema one names, is read before any file is checked, so a
217    // bad one stops the run at once.
218    let files = jobs(files, &shown, &settings_for, args.schema.as_deref(), &load)?;
219    let mut catalog = args
220        .schema_catalog
221        .then(catalog::Catalog::new)
222        .transpose()?;
223    let look = look(args.format, args.color, terminal);
224    // Standard input fixed to standard output leaves only standard error for the problems.
225    let stdin_fix = args.fix && files.iter().any(|(_, file)| file == STDIN);
226    let mut out = out;
227    let mut stderr = io::stderr();
228    // Standard error stands in for standard output's problems when that carries a fixed text.
229    let sink: &mut dyn Write = if stdin_fix { &mut stderr } else { &mut out };
230    let mut printer = Printer::new(sink, args.format, annotate, look, stdin_fix);
231    let mut fixed = FixRun::default();
232    let mut counts = Counts::default();
233    // Files are read and checked on every core, a chunk at a time, then printed here in the
234    // order given. A chunk ends once its files hold CHUNK_BYTES, so memory stays near that. With
235    // a schema a file takes SCHEMA_BYTES times its size, so the chunk is cut from the sizes
236    // before any file is read: otherwise every core would start on a file at once. Without
237    // a schema the files are not sized first, which made a walk of 10,000 files 7 ms slower.
238    // A file whose own `$schema` may name a schema is known only once read: it counts the same,
239    // and the texts read past the end of the chunk are kept, in `ahead`, for the next one.
240    // NOTE: each chunk waits for its slowest file before the next starts; hand files out
241    // one at a time across chunks if one big file among many small ones holds a run up.
242    let mut rest = &files[..];
243    let shares = schema_shares(&files, threads);
244    let mut ahead = Vec::new();
245    let written = loop {
246        if rest.is_empty() {
247            let noted = (schemas.missing_notes().into_iter()).try_for_each(|n| printer.note(n));
248            let line = (!files.is_empty()).then(|| counts.summary(files.len()));
249            let found = counts.errors + counts.warnings > 0;
250            break noted
251                .and_then(|()| print_summary(&mut printer, line.as_deref(), terminal.tty, found));
252        }
253        let shares = shares.get(files.len() - rest.len()..);
254        let with_schema =
255            |job: &Job, text: &str| args.schema_catalog || with_schema(job, &shown(&job.1), text);
256        let mut texts = take_chunk(&mut ahead, rest, shares, threads, &with_schema);
257        let chunk;
258        (chunk, rest) = rest.split_at(texts.len());
259        let known = if args.fix {
260            fix_chunk(
261                chunk,
262                &mut texts,
263                &shown,
264                args.unsafe_fixes,
265                threads,
266                &mut fixed,
267            )
268        } else {
269            vec![None; texts.len()]
270        };
271        let mut selected = None;
272        if let Some(catalog) = &mut catalog {
273            selected = Some(catalog.select(chunk, &texts, &shown, &schemas)?);
274            if let Err(e) = (catalog.take_notes().into_iter()).try_for_each(|n| printer.note(n)) {
275                break Err(e);
276            }
277        }
278        let chunk = selected.as_deref().unwrap_or(chunk);
279        let jobs: Vec<_> = chunk.iter().zip(&texts).zip(&known).collect();
280        let parts = (threads / jobs.len()).max(1);
281        let checked = parallel(
282            &jobs,
283            threads,
284            || false,
285            |((((settings, schema), file), (text, with)), known)| {
286                let name = shown(file);
287                let name = name.to_string_lossy();
288                text.as_deref().map(|text| match schema {
289                    Some(schema) => check(&name, text, settings, Some(schema), parts, None),
290                    None if *with => {
291                        check_own_schema(&name, text, settings, &schemas, &root, &load, parts)
292                    }
293                    None => check(&name, text, settings, None, parts, known.as_deref()),
294                })
295            },
296        );
297        schemas.over().map_err(|e| output::printable(&e))?;
298        let outcome = print_checked(chunk, checked, &shown, &mut printer, &mut counts);
299        if outcome.is_err() {
300            break outcome;
301        }
302    };
303    // A JSON or SARIF report on standard error is all of it, so the count stays out.
304    let count = !stdin_fix || args.format == Format::Text;
305    // The fixed text goes out even when the problems could not be written.
306    let finished = fixed.finish(count, &mut out);
307    let written = written.and(finished);
308    conclude(written, &counts, args.max_warnings)
309}
310
311/// A file's report and its schema errors.
312type Checked<'t> = (Report<'t>, Vec<(usize, String, bool)>);
313
314/// Print what checking the files of `chunk` found, in order: the problems of each, or why it
315/// could not be read.
316fn print_checked(
317    chunk: &[Job],
318    checked: Vec<Result<Checked<'_>, &String>>,
319    shown: &impl Fn(&OsStr) -> OsString,
320    printer: &mut Printer<impl Write>,
321    counts: &mut Counts,
322) -> io::Result<()> {
323    chunk
324        .iter()
325        .zip(checked)
326        .try_for_each(|((_, file), checked)| match checked {
327            Ok((report, schema_errors)) => {
328                print_file(&shown(file), &report, &schema_errors, printer, counts)
329            }
330            Err(message) => {
331                counts.errors += 1;
332                counts.files += 1;
333                printer.unreadable(&shown(file), message)
334            }
335        })
336}
337
338/// The exit code once the output is written: 1 for an error, or for more warnings than
339/// `max_warnings` allows.
340fn conclude(
341    written: io::Result<()>,
342    counts: &Counts,
343    max_warnings: Option<usize>,
344) -> Result<u8, String> {
345    // A reader that has gone, as with `| head`, wants no more output: the verdict so far stands.
346    match written {
347        Err(e) if e.kind() != io::ErrorKind::BrokenPipe => {
348            return Err(messages::fill("error.write", &[("error", &e)]));
349        }
350        _ => {}
351    }
352    let too_many_warnings = max_warnings.is_some_and(|max| counts.warnings > max);
353    Ok(u8::from(counts.errors > 0 || too_many_warnings))
354}
355
356/// The texts of the first of `files`, read on up to `threads` threads until their [`weight`]s
357/// reach [`CHUNK_BYTES`], and always the first, each with whether `with_schema` says it is
358/// checked against a schema.
359fn read_chunk<S: Sync>(
360    files: &[(S, OsString)],
361    threads: usize,
362    with_schema: &(impl Fn(&(S, OsString), &str) -> bool + Sync),
363) -> Vec<(Result<String, String>, bool)> {
364    let bytes = AtomicUsize::new(0);
365    let full = || bytes.load(Ordering::Relaxed) >= CHUNK_BYTES;
366    parallel(files, threads, full, |file| {
367        let text = if file.1 == STDIN {
368            read_stdin()
369        } else {
370            read(Path::new(&file.1))
371        };
372        let with = text.as_ref().is_ok_and(|text| with_schema(file, text));
373        bytes.fetch_add(weight(&text, with), Ordering::Relaxed);
374        (text, with)
375    })
376}
377
378/// The texts of the next chunk of `rest`, the files left, each with whether it is checked
379/// against a schema: up to and including the one whose [`weight`] fills [`CHUNK_BYTES`], from
380/// `ahead`, the texts of the first of them read already, or if none are, from those read now.
381/// `shares` are the files' [`schema_share`]s, if any.
382fn take_chunk(
383    ahead: &mut Vec<(Result<String, String>, bool)>,
384    rest: &[Job],
385    shares: Option<&[usize]>,
386    threads: usize,
387    with_schema: &(impl Fn(&Job, &str) -> bool + Sync),
388) -> Vec<(Result<String, String>, bool)> {
389    if ahead.is_empty() {
390        let offered = match shares {
391            Some(shares) if !shares.is_empty() => &rest[..chunk_len(shares)],
392            _ => rest,
393        };
394        *ahead = read_chunk(offered, threads, with_schema);
395    }
396    let weights: Vec<usize> = ahead
397        .iter()
398        .map(|(text, with)| weight(text, *with))
399        .collect();
400    ahead.drain(..chunk_len(&weights)).collect()
401}
402
403/// The memory a text read takes while it is checked, as far as a chunk counts it: its size, or
404/// with a schema, [`SCHEMA_BYTES`] times that, capped at [`CHUNK_BYTES`].
405fn weight(text: &Result<String, String>, with_schema: bool) -> usize {
406    let size = text.as_ref().map_or(0, String::len);
407    if with_schema {
408        schema_bytes(size)
409    } else {
410        size
411    }
412}
413
414/// [`SCHEMA_BYTES`] times `size`, capped at [`CHUNK_BYTES`].
415fn schema_bytes(size: usize) -> usize {
416    size.saturating_mul(SCHEMA_BYTES).min(CHUNK_BYTES)
417}
418
419/// The memory a file takes while a schema checks it, as far as a chunk counts it:
420/// [`schema_bytes`] of its size. Standard input's size is known
421/// only once it is read, so it takes a chunk's worth. A file that cannot be read takes nothing:
422/// `read` reports why.
423fn schema_share(file: &OsStr) -> usize {
424    if file == STDIN {
425        return CHUNK_BYTES;
426    }
427    let size = std::fs::metadata(file).map_or(0, |meta| meta.len());
428    schema_bytes(usize::try_from(size).unwrap_or(usize::MAX))
429}
430
431/// Each file's [`schema_share`], or 0 for a file with no schema; nothing if no file has one.
432fn schema_shares(files: &[Job], threads: usize) -> Vec<usize> {
433    if files.iter().all(|((_, schema), _)| schema.is_none()) {
434        return Vec::new();
435    }
436    parallel(
437        files,
438        threads,
439        || false,
440        |((_, schema), file)| schema.as_ref().map_or(0, |_| schema_share(file)),
441    )
442}
443
444/// How many of the files with these shares the next chunk takes: up to and including the one
445/// that fills [`CHUNK_BYTES`], so always at least one.
446fn chunk_len(shares: &[usize]) -> usize {
447    let mut total = 0;
448    shares
449        .iter()
450        .position(|share| {
451            total += share;
452            total >= CHUNK_BYTES
453        })
454        .map_or(shares.len(), |last| last + 1)
455}
456
457/// The files to check: each argument as given, except that a directory stands for the files under
458/// it that jsonvet checks by default, in sorted order. The walk skips hidden files and what
459/// `.gitignore`, `.ignore` and the `exclude` setting of each folder's settings name, as ripgrep
460/// would.
461fn expand(
462    args: Vec<OsString>,
463    resolver: &Arc<Mutex<Resolver>>,
464    threads: usize,
465) -> Result<Vec<OsString>, String> {
466    let mut files = Vec::with_capacity(args.len());
467    // Each argument costs a call to the system, so thousands of them are asked about in parallel:
468    // whether it is a folder, and which file it is, for `unique`.
469    let found = parallel(
470        &args,
471        threads,
472        || false,
473        |arg| {
474            let meta = std::fs::metadata(arg);
475            let dir = arg != STDIN && meta.as_ref().is_ok_and(std::fs::Metadata::is_dir);
476            (dir, identity(arg, &meta))
477        },
478    );
479    // The identity of each of `files`, or `None` for a file a walk found.
480    let mut ids = Vec::with_capacity(args.len());
481    // Folders that share no file need no check for repeats: the walk follows no symlinks.
482    let mut roots = Vec::new();
483    let mut distinct = true;
484    // The user's global git ignore file, read once for every folder.
485    let mut global = None;
486    for (arg, (dir, id)) in args.into_iter().zip(found) {
487        if !dir {
488            distinct = false;
489            files.push(arg);
490            ids.push(Some(id));
491            continue;
492        }
493        let start = files.len();
494        let above = std::fs::canonicalize(&arg).ok();
495        match &above {
496            Some(root) => roots.push(root.clone()),
497            None => distinct = false,
498        }
499        let global = global.get_or_insert_with(|| Arc::new(Gitignore::global().0));
500        walk(&arg, above, global, resolver, &mut files)?;
501        ids.resize(files.len(), None);
502        files[start..].sort_unstable();
503    }
504    // Checking costs a call to the system for each file, 12% of a run over 10,000 small files.
505    let overlap = |i: usize, root: &PathBuf| {
506        roots[..i]
507            .iter()
508            .any(|r| r.starts_with(root) || root.starts_with(r))
509    };
510    if distinct && !roots.iter().enumerate().any(|(i, root)| overlap(i, root)) {
511        return Ok(files);
512    }
513    Ok(unique(files, ids, threads))
514}
515
516/// Add to `files` the files under the folder `arg` that jsonvet checks by default, in the order
517/// the walk finds them. `above` is the folder as a canonical path, if it has one, and `global`
518/// the rules of the user's global git ignore file.
519fn walk(
520    arg: &OsStr,
521    above: Option<PathBuf>,
522    global: &Arc<Gitignore>,
523    resolver: &Arc<Mutex<Resolver>>,
524    files: &mut Vec<OsString>,
525) -> Result<(), String> {
526    let global = Arc::clone(global);
527    // The rules of the folders above the walk, from `/` down, then of the walk's own.
528    let mut rules = None;
529    let mut ups: Vec<_> = above
530        .iter()
531        .flat_map(|dir| dir.ancestors().skip(1))
532        .collect();
533    ups.reverse();
534    for dir in ups {
535        rules = folder_rules(rules.as_ref(), dir, true)?;
536    }
537    let root = PathBuf::from(arg);
538    let rules = folder_rules(rules.as_ref(), &root, false)?;
539    let absolute = above.unwrap_or_default();
540    // The rules of each folder the walk has let in, by its path as walked.
541    let mut folders = std::collections::HashMap::new();
542    folders.insert(root.clone(), rules);
543    let folders = Mutex::new(folders);
544    let skipped = Arc::new(Mutex::new(None));
545    let skip = Arc::clone(&skipped);
546    // The filter must own what it reads.
547    let resolver = Arc::clone(resolver);
548    // A config file that cannot be read excludes nothing here: the run reports it when it
549    // looks up the settings of the files under it. The `ignore` crate's own ignore-file
550    // readers stay off: they open a FIFO or `/dev/zero` unchecked.
551    let walk = ignore::WalkBuilder::new(arg)
552        .standard_filters(false)
553        .filter_entry(move |entry| {
554            let path = entry.path();
555            let dir = entry.file_type().is_some_and(|t| t.is_dir());
556            let mut folders = folders
557                .lock()
558                .unwrap_or_else(std::sync::PoisonError::into_inner);
559            let rules = path
560                .parent()
561                .and_then(|up| folders.get(up))
562                .cloned()
563                .flatten();
564            let matched = rules.as_ref().map_or(ignore::Match::None, |rules| {
565                let absolute = || absolute.join(path.strip_prefix(&root).unwrap_or(path));
566                rules.matched(path, dir, absolute, &global)
567            });
568            if matched.is_ignore() || (matched.is_none() && hidden(entry)) {
569                return false;
570            }
571            let settings = lock(&resolver).for_file(path);
572            if settings.is_ok_and(|settings| settings.excluded(path)) {
573                return false;
574            }
575            if dir {
576                match folder_rules(rules.as_ref(), path, false) {
577                    Ok(rules) => {
578                        folders.insert(path.to_path_buf(), rules);
579                    }
580                    Err(problem) => {
581                        // A poisoned lock still holds the message.
582                        skip.lock()
583                            .unwrap_or_else(std::sync::PoisonError::into_inner)
584                            .get_or_insert(problem);
585                        return false;
586                    }
587                }
588            }
589            true
590        })
591        .build();
592    for entry in walk {
593        let entry = entry
594            .map_err(|e| output::printable(&messages::fill("error.walk", &[("error", &e)])))?;
595        if entry.file_type().is_some_and(|t| t.is_file())
596            && Dialect::for_path(entry.path()).is_some()
597        {
598            files.push(entry.into_path().into_os_string());
599        }
600    }
601    if let Some(problem) = skipped
602        .lock()
603        .unwrap_or_else(std::sync::PoisonError::into_inner)
604        .take()
605    {
606        return Err(problem);
607    }
608    Ok(())
609}
610
611/// What names a file apart from its spelling.
612#[derive(Clone, PartialEq, Eq, Hash)]
613enum Identity {
614    #[cfg(unix)]
615    Inode(u64, u64),
616    Path(PathBuf),
617}
618
619/// The identity of `file`, from `meta`, what `std::fs::metadata` said about it.
620fn identity(file: &OsStr, meta: &io::Result<std::fs::Metadata>) -> Identity {
621    #[cfg(unix)]
622    if let Ok(meta) = meta {
623        use std::os::unix::fs::MetadataExt;
624        return Identity::Inode(meta.dev(), meta.ino());
625    }
626    // Elsewhere the metadata has no stable file id, so the canonical path stands in.
627    #[cfg(not(unix))]
628    let _ = meta;
629    // A file that cannot be found stays as spelled, so it is reported once per spelling.
630    Identity::Path(std::fs::canonicalize(file).unwrap_or_else(|_| file.into()))
631}
632
633/// `files` without any that is the same file as an earlier one, so that each is checked once,
634/// under its first spelling. Standard input can be given only once, as the arguments check.
635/// `ids` holds each file's identity where it is known already.
636fn unique(files: Vec<OsString>, ids: Vec<Option<Identity>>, threads: usize) -> Vec<OsString> {
637    let ids: Vec<Identity> = if ids.iter().all(Option::is_some) {
638        ids.into_iter().flatten().collect()
639    } else {
640        let files: Vec<_> = files.iter().zip(ids).collect();
641        parallel(
642            &files,
643            threads,
644            || false,
645            |(file, id)| {
646                id.clone()
647                    .unwrap_or_else(|| identity(file, &std::fs::metadata(file)))
648            },
649        )
650    };
651    let mut seen = std::collections::HashSet::with_capacity(files.len());
652    files
653        .into_iter()
654        .zip(ids)
655        .filter_map(|(file, id)| seen.insert(id).then_some(file))
656        .collect()
657}
658
659/// The resolver, even if a panic elsewhere left its lock poisoned: it holds only a cache.
660fn lock(resolver: &Mutex<Resolver>) -> std::sync::MutexGuard<'_, Resolver> {
661    resolver
662        .lock()
663        .unwrap_or_else(std::sync::PoisonError::into_inner)
664}
665
666/// What `--fix` did over the whole run.
667#[derive(Default)]
668struct FixRun {
669    problems: usize,
670    files: usize,
671    /// Standard input as it is after the fixes, for standard output.
672    stdin: Option<String>,
673}
674
675impl FixRun {
676    /// Print the count to standard error when `count` is set, then standard input's fixed text to
677    /// `out`, which is written even if the count was not.
678    fn finish(&self, count: bool, out: &mut impl Write) -> io::Result<()> {
679        let counted = match self.summary() {
680            Some(summary) if count => writeln!(io::stderr(), "{summary}"),
681            _ => Ok(()),
682        };
683        let text = match &self.stdin {
684            Some(text) => out.write_all(text.as_bytes()).and_then(|()| out.flush()),
685            None => Ok(()),
686        };
687        counted.and(text)
688    }
689
690    /// Such as `fixed 3 problems in 2 files`, or `None` when nothing was fixed.
691    fn summary(&self) -> Option<String> {
692        (self.problems > 0).then(|| {
693            messages::fill(
694                "summary.fixed",
695                &[
696                    (
697                        "problems",
698                        &messages::count("summary.problems", self.problems),
699                    ),
700                    ("files", &messages::count("summary.files", self.files)),
701                ],
702            )
703        })
704    }
705}
706
707/// Fix the `texts` of `chunk` in place, and write each changed file, so the check that follows
708/// reports what is left. A file that cannot be written gets the reason in place of its text.
709fn fix_chunk(
710    chunk: &[Job],
711    texts: &mut [(Result<String, String>, bool)],
712    shown: &(impl Fn(&OsStr) -> OsString + Sync),
713    unsafe_fixes: bool,
714    threads: usize,
715    run: &mut FixRun,
716) -> Vec<Option<Vec<Problem>>> {
717    let jobs: Vec<_> = chunk.iter().zip(texts.iter()).collect();
718    let done = parallel(
719        &jobs,
720        threads,
721        || false,
722        |(((settings, _), file), (text, _))| {
723            let text = text.as_ref().ok()?;
724            let name = shown(file);
725            let config = settings.config_for(&name.to_string_lossy());
726            let fixed = match jsonvet::try_fix(text, &config, unsafe_fixes, None) {
727                Ok(fixed) => fixed,
728                Err(problems) => return Some(Err(problems)),
729            };
730            let saved = if file == STDIN {
731                Ok(())
732            } else {
733                write_atomically(Path::new(file), &fixed.text)
734            };
735            Some(Ok((fixed, saved)))
736        },
737    );
738    drop(jobs);
739    let mut known = Vec::with_capacity(done.len());
740    for (((_, file), entry), done) in chunk.iter().zip(texts.iter_mut()).zip(done) {
741        // The scan's problems stand for the check only while the text is unchanged.
742        known.push(match &done {
743            Some(Err(problems)) => problems.clone(),
744            _ => None,
745        });
746        let done = done.and_then(Result::ok);
747        match done {
748            Some((fixed, Ok(()))) => {
749                run.problems += fixed.fixes.len();
750                run.files += 1;
751                entry.0 = Ok(fixed.text);
752            }
753            Some((_, Err(error))) => {
754                entry.0 = Err(error);
755            }
756            None => {}
757        }
758        if file == STDIN
759            && let Ok(text) = &entry.0
760        {
761            run.stdin = Some(text.clone());
762        }
763    }
764    known
765}
766
767#[derive(Default)]
768struct Counts {
769    errors: usize,
770    warnings: usize,
771    /// Files with at least one problem, or that could not be read.
772    files: usize,
773}
774
775impl Counts {
776    /// Such as `3 errors and 1 warning in 2 of 5 files`.
777    fn summary(&self, checked: usize) -> String {
778        let files = messages::count("summary.files", checked);
779        if self.errors + self.warnings == 0 {
780            return messages::fill("summary.none", &[("files", &files)]);
781        }
782        let errors = || messages::count("summary.errors", self.errors);
783        let warnings = || messages::count("summary.warnings", self.warnings);
784        let problems = match (self.errors, self.warnings) {
785            (_, 0) => errors(),
786            (0, _) => warnings(),
787            _ => messages::fill(
788                "summary.both",
789                &[("errors", &errors()), ("warnings", &warnings())],
790            ),
791        };
792        messages::fill(
793            "summary.some",
794            &[
795                ("problems", &problems),
796                ("failed", &self.files),
797                ("files", &files),
798            ],
799        )
800    }
801}
802
803/// `f` on the items, on up to `threads` threads counting this one, each with a [`STACK`]-byte
804/// stack, until `stop` says so before an item is taken: the results for the items done, in
805/// order, always at least the first. A panic in `f` carries on here.
806fn parallel<T: Sync, U: Send>(
807    items: &[T],
808    threads: usize,
809    stop: impl Fn() -> bool + Sync,
810    f: impl Fn(&T) -> U + Sync,
811) -> Vec<U> {
812    let next = AtomicUsize::new(0);
813    // Each thread takes the next item in turn, so the items taken are always the first ones,
814    // and the results are tagged to put them back in order.
815    let work = || {
816        let mut done = Vec::new();
817        loop {
818            if stop() && next.load(Ordering::Relaxed) > 0 {
819                break done;
820            }
821            let i = next.fetch_add(1, Ordering::Relaxed);
822            let Some(item) = items.get(i) else { break done };
823            done.push((i, f(item)));
824        }
825    };
826    let mut done = std::thread::scope(|scope| {
827        // A thread that cannot start leaves its share to the others.
828        let helpers: Vec<_> = (1..threads.min(items.len()))
829            .filter_map(|_| {
830                std::thread::Builder::new()
831                    .stack_size(STACK)
832                    .spawn_scoped(scope, work)
833                    .ok()
834            })
835            .collect();
836        let mut done = work();
837        for helper in helpers {
838            done.extend(
839                helper
840                    .join()
841                    .unwrap_or_else(|panic| std::panic::resume_unwind(panic)),
842            );
843        }
844        done
845    });
846    done.sort_unstable_by_key(|&(i, _)| i);
847    done.into_iter().map(|(_, result)| result).collect()
848}
849
850/// The parts to check a text in `dialect` in, on up to `parts` threads: more than one only for
851/// JSON Lines of [`SPLIT_BYTES`] or more.
852fn split(text: &str, dialect: Dialect, parts: usize) -> Vec<std::ops::Range<usize>> {
853    let split = dialect == Dialect::JsonLines && parts > 1 && text.len() >= SPLIT_BYTES;
854    jsonvet::line_parts(text, if split { parts } else { 1 })
855}
856
857/// Check a file's text, named by its lossy UTF-8 form for glob matching, and against `schema`
858/// if there is one, on up to `parts` threads: the report and the schema errors.
859fn check<'t>(
860    name: &str,
861    text: &'t str,
862    settings: &Settings,
863    schema: Option<&Schema>,
864    parts: usize,
865    known: Option<&[Problem]>,
866) -> (Report<'t>, Vec<(usize, String, bool)>) {
867    let config = settings.config_for(name);
868    let ranges = split(text, config.dialect(), parts);
869    if ranges.len() > 1 {
870        // Each line is a document of its own, and the error cap counts per document, so each
871        // part's schema errors are the ones a single pass finds in its lines.
872        let (lines, errors): (Vec<_>, Vec<_>) = parallel(
873            &ranges,
874            parts,
875            || false,
876            |lines| match schema {
877                Some(schema) => schema.check_lines(text, lines.clone(), &config),
878                None => (
879                    jsonvet::parse_lines(text, lines.clone(), &config, None),
880                    Vec::new(),
881                ),
882            },
883        )
884        .into_iter()
885        .unzip();
886        let (report, syntax_error) = jsonvet::join_lines(text, lines);
887        // A file with a syntax error in any part gets no schema errors.
888        let errors = if syntax_error {
889            Vec::new()
890        } else {
891            errors.into_iter().flatten().collect()
892        };
893        return (report, errors);
894    }
895    match schema {
896        // A file with a syntax error gets no schema errors.
897        Some(schema) => schema.check(text, &config),
898        // The problems `--fix` found on its way, when it changed nothing.
899        None => match known {
900            Some(problems) => (
901                jsonvet::report(text, problems.to_vec(), &config),
902                Vec::new(),
903            ),
904            None => (jsonvet::check(text, &config), Vec::new()),
905        },
906    }
907}
908
909/// Check `text` as a run checks the file at `path`: with its settings and its schema, if any.
910/// `Err` is what ends such a run: a config file or schema that cannot be used.
911/// `on_schema` receives local schema dependencies, including missing ones and failed loads.
912fn check_file<'t>(
913    path: &Path,
914    text: &'t str,
915    resolver: Resolver,
916    on_schema: &impl Fn(&Path),
917) -> Result<Checked<'t>, String> {
918    let resolver = Mutex::new(resolver);
919    let settings_for = |path: &Path| lock(&resolver).for_file(path);
920    let schemas = Schemas::default();
921    let load = |path: &Path, quiet| {
922        on_schema(path);
923        schemas.get(path, quiet, |admit| {
924            Schema::load(path, &*settings_for(path)?, quiet, admit)
925        })
926    };
927    let root = |path: &Path| lock(&resolver).root(path);
928    let jobs = jobs(
929        vec![path.into()],
930        &OsStr::to_owned,
931        &settings_for,
932        None,
933        &load,
934    )?;
935    // One file in, one job out.
936    let Some(job @ ((settings, schema), _)) = jobs.first() else {
937        return Err(String::new());
938    };
939    let name = path.to_string_lossy();
940    let checked = match schema {
941        Some(schema) => check(&name, text, settings, Some(schema), 1, None),
942        None if with_schema(job, path.as_os_str(), text) => {
943            check_own_schema(&name, text, settings, &schemas, &root, &load, 1)
944        }
945        None => check(&name, text, settings, None, 1, None),
946    };
947    schemas.observe_missing(on_schema);
948    schemas.over()?;
949    Ok(checked)
950}
951
952/// How text output looks: code frames on a terminal and lines otherwise, so tools reading a pipe
953/// always get `file:line:col`, and in color as `--color` and the environment ask.
954fn look(format: Format, color: Option<bool>, terminal: Terminal) -> Look {
955    let text = format == Format::Text;
956    Look {
957        frames: text && terminal.tty,
958        color: text && color.or(terminal.color).unwrap_or(terminal.tty),
959    }
960}
961
962/// Print one file's problems and schema errors, and add them to `counts`, each before it is
963/// printed, so a failed write still leaves the verdict. The file count is added last: only the
964/// summary reads it, and a failed write ends the run before the summary.
965fn print_file(
966    file: &OsStr,
967    report: &Report<'_>,
968    schema_errors: &[(usize, String, bool)],
969    printer: &mut Printer<impl Write>,
970    counts: &mut Counts,
971) -> io::Result<()> {
972    let before = counts.errors + counts.warnings;
973    for problem in report.problems() {
974        match problem.severity() {
975            Severity::Error => counts.errors += 1,
976            Severity::Warning => counts.warnings += 1,
977            _ => {}
978        }
979        printer.problem(file, report, problem)?;
980    }
981    for (offset, message, summary) in schema_errors {
982        counts.errors += usize::from(!summary);
983        printer.schema_error(file, report, *offset, message, *summary)?;
984    }
985    counts.files += usize::from(counts.errors + counts.warnings > before);
986    Ok(())
987}
988
989/// `path` opened for reading, with its size, if it is a regular file or a symlink to one. The
990/// type is checked on the open file, not the path, so a path swapped for a FIFO or a device after
991/// a check is still refused, and on Unix opening a FIFO does not wait for a writer.
992fn open_regular(path: &Path) -> io::Result<Option<(std::fs::File, u64)>> {
993    // shortcut: elsewhere the path is checked before the open, which a swap can race; Windows
994    // has no FIFO to swap in at a file path.
995    #[cfg(not(unix))]
996    if !std::fs::metadata(path)?.is_file() {
997        return Ok(None);
998    }
999    let mut options = std::fs::OpenOptions::new();
1000    options.read(true);
1001    // A regular file reads the same without blocking.
1002    #[cfg(unix)]
1003    std::os::unix::fs::OpenOptionsExt::custom_flags(&mut options, libc::O_NONBLOCK);
1004    let file = options.open(path)?;
1005    let meta = file.metadata()?;
1006    Ok(meta.is_file().then_some((file, meta.len())))
1007}
1008
1009/// A file's text, or why it has none. Only regular files are read, so a FIFO cannot hang the
1010/// run and a device such as `/dev/zero` cannot fill memory. A symlink to a regular file is
1011/// followed, as `cat` would.
1012fn read(path: &Path) -> Result<String, String> {
1013    let read = open_regular(path).and_then(|opened| {
1014        if let Some((file, len)) = opened {
1015            // The size is known already: `fs::read`, and `File`'s own `read_to_end`, would ask
1016            // the system for it again. `take` reads to the end without asking. A file too big for
1017            // memory fails alone, as an error, rather than aborting the run, but only where an
1018            // oversized reservation fails: Linux's default heuristic overcommit
1019            // (`vm.overcommit_memory=0`). Under mode 1 or on macOS it succeeds and the read can
1020            // fill memory. NOTE: no cap against available memory, as std has no portable query.
1021            let mut bytes = Vec::new();
1022            bytes.try_reserve_exact(usize::try_from(len).unwrap_or(usize::MAX))?;
1023            file.take(u64::MAX)
1024                .read_to_end(&mut bytes)
1025                .map(|_| Some(bytes))
1026        } else {
1027            Ok(None)
1028        }
1029    });
1030    match read {
1031        Ok(Some(bytes)) => decode(bytes),
1032        Ok(None) => Err(messages::text("read.not-regular").to_owned()),
1033        Err(e) if e.kind() == io::ErrorKind::NotFound => {
1034            Err(messages::text("read.missing").to_owned())
1035        }
1036        Err(e) => Err(messages::fill("read.error", &[("error", &e)])),
1037    }
1038}
1039
1040/// Standard input's text, or why it has none. Empty input is an empty file.
1041fn read_stdin() -> Result<String, String> {
1042    let mut bytes = Vec::new();
1043    match io::stdin().lock().read_to_end(&mut bytes) {
1044        Ok(_) => decode(bytes),
1045        Err(e) => Err(messages::fill("read.error", &[("error", &e)])),
1046    }
1047}
1048
1049/// A file's bytes as UTF-8 text. A UTF-16 or UTF-32 file says so, rather than that its first
1050/// byte is not UTF-8: by its byte order mark, or without one by where the zero bytes of its first
1051/// two characters fall, as RFC 4627 section 3 tells the encodings apart.
1052fn decode(bytes: Vec<u8>) -> Result<String, String> {
1053    // Without a mark, a text these patterns match would fail the check anyway: a zero byte is
1054    // U+0000, and outside a string no dialect allows it. JSON5 does allow it raw inside one, so
1055    // a first byte that opens a string is left alone.
1056    let opens_string = |b: u8| b == b'"' || b == b'\'';
1057    // UTF-32 LE first: its mark starts with UTF-16 LE's.
1058    let encoding = match *bytes.as_slice() {
1059        [0xFF, 0xFE, 0, 0, ..] => Some("UTF-32 (LE)"),
1060        [0, 0, 0xFE, 0xFF, ..] => Some("UTF-32 (BE)"),
1061        [0xFF, 0xFE, ..] => Some("UTF-16 (LE)"),
1062        [0xFE, 0xFF, ..] => Some("UTF-16 (BE)"),
1063        [0, 0, 0, a, ..] if a != 0 => Some("UTF-32 (BE)"),
1064        [0, a, 0, b, ..] if a != 0 && b != 0 => Some("UTF-16 (BE)"),
1065        [a, 0, 0, 0, ..] if a != 0 && !opens_string(a) => Some("UTF-32 (LE)"),
1066        [a, 0, b, 0, ..] if a != 0 && b != 0 && !opens_string(a) => Some("UTF-16 (LE)"),
1067        _ => None,
1068    };
1069    if let Some(encoding) = encoding {
1070        return Err(messages::fill("read.encoding", &[("encoding", &encoding)]));
1071    }
1072    String::from_utf8(bytes).map_err(|e| {
1073        let byte = e.utf8_error().valid_up_to();
1074        messages::fill("read.not-utf8", &[("byte", &byte)])
1075    })
1076}
1077
1078/// The most a config file or an ignore file may hold: real ones take a few kilobytes.
1079pub(crate) const MAX_CONFIG_BYTES: u64 = 16 << 20;
1080
1081/// The most a `--schema` file may hold: 40 times the largest schema on schemastore.org
1082/// (1.6 MB), and room for the 5,000,000 objects and arrays a schema may hold at 2 to 3 bytes each.
1083pub(crate) const MAX_SCHEMA_BYTES: u64 = 64 << 20;
1084
1085/// The text of a config or schema file of at most `max` bytes. As in [`read`], only a regular
1086/// file, or a symlink to one, is opened, so a FIFO cannot hang the run and `/dev/zero` cannot
1087/// fill memory.
1088pub(crate) fn read_small(path: &Path, max: u64) -> io::Result<String> {
1089    String::from_utf8(read_small_bytes(path, max)?).map_err(|_| {
1090        io::Error::new(
1091            io::ErrorKind::InvalidData,
1092            "stream did not contain valid UTF-8",
1093        )
1094    })
1095}
1096
1097/// [`read_small`], as bytes.
1098fn read_small_bytes(path: &Path, max: u64) -> io::Result<Vec<u8>> {
1099    let Some((file, _)) = open_regular(path)? else {
1100        return Err(io::Error::other(messages::text("read.not-regular")));
1101    };
1102    let mut bytes = Vec::new();
1103    // The cap holds even for a file that grows after the check.
1104    file.take(max + 1).read_to_end(&mut bytes)?;
1105    if bytes.len() as u64 > max {
1106        let mb = max >> 20;
1107        return Err(io::Error::other(messages::fill(
1108            "read.too-large",
1109            &[("mb", &mb)],
1110        )));
1111    }
1112    Ok(bytes)
1113}
1114
1115/// The rules of one folder's ignore files, and of the folders above it that have any, applied
1116/// as the `ignore` crate applies them by default.
1117struct IgnoreRules {
1118    up: Option<Arc<IgnoreRules>>,
1119    /// A folder above the walk's own, whose rules match absolute paths.
1120    above: bool,
1121    /// The folder holds a `.git` or a `.jj`, so it is a repository's root.
1122    repo: bool,
1123    ignore: Gitignore,
1124    git: Gitignore,
1125    exclude: Gitignore,
1126}
1127
1128impl IgnoreRules {
1129    /// What the rules say of `path`, as walked; `absolute` gives it as an absolute path, for the
1130    /// folders above the walk. `.ignore` wins over `.gitignore`, over `info/exclude`, over the
1131    /// user's `global` file, and the nearest folder wins within each. The git files apply only in
1132    /// a repository, and not across a nested repository's root.
1133    fn matched<'a>(
1134        &'a self,
1135        path: &Path,
1136        dir: bool,
1137        absolute: impl Fn() -> PathBuf,
1138        global: &'a Gitignore,
1139    ) -> ignore::Match<&'a ignore::gitignore::Glob> {
1140        let chain = || std::iter::successors(Some(self), |rules| rules.up.as_deref());
1141        let in_repo = chain().any(|rules| rules.repo);
1142        let (mut ignore, mut git, mut exclude) = (Match::None, Match::None, Match::None);
1143        let mut past_repo = false;
1144        let mut whole = None;
1145        for rules in chain() {
1146            let path = if rules.above {
1147                whole.get_or_insert_with(&absolute).as_path()
1148            } else {
1149                path
1150            };
1151            if ignore.is_none() {
1152                ignore = rules.ignore.matched(path, dir);
1153            }
1154            if in_repo && !past_repo {
1155                if git.is_none() {
1156                    git = rules.git.matched(path, dir);
1157                }
1158                if exclude.is_none() {
1159                    exclude = rules.exclude.matched(path, dir);
1160                }
1161            }
1162            past_repo |= rules.repo;
1163        }
1164        let global = if in_repo {
1165            global.matched(path, dir)
1166        } else {
1167            Match::None
1168        };
1169        ignore.or(git).or(exclude).or(global)
1170    }
1171}
1172
1173/// The ignore rules for `dir`, under those of the folders above it, `up`: `up` itself when
1174/// `dir` adds none. Each ignore file is read as a config file is, so a link to `/dev/zero`
1175/// or a FIFO stops the walk with an error instead of filling memory or hanging it. A file that
1176/// is missing or cannot be opened adds no rules, as in git.
1177fn folder_rules(
1178    up: Option<&Arc<IgnoreRules>>,
1179    dir: &Path,
1180    above: bool,
1181) -> Result<Option<Arc<IgnoreRules>>, String> {
1182    let dot_git = std::fs::metadata(dir.join(".git")).ok();
1183    let repo = dot_git.is_some() || dir.join(".jj").exists();
1184    let ignore = ignore_rules(dir, Some(&dir.join(".ignore")))?;
1185    let git = ignore_rules(dir, Some(&dir.join(".gitignore")))?;
1186    let git_dir = match dot_git {
1187        Some(meta) if meta.is_file() => common_dir(dir)?,
1188        Some(_) => Some(dir.join(".git")),
1189        None => None,
1190    };
1191    let exclude = ignore_rules(dir, git_dir.map(|d| d.join("info/exclude")).as_deref())?;
1192    if !repo && ignore.is_empty() && git.is_empty() && exclude.is_empty() {
1193        return Ok(up.cloned());
1194    }
1195    Ok(Some(Arc::new(IgnoreRules {
1196        up: up.cloned(),
1197        above,
1198        repo,
1199        ignore,
1200        git,
1201        exclude,
1202    })))
1203}
1204
1205/// The folder a worktree's `.git` file leads to, which holds its `info/exclude`, if it has one.
1206fn common_dir(dir: &Path) -> Result<Option<PathBuf>, String> {
1207    let first_line = |path: &Path| match read_small(path, MAX_CONFIG_BYTES) {
1208        Ok(text) => Ok(text.lines().next().map(str::to_owned)),
1209        Err(e) if e.kind() == io::ErrorKind::Other => Err(walk_error(path, &e)),
1210        Err(_) => Ok(None),
1211    };
1212    let Some(git_dir) = first_line(&dir.join(".git"))?
1213        .and_then(|line| line.strip_prefix("gitdir: ").map(|d| dir.join(d)))
1214    else {
1215        return Ok(None);
1216    };
1217    // A submodule's `.git` file leads to a folder with no `commondir`, and git reads no
1218    // `info/exclude` for it.
1219    let Some(common) = first_line(&git_dir.join("commondir"))? else {
1220        return Ok(None);
1221    };
1222    Ok(Some(git_dir.join(common)))
1223}
1224
1225/// The rules in the ignore file at `file`, matched relative to `dir`. As in git, a line that is
1226/// not a valid pattern matches nothing, and reading stops at the first line that is not UTF-8.
1227fn ignore_rules(dir: &Path, file: Option<&Path>) -> Result<Gitignore, String> {
1228    let mut builder = GitignoreBuilder::new(dir);
1229    if let Some(file) = file {
1230        match read_small_bytes(file, MAX_CONFIG_BYTES) {
1231            Ok(bytes) => {
1232                let text = match std::str::from_utf8(&bytes) {
1233                    Ok(text) => text,
1234                    Err(e) => {
1235                        let valid = &bytes[..e.valid_up_to()];
1236                        let whole = valid
1237                            .iter()
1238                            .rposition(|&b| b == b'\n')
1239                            .map_or(&[][..], |end| &valid[..end]);
1240                        std::str::from_utf8(whole).unwrap_or_default()
1241                    }
1242                };
1243                let text = text.strip_prefix('\u{feff}').unwrap_or(text);
1244                for line in text.lines() {
1245                    let _ = builder.add_line(None, line);
1246                }
1247            }
1248            Err(e) if e.kind() == io::ErrorKind::Other => return Err(walk_error(file, &e)),
1249            Err(_) => {}
1250        }
1251    }
1252    Ok(builder.build().unwrap_or_else(|_| Gitignore::empty()))
1253}
1254
1255/// The walk's error for the file at `path`.
1256fn walk_error(path: &Path, problem: &io::Error) -> String {
1257    let error = format!("{}: {problem}", path.display());
1258    output::printable(&messages::fill("error.walk", &[("error", &error)]))
1259}
1260
1261/// Whether the walk skips `entry` as hidden: its name starts with a dot, or on Windows the
1262/// system marks it hidden.
1263fn hidden(entry: &ignore::DirEntry) -> bool {
1264    #[cfg(windows)]
1265    {
1266        use std::os::windows::fs::MetadataExt;
1267        // FILE_ATTRIBUTE_HIDDEN
1268        if entry
1269            .metadata()
1270            .is_ok_and(|meta| meta.file_attributes() & 0x2 != 0)
1271        {
1272            return true;
1273        }
1274    }
1275    entry.file_name().as_encoded_bytes().starts_with(b".")
1276}
1277
1278fn print(mut out: impl Write, text: &str) -> Result<u8, String> {
1279    match writeln!(out, "{text}").and_then(|()| out.flush()) {
1280        Err(e) if e.kind() != io::ErrorKind::BrokenPipe => {
1281            Err(messages::fill("error.write", &[("error", &e)]))
1282        }
1283        _ => Ok(0),
1284    }
1285}
1286
1287#[cfg(test)]
1288mod tests {
1289    use std::fmt::Write as _;
1290    use std::sync::atomic::{AtomicUsize, Ordering};
1291
1292    use jsonvet::Dialect;
1293
1294    use super::{
1295        CHUNK_BYTES, MAX_CONFIG_BYTES, SCHEMA_BYTES, SPLIT_BYTES, STDIN, Schema, Settings, check,
1296        chunk_len, folder_rules, parallel, read_small, schema_share, schema_shares, split,
1297        take_chunk,
1298    };
1299    use crate::schema::select::Job;
1300    use std::sync::Arc;
1301
1302    /// The type is checked on the opened file, and opening a FIFO with no writer returns at
1303    /// once, so a path swapped for one between a check and the open cannot hang the run.
1304    #[cfg(unix)]
1305    #[test]
1306    fn opening_a_fifo_does_not_wait_for_a_writer() {
1307        let dir = std::env::temp_dir().join(format!("jsonvet-fifo-{}", std::process::id()));
1308        std::fs::create_dir_all(&dir).expect("a temporary directory");
1309        let fifo = dir.join("fifo.json");
1310        let made = std::process::Command::new("mkfifo")
1311            .arg(&fifo)
1312            .status()
1313            .expect("run mkfifo");
1314        assert!(made.success());
1315        let (send, receive) = std::sync::mpsc::channel();
1316        let path = fifo.clone();
1317        std::thread::spawn(move || {
1318            let opened = super::open_regular(&path).map(|o| o.is_none());
1319            let _ = send.send(opened.map_err(|e| e.to_string()));
1320        });
1321        let refused = receive.recv_timeout(std::time::Duration::from_secs(5));
1322        let _ = std::fs::remove_dir_all(&dir);
1323        assert_eq!(
1324            refused,
1325            Ok(Ok(true)),
1326            "the open waited, or did not refuse the FIFO"
1327        );
1328    }
1329
1330    /// A schema that takes any value, in a fresh folder named for `test`, which also holds an
1331    /// empty folder `x`.
1332    fn any_schema(test: &str) -> (std::path::PathBuf, Schema) {
1333        let dir = std::env::temp_dir().join(format!("jsonvet-{test}-{}", std::process::id()));
1334        std::fs::create_dir_all(dir.join("x")).expect("a temporary directory");
1335        std::fs::write(dir.join("s.json"), "{}").expect("written");
1336        let schema = Schema::load(&dir.join("s.json"), &Settings::default(), false, |_| Ok(()));
1337        (dir, schema.expect("a schema"))
1338    }
1339
1340    #[test]
1341    fn config_text_reports_a_bad_file() {
1342        let path = std::path::Path::new("/nonexistent/jsonvet.toml");
1343        assert_eq!(super::check_config_text(path, "rules = {}"), Ok(()));
1344        let message = super::check_config_text(path, "rules = [").expect_err("bad toml");
1345        assert!(
1346            message.starts_with("/nonexistent/jsonvet.toml:1:"),
1347            "{message}"
1348        );
1349    }
1350
1351    #[test]
1352    fn only_files_with_a_schema_take_a_share() {
1353        let (dir, schema) = any_schema("shares");
1354        let _ = std::fs::remove_dir_all(&dir);
1355        let schema = Arc::new(schema);
1356        let settings = Arc::new(Settings::default());
1357        let job =
1358            |schema: Option<&Arc<Schema>>| ((Arc::clone(&settings), schema.cloned()), STDIN.into());
1359        assert_eq!(
1360            schema_shares(&[job(None), job(None)], 2),
1361            Vec::<usize>::new()
1362        );
1363        assert_eq!(
1364            schema_shares(&[job(None), job(Some(&schema)), job(None)], 2),
1365            [0, CHUNK_BYTES, 0]
1366        );
1367    }
1368
1369    #[test]
1370    fn small_files_are_read_up_to_their_limit_and_no_further() {
1371        let dir = std::env::temp_dir().join(format!("jsonvet-small-{}", std::process::id()));
1372        std::fs::create_dir_all(&dir).expect("a temporary directory");
1373        let path = dir.join("f");
1374        std::fs::write(&path, "0123456789").expect("written");
1375        assert_eq!(read_small(&path, 10).expect("ten bytes"), "0123456789");
1376        assert!(read_small(&path, 9).is_err());
1377        // An ignore file is judged by its size alone; sparse files, so nothing is written.
1378        let sized = |len: u64| {
1379            std::fs::File::create(dir.join(".gitignore"))
1380                .and_then(|file| file.set_len(len))
1381                .expect("a large file");
1382        };
1383        sized(MAX_CONFIG_BYTES);
1384        assert!(folder_rules(None, &dir, false).is_ok());
1385        sized(MAX_CONFIG_BYTES + 1);
1386        assert!(folder_rules(None, &dir, false).is_err_and(|e| e.ends_with("larger than 16 MB")));
1387        let _ = std::fs::remove_dir_all(&dir);
1388    }
1389
1390    #[test]
1391    fn only_json_lines_of_the_split_size_is_split_one_part_per_thread() {
1392        let line = "[1]\n".repeat(SPLIT_BYTES / 4);
1393        assert_eq!(split(&line, Dialect::JsonLines, 8).len(), 8);
1394        assert_eq!(split(&line, Dialect::JsonLines, 1).len(), 1);
1395        assert_eq!(split(&line, Dialect::Json, 8).len(), 1);
1396        assert_eq!(split(&line[4..], Dialect::JsonLines, 8).len(), 1);
1397    }
1398
1399    #[test]
1400    fn a_large_json_lines_file_checked_in_parts_reports_what_one_pass_does() {
1401        // Problems in every part: duplicate keys, and strings the schema wants as integers. Every
1402        // thousandth line has more schema errors than a document reports, and two in each
1403        // thousand have values the schema cannot check.
1404        let pad = "x".repeat(500);
1405        let wide: Vec<String> = (0..150).map(|k| format!("\"k{k}\": \"s\"")).collect();
1406        let wide = format!("{{{}}}", wide.join(", "));
1407        let mut text = String::from("\u{FEFF}");
1408        for i in 0..4_000 {
1409            let _ = match i % 7 {
1410                _ if i % 1_000 == 999 => writeln!(text, "{wide}"),
1411                _ if i % 1_000 == 500 => writeln!(text, "{{\"a\": 1e400}}"),
1412                _ if i % 1_000 == 501 => writeln!(text, r#"{{"\ud800": 1, "\ud801": 2}}"#),
1413                0 => writeln!(text, "{{\"a\": {i}, \"a\": 1}}"),
1414                1 => write!(text, "{{\"a\": \"{i}\", \"p\": \"{pad}\"}}\r\n"),
1415                _ => writeln!(text, "{{\"a\": {i}, \"p\": \"{pad}\"}}"),
1416            };
1417        }
1418        assert!(text.len() > SPLIT_BYTES);
1419        let deep = format!("{}{}\n", "[".repeat(600), "]".repeat(600));
1420        let texts = [
1421            text.clone(),
1422            format!("{text}{{\"a\": 1\n{text}"),
1423            format!("{text}{deep}{text}"),
1424        ];
1425        let dir = std::env::temp_dir().join(format!("jsonvet-split-{}", std::process::id()));
1426        std::fs::create_dir_all(&dir).expect("a temporary directory");
1427        let path = dir.join("schema.json");
1428        let schema = concat!(
1429            r#"{"properties": {"a": {"type": "integer"}}, "#,
1430            r#""patternProperties": {"^k": {"type": "integer"}}}"#,
1431        );
1432        std::fs::write(&path, schema).expect("written");
1433        let settings = Settings::default();
1434        let schema = Schema::load(&path, &settings, false, |_| Ok(())).expect("a schema");
1435        let _ = std::fs::remove_dir_all(&dir);
1436        for text in &texts {
1437            for schema in [None, Some(&schema)] {
1438                let (whole, whole_errors) = check("x.jsonl", text, &settings, schema, 1, None);
1439                let (split, split_errors) = check("x.jsonl", text, &settings, schema, 8, None);
1440                assert_ne!(whole.problems(), &[]);
1441                assert_eq!(split.problems(), whole.problems());
1442                assert_eq!(split_errors, whole_errors);
1443            }
1444        }
1445        let (_, errors) = check("x.jsonl", &texts[0], &settings, Some(&schema), 8, None);
1446        // 570 strings, 100 and a summary for each wide line, and one for each line not checked.
1447        assert_eq!(errors.len(), 570 + 4 * 101 + 8);
1448        let unchecked = errors.iter().filter(|(_, m, _)| m.contains("not checked"));
1449        assert_eq!(unchecked.count(), 8);
1450    }
1451
1452    #[test]
1453    fn parallel_keeps_the_order_and_stops_after_a_prefix() {
1454        let items: Vec<usize> = (0..1000).collect();
1455        for threads in [1, 3, 16] {
1456            let squares = parallel(&items, threads, || false, |i| i * i);
1457            assert_eq!(squares, items.iter().map(|i| i * i).collect::<Vec<_>>());
1458            // Stopped from the start, it still does the first item, and a thread that looks
1459            // before another has taken it may take one more.
1460            let stopped = parallel(&items, threads, || true, |&i| i);
1461            assert!((1..=threads).contains(&stopped.len()), "{stopped:?}");
1462            assert_eq!(stopped, (0..stopped.len()).collect::<Vec<_>>());
1463            let done = AtomicUsize::new(0);
1464            let some = parallel(
1465                &items,
1466                threads,
1467                || done.load(Ordering::Relaxed) >= 100,
1468                |&i| {
1469                    done.fetch_add(1, Ordering::Relaxed);
1470                    i
1471                },
1472            );
1473            // At least 100, plus at most one per thread already under way.
1474            assert!((100..100 + threads).contains(&some.len()), "{}", some.len());
1475            assert_eq!(some, (0..some.len()).collect::<Vec<_>>());
1476        }
1477        assert_eq!(
1478            parallel(&[] as &[u8], 4, || false, |&b| b),
1479            Vec::<u8>::new()
1480        );
1481    }
1482
1483    #[test]
1484    fn a_chunk_ends_with_the_file_that_fills_it() {
1485        assert_eq!(chunk_len(&[]), 0);
1486        assert_eq!(chunk_len(&[1, 2, 3]), 3);
1487        assert_eq!(chunk_len(&[CHUNK_BYTES, 1]), 1);
1488        assert_eq!(chunk_len(&[CHUNK_BYTES - 1, 1, 1]), 2);
1489        assert_eq!(chunk_len(&[CHUNK_BYTES - 2, 1, 1]), 3);
1490        assert_eq!(chunk_len(&[0, CHUNK_BYTES - 1, 0, 2, 5]), 4);
1491    }
1492
1493    #[test]
1494    fn take_chunk_respects_shares() {
1495        let dir = std::env::temp_dir().join(format!("jsonvet-chunk-{}", std::process::id()));
1496        std::fs::create_dir_all(&dir).expect("dir");
1497        let f1 = dir.join("1.json");
1498        let f2 = dir.join("2.json");
1499        std::fs::write(&f1, "{}").expect("write");
1500        std::fs::write(&f2, "{}").expect("write");
1501        let settings = Arc::new(Settings::default());
1502        let job1: Job = ((settings.clone(), None), f1.into_os_string());
1503        let job2: Job = ((settings.clone(), None), f2.into_os_string());
1504        let rest = [job1, job2];
1505        let mut ahead = Vec::new();
1506        let shares = [CHUNK_BYTES, CHUNK_BYTES];
1507        let taken = take_chunk(&mut ahead, &rest, Some(&shares), 1, &|_, _| false);
1508        assert_eq!(taken.len(), 1);
1509        assert_eq!(ahead, []);
1510        let _ = std::fs::remove_dir_all(&dir);
1511    }
1512
1513    #[test]
1514    fn a_file_counts_its_size_twenty_times_with_a_schema() {
1515        let dir = std::env::temp_dir().join(format!("jsonvet-share-{}", std::process::id()));
1516        std::fs::create_dir_all(&dir).expect("a temp dir");
1517        let path = dir.join("a.json");
1518        std::fs::write(&path, " ".repeat(1000)).expect("write");
1519        let path = path.as_os_str();
1520        assert_eq!(schema_share(path), 1000 * SCHEMA_BYTES);
1521        std::fs::write(path, " ".repeat(CHUNK_BYTES / SCHEMA_BYTES + 1)).expect("write");
1522        assert_eq!(schema_share(path), CHUNK_BYTES, "capped");
1523        assert_eq!(schema_share(dir.join("gone.json").as_os_str()), 0);
1524        assert_eq!(schema_share(std::ffi::OsStr::new("-")), CHUNK_BYTES);
1525        let _ = std::fs::remove_dir_all(&dir);
1526    }
1527
1528    #[test]
1529    fn frames_show_on_a_terminal_and_color_follows_the_option_then_the_environment() {
1530        use super::{Format, Look, Terminal, look};
1531        let terminal = |tty, color| Terminal { tty, color };
1532        // Off a terminal, `color_forced_off_a_terminal_keeps_one_line_per_problem` in
1533        // tests/cli.rs covers each case end to end.
1534        let frames = |color| Look {
1535            frames: true,
1536            color,
1537        };
1538        let lines = Look {
1539            frames: false,
1540            color: false,
1541        };
1542        let cases = [
1543            (Format::Text, None, terminal(true, None), frames(true)),
1544            (
1545                Format::Text,
1546                None,
1547                terminal(true, Some(false)),
1548                frames(false),
1549            ),
1550            (
1551                Format::Text,
1552                Some(false),
1553                terminal(true, Some(true)),
1554                frames(false),
1555            ),
1556            (Format::Sarif, Some(true), terminal(true, None), lines),
1557            (Format::Json, Some(true), terminal(true, None), lines),
1558        ];
1559        for (format, color, terminal, want) in cases {
1560            assert_eq!(
1561                look(format, color, terminal),
1562                want,
1563                "{format:?} {color:?} {terminal:?}"
1564            );
1565        }
1566    }
1567
1568    #[test]
1569    fn the_summary_counts_problems_and_files() {
1570        let dir = std::env::temp_dir().join(format!("jsonvet-summary-{}", std::process::id()));
1571        std::fs::create_dir_all(&dir).expect("a temp dir");
1572        let dup = dir.join("dup.json");
1573        let ok = dir.join("ok.json");
1574        std::fs::write(&dup, "{\"a\": 1, \"a\": 2}").expect("write");
1575        std::fs::write(&ok, "{}").expect("write");
1576        let run = |summary: bool, files: &[&std::path::Path]| {
1577            let mut out = Vec::new();
1578            let args = ["--no-config".into()]
1579                .into_iter()
1580                .chain(files.iter().map(|f| f.as_os_str().to_owned()));
1581            let terminal = super::Terminal {
1582                tty: summary,
1583                color: Some(false),
1584            };
1585            let code = super::run_with(args, false, terminal, &mut out).expect("a check");
1586            (code, String::from_utf8(out).expect("UTF-8"))
1587        };
1588        let (code, out) = run(true, &[&dup, &ok]);
1589        assert_eq!(code, 1);
1590        assert!(out.ends_with("\n1 error in 1 of 2 files\n"), "{out}");
1591        // A file that cannot be read counts as a file with an error.
1592        let gone = dir.join("gone.json");
1593        // The clean file last, so counting it instead of a file with problems shows.
1594        let dup2 = dir.join("dup2.json");
1595        std::fs::write(&dup2, "{\"a\": 1, \"a\": 2}").expect("write");
1596        let (_, out) = run(true, &[&dup, &gone, &dup2, &ok]);
1597        assert!(out.ends_with("\n3 errors in 3 of 4 files\n"), "{out}");
1598        assert_eq!(run(true, &[&ok]).1, "no problems in 1 file\n");
1599        // No summary off a terminal, nor for no files.
1600        assert!(!run(false, &[&dup]).1.contains(" in 1 of 1 file"));
1601        assert_eq!(run(true, &[]).1, "");
1602        // SARIF and JSON stay one document on a terminal too.
1603        for format in ["sarif", "json"] {
1604            let args = ["--no-config", "--format", format]
1605                .map(std::ffi::OsString::from)
1606                .into_iter()
1607                .chain([dup.clone().into_os_string()]);
1608            let mut out = Vec::new();
1609            let terminal = super::Terminal {
1610                tty: true,
1611                color: None,
1612            };
1613            super::run_with(args, false, terminal, &mut out).expect("a check");
1614            let out = String::from_utf8(out).expect("UTF-8");
1615            assert!(
1616                serde_json::from_str::<serde_json::Value>(&out).is_ok(),
1617                "{out}"
1618            );
1619        }
1620        let _ = std::fs::remove_dir_all(&dir);
1621    }
1622
1623    /// Text output takes a translation; SARIF and JSON stay English with one in place.
1624    #[test]
1625    fn a_translation_reaches_text_output_only() {
1626        let dir = std::env::temp_dir().join(format!("jsonvet-locale-{}", std::process::id()));
1627        std::fs::create_dir_all(&dir).expect("a temp dir");
1628        let dup = dir.join("dup.json");
1629        std::fs::write(&dup, "{\"a\": 1, \"a\": 2}").expect("write");
1630        crate::messages::use_translation(
1631            "\"duplicate-key\" = \"XX {path}\"\n\"related.first-at\" = \"XX {line}:{column}\"\n",
1632        );
1633        let run = |format: &str| {
1634            let args = ["--no-config", "--format", format]
1635                .map(std::ffi::OsString::from)
1636                .into_iter()
1637                .chain([dup.clone().into_os_string()]);
1638            let mut out = Vec::new();
1639            super::run_with(args, false, super::Terminal::default(), &mut out).expect("a check");
1640            String::from_utf8(out).expect("UTF-8")
1641        };
1642        let text = run("text");
1643        assert!(text.contains("XX $.a (XX 1:2)"), "{text}");
1644        for format in ["json", "sarif"] {
1645            let out = run(format);
1646            assert!(out.contains("duplicate key $.a (first at 1:2)"), "{out}");
1647            assert!(!out.contains("XX"), "{out}");
1648        }
1649        let _ = std::fs::remove_dir_all(&dir);
1650    }
1651
1652    /// A JSON Lines file and a `--schema` error get the same code frames on a terminal as any
1653    /// other problem, at the line they are on.
1654    #[test]
1655    fn json_lines_and_schema_errors_get_code_frames_on_a_terminal() {
1656        let dir = std::env::temp_dir().join(format!("jsonvet-frames-{}", std::process::id()));
1657        std::fs::create_dir_all(&dir).expect("a temp dir");
1658        let lines = dir.join("l.jsonl");
1659        let schema = dir.join("s.json");
1660        let data = dir.join("d.json");
1661        std::fs::write(&lines, "{\"a\": 1}\n{\"a\": True}\n").expect("write");
1662        std::fs::write(&schema, r#"{"properties": {"a": {"type": "string"}}}"#).expect("write");
1663        std::fs::write(&data, "{\n  \"a\": 1\n}\n").expect("write");
1664        let run = |args: &[&std::ffi::OsStr]| {
1665            let args = ["--no-config".into()]
1666                .into_iter()
1667                .chain(args.iter().map(|a| (*a).to_owned()));
1668            let terminal = super::Terminal {
1669                tty: true,
1670                color: Some(false),
1671            };
1672            let mut out = Vec::new();
1673            let code = super::run_with(args, false, terminal, &mut out).expect("a check");
1674            let out = String::from_utf8(out).expect("UTF-8");
1675            (
1676                code,
1677                out.replace(
1678                    &format!("{}{}", dir.display(), std::path::MAIN_SEPARATOR),
1679                    "",
1680                ),
1681            )
1682        };
1683        assert_eq!(
1684            run(&[lines.as_os_str()]),
1685            (
1686                1,
1687                "error[syntax]: expected a value, found `True`; did you mean `true`?\n \
1688                 --> l.jsonl:2:7\n  |\n2 | {\"a\": True}\n  |       ^^^^\n  |\n  = help: https://simonvanlierde.com/jsonvet/docs/usage.html#syntax\n\n\
1689                 1 error in 1 of 1 file\n"
1690                    .to_owned()
1691            )
1692        );
1693        assert_eq!(
1694            run(&["--schema".as_ref(), schema.as_os_str(), data.as_os_str()]),
1695            (
1696                1,
1697                "error[schema]: 1 is not of type \"string\" (at $.a)\n \
1698                 --> d.json:2:8\n  |\n2 |   \"a\": 1\n  |        ^\n  |\n  = help: https://simonvanlierde.com/jsonvet/docs/usage.html#schema\n\n\
1699                 1 error in 1 of 1 file\n"
1700                    .to_owned()
1701            )
1702        );
1703        let _ = std::fs::remove_dir_all(&dir);
1704    }
1705
1706    #[test]
1707    fn the_summary_names_errors_and_warnings_together() {
1708        let counts = |errors, warnings| super::Counts {
1709            errors,
1710            warnings,
1711            files: 1,
1712        };
1713        assert_eq!(
1714            counts(1, 1).summary(2),
1715            "1 error and 1 warning in 1 of 2 files"
1716        );
1717        assert_eq!(counts(0, 2).summary(1), "2 warnings in 1 of 1 file");
1718    }
1719
1720    #[test]
1721    fn a_line_separator_ends_a_frame_line_only_in_a_json5_file() {
1722        let dir = std::env::temp_dir().join(format!("jsonvet-u2028-{}", std::process::id()));
1723        std::fs::create_dir_all(&dir).expect("a temp dir");
1724        let frame = |name: &str| {
1725            let file = dir.join(name);
1726            std::fs::write(&file, "{\"a\": \"x\u{2028}y\", \"a\": 2}\n").expect("write");
1727            let mut out = Vec::new();
1728            let args = ["--no-config".into(), file.into_os_string()];
1729            let terminal = super::Terminal {
1730                tty: true,
1731                color: Some(false),
1732            };
1733            super::run_with(args, false, terminal, &mut out).expect("a check");
1734            String::from_utf8(out).expect("UTF-8")
1735        };
1736        let five = frame("a.json5");
1737        // Both the duplicate and the first copy, whose line ends at the separator.
1738        assert!(five.contains("\n2 | y\", \"a\": 2}\n"), "{five}");
1739        assert!(five.contains("\n1 | {\"a\": \"x\n"), "{five}");
1740        let other = frame("a.jsonc");
1741        // The frame shows U+2028 as a replacement character, on the one line.
1742        assert!(
1743            other
1744                .lines()
1745                .any(|l| l.starts_with("1 | {\"a\": \"x\u{FFFD}y\", \"a\": 2}")),
1746            "{other}"
1747        );
1748        let _ = std::fs::remove_dir_all(&dir);
1749    }
1750
1751    #[test]
1752    fn a_panic_on_the_big_stack_carries_on_in_the_caller() {
1753        static RUNS: AtomicUsize = AtomicUsize::new(0);
1754        let panic = std::panic::catch_unwind(|| {
1755            super::on_big_stack(|| -> u8 {
1756                RUNS.fetch_add(1, Ordering::Relaxed);
1757                panic!("on the big stack")
1758            })
1759        })
1760        .expect_err("the panic carries on");
1761        assert_eq!(panic.downcast_ref::<&str>(), Some(&"on the big stack"));
1762        assert_eq!(RUNS.load(Ordering::Relaxed), 1);
1763    }
1764}