zshrs 0.12.42

The first JIT-compiled Unix shell — bytecode VM, Cranelift JIT, worker pool, AOP intercept, Rkyv caching
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
//! zshrs logging & profiling framework.
//!
//! **zshrs-original infrastructure — no C source counterpart.** C
//! zsh emits all diagnostics through `zwarn()` / `zwarnnam()` /
//! `zerr()` / `zerrnam()` in Src/utils.c, which print directly to
//! stderr. This module replaces that pattern with a structured
//! `tracing`-based subscriber that writes to `~/.zshrs/zshrs.log`.
//! Per the project's "no startup chatter" rule, all init progress
//! goes here instead of stderr.
//!
//! **Logging** (always on):
//!   - File: `$HOME/.zshrs/{binary}.log` — five separate files so the
//!     processes don't interleave their tracing output:
//!       - `zshrs.log`           (the shell — interactive / scripted)
//!       - `zshrs-lsp.log`       (`zshrs --lsp` — IDE language server)
//!       - `zshrs-dap.log`       (`zshrs --dap` — IDE debug adapter)
//!       - `zshrs-daemon.log`    (daemon, set up via daemon/log.rs)
//!       - `zshrs-recorder.log`  (single-shot recorder runs)
//!     The IDE plugin side has its own `zshrs-plugin.log` written by
//!     `editors/intellij/.../ZshrsDebugLog.kt`.
//!   - Level: ZSHRS_LOG env var (default: info)
//!   - Structured key=value fields, ISO timestamps, thread names, module paths
//!
//! **Profiling** (feature-gated, zero cost when off):
//!   - `--features profiling`  → chrome://tracing JSON  → $HOME/.zshrs/trace-{PID}.json
//!   - `--features flamegraph` → folded stacks          → $HOME/.zshrs/flame-{PID}.folded
//!   - `--features prometheus` → metrics on :9090/metrics
//!
//! Call `zsh::log::init()` (defaults to `zshrs.log`) or
//! `zsh::log::init_named("…")` once at startup. Use
//! `tracing::{info,debug,trace,warn,error}!` everywhere. Use
//! `#[tracing::instrument]` or `zsh::log::span!` for timed sections.

use std::path::PathBuf;
use std::sync::OnceLock;
use tracing_subscriber::prelude::*;

/// Guards that must live for the duration of the process.
/// Dropping any of these flushes and stops the associated writer.
struct Guards {
    #[cfg(feature = "profiling")]
    _chrome: tracing_chrome::FlushGuard,
    #[cfg(feature = "flamegraph")]
    _flame: tracing_flame::FlushGuard<std::io::BufWriter<std::fs::File>>,
}

static GUARDS: OnceLock<Guards> = OnceLock::new();

/// Resolve log/profile output directory: `$ZSHRS_HOME` or
/// `$HOME/.zshrs`.
/// zshrs-original — no C counterpart. C zsh has no log file at all
/// (it writes to stderr).
pub fn log_dir() -> PathBuf {
    if let Some(custom) = std::env::var_os("ZSHRS_HOME") {
        return PathBuf::from(custom);
    }
    dirs::home_dir()
        .unwrap_or_else(|| PathBuf::from("/tmp"))
        .join(".zshrs")
}

/// Resolve the full log path for the shell binary
/// (`zshrs.log`).
/// zshrs-original — no C counterpart.
pub fn log_path() -> PathBuf {
    log_dir().join("zshrs.log")
}

/// Initialize logging + optional profiling subscribers using the
/// default `zshrs.log` filename. Equivalent to
/// `init_named("zshrs.log")`. Safe to call multiple times — only
/// the first call takes effect.
/// zshrs-original — no C counterpart. C zsh's `Src/init.c`
/// `setupshin()` doesn't open a log file.
///
/// Env vars:
///   ZSHRS_LOG=debug|trace|info|warn|error  (default: info)
pub fn init() {
    init_named("zshrs.log");
}

