Refactor `working-directory` & Fix expanding tilde (~) by arnodirlam · Pull Request #10205 · ghostty-org/ghostty

2 min read Original article ↗

Fixes #3328.

Problem

The working-directory config option doesn't expand tilde (~) paths like ~/dev to the user's home directory. When users set working-directory = ~/dev in their config file, ghostty starts at / instead of the expected directory.

Root Cause

In src/config/Config.zig, working-directory is defined as ?[]const u8 (line 1430), not as a Path type. The expandPaths function (lines 4099-4130) only processes fields of type Path, ?Path, RepeatablePath, or ?RepeatablePath. Since working-directory is ?[]const u8, it's skipped during path expansion. The tilde expansion logic already exists in src/config/path.zig (Path.expand method, lines 120-220, with tilde expansion at lines 148-182) and works correctly for other path fields, but is never called for working-directory.

Solution

Refactor working-directory from ?[]const u8 to a tagged union type (similar to Command) with variants for home, inherit, and path, addressing the TODO at line 4382 and enabling automatic tilde expansion through the existing expandPaths mechanism. This eliminates runtime string comparisons and special-case handling while providing type safety.

Testing

Tested successfully on the following system:

  • OS: macOS 26.2 (Build 25C56)
  • Architecture: arm64 (Apple Silicon)
  • Zig Version: 0.15.2 (managed via mise)
  • Shell: zsh

The following config values were tested:

  • working-directory = ~ - Expands to "home", starts in user's home directory
  • working-directory = ~/dev - Expands to /Users/arno/dev, starts in the specified subdirectory
  • working-directory = home - Preserved as-is, starts in user's home directory
  • working-directory = /absolute/path - Preserved as-is, starts in the absolute path
  • working-directory = ~/nonexistent - Error logged to diagnostics, path expansion fails gracefully

Not tested:

  • working-directory = inherit - not sure how to test this