refactor(config): parse config values from raw bytes like Zig

Zig parses config values (cli/args.zig, Config.zig field parsers,
config/string.zig) from raw byte slices, and each field type decides
whether it requires UTF-8 (InvalidValue otherwise) or stores bytes. The
port escape-encoded raw bytes into Strings at the line reader and let
fields parse the escaped text, so fields could observe encoding
artifacts; successive audits kept finding such leaks (RV5-RV8).

Field parsers now receive the original bytes: text fields validate UTF-8
where Zig does, byte/path fields store bytes (EncodedOs/OsString). This
resolves all 9 RV8 findings (docs/rewrite/notes/review-rv8-codex.md),
each with a regression test: Windows path conversion fails with
BadPathName instead of substituting U+FFFD; Windows process launch uses
strict UTF-8 like Zig's Command; the WTF-8 decoder accepts paired-
surrogate encodings like Zig; literal U+FDD0 Unicode key triggers;
selection-word-chars and clipboard-codepoint-map reject invalid UTF-8;
font style / codepoint-map family names use original bytes; C title
actions forward bytes; D-Bus new-window/Flatpak conversions match Zig.
Design, sweep and validation: docs/rewrite/notes/final-bytes-refactor.md.

Also moves the bundled iTerm2-Color-Schemes license out of
resources/themes/ so +list-themes no longer lists it as a theme
(output is now byte-identical to Ghostty 1.3.1).

Done by Codex (gpt-6-astra), reviewed and verified by the orchestrator.

Test Plan:
- cargo test --workspace --no-fail-fast (3428 passed, exit 0)
- just check-macos (both), just check-windows, just check-wasm
- +show-config --default --docs, +list-keybinds, +list-actions --docs,
  +list-themes --plain compared with /usr/bin/ghostty
This commit is contained in:
ddidderr committed 2026-10-12 11:57:01 +02:00
1 parent 37a1646611
commit 672e8a4ae1
100 files changed
+2310 -1054

No files matched your search

+77 -2
View File
@@ -49,7 +49,7 @@ cli_options!(Options, help);
/// * `--docs`: Print the documentation above each option as a comment,
/// This is very noisy but is very useful to learn about available
/// options, especially paired with `--default`.
pub fn run(args: &[String]) -> Result<u8, Error> {
pub fn run(args: &[impl AsRef<[u8]>]) -> Result<u8, Error> {
let mut opts = Options::default();
crate::parse_options(&mut opts, args)?;
@@ -113,6 +113,81 @@ mod tests {
assert!(s.contains("\nfont-size = 12\n"));
}
#[test]
fn show_config_preserves_raw_and_text_fields_on_reload() {
use ghostty_config::cli_args::LineIterator;
let raw = b"title=x\xff\xef\xb7\x90\xc3\xa9\nfont-style=style\xfe\xef\xb7\x90\nfont-codepoint-map=U+0041=family\xff\nselection-word-chars=\xef\xb7\x90\xc3\xa9\nclipboard-codepoint-map=U+0041=\xef\xb7\x90\xc3\xa9\nkeybind=\xef\xb7\x90=set_surface_title:payload\ncommand-palette-entry=title:\"name\\xff\",action:\"text:payload\\xfe\"\n";
let mut config = Config::new_default();
config
.load_iter(&mut LineIterator::new(raw.as_slice()))
.unwrap();
assert_eq!(config.diagnostics.items(), []);
let output = format_config_bytes(&config, &Options::default()).unwrap();
for line in [
b"title = x\xff\xef\xb7\x90\xc3\xa9".as_slice(),
b"font-style = style\xfe\xef\xb7\x90",
b"font-codepoint-map = U+0041=family\xff",
b"selection-word-chars = \xef\xb7\x90\xc3\xa9",
b"clipboard-codepoint-map = U+0041=\xef\xb7\x90\xc3\xa9",
b"keybind = \xef\xb7\x90=set_surface_title:payload",
] {
assert!(
output.split(|b| *b == b'\n').any(|value| value == line),
"missing {line:?} in {output:?}"
);
}
let mut reloaded = Config::new_default();
reloaded
.load_iter(&mut LineIterator::new(output.as_slice()))
.unwrap();
assert_eq!(reloaded.diagnostics.items(), []);
assert_eq!(reloaded.title, config.title);
assert_eq!(reloaded.font_style, config.font_style);
assert_eq!(reloaded.font_codepoint_map, config.font_codepoint_map);
assert_eq!(reloaded.selection_word_chars, config.selection_word_chars);
assert_eq!(
reloaded.clipboard_codepoint_map,
config.clipboard_codepoint_map
);
let trigger = ghostty_input::binding::Trigger::parse("\u{fdd0}").unwrap();
assert_eq!(
reloaded
.keybind
.set
.get(trigger)
.unwrap()
.as_leaf()
.unwrap()
.action,
config
.keybind
.set
.get(trigger)
.unwrap()
.as_leaf()
.unwrap()
.action,
);
assert_eq!(
reloaded.command_palette_entry.value.last(),
config.command_palette_entry.value.last()
);
}
#[test]
fn show_config_action_escaping_uses_original_bytes() {
use ghostty_config::cli_args::LineIterator;
let mut config = Config::new_default();
config
.load_iter(&mut LineIterator::new(
b"keybind=a=set_surface_title:payload\xff\xef\xb7\x90".as_slice(),
))
.unwrap();
let output = format_config_bytes(&config, &Options::default()).unwrap();
// Zig formats action byte strings with std.zig.stringEscape.
assert!(output.ends_with(b"keybind = a=set_surface_title:payload\\xff\\xef\\xb7\\x90\n"));
}
#[cfg(unix)]
#[test]
fn show_config_round_trips_non_utf8_and_fdd0_paths() {
@@ -152,7 +227,7 @@ mod tests {
assert!(opts.default);
assert!(!opts.changes_only);
assert!(matches!(
crate::parse_options(&mut opts, &["g".into(), "--help".into()]),
crate::parse_options(&mut opts, &["g", "--help"]),
Err(Error::ActionHelpRequested)
));
}