/// Initialize logging + optional profiling subscribers, writing to
/// `$ZSHRS_HOME/<filename>` (or `$HOME/.zshrs/<filename>`). Used
/// by `zshrs-recorder` to land tracing in `zshrs-recorder.log`
/// instead of the shell's `zshrs.log`. The daemon owns its own
/// subscriber via `daemon/log.rs` and uses the path from
/// `paths.log_file_name`.
/// zshrs-original — no C counterpart.
///
/// Safe to call multiple times — only the first call takes effect.
pub fn init_named(filename: &str) {
    GUARDS.get_or_init(|| {
        let dir = log_dir();
        let _ = std::fs::create_dir_all(&dir);
        let pid = std::process::id();

        // --- File log layer (always on) ---
        // Use a blocking Mutex<File> writer — log writes are microseconds and this
        // guarantees data reaches disk even when std::process::exit() skips destructors.
        // c:Src/utils.c:1990 movefd — the shell's own descriptors must not sit
        // in the script's fd range. Without this the log landed on fd 3, so
        // `print -u 3 -r -- x` appended to it (and reported success), and
        // `exec 3>file` would dup2 over the live log handle.
        let _lowfd = crate::lowfd::LowFdGuard::new();
        // Size-capped rotation at init (client-side, no background
        // threads — the daemon owns scheduled rotation, but daemonless
        // invocations must still bound the file). Millions of short
        // `-fc` runs (parity fuzz harnesses) each append startup lines;
        // unrotated this reached 18 GB / 142M lines and exhausted the
        // disk. One stat() per shell start; over the cap the current
        // file becomes `<name>.1` (previous `.1` is replaced) and a
        // fresh file starts. rename() keeps writers on the old inode
        // consistent (they finish writing to `.1`).
        const LOG_ROTATE_BYTES: u64 = 512 * 1024 * 1024;
        let log_path = dir.join(filename);
        if std::fs::metadata(&log_path)
            .map(|m| m.len() > LOG_ROTATE_BYTES)
            .unwrap_or(false)
        {
            let _ = std::fs::rename(&log_path, dir.join(format!("{filename}.1")));
        }
        // Logging is diagnostic infrastructure and must NEVER be able to stop
        // the shell from starting. This used to `.expect("cannot open any log
        // file")`, so an unwritable `$HOME` (unmounted NFS, read-only image,
        // `su` to an account whose home is absent, a container with no home)
        // killed zshrs outright where zsh starts fine.
        //
        // The fallback is also per-user. It was a fixed `/tmp/zshrs.log`,
        // which in a shared /tmp is owned by whoever ran zshrs first — every
        // other user then hit EACCES and, before this, the panic. A fixed name
        // in a world-writable directory is equally a squatting target: a
        // pre-created symlink there would have been appended to. `$TMPDIR` is
        // per-user on macOS, and the uid suffix keeps it unambiguous elsewhere.
        let tmp_dir = std::env::var("TMPDIR").unwrap_or_else(|_| "/tmp".to_string());
        let uid = unsafe { libc::getuid() };
        let log_file = std::fs::OpenOptions::new()
            .create(true)
            .append(true)
            .open(&log_path)
            .or_else(|_| {
                std::fs::OpenOptions::new()
                    .create(true)
                    .append(true)
                    .open(std::path::Path::new(&tmp_dir).join(format!("{filename}.{uid}")))
            })
            .ok();
        // `None` => no file layer at all; the shell runs, just without a log.
        // Same `Option<Layer>` shape the chrome/flame layers below already use.
        let file_layer = log_file.map(|f| {
            tracing_subscriber::fmt::layer()
                .with_writer(std::sync::Mutex::new(f))
                .with_ansi(false)
                .with_target(true)
                .with_thread_names(true)
                .compact()
        });

        // Filter precedence:
        //   1. $ZSHRS_LOG (env var, ad-hoc / one-shot debugging)
        //   2. [log] level in $ZSHRS_HOME/zshrs.toml (the persistent
        //      knob — set once, no env var needed every session)
        //   3. "info" hard default
        let env_filter = std::env::var("ZSHRS_LOG")
            .unwrap_or_else(|_| crate::daemon_presence::read_log_directive());

        // --- Chrome tracing layer (--features profiling) ---
        #[cfg(feature = "profiling")]
        let (chrome_layer, chrome_guard) = {
            let trace_path = dir.join(format!("trace-{}.json", pid));
            let (layer, guard) = tracing_chrome::ChromeLayerBuilder::new()
                .file(trace_path)
                .include_args(true)
                .build();
            (Some(layer), guard)
        };
        #[cfg(not(feature = "profiling"))]
        let chrome_layer: Option<tracing_subscriber::layer::Identity> = None;

        // --- Flamegraph layer (--features flamegraph) ---
        #[cfg(feature = "flamegraph")]
        let (flame_layer, flame_guard) = {
            let flame_path = dir.join(format!("flame-{}.folded", pid));
            let file =
                std::fs::File::create(&flame_path).expect("cannot create flamegraph output file");
            let writer = std::io::BufWriter::new(file);
            let (layer, guard) = tracing_flame::FlameLayer::with_writer(writer).build();
            (Some(layer), guard)
        };
        #[cfg(not(feature = "flamegraph"))]
        let flame_layer: Option<tracing_subscriber::layer::Identity> = None;

        // --- Prometheus metrics (--features prometheus) ---
        #[cfg(feature = "prometheus")]
        {
            // Spawn metrics HTTP server on :9090 in background
            let builder = metrics_exporter_prometheus::PrometheusBuilder::new();
            if let Err(e) = builder.with_http_listener(([127, 0, 0, 1], 9090)).install() {
                eprintln!("zshrs: failed to start prometheus exporter: {}", e);
            }
        }

        // --- Assemble the subscriber registry ---
        let subscriber = tracing_subscriber::registry()
            .with(tracing_subscriber::EnvFilter::new(&env_filter))
            .with(file_layer)
            .with(chrome_layer)
            .with(flame_layer);

        let _ = tracing::subscriber::set_global_default(subscriber);

        // c:Src/utils.c:2007-2010 — record the log file as a descriptor
        // the shell owns. The appender never exposes it, so it cannot be
        // registered at the open the way `movefd` registers its result;
        // the sweep finds it by elimination. Done here, with the guard
        // above still held, because the sweep only inspects descriptors
        // at or above 10 and the guard's reservations are 3-9.
        crate::lowfd::register_internal_fds();

        Guards {
            #[cfg(feature = "profiling")]
            _chrome: chrome_guard,
            #[cfg(feature = "flamegraph")]
            _flame: flame_guard,
        }
    });
}

