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 0011).
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` or `toon` stdout also receives the `error` document (kind
60/// `internal`) 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 0010), 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/// or TOON.
112/// The panic hook has already printed the message and location to stderr. The document names the location the hook
113/// recorded on this thread, or only the payload when the hook did not run for this panic: a host replaced it, or the
114/// panic was raised on another thread and resumed here, after a panic `body` caught itself was recorded.
115fn exit_two_on_panic(raw: &[OsString], stdout: &mut dyn Write, body: impl FnOnce() -> u8) -> u8 {
116    record_panics();
117    LAST_PANIC.with(|last| last.set(None));
118    let outcome = std::panic::catch_unwind(std::panic::AssertUnwindSafe(body));
119    skif_core::timing::flush_stderr();
120    outcome.unwrap_or_else(|payload| {
121        let recorded = LAST_PANIC.with(Cell::take);
122        let format = error_format(raw);
123        if matches!(format, Format::Json | Format::Toon) {
124            let text = payload
125                .downcast_ref::<&str>()
126                .copied()
127                .or_else(|| payload.downcast_ref::<String>().map(String::as_str));
128            let report = match (recorded, text) {
129                (Some(recorded), Some(text)) if !recorded.ends_with(text) => text.to_owned(),
130                (Some(recorded), _) => recorded,
131                (None, text) => text.unwrap_or("panic").to_owned(),
132            };
133            emit_error_with(&internal_error(&report), format, Map::new(), stdout);
134        }
135        2
136    })
137}
138
139/// The error a panic becomes; `report` describes it: the panic hook's `panicked at <location>:` and the message, or
140/// the payload alone.
141fn internal_error(report: &str) -> CliError {
142    CliError::new(Kind::Internal, format!("internal error: {report}"))
143}
144
145/// Like [`run`], for a process that exits as soon as this returns: the lint command leaves its diagnostics, workspace
146/// model and file caches to the operating system instead of freeing them one allocation at a time, a measurable part
147/// of a large run. Never call it from a long-lived host such as the npm addon: that memory would leak on every call.
148///
149/// Under `bazel run` (`BUILD_WORKSPACE_DIRECTORY` is set) it first changes the process's current directory to
150/// `BUILD_WORKING_DIRECTORY`, else to the workspace root, so paths on the command line resolve as the user typed them
151/// rather than in the runfiles tree (ADR 0011); a directory it cannot enter exits 2.
152///
153/// A panic exits 2 as in [`run`]. The release binary unwinds (`panic = "unwind"`) instead of aborting, so the panic
154/// reaches this call, and a panic skif catches on the way, inside the type checker, falls back to the untyped
155/// analysis instead of ending the process (ADR 0010).
156pub fn run_to_exit(args: impl IntoIterator<Item = OsString>) -> u8 {
157    let raw: Vec<OsString> = args.into_iter().collect();
158    if let Err(error) = env::enter_bazel_run_directory() {
159        emit_error(&error, error_format(&raw));
160        return 2;
161    }
162    exit_two_on_panic(&raw, &mut std::io::stdout(), || run_with(raw.clone(), Teardown::Leak))
163}
164
165fn run_with(args: impl IntoIterator<Item = OsString>, teardown: Teardown) -> u8 {
166    let raw: Vec<OsString> = args.into_iter().collect();
167    configure_global_pool();
168    env::enable_timings();
169    let cli = match Cli::try_parse_from(&raw) {
170        Ok(cli) => cli,
171        Err(error) => return usage(&raw, &error),
172    };
173    match cli.command {
174        None if cli.lint.watch => watch::run(&cli.lint, Env::detect()),
175        None => {
176            if teardown == Teardown::Leak {
177                // The process exits after this one run, so the address range retired regions give up costs nothing.
178                skif::retire_freed_sources();
179            }
180            lint::run(&cli.lint, Env::detect(), teardown)
181        }
182        Some(Command::Rules(args)) => commands::rules(&args),
183        Some(Command::Schema) => commands::schema(),
184        Some(Command::Init(args)) => commands::init(&args),
185        Some(Command::Config(args)) => config::run(&args),
186        Some(Command::ExplainUsage(args)) => explain::run(&args),
187        Some(Command::Migrate(args)) => migrate::run(&args),
188        Some(Command::Baseline(args)) => baseline::run(&args),
189        Some(Command::Update(args)) => update::run(&args, Env::detect()),
190        Some(Command::List(args)) => instances::list(&args),
191        Some(Command::Json(args)) => instances::json(&args),
192        Some(Command::Format(args)) => format::run(&args, Env::detect(), teardown),
193        Some(Command::Mcp) => mcp::run(),
194        Some(Command::Lsp) => lsp::run(),
195    }
196}
197
198/// Help and version go to stdout with exit 0; usage errors exit 2, as the `error` document when `--format json` or
199/// `--format toon` is on the command line.
200fn usage(raw: &[OsString], error: &clap::Error) -> u8 {
201    if !error.use_stderr() {
202        let _ = error.print();
203        return 0;
204    }
205    let format = error_format(raw);
206    if matches!(format, Format::Json | Format::Toon) {
207        let message = error.to_string();
208        emit_error(&CliError::new(Kind::Usage, message.trim_end()), format);
209    } else {
210        let _ = error.print();
211    }
212    2
213}
214
215/// The format of the errors the command line asks for: `json` or `toon` when that is the format that goes to stdout,
216/// or, when every `--format` names a file, the first one (ADR 0012); else human. Read from the raw arguments, before
217/// clap has parsed them.
218fn error_format(raw: &[OsString]) -> Format {
219    let args: Vec<&str> = raw.iter().filter_map(|arg| arg.to_str()).collect();
220    let mut values = Vec::new();
221    for (index, arg) in args.iter().enumerate() {
222        if let Some(value) = arg.strip_prefix("--format=") {
223            values.push(value);
224        } else if *arg == "--format" {
225            values.extend(args.get(index + 1));
226        }
227    }
228    let chosen = values
229        .iter()
230        .find(|value| !value.contains('='))
231        .or_else(|| values.first())
232        .and_then(|value| value.split('=').next());
233    match chosen {
234        Some("json") => Format::Json,
235        Some("toon") => Format::Toon,
236        _ => Format::Human,
237    }
238}
239
240#[cfg(test)]
241mod tests {
242    use super::*;
243
244    fn args(list: &[&str]) -> Vec<OsString> {
245        list.iter().map(OsString::from).collect()
246    }
247
248    #[test]
249    fn a_panic_exits_two_in_every_format() {
250        for (raw, format) in [
251            (args(&["skif"]), Format::Human),
252            (args(&["skif", "--format", "json"]), Format::Json),
253            (args(&["skif", "--format", "toon"]), Format::Toon),
254        ] {
255            let mut stdout = Vec::new();
256            assert_eq!(exit_two_on_panic(&raw, &mut stdout, || panic!("boom")), 2);
257            let text = String::from_utf8_lossy(&stdout);
258            let document: serde_json::Value = match format {
259                Format::Json => serde_json::from_str(&text).unwrap(),
260                Format::Toon => serde_toon::decode_to_value(&text).unwrap_or_else(|error| panic!("{error}: {text:?}")),
261                _ => {
262                    assert!(stdout.is_empty(), "{text}");
263                    continue;
264                }
265            };
266            assert_eq!(document["error"]["kind"], "internal", "{document:#}");
267            let message = document["error"]["message"].as_str().unwrap();
268            assert!(
269                message.starts_with("internal error: panicked at ")
270                    && message.contains("lib.rs:")
271                    && message.ends_with(":\nboom"),
272                "the message names the location and the panic: {message}"
273            );
274        }
275    }
276
277    /// The JSON `error` message of a run of `body` that exits 2.
278    fn panic_message(body: impl FnOnce() -> u8) -> String {
279        let mut stdout = Vec::new();
280        assert_eq!(
281            exit_two_on_panic(&args(&["skif", "--format", "json"]), &mut stdout, body),
282            2
283        );
284        let document: serde_json::Value = serde_json::from_slice(&stdout).unwrap();
285        document["error"]["message"].as_str().unwrap().to_owned()
286    }
287
288    #[test]
289    fn a_panic_on_another_thread_is_not_reported_as_this_one() {
290        // `resume_unwind` skips the panic hook, so only the payload describes this panic; the other thread's panic
291        // goes through the hook while this run is in progress.
292        let message = panic_message(|| {
293            let other = std::thread::spawn(|| std::panic::panic_any("another thread's panic"));
294            assert!(other.join().is_err(), "the other thread panicked");
295            std::panic::resume_unwind(Box::new("boom"))
296        });
297        assert_eq!(message, "internal error: boom");
298    }
299
300    #[test]
301    fn a_panic_caught_earlier_on_this_thread_is_not_reported_again() {
302        record_panics();
303        let earlier = std::panic::catch_unwind(|| std::panic::panic_any("an earlier panic"));
304        assert!(earlier.is_err(), "the earlier body panicked");
305        let message = panic_message(|| std::panic::resume_unwind(Box::new(String::from("boom"))));
306        assert_eq!(message, "internal error: boom");
307    }
308
309    #[test]
310    fn a_panic_caught_inside_the_run_does_not_end_it() {
311        let mut stdout = Vec::new();
312        let code = exit_two_on_panic(&args(&["skif", "--format", "json"]), &mut stdout, || {
313            let fallback = std::panic::catch_unwind(|| std::panic::panic_any("the checker's panic"));
314            assert!(fallback.is_err(), "the inner body panicked");
315            0
316        });
317        assert_eq!(code, 0);
318        assert_eq!(stdout, b"");
319    }
320
321    #[test]
322    fn a_panic_caught_inside_the_run_is_not_reported_as_the_one_that_ended_it() {
323        let message = panic_message(|| {
324            let fallback = std::panic::catch_unwind(|| std::panic::panic_any("the checker's panic"));
325            assert!(fallback.is_err(), "the inner body panicked");
326            std::panic::resume_unwind(Box::new("a worker's panic"))
327        });
328        assert_eq!(message, "internal error: a worker's panic");
329    }
330
331    #[test]
332    fn a_panic_of_a_rayon_worker_exits_two() {
333        let message = panic_message(|| {
334            rayon::scope(|scope| scope.spawn(|_| panic!("a worker's panic")));
335            0
336        });
337        assert!(message.ends_with("a worker's panic"), "{message}");
338    }
339
340    #[test]
341    fn without_a_panic_the_exit_code_is_the_body_s() {
342        let mut stdout = Vec::new();
343        assert_eq!(
344            exit_two_on_panic(&args(&["skif", "--format", "json"]), &mut stdout, || 1),
345            1
346        );
347        assert_eq!(stdout, b"");
348    }
349}