Skip to content
Closed
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
22 changes: 19 additions & 3 deletions shell/shell.qml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ ShellRoot {

property var defaultsConfig: builtinShellConfig
property var shellConfig: builtinShellConfig
// True once a versioned user shell.json has been applied successfully.
// Empty/truncated reloads must retain that in-memory config (#12990).
property bool hasAppliedUserShellConfig: false
property bool pluginReloading: false
property bool pluginReloadPending: false

Expand All @@ -73,19 +76,32 @@ ShellRoot {
function applyShellConfig() {
// Decide which source is canonical: a valid user shell.json overrides
// defaults entirely; otherwise fall back to defaults. We do not deep-merge.
// After a valid user config has been applied, an empty/truncated or
// unparseable reload retains the last valid in-memory config instead of
// clobbering with builtins (#12990). First load with nothing valid still
// falls back to defaults.
var defaults = Util.isPlainObject(defaultsConfig) ? defaultsConfig : builtinShellConfig
var user = null
var userText = userConfigFile.text() || ""
if (userText.trim()) {
try {
var parsed = JSON.parse(userText)
if (Util.isPlainObject(parsed) && parsed.version === 1) user = parsed
else if (Util.isPlainObject(parsed)) console.warn("shell.json missing version: 1, using defaults")
else if (Util.isPlainObject(parsed)) console.warn("shell.json missing version: 1")
} catch (e) {
console.warn("shell.json parse failed, using defaults:", e)
console.warn("shell.json parse failed:", e)
}
}
shellConfig = user || defaults
if (user) {
shellConfig = user
hasAppliedUserShellConfig = true
return
}
if (hasAppliedUserShellConfig) {
console.warn("shell.json empty or invalid after a prior valid load; retaining last valid config")
return
}
shellConfig = defaults
}

function loadDefaults(raw) {
Expand Down
141 changes: 141 additions & 0 deletions test/shell.d/shell-config-empty-retain-test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
#!/bin/bash

set -euo pipefail

source "$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)/base-test.sh"

# A shell.json that empties while the shell runs must not become the defaults,
# or the next shell-side write persists them over the user's layout (#12990).

test_tmp=""
qs_pid=""

cleanup() {
if [[ -n $qs_pid ]] && kill -0 "$qs_pid" 2>/dev/null; then
kill "$qs_pid" 2>/dev/null || true
wait "$qs_pid" 2>/dev/null || true
fi
# The plugin registry's inotifywait can outlive the shell and holds the runner's lock fd.
[[ -n $test_tmp ]] && pkill -f "$test_tmp" 2>/dev/null || true
[[ -n $test_tmp && -d $test_tmp ]] && rm -rf "$test_tmp"
return 0
}
trap cleanup EXIT

require_compositor "shell config truncation test"

if ! command -v quickshell >/dev/null 2>&1; then
skip "quickshell not installed; skipping shell config truncation test"
exit 0
fi

require_command jq

test_tmp=$(mktemp -d)
test_root="$test_tmp/omarchy"
test_home="$test_tmp/home"
log="$test_tmp/quickshell.log"
user_config="$test_home/.config/omarchy/shell.json"
mkdir -p "$test_root" "$test_home/.config/omarchy"
cp -a "$ROOT/shell" "$test_root/shell"
ln -s "$ROOT/config" "$test_root/config"
ln -s "$ROOT/bin" "$test_root/bin"

shell_ipc() {
OMARCHY_PATH="$test_root" HOME="$test_home" "$ROOT/bin/omarchy-shell" "$@"
}

fail_with_log() {
sed -n '1,200p' "$log" >&2
fail "$1"
}

effective() {
jq -r "$1" <<<"$(shell_ipc shell listShellConfig)"
}

# An empty layout writes nothing back on its own, so this script is the file's only writer.
write_user_config() {
cat >"$user_config" <<JSON
{
"version": 1,
"marker": "$1",
"bar": { "position": "bottom", "transparent": false, "layout": { "left": [], "center": [], "right": [] } },
"plugins": []
}
JSON
}

reload_until() {
shell_ipc -q shell reloadConfig >/dev/null
for _ in {1..40}; do
[[ $(effective '.marker // "none"') == "$1" ]] && return 0
kill -0 "$qs_pid" 2>/dev/null || fail_with_log "test shell exited while reloading shell.json"
sleep 0.1
done
return 1
}

# Unchanged state proves nothing until the reload has landed, so wait for the shell to say it retained.
reload_retaining() {
local before
before=$(grep -c 'retaining last valid config' "$log" || true)
shell_ipc -q shell reloadConfig >/dev/null
for _ in {1..50}; do
if (( $(grep -c 'retaining last valid config' "$log" || true) > before )); then
[[ $(effective '.marker // "none"') == "$1" ]]
return
fi
sleep 0.1
done
return 1
}

write_user_config first

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 First-load fallback is untested

The test always starts with a valid shell.json, so it never checks what happens when the file is empty or invalid before any valid config has loaded. A future change could break that fallback while this test still passes. Add fresh-shell cases for empty and invalid files, and check that the shipped defaults apply.

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!


OMARCHY_PATH="$test_root" \
HOME="$test_home" \
XDG_CONFIG_HOME="$test_home/.config" \
XDG_CACHE_HOME="$test_home/.cache" \
XDG_STATE_HOME="$test_home/.local/state" \
PATH="$ROOT/bin:$PATH" \
quickshell -p "$test_root/shell" --no-color >"$log" 2>&1 &
qs_pid=$!

ready=0
for _ in {1..100}; do
if [[ $(shell_ipc shell ping 2>/dev/null) == "ok" ]]; then
ready=1
break
fi
kill -0 "$qs_pid" 2>/dev/null || fail_with_log "test shell exited before IPC became available"
sleep 0.1
done
(( ready )) || fail_with_log "test shell answers IPC"

reload_until first || fail_with_log "a valid shell.json applies"
pass "a valid shell.json applies"

: >"$user_config"
reload_retaining first || fail_with_log "an emptied shell.json keeps the loaded config (got $(effective '.marker // "none"'))"
pass "an emptied shell.json keeps the loaded config"

for _ in {1..50}; do
[[ $(shell_ipc shell toggleBarTransparency) == "ok" ]] && break
sleep 0.2
done
for _ in {1..40}; do
[[ -s $user_config ]] && break
sleep 0.1
done
[[ $(jq -r '.marker // "none"' "$user_config") == "first" && $(jq -r '.bar.position' "$user_config") == "bottom" ]] ||
fail_with_log "the next shell-side write persists the user's config, not the defaults"
pass "the next shell-side write persists the user's config, not the defaults"

printf '{\n' >"$user_config"
reload_retaining first || fail_with_log "an unparseable shell.json keeps the loaded config"
pass "an unparseable shell.json keeps the loaded config"

write_user_config second
reload_until second || fail_with_log "a later valid shell.json still applies"
pass "a later valid shell.json still applies"