Skip to main content

skif_cli/
lib.rs

1//! The `skif` command line as a library: a thin consumer of the `skif` library.
2//!
3//! It is the only code that reads the environment, prints, prompts, writes files, spawns a process and opens a network
4//! connection (`skif update` only); [`run`] returns an exit code and no function below calls `std::process::exit`.
5//! The npm addon calls [`run`]; the binary calls [`run_to_exit`], which also changes the current directory under
6//! `bazel run` and leaves the memory of a run to the operating system.
7
8use std::cell::Cell;
9use std::ffi::OsString;
10use std::io::Write;
11use std::sync::Once;
12
13use clap::Parser;
14use serde_json::Map;
15
16mod args;
17mod baseline;
18mod commands;
19mod config;
20mod config_toml;
21mod env;
22mod error;
23mod explain;
24mod filter;
25mod fix;
26mod format;
27mod install;
28mod instances;
29mod lint;
30mod lsp;
31mod mcp;
32mod migrate;
33mod module_cache;
34mod npmrc;
35mod output;
36mod registry;
37mod roots;
38mod trace;
39mod update;
40mod watch;
41mod write;
42
43use args::{Cli, Command, Format};
44use env::Env;
45pub use error::{CliError, Kind};
46use lint::Teardown;
47pub use lint::lint_to_json;
48use output::{emit_error, emit_error_with};
49
50/// Runs the CLI with `args` (the first item is the program name, as in `std::env::args_os`) and returns the exit
51/// code. The npm addon calls this; the `skif` binary calls [`run_to_exit`], which differs only in what happens to
52/// memory and in entering the `bazel run` directory; everything else is shared, so their behaviour cannot drift.
53///
54/// It never changes the process's current directory, even under `bazel run`: in a host such as Node.js, which caches
55/// its own current directory, a change made here would leave the host out of step. A host that runs skif under
56/// `bazel run` enters that directory itself first, as the npm package's `skif` bin does (ADR 0031).
57///
58/// A panic in skif that reaches this call is caught and exits 2, as in the binary; the panic hook prints the message
59/// and its location to stderr, and with `--format json` stdout also receives the `error` document (kind `internal`)
60/// naming both. A panic that skif catches itself on the way, such as one inside the type checker that falls back to
61/// the untyped analysis (ADR 0034), does not end the run.
62///
63/// To learn the location, the first call chains a hook in front of the process's panic hook (`std::panic::set_hook`),
64/// once per process; it records the panic for the thread that raised it and then calls the hook it replaced, so the
65/// host's own handling still runs. A host that sets its own hook after that call drops the recording hook: a panic
66/// still exits 2, but the `error` document then names only the panic's payload, without the location.
67pub fn run(args: impl IntoIterator<Item = OsString>) -> u8 {
68    let raw: Vec<OsString> = args.into_iter().collect();
69    exit_two_on_panic(&raw, &mut std::io::stdout(), || run_with(raw.clone(), Teardown::Free))
70}
71
72thread_local! {
73    /// The last panic of this thread seen by the hook [`record_panics`] chains in front of the host's: its message
74    /// and location, which the payload that `catch_unwind` returns does not carry. Per thread, since `catch_unwind`
75    /// runs on the thread that panicked and a panic elsewhere in the process must not be reported as this one.
76    static LAST_PANIC: Cell<Option<String>> = const { Cell::new(None) };
77}
78
79/// Chains a hook in front of the current panic hook, once per process, that records each panic in [`LAST_PANIC`]
80/// and then calls the hook it replaced, so the host's panic handling is unchanged.
81fn record_panics() {
82    static ONCE: Once = Once::new();
83    ONCE.call_once(|| {
84        let previous = std::panic::take_hook();
85        std::panic::set_hook(Box::new(move |info| {
86            // A panic while the thread's locals are torn down goes unrecorded; the payload then describes it.
87            let _ = LAST_PANIC.try_with(|last| last.set(Some(info.to_string())));
88            previous(info);
89        }));
90    });
91}
92
93/// The stack of each worker of rayon's global pool. A worker that waits for a parallel job takes other queued jobs
94/// on its own stack, so the analyses of the roots of a recursive run nest on one worker, and with few cores that
95/// outgrew the 2 MiB default. Only the pages a thread touches are committed.
96const WORKER_STACK_SIZE: usize = 64 << 20;
97
98/// Builds rayon's global pool, once per process, with 64 MiB stacks. The thread count still follows
99/// `RAYON_NUM_THREADS`. A host that built the global pool first keeps its own.
100pub fn configure_global_pool() {
101    static ONCE: Once = Once::new();
102    ONCE.call_once(|| {
103        let _ = rayon::ThreadPoolBuilder::new()
104            .stack_size(WORKER_STACK_SIZE)
105            .thread_name(|index| format!("skif-worker-{index}"))
106            .build_global();
107    });
108}
109
110/// `body`'s exit code, or 2 when it panics, after the `internal` error document on `stdout` when `raw` asks for JSON.
111/// The panic hook has already printed the message and location to stderr. The document names the location the hook
112/// recorded on this thread, or only the payload when the hook did not run for this panic: a host replaced it, or the
113/// panic was raised on another thread and resumed here, after a panic `body` caught itself was recorded.
114fn exit_two_on_panic(raw: &[OsString], stdout: &mut dyn Write, body: impl FnOnce() -> u8) -> u8 {
115    record_panics();
116    LAST_PANIC.with(|last| last.set(None));
117    std::panic::catch_unwind(std::panic::AssertUnwindSafe(body)).unwrap_or_else(|payload| {
118        let recorded = LAST_PANIC.with(Cell::take);
119        if wants_json(raw) {
120            let text = payload
121                .downcast_ref::<&str>()
122                .copied()
123                .or_else(|| payload.downcast_ref::<String>().map(String::as_str));
124            let report = match (recorded, text) {
125                (Some(recorded), Some(text)) if !recorded.ends_with(text) => text.to_owned(),
126                (Some(recorded), _) => recorded,
127                (None, text) => text.unwrap_or("panic").to_owned(),
128            };
129            emit_error_with(&internal_error(&report), Format::Json, Map::new(), stdout);
130        }
131        2
132    })
133}
134
135/// The error a panic becomes; `report` describes it: the panic hook's `panicked at <location>:` and the message, or
136/// the payload alone.
137fn internal_error(report: &str) -> CliError {
138    CliError::new(Kind::Internal, format!("internal error: {report}"))
139}
140
141/// Like [`run`], for a process that exits as soon as this returns: the lint command leaves its diagnostics, workspace
142/// model and file caches to the operating system instead of freeing them one allocation at a time, a measurable part
143/// of a large run. Never call it from a long-lived host such as the npm addon: that memory would leak on every call.
144///
145/// Under `bazel run` (`BUILD_WORKSPACE_DIRECTORY` is set) it first changes the process's current directory to
146/// `BUILD_WORKING_DIRECTORY`, else to the workspace root, so paths on the command line resolve as the user typed them
147/// rather than in the runfiles tree (ADR 0031); a directory it cannot enter exits 2.
148///
149/// A panic exits 2 as in [`run`]. The release binary unwinds (`panic = "unwind"`) instead of aborting, so the panic
150/// reaches this call, and a panic skif catches on the way, inside the type checker, falls back to the untyped
151/// analysis instead of ending the process (ADR 0034).
152pub fn run_to_exit(args: impl IntoIterator<Item = OsString>) -> u8 {
153    let raw: Vec<OsString> = args.into_iter().collect();
154    if let Err(error) = env::enter_bazel_run_directory() {
155        emit_error(&error, if wants_json(&raw) { Format::Json } else { Format::Human });
156        return 2;
157    }
158    exit_two_on_panic(&raw, &mut std::io::stdout(), || run_with(raw.clone(), Teardown::Leak))
159}
160
161fn run_with(args: impl IntoIterator<Item = OsString>, teardown: Teardown) -> u8 {
162    let raw: Vec<OsString> = args.into_iter().collect();
163    configure_global_pool();
164    env::enable_timings();
165    let cli = match Cli::try_parse_from(&raw) {
166        Ok(cli) => cli,
167        Err(error) => return usage(&raw, &error),
168    };
169    match cli.command {
170        None if cli.lint.watch => watch::run(&cli.lint, Env::detect()),
171        None => lint::run(&cli.lint, Env::detect(), teardown),
172        Some(Command::Rules(args)) => commands::rules(&args),
173        Some(Command::Schema) => commands::schema(),
174        Some(Command::Init(args)) => commands::init(&args),
175        Some(Command::Config(args)) => config::run(&args),
176        Some(Command::ExplainUsage(args)) => explain::run(&args),
177        Some(Command::Migrate(args)) => migrate::run(&args),
178        Some(Command::Baseline(args)) => baseline::run(&args),
179        Some(Command::Update(args)) => update::run(&args, Env::detect()),
180        Some(Command::List(args)) => instances::list(&args),
181        Some(Command::Json(args)) => instances::json(&args),
182        Some(Command::Format(args)) => format::run(&args, Env::detect(), teardown),
183        Some(Command::Mcp) => mcp::run(),
184        Some(Command::Lsp) => lsp::run(),
185    }
186}
187
188/// Help and version go to stdout with exit 0; usage errors exit 2, as a JSON `error` document when `--format json`
189/// is on the command line.
190fn usage(raw: &[OsString], error: &clap::Error) -> u8 {
191    if !error.use_stderr() {
192        let _ = error.print();
193        return 0;
194    }
195    if wants_json(raw) {
196        let message = error.to_string();
197        emit_error(&CliError::new(Kind::Usage, message.trim_end()), Format::Json);
198    } else {
199        let _ = error.print();
200    }
201    2
202}
203
204/// Whether the command line asks for JSON-mode errors: the format that goes to stdout is `json`, or, when every
205/// `--format` names a file, the first one is (ADR 0022). Read from the raw arguments, before clap has parsed them.
206fn wants_json(raw: &[OsString]) -> bool {
207    let args: Vec<&str> = raw.iter().filter_map(|arg| arg.to_str()).collect();
208    let mut values = Vec::new();
209    for (index, arg) in args.iter().enumerate() {
210        if let Some(value) = arg.strip_prefix("--format=") {
211            values.push(value);
212        } else if *arg == "--format" {
213            values.extend(args.get(index + 1));
214        }
215    }
216    values
217        .iter()
218        .find(|value| !value.contains('='))
219        .or_else(|| values.first())
220        .is_some_and(|value| value.split('=').next() == Some("json"))
221}
222
223#[cfg(test)]
224mod tests {
225    use super::*;
226
227    fn args(list: &[&str]) -> Vec<OsString> {
228        list.iter().map(OsString::from).collect()
229    }
230
231    #[test]
232    fn a_panic_exits_two_in_every_format() {
233        for (raw, json) in [(args(&["skif"]), false), (args(&["skif", "--format", "json"]), true)] {
234            let mut stdout = Vec::new();
235            assert_eq!(exit_two_on_panic(&raw, &mut stdout, || panic!("boom")), 2);
236            if !json {
237                assert!(stdout.is_empty(), "{}", String::from_utf8_lossy(&stdout));
238                continue;
239            }
240            let document: serde_json::Value = serde_json::from_slice(&stdout).unwrap();
241            assert_eq!(document["error"]["kind"], "internal", "{document:#}");
242            let message = document["error"]["message"].as_str().unwrap();
243            assert!(
244                message.starts_with("internal error: panicked at ")
245                    && message.contains("lib.rs:")
246                    && message.ends_with(":\nboom"),
247                "the message names the location and the panic: {message}"
248            );
249        }
250    }
251
252    /// The JSON `error` message of a run of `body` that exits 2.
253    fn panic_message(body: impl FnOnce() -> u8) -> String {
254        let mut stdout = Vec::new();
255        assert_eq!(
256            exit_two_on_panic(&args(&["skif", "--format", "json"]), &mut stdout, body),
257            2
258        );
259        let document: serde_json::Value = serde_json::from_slice(&stdout).unwrap();
260        document["error"]["message"].as_str().unwrap().to_owned()
261    }
262
263    #[test]
264    fn a_panic_on_another_thread_is_not_reported_as_this_one() {
265        // `resume_unwind` skips the panic hook, so only the payload describes this panic; the other thread's panic
266        // goes through the hook while this run is in progress.
267        let message = panic_message(|| {
268            let other = std::thread::spawn(|| std::panic::panic_any("another thread's panic"));
269            assert!(other.join().is_err(), "the other thread panicked");
270            std::panic::resume_unwind(Box::new("boom"))
271        });
272        assert_eq!(message, "internal error: boom");
273    }
274
275    #[test]
276    fn a_panic_caught_earlier_on_this_thread_is_not_reported_again() {
277        record_panics();
278        let earlier = std::panic::catch_unwind(|| std::panic::panic_any("an earlier panic"));
279        assert!(earlier.is_err(), "the earlier body panicked");
280        let message = panic_message(|| std::panic::resume_unwind(Box::new(String::from("boom"))));
281        assert_eq!(message, "internal error: boom");
282    }
283
284    #[test]
285    fn a_panic_caught_inside_the_run_does_not_end_it() {
286        let mut stdout = Vec::new();
287        let code = exit_two_on_panic(&args(&["skif", "--format", "json"]), &mut stdout, || {
288            let fallback = std::panic::catch_unwind(|| std::panic::panic_any("the checker's panic"));
289            assert!(fallback.is_err(), "the inner body panicked");
290            0
291        });
292        assert_eq!(code, 0);
293        assert_eq!(stdout, b"");
294    }
295
296    #[test]
297    fn a_panic_caught_inside_the_run_is_not_reported_as_the_one_that_ended_it() {
298        let message = panic_message(|| {
299            let fallback = std::panic::catch_unwind(|| std::panic::panic_any("the checker's panic"));
300            assert!(fallback.is_err(), "the inner body panicked");
301            std::panic::resume_unwind(Box::new("a worker's panic"))
302        });
303        assert_eq!(message, "internal error: a worker's panic");
304    }
305
306    #[test]
307    fn a_panic_of_a_rayon_worker_exits_two() {
308        let message = panic_message(|| {
309            rayon::scope(|scope| scope.spawn(|_| panic!("a worker's panic")));
310            0
311        });
312        assert!(message.ends_with("a worker's panic"), "{message}");
313    }
314
315    #[test]
316    fn without_a_panic_the_exit_code_is_the_body_s() {
317        let mut stdout = Vec::new();
318        assert_eq!(
319            exit_two_on_panic(&args(&["skif", "--format", "json"]), &mut stdout, || 1),
320            1
321        );
322        assert_eq!(stdout, b"");
323    }
324}