Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 27 additions & 10 deletions bin/omarchy-launch-editor
Original file line number Diff line number Diff line change
@@ -1,16 +1,29 @@
#!/bin/bash

# omarchy:summary=Launch the default editor selected via Omarchy defaults.
# omarchy:args=[--inline] <path>
# omarchy:args=[--inline|--sudo] <path>

default_editor="$HOME/.local/state/omarchy/defaults/editor"

if [[ ${1:-} == "--inline" ]]; then
inline=true
# --inline runs a terminal editor in the caller's terminal rather than opening
# one for it. --sudo is what SUDO_EDITOR points at: sudo hands the editor a
# temporary copy and keeps whatever is on disk once the editor exits, so an
# editor that detaches reads as "closed without changing anything" and the edit
# is discarded with no error. That mode stays on a terminal editor, which also
# keeps a graphical editor from being run as root.
case "${1:-}" in
--inline)
mode="inline"
shift
else
inline=false
fi
;;
--sudo)
mode="sudo"
shift
;;
*)
mode="windowed"
;;
esac

if [[ -f $default_editor ]]; then
read -r editor <"$default_editor"
Expand All @@ -25,13 +38,17 @@ omarchy-cmd-present "$editor" || editor="nvim"

case "${editor##*/}" in
nvim | vim | nano | micro | hx | helix | fresh)
if [[ $inline == "true" ]]; then
exec "$editor" -- "$@"
else
if [[ $mode == "windowed" ]]; then
exec omarchy-launch-tui "$editor" -- "$@"
else
exec "$editor" -- "$@"
fi
;;
*)
exec setsid uwsm-app -- "$editor" -- "$@"
if [[ $mode == "sudo" ]]; then
exec nvim -- "$@"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Fallback assumes Neovim exists

If a graphical editor is selected but Neovim has been removed or is unavailable on PATH, --sudo runs nvim without checking for it. The earlier availability check only tests the selected graphical editor, so sudoedit and visudo cannot open the temporary file.

else
exec setsid uwsm-app -- "$editor" -- "$@"
fi
;;
esac
13 changes: 12 additions & 1 deletion default/bash/envs
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
# Editor used by CLI
export EDITOR="${EDITOR:-omarchy-launch-editor --inline}"
export SUDO_EDITOR="$EDITOR"

# sudo keeps whatever is on disk once the editor exits, so an editor that
# detaches -- every graphical one omarchy-default-editor offers -- reads as
# "closed without changing anything" and sudoedit and visudo throw the edit
# away with no error. Send the Omarchy default through --sudo, which stays on a
# terminal editor. An EDITOR the user set themselves is left to them, and an
# explicit SUDO_EDITOR still wins over both.
if [[ $EDITOR == "omarchy-launch-editor --inline" ]]; then
export SUDO_EDITOR="${SUDO_EDITOR:-omarchy-launch-editor --sudo}"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Inherited editor still detaches

If a new shell inherits the old Omarchy value SUDO_EDITOR=omarchy-launch-editor --inline, this assignment preserves it instead of selecting --sudo. With a graphical default editor, sudoedit or visudo still launches the editor detached and discards the edit, so the bug this PR intends to fix remains reachable after an upgrade.

else
export SUDO_EDITOR="${SUDO_EDITOR:-$EDITOR}"
fi

# Used by terminal programs (like gh) to open URLs detached from the terminal
# process tree. Shell-scoped on purpose: exporting BROWSER session-wide makes
Expand Down
16 changes: 14 additions & 2 deletions test/shell.d/editor-env-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,18 @@ editor=$(EDITOR=helix bash -c 'source "$1"; printf "%s" "$EDITOR"' bash "$envs")
[[ $editor == "helix" ]] || fail "bash env preserves the inherited editor" "actual: $editor"
pass "bash env preserves the inherited editor"

# sudo keeps whatever is on disk once the editor exits, so the sudo path has to
# be an editor that blocks. --inline detaches a graphical editor, which reads
# as "closed without changing anything" and loses the edit, so the Omarchy
# default routes through --sudo instead.
sudo_editor=$(env -u EDITOR -u SUDO_EDITOR bash -c 'source "$1"; printf "%s" "$SUDO_EDITOR"' bash "$envs")
[[ $sudo_editor == "omarchy-launch-editor --inline" ]] || fail "sudo uses the default editor" "actual: $sudo_editor"
pass "sudo uses the default editor"
[[ $sudo_editor == "omarchy-launch-editor --sudo" ]] || fail "sudo gets an editor that blocks" "actual: $sudo_editor"
pass "sudo gets an editor that blocks until the edit is done"

