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, Write};
5use std::path::Path;
6use std::process::ExitCode;
7
8use jsonvet::Severity;
9
10mod args;
11mod glob;
12mod output;
13mod sarif;
14mod schema;
15mod settings;
16
17use args::{Command, ConfigSource};
18use output::Printer;
19use schema::Schema;
20use settings::Settings;
21
22/// How deep `--schema` reads a file. Past it, the file has a syntax error. jsonschema 0.58 takes
23/// memory quadratic in depth for an error deep under a recursive `$ref`: 1.3 GB for a 50 KB file
24/// nested 10,000 levels.
25const SCHEMA_MAX_DEPTH: usize = 512;
26
27/// The stack every run uses, for `--schema`: jsonschema validates recursively, and building,
28/// validating and dropping a value [`SCHEMA_MAX_DEPTH`] levels deep against a recursive schema
29/// took up to 2.2 MB in a debug build, more than Windows' 1 MB main thread. Only the pages used
30/// are committed, so a plain check costs no more memory.
31const STACK: usize = 64 << 20;
32
33/// Run the command with the process's arguments and environment, and return its exit code.
34pub fn run() -> ExitCode {
35    on_big_stack(run_here)
36}
37
38/// Run `f` on a thread with a [`STACK`]-byte stack, or here if no thread can start. A panic in
39/// `f` carries on here.
40fn on_big_stack<T: Send + 'static>(f: fn() -> T) -> T {
41    let thread = std::thread::Builder::new().name("jsonvet".into());
42    match thread.stack_size(STACK).spawn(f) {
43        Ok(thread) => thread
44            .join()
45            .unwrap_or_else(|panic| std::panic::resume_unwind(panic)),
46        Err(_) => f(),
47    }
48}
49
50fn run_here() -> ExitCode {
51    let annotate = std::env::var_os("GITHUB_ACTIONS").is_some_and(|v| v == "true");
52    let stdout = io::stdout().lock();
53    match run_with(
54        std::env::args_os().skip(1),
55        annotate,
56        BufWriter::new(stdout),
57    ) {
58        Ok(code) => ExitCode::from(code),
59        Err(message) => {
60            eprintln!("jsonvet: {message}");
61            ExitCode::from(2)
62        }
63    }
64}
65
66/// The command, with its inputs passed in: 0 when no file has an error, 1 when one does.
67/// A usage error comes back as `Err`, which `run` turns into exit status 2.
68fn run_with(
69    args: impl IntoIterator<Item = OsString>,
70    annotate: bool,
71    out: impl Write,
72) -> Result<u8, String> {
73    // Usage errors quote arguments and the schema file, which can hold a newline or an escape.
74    let args = match args::parse(args).map_err(|e| {
75        format!(
76            "{}\n\nRun `jsonvet --help` for usage.",
77            output::printable(&e)
78        )
79    })? {
80        Command::Help => return print(out, args::HELP),
81        Command::Version => return print(out, concat!("jsonvet ", env!("CARGO_PKG_VERSION"))),
82        Command::Check(args) => args,
83    };
84    let from_file = match &args.config {
85        ConfigSource::None => Settings::default(),
86        ConfigSource::File(path) => settings::load(path)?,
87        ConfigSource::Search => {
88            let cwd = std::env::current_dir()
89                .map_err(|e| format!("cannot read the working directory: {e}"))?;
90            match settings::discover(&cwd) {
91                Some(path) => settings::load(&path)?,
92                None => Settings::default(),
93            }
94        }
95    };
96    let settings = from_file.overridden_by(args.settings);
97    let schema = args
98        .schema
99        .as_deref()
100        .map(|path| Schema::load(path, &settings))
101        .transpose()
102        .map_err(|e| output::printable(&e))?;
103
104    let mut printer = Printer::new(out, args.format, annotate);
105    let mut counts = Counts::default();
106    let written = args
107        .files
108        .iter()
109        .try_for_each(|file| {
110            check_file(file, &settings, schema.as_ref(), &mut printer, &mut counts)
111        })
112        .and_then(|()| printer.finish());
113    // A reader that has gone, as with `| head`, wants no more output: the verdict so far stands.
114    match written {
115        Err(e) if e.kind() != io::ErrorKind::BrokenPipe => {
116            return Err(format!("cannot write output: {e}"));
117        }
118        _ => {}
119    }
120    let too_many_warnings = args.max_warnings.is_some_and(|max| counts.warnings > max);
121    Ok(u8::from(counts.errors > 0 || too_many_warnings))
122}
123
124#[derive(Default)]
125struct Counts {
126    errors: usize,
127    warnings: usize,
128}
129
130/// Check one file, and against `schema` if there is one, and add its problems to `counts`,
131/// each before it is printed, so a failed write still leaves the verdict. The name is opened as
132/// given; its lossy UTF-8 form is only for output and glob matching.
133fn check_file(
134    file: &OsStr,
135    settings: &Settings,
136    schema: Option<&Schema>,
137    printer: &mut Printer<impl Write>,
138    counts: &mut Counts,
139) -> io::Result<()> {
140    let text = read(Path::new(file));
141    let name = file.to_string_lossy();
142    let text = match text {
143        Ok(text) => text,
144        Err(message) => {
145            counts.errors += 1;
146            return printer.unreadable(file, &message);
147        }
148    };
149    let config = settings.config_for(&name);
150    let (report, events) = match schema {
151        Some(_) => jsonvet::parse(&text, &config.max_depth(SCHEMA_MAX_DEPTH)),
152        None => (jsonvet::check(&text, &config), None),
153    };
154    for problem in report.problems() {
155        match problem.severity() {
156            Severity::Error => counts.errors += 1,
157            Severity::Warning => counts.warnings += 1,
158            _ => {}
159        }
160        printer.problem(file, &report, problem)?;
161    }
162    // `events` is `None` after a syntax error, so such a file gets no schema errors.
163    if let (Some(schema), Some(events)) = (schema, events) {
164        for (offset, message, summary) in schema.errors(&events) {
165            counts.errors += usize::from(!summary);
166            printer.schema_error(file, &report, offset, &message, summary)?;
167        }
168    }
169    Ok(())
170}
171
172/// A file's text, or why it has none. Only regular files are read, so a FIFO cannot hang the
173/// run and a device such as `/dev/zero` cannot fill memory. A symlink to a regular file is
174/// followed, as `cat` would.
175fn read(path: &Path) -> Result<String, String> {
176    let read = std::fs::metadata(path).and_then(|meta| {
177        if meta.is_file() {
178            std::fs::read(path).map(Some)
179        } else {
180            Ok(None)
181        }
182    });
183    match read {
184        Ok(Some(bytes)) => String::from_utf8(bytes)
185            .map_err(|e| format!("not valid UTF-8 (byte {})", e.utf8_error().valid_up_to())),
186        Ok(None) => Err("not a regular file".to_owned()),
187        Err(e) if e.kind() == io::ErrorKind::NotFound => Err("no such file".to_owned()),
188        Err(e) => Err(format!("cannot read: {e}")),
189    }
190}
191
192fn print(mut out: impl Write, text: &str) -> Result<u8, String> {
193    match writeln!(out, "{text}").and_then(|()| out.flush()) {
194        Err(e) if e.kind() != io::ErrorKind::BrokenPipe => Err(format!("cannot write output: {e}")),
195        _ => Ok(0),
196    }
197}
198
199#[cfg(test)]
200mod tests {
201    use std::sync::atomic::{AtomicUsize, Ordering};
202
203    #[test]
204    fn a_panic_on_the_big_stack_carries_on_in_the_caller() {
205        static RUNS: AtomicUsize = AtomicUsize::new(0);
206        let panic = std::panic::catch_unwind(|| {
207            super::on_big_stack(|| -> u8 {
208                RUNS.fetch_add(1, Ordering::Relaxed);
209                panic!("on the big stack")
210            })
211        })
212        .expect_err("the panic carries on");
213        assert_eq!(panic.downcast_ref::<&str>(), Some(&"on the big stack"));
214        assert_eq!(RUNS.load(Ordering::Relaxed), 1);
215    }
216}