Files
ghostty-rs/crates/ghostty-cli/src/validate_config.rs
T
ddidderr f76179f49a fix: +show-face u21/UTF-8 semantics, Zig output buffering, isAbsolute
Resolves RV14 (docs/rewrite/notes/review-rv14-codex.md, final-Z17.md):
- +show-face --cp parses as u21 (new U21 field type): out-of-range
  values give an invalid-value diagnostic and exit 1 instead of a panic;
  --string accepts raw bytes and fails with error.InvalidUtf8 after the
  codepoint lookup like Zig.
- CLI actions write through ZigWriter, a model of Zig 0.15's buffered
  File.writer (fixed buffer, drain on overflow, explicit flush), so
  stdout/stderr interleave in Zig's flush order (+show-face, help,
  version, validate-config, show-config, list-*, ssh-cache, crash-report,
  edit-config, new-window).
- Zig 0.15.2 std.fs.path.isAbsolute port (Windows: \theme and /theme are
  absolute) replaces Path::is_absolute at every Zig isAbsolute site.
- io_uring is available iff a 256-entry ring can be created, like
  libxev; the reactor falls back to IORING_OP_TIMEOUT without EXT_ARG.
- Windows basename (`λ:theme`) verified against 0.15.2 (test added).

Test Plan:
- cargo test --workspace --no-fail-fast
- diff +show-face outputs (pipe) against /usr/bin/ghostty
2026-10-12 15:15:18 +02:00

158 lines
5.0 KiB
Rust

//! `+validate-config` (ZIG/src/cli/validate_config.zig).
use std::io::Write;
use ghostty_config::{config::Config, config_struct};
use ghostty_os::osbytes::EncodedOs;
use crate::error::Error;
config_struct! {
pub struct Options {
/// The path of the config file to validate. If this isn't specified,
/// then the default config file paths will be validated.
"config-file" => config_file: Option<EncodedOs> = None,
}
}
cli_options!(Options, help);
/// The `validate-config` command is used to validate a Ghostty config file.
///
/// When executed without any arguments, this will load the config from the default
/// location.
///
/// Flags:
///
/// * `--config-file`: can be passed to validate a specific target config file in
/// a non-default location
pub fn run(args: &[impl AsRef<[u8]>]) -> Result<u8, Error> {
let mut opts = Options::default();
crate::parse_options(&mut opts, args)?;
let mut stdout = crate::zig_writer::ZigWriter::new(crate::zig_writer::stdout_sink(), 1024);
let result = run_inner(&opts, &mut stdout);
stdout.end()?;
result
}
fn run_inner(opts: &Options, stdout: &mut dyn Write) -> Result<u8, Error> {
let mut cfg = Config::new_default();
// If a config path is passed, validate it, otherwise validate default configs
if let Some(config_path) = &opts.config_file {
// Byte-preserving: the option string carries non-UTF-8 bytes via the
// reversible `osbytes` escape and the canonical path may too.
let abs_path = std::fs::canonicalize(config_path.to_path()?)?;
cfg.load_file(&EncodedOs::from_path(&abs_path))?;
cfg.load_recursive_files()?;
} else {
cfg = Config::load()?;
}
cfg.finalize()?;
if !cfg.diagnostics.items().is_empty() {
for diag in cfg.diagnostics.items() {
// Diagnostics embed config values in the reversible byte
// encoding; print the original bytes.
stdout.write_all(&ghostty_os::osbytes::config_str_to_bytes(&format!(
"{diag}\n"
)))?;
}
return Ok(1);
}
Ok(0)
}
#[cfg(test)]
mod tests {
#![allow(clippy::unwrap_used)]
use super::*;
fn write_tmp(name: &str, content: &str) -> String {
let dir =
std::env::temp_dir().join(format!("ghostty-cli-vc-{}-{name}", std::process::id()));
std::fs::create_dir_all(&dir).unwrap();
let p = dir.join("config");
std::fs::write(&p, content).unwrap();
p.to_string_lossy().into_owned()
}
#[test]
fn validate_good_file() {
let p = write_tmp("good", "font-size = 14\n");
let opts = Options {
config_file: Some(p.into()),
};
let mut out = Vec::new();
assert_eq!(0, run_inner(&opts, &mut out).unwrap());
assert_eq!(0, out.len());
}
#[test]
fn validate_bad_file_prints_diagnostics() {
let p = write_tmp("bad", "not-a-key = 1\nfont-size = abc\n");
let opts = Options {
config_file: Some(p.clone().into()),
};
let mut out = Vec::new();
assert_eq!(1, run_inner(&opts, &mut out).unwrap());
let s = String::from_utf8(out).unwrap();
assert!(
s.contains(&format!("{p}:1:not-a-key: unknown field\n")),
"{s}"
);
assert!(
s.contains(&format!("{p}:2:font-size: invalid value \"abc\"\n")),
"{s}"
);
}
#[cfg(unix)]
#[test]
fn diagnostics_print_original_bytes() {
use std::os::unix::ffi::OsStrExt;
let tmp = tempfile::tempdir().unwrap();
let path = tmp.path().join("config");
std::fs::write(&path, b"bad\xffkey = 1\n").unwrap();
let opts = Options {
config_file: Some(EncodedOs::from_path(&path)),
};
let mut out = Vec::new();
assert_eq!(1, run_inner(&opts, &mut out).unwrap());
assert!(
out.windows(7).any(|w| w == b"bad\xffkey"),
"{:?}",
String::from_utf8_lossy(&out)
);
assert!(!out.contains(&0xef), "no escape encoding in the output");
let _ = path.as_os_str().as_bytes();
}
#[test]
fn validate_missing_file_is_error() {
let opts = Options {
config_file: Some("/nonexistent/ghostty/config".into()),
};
assert!(run_inner(&opts, &mut Vec::new()).is_err());
}
#[cfg(unix)]
#[test]
fn validate_symlink_to_non_utf8_target() {
use std::os::unix::ffi::OsStrExt;
let tmp = tempfile::tempdir().unwrap();
let target = tmp.path().join(std::ffi::OsStr::from_bytes(b"cfg\xff"));
std::fs::write(&target, "font-size = 14\n").unwrap();
let link = tmp.path().join("link");
std::os::unix::fs::symlink(&target, &link).unwrap();
let opts = Options {
config_file: Some(EncodedOs::from_path(&link)),
};
let mut out = Vec::new();
assert_eq!(0, run_inner(&opts, &mut out).unwrap());
assert_eq!(out.len(), 0);
}
}