sudo_editor=$(env -u SUDO_EDITOR EDITOR=helix bash -c 'source "$1"; printf "%s" "$SUDO_EDITOR"' bash "$envs")
[[ $sudo_editor == "helix" ]] || fail "sudo follows an editor the user set" "actual: $sudo_editor"
pass "sudo follows an editor the user set"

sudo_editor=$(env -u EDITOR SUDO_EDITOR=nano bash -c 'source "$1"; printf "%s" "$SUDO_EDITOR"' bash "$envs")
[[ $sudo_editor == "nano" ]] || fail "an explicit sudo editor wins" "actual: $sudo_editor"
pass "an explicit sudo editor wins"
86 changes: 86 additions & 0 deletions test/shell.d/launch-editor-sudo-test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#!/bin/bash

set -euo pipefail

source "$(dirname "$0")/base-test.sh"

launcher="$ROOT/bin/omarchy-launch-editor"
chooser="$ROOT/bin/omarchy-default-editor"

test_tmp=$(mktemp -d)
trap 'rm -rf "$test_tmp"' EXIT

stub_bin="$test_tmp/bin"
home="$test_tmp/home"
mkdir -p "$stub_bin" "$home/.local/state/omarchy/defaults"

# Every command the launcher can hand the file to logs its own argv, so the
# assertions read the process that would actually have run.
for stub in nvim vim nano micro hx helix fresh code cursor zeditor sublime_text \
emacs setsid uwsm-app omarchy-launch-tui; do
cat >"$stub_bin/$stub" <<SH
#!/bin/bash
printf '%s %s\n' "$stub" "\$*" >>"\$LAUNCH_LOG"
SH
chmod +x "$stub_bin/$stub"
done

# Without this the launcher falls back to nvim for every editor and the run
# proves nothing about the editor that was chosen.
cat >"$stub_bin/omarchy-cmd-present" <<'SH'
#!/bin/bash
exit 0
SH
chmod +x "$stub_bin/omarchy-cmd-present"

launch_with() {
local editor="$1"
shift

printf '%s\n' "$editor" >"$home/.local/state/omarchy/defaults/editor"
: >"$test_tmp/log"
LAUNCH_LOG="$test_tmp/log" HOME="$home" PATH="$stub_bin:$PATH" \
bash "$launcher" "$@" /etc/sudoers.tmp >/dev/null 2>&1 || true
cat "$test_tmp/log"
}

# The editors the menu can actually put in that state file, read off the
# chooser so an editor added there is covered here without a second edit.
mapfile -t editors < <(grep -oE 'editor="[a-z_]+"' "$chooser" | cut -d'"' -f2 | sort -u)
((${#editors[@]} >= 8)) ||
fail "the chooser's editor list was read" "got: ${editors[*]}"

for editor in "${editors[@]}"; do
launched=$(launch_with "$editor" --sudo)

[[ $launched == *"/etc/sudoers.tmp"* ]] ||
fail "--sudo hands $editor the file to edit" "got: $launched"

# setsid and uwsm-app both return before the editor is done, which is what
# loses the edit. Neither belongs anywhere on the sudo path.
[[ $launched != *"setsid"* && $launched != *"uwsm-app"* ]] ||
fail "--sudo does not detach the editor for $editor" "got: $launched"

# omarchy-launch-tui opens a separate terminal window and returns, so it is
# no better here than detaching outright.
[[ $launched != *"omarchy-launch-tui"* ]] ||
fail "--sudo does not hand $editor to a separate terminal" "got: $launched"
done

pass "--sudo blocks for every editor the chooser offers"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Blocking behavior remains untested

The test reports that --sudo blocks, but its editor stubs exit immediately and the assertions check only logged arguments. A path that launches a different non-blocking editor could still pass, leaving the behavior this test is meant to protect unverified. A process-liveness check would cover that gap.

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!


# The windowed and inline paths are unchanged: a graphical editor still gets
# detached when it is not sudo asking.
windowed=$(launch_with code)
[[ $windowed == "setsid uwsm-app -- code -- /etc/sudoers.tmp" ]] ||
fail "a graphical editor is still detached when launched windowed" "got: $windowed"

windowed=$(launch_with nvim)
[[ $windowed == "omarchy-launch-tui nvim -- /etc/sudoers.tmp" ]] ||
fail "a terminal editor is still opened in a terminal when launched windowed" "got: $windowed"

inline=$(launch_with nvim --inline)
[[ $inline == "nvim -- /etc/sudoers.tmp" ]] ||
fail "--inline still runs a terminal editor in place" "got: $inline"

pass "the windowed and inline paths are unchanged"