omarchy icon indicating copy to clipboard operation
omarchy copied to clipboard

omarchy-cmd-terminal-cwd returns wrong directory with multiple Ghostty windows

Open NixBiks opened this issue 11 months ago • 11 comments

Problem

omarchy-cmd-terminal-cwd returns the wrong working directory when multiple Ghostty windows are open.

Root Cause

Ghostty uses a single process for all windows. When hyprctl activewindow returns the PID, it's the same PID for every Ghostty window:

# Two different Ghostty windows, same PID
$ hyprctl activewindow  # Window 1
Window 55a0693ae7f0 → Ghostty:
    pid: 241028

$ hyprctl activewindow  # Window 2  
Window 55a0693be1b0 → Ghostty:
    pid: 241028

The script then runs pgrep -P 241028 which returns ALL shell children from ALL Ghostty windows:

$ pgrep -P 241028
251877  # shell from window 1
257118  # shell from window 2
269931  # shell from window 3
...

Using tail -n1 picks an arbitrary shell, not the one in the focused window.

The Real Issue

There's no way to map a Hyprland window address (55a0693ae7f0) to its specific shell process. Ghostty would need to expose this information somehow.

Possible Solutions

  1. Ghostty IPC/socket - Query Ghostty for the focused window's shell PID or CWD
  2. Environment variable - Ghostty could set a unique identifier per window that shells inherit
  3. PTY file descriptor matching - Match the window's PTY to the shell's controlling terminal

Environment

  • Omarchy 3.2.3
  • Ghostty 1.2.3
  • Hyprland 0.52.2

NixBiks avatar Dec 22 '25 10:12 NixBiks

Maybe @mitchellh has an idea of how we might solve this. I've seen it as well. I thought there was something built-in, though? So maybe we don't even need this when the terminal is ghostty?

dhh avatar Dec 22 '25 21:12 dhh

Probably want this:

https://ghostty.org/docs/config/reference#window-inherit-working-directory

daviewales avatar Dec 24 '25 03:12 daviewales

Probably want this:

https://ghostty.org/docs/config/reference#window-inherit-working-directory

This is unrelated, I believe, and default true anyways.

I don't have a good answer to this, but I'm open to suggestions. For IPC, we don't really have a good mechanism to do this yet. We plan on exposing an env var with a unique ID per window at some point, but I'm not sure how you'd map that back to a pwd.

mitchellh avatar Dec 24 '25 03:12 mitchellh

What I was imagining is that if Ghostty defaults to opening new windows with the directory of the previously active window, then you could skip omarchy-cmd-terminal-cwd entirely, and just ask Ghostty to open a new window, which would automatically inherit the correct working directory.

I've tested this by commenting out the --dir argument in ~/.config/hypr/bindings.conf:

bindd = SUPER, RETURN, Terminal, exec, $terminal #--dir="$(omarchy-cmd-terminal-cwd)"

However, this doesn't seem to work 100% of the time.

Essentially, this triggers the following when you press the shortcut to open a new terminal:

uwsm app -- xdg-terminal-exec

However, there appears to be a bug such that if I change directory, then immediately open a new terminal with the above command (or the keyboard shortcut), it sometimes opens with the previous working directory, rather than the current one. The next time I open a new window, it gets it right:

https://github.com/user-attachments/assets/865ffdf6-3182-4b57-a939-259f8257787a

Alternatively, if I start a new terminal by running ghostty directly, or even just with Shift+Ctrl+N, it gets it right:

https://github.com/user-attachments/assets/37d78fb9-4f1e-46ef-b372-38b26161b247

daviewales avatar Dec 24 '25 05:12 daviewales

Looking at /usr/share/applications/com.mitchellh.ghostty.desktop, this appears to be a bug when using the --gtk-single-instance=true flag:

https://github.com/user-attachments/assets/6826c31b-5c45-4543-aa95-810fae67d97a

Observe in the video above that when I freshly open a new terminal, change to a new directory, then run ghostty, it correctly opens a new window with the current directory of the previous window.

However, when I freshly open a new terminal, change to a new directory, then run ghostty --gtk-single-instance=true, it doesn't inherit the working directory. (It seems to get it right on the second attempt, however.)

daviewales avatar Dec 24 '25 05:12 daviewales

To eliminate the possibility of inheriting the working directory from the parent shell (see edit above), I've experimented with just binding the new terminal shortcut to directly invoke ghostty.

Now, I find that both ghostty without arguments, and ghostty --gtk-single-instance=true yield the same result. The first launch after changing directory in the previous terminal yields the wrong directory. The second launch gets it right:

https://github.com/user-attachments/assets/b143bbed-c01d-4120-855f-389f41e96d44

The behaviour above occurs using either of the following bindings:

bindd = SUPER, RETURN, Terminal, exec, ghostty --gtk-single-instance=true
bindd = SUPER, RETURN, Terminal, exec, ghostty

Edit: In an attempt to narrow this down to something easily reproducible on the ghostty side, I tried launching new instances of ghostty directly from the app launcher (Super+Space > ghostty). However, this opens with the correct working directory every time:

