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