/// Flush all log writers. Call before `std::process::exit()` to
/// ensure buffered log data reaches disk — `exit()` doesn't run
/// destructors.
/// zshrs-original — no C counterpart. C zsh's stderr is line-
/// buffered (or unbuffered), so it doesn't need an explicit flush
/// step.
pub fn flush() {
    // The WorkerGuard flushes on drop, but we can't drop a static.
    // Instead, give the non-blocking writer time to drain its buffer.
    // 50ms is more than enough for any reasonable log volume.
    std::thread::sleep(std::time::Duration::from_millis(50));
}

/// Check if the chrome-tracing profiling feature is compiled in.
/// zshrs-original — no C counterpart. C zsh has the `zsh/zprof`
/// module (Src/Modules/zprof.c) for function-level profiling but
/// nothing as fine-grained as chrome:// tracing spans.
pub fn profiling_enabled() -> bool {
    cfg!(feature = "profiling")
}

/// Check if the flamegraph feature is compiled in.
/// zshrs-original — no C counterpart.
pub fn flamegraph_enabled() -> bool {
    cfg!(feature = "flamegraph")
}

/// Check if the Prometheus metrics exporter is compiled in.
/// zshrs-original — no C counterpart.
pub fn prometheus_enabled() -> bool {
    cfg!(feature = "prometheus")
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::sync::Mutex;

    // Serialize env-mutating tests to avoid races. Other suites may
    // touch ZSHRS_HOME concurrently; this lock keeps us inside ours.
    static ENV_LOCK: Mutex<()> = Mutex::new(());

    fn with_zshrs_home<F: FnOnce()>(value: Option<&str>, f: F) {
        let _g = ENV_LOCK.lock().unwrap();
        let prev = std::env::var_os("ZSHRS_HOME");
        match value {
            Some(v) => std::env::set_var("ZSHRS_HOME", v),
            None => std::env::remove_var("ZSHRS_HOME"),
        }
        f();
        match prev {
            Some(v) => std::env::set_var("ZSHRS_HOME", v),
            None => std::env::remove_var("ZSHRS_HOME"),
        }
    }

    #[test]
    fn log_dir_honors_zshrs_home_env_var() {
        with_zshrs_home(Some("/tmp/zshrs-test-home"), || {
            assert_eq!(log_dir(), PathBuf::from("/tmp/zshrs-test-home"));
        });
    }

    #[test]
    fn log_path_appends_zshrs_log_filename() {
        with_zshrs_home(Some("/tmp/zshrs-test-path"), || {
            assert_eq!(log_path(), PathBuf::from("/tmp/zshrs-test-path/zshrs.log"));
        });
    }

    #[test]
    fn log_dir_falls_back_when_zshrs_home_unset() {
        with_zshrs_home(None, || {
            let dir = log_dir();
            // Must end in `.zshrs` regardless of whether HOME is set
            // (no HOME → /tmp/.zshrs).
            assert_eq!(dir.file_name().and_then(|s| s.to_str()), Some(".zshrs"));
        });
    }

    #[test]
    fn feature_flag_helpers_match_cfg() {
        // Each helper must agree with the underlying cfg!() macro —
        // a regression here would mean someone special-cased the
        // helper independent of the feature.
        assert_eq!(profiling_enabled(), cfg!(feature = "profiling"));
        assert_eq!(flamegraph_enabled(), cfg!(feature = "flamegraph"));
        assert_eq!(prometheus_enabled(), cfg!(feature = "prometheus"));
    }

    // ========================================================
    // log_dir — ZSHRS_HOME precedence and fallback
    // ========================================================

    #[test]
    fn log_dir_returns_absolute_when_zshrs_home_absolute() {
        with_zshrs_home(Some("/var/log/zshrs"), || {
            let d = log_dir();
            assert!(d.is_absolute(), "log_dir should be absolute: {:?}", d);
            assert_eq!(d, PathBuf::from("/var/log/zshrs"));
        });
    }

    #[test]
    fn log_dir_honors_relative_zshrs_home_verbatim() {
        // No magic normalization — whatever ZSHRS_HOME is, log_dir
        // returns it (with the `.zshrs` suffix replaced).
        with_zshrs_home(Some("relative/path"), || {
            assert_eq!(log_dir(), PathBuf::from("relative/path"));
        });
    }

    #[test]
    fn log_dir_distinct_from_log_path() {
        with_zshrs_home(Some("/tmp/zshrs-distinct"), || {
            let d = log_dir();
            let p = log_path();
            assert_ne!(d, p, "dir and path must differ");
            assert_eq!(p.parent(), Some(d.as_path()));
        });
    }

    #[test]
    fn log_path_filename_is_zshrs_dot_log() {
        with_zshrs_home(Some("/tmp/zshrs-fname"), || {
            assert_eq!(
                log_path().file_name().and_then(|s| s.to_str()),
                Some("zshrs.log")
            );
        });
    }

    #[test]
    fn log_dir_overrides_persist_for_repeated_calls() {
        // Calling twice in sequence should return identical values
        // — confirms there is no hidden caching that pins the first
        // call's result.
        with_zshrs_home(Some("/tmp/zshrs-cache-1"), || {
            assert_eq!(log_dir(), PathBuf::from("/tmp/zshrs-cache-1"));
        });
        with_zshrs_home(Some("/tmp/zshrs-cache-2"), || {
            assert_eq!(log_dir(), PathBuf::from("/tmp/zshrs-cache-2"));
        });
    }

    #[test]
    fn log_dir_empty_zshrs_home_is_taken_verbatim() {
        // env::var_os returns Some("") for empty — code path uses
        // PathBuf::from("") which still gets returned. Verify no
        // panic / silent fallback.
        with_zshrs_home(Some(""), || {
            assert_eq!(log_dir(), PathBuf::from(""));
        });
    }

    #[test]
    fn log_dir_fallback_path_ends_with_dot_zshrs_filename_component() {
        with_zshrs_home(None, || {
            let d = log_dir();
            assert_eq!(d.file_name().and_then(|s| s.to_str()), Some(".zshrs"));
        });
    }

    #[test]
    fn log_path_inside_zshrs_home_directory() {
        with_zshrs_home(Some("/tmp/zshrs-inside"), || {
            let p = log_path();
            assert!(
                p.starts_with("/tmp/zshrs-inside"),
                "log_path should live inside ZSHRS_HOME: {:?}",
                p
            );
        });
    }

    // ========================================================
    // Feature-flag helpers — interaction matrix
    // ========================================================

    #[test]
    fn feature_helpers_are_stable_within_one_call() {
        // Two back-to-back calls produce identical output (no random
        // toggling). Trivial sanity check, catches any future
        // accidental side-effect inside the helpers.
        assert_eq!(profiling_enabled(), profiling_enabled());
        assert_eq!(flamegraph_enabled(), flamegraph_enabled());
        assert_eq!(prometheus_enabled(), prometheus_enabled());
    }

    #[test]
    fn flush_is_idempotent_and_returns_unit() {
        // `flush` sleeps ~50ms but must always return cleanly. Two
        // back-to-back calls should still be safe.
        flush();
        flush();
    }
}