https://github.com/user-attachments/assets/6e926965-a918-4072-b18b-1750445a0c69

Edit: Another way to reproduce this is as follows:

  1. Open ghostty, either through the app launcher, or through the key binding.
  2. Immediately paste and run the following: cd /usr/ && ghostty --gtk-single-instance=true
  3. Expected: Both first and second terminals to have working directory /usr. Actual: First terminal has working directory /usr, second has working directory ~.

daviewales avatar Dec 24 '25 10:12 daviewales

I fix this problem doing these two things:

  1. Setting window-inherit-working-directory = false in .config/ghostty/config as @daviewales suggest
  2. Changing the Hyprland binding to bindd = SUPER, RETURN, Terminal, exec, uwsm-app -- ghostty --working-directory="$(omarchy-cmd-terminal-cwd)"

For some reason I didn't want to look into, xdg-terminal-exec does not pass --working-directory=xyz to ghostty

pedro757 avatar Jan 15 '26 12:01 pedro757

I could not find another workaround, so i also had to use step1 and 2 suggested by @pedro757, this to display pwd in waybar.
https://github.com/kurdt94/omarchy-pwd-and-git-in-waybar. I have not encountered any issues by running multiple ghostty instances.

kurdt94 avatar Jan 17 '26 02:01 kurdt94

I reproduced this as a Super+Enter launch failure/hang on Arch+Hyprland+Ghostty. In my case the chain was: SUPER+RETURN -> uwsm-app -- xdg-terminal-exec --dir="$(omarchy-cmd-terminal-cwd)" (from migration) and Ghostty/xdg-terminal-exec translation. Direct bind to Ghostty fixes it: uwsm-app -- ghostty --working-directory="$(omarchy-cmd-terminal-cwd)". This appears to be the same regression path (--dir + Ghostty + multi-window/shell pid ambiguity).

derekszen avatar Feb 27 '26 03:02 derekszen

Reproduced on another current Omarchy system and implemented an Omarchy-side fix in #8033.

Affected system

Component Details
System Framework Desktop (AMD Ryzen AI Max 300 Series), revision A6
CPU AMD Ryzen AI Max+ 395
GPU AMD Strix Halo Radeon 8060S
Kernel Linux 7.1.8-arch1-3 x86_64
Omarchy omarchy-dev 4.0.0.r1791.ged7bae4-1
Hyprland 0.56.2-1
Ghostty 1.3.1-2
xdg-terminal-exec 0.14.2-1
uwsm 0.26.6-1

Reproduction

  1. Select Ghostty as the default terminal.
  2. Open two independent Ghostty windows and change each shell to a different directory.
  3. Focus the first window and press Super+Enter.
  4. The new window can inherit the other Ghostty window's directory instead of the focused window's directory.

The packaged /usr/share/applications/com.mitchellh.ghostty.desktop launched both actions with --gtk-single-instance=true and enabled D-Bus activation. Independent top-level windows therefore shared one Ghostty PID. omarchy-cmd-terminal-cwd starts from the PID returned by hyprctl activewindow and selects a child shell, so the shared PID could not uniquely identify the focused window's shell.

Validated fix

The behavior was corrected locally by:

  • setting gtk-single-instance = false;
  • installing a user Ghostty desktop entry with bare Exec=/usr/bin/ghostty lines and no D-Bus activation;
  • deleting the xdg-terminal-exec cache before resolving the launcher again; and
  • opening new top-level Ghostty windows so each had a process Omarchy could map to its shell.

After that, Super+Enter inherited the focused top-level window's directory. Tabs and splits remain a separate limitation because they still share their window process.

PR #8033 turns this into an automatic Omarchy integration: it updates the default config, ships the desktop override, installs it without replacing user customization, invalidates the terminal cache, and includes an idempotent migration for existing users. The full test suite passes all 192 shell test files with the companion packaging and ISO fixtures available.

sebishogun avatar Aug 24 '26 11:08 sebishogun

Checked against quattro today: the bug is still there. Super+Enter passes the focused window's PID to omarchy-cmd-terminal-cwd, which now reads /proc/<pid>/task/*/children instead of pgrep, but still takes the newest child shell. With Ghostty's packaged desktop entry forcing --gtk-single-instance=true, every top-level window shares that PID, so the shell it picks can belong to another window. config/ghostty/config on quattro does not set gtk-single-instance = false.

The open fix is #8033, which already says it fixes this issue. It turns single-instance off by default, ships a Ghostty desktop entry without the forced flag, and migrates existing configs, so separately launched windows get their own process again. Tabs and splits inside one Ghostty window still share a process, and Omarchy can't tell their shells apart until Ghostty exposes the focused surface's process or directory; #8033 says this too.

This was read in source rather than reproduced live. The reading and the check of #8033 were done by Claude Opus 5.5 and independently by Codex Medium, which reached the same conclusion. Independence between the two isn't guaranteed. #8033 is being reviewed as its own item, and the issue now waits on the maintainer's decision there.

omarchybot avatar Oct 07 '26 23:10 omarchybot