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
127 changes: 127 additions & 0 deletions bin/omarchy-mise-remove-work-bin-path
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
#!/bin/bash

# omarchy:summary=Remove automatic project bin directories from the Mise PATH
# omarchy:hidden=true

set -euo pipefail

work_dir="$HOME/Work"
mise_config="$work_dir/.mise.toml"
# install/user/mise-work.sh as shipped in Omarchy 4.0.3.
stock_sha="bd04f191d63bbde86920f44f76f0989fad980afc84e268e8474c201ec7149245"
cwd_bin='\{\{[[:space:]]*cwd[[:space:]]*\}\}/bin'
unsafe_path="^[[:space:]]*_[.]path[[:space:]]*=[[:space:]]*(\"$cwd_bin\"|'$cwd_bin')[[:space:]]*(#.*)?$"
env_section='^[[:space:]]*\[[[:space:]]*env[[:space:]]*\][[:space:]]*(#.*)?$'
any_section='^[[:space:]]*\[\[?.*\]\]?[[:space:]]*(#.*)?$'

remove_empty_work_dir=false
if [[ ! -e $work_dir ]]; then
mkdir -p "$work_dir"
remove_empty_work_dir=true
fi

was_ignored=false
if [[ -d $work_dir ]]; then
mise_state_dir=${MISE_STATE_DIR:-${XDG_STATE_HOME:-$HOME/.local/state}/mise}
# Mise expands a leading ~/ that a value set outside a shell leaves literal.
if [[ $mise_state_dir == "~/"* ]]; then
mise_state_dir="$HOME/${mise_state_dir#"~/"}"
fi
work_target=$(readlink -m "$work_dir")
config_path_target="$work_target/.mise.toml"
config_target=$(readlink -m "$mise_config")

# Mise records trust and ignore decisions as symlinks to the config or its root.
has_work_record() {
local entry target

[[ -d $1 ]] || return 1
for entry in "$1"/*; do
[[ -L $entry ]] || continue
target=$(readlink "$entry")
if [[ $target == $work_target || $target == $config_path_target || $target == $config_target ]]; then
return 0
fi
done
return 1
}

# Skip only the revoke when Mise is absent. The PATH edit below still runs.
revoke_work_trust() {
omarchy-cmd-present mise || return 0

# Normal Mise trust is recorded against the config-root directory, while
# paranoid trust is recorded against the file and its contents. Stage an
# empty, inert config when the legacy file is gone so either trust mode can
# resolve and revoke the original grant.
remove_empty_mise_config=false
if [[ ! -e $mise_config && ! -L $mise_config ]]; then
if (set -o noclobber; : >"$mise_config") 2>/dev/null; then
remove_empty_mise_config=true
fi
fi

untrust_target="$work_dir"
if [[ -f $mise_config ]]; then
untrust_target="$mise_config"
fi

if ! mise trust --untrust "$untrust_target"; then
if [[ $remove_empty_mise_config == "true" ]]; then
rm -f -- "$mise_config"
fi
exit 1
fi

if [[ $remove_empty_mise_config == "true" ]]; then
rm -f -- "$mise_config"
fi
}

if has_work_record "$mise_state_dir/ignored-configs"; then
was_ignored=true
fi

if [[ $was_ignored == "false" ]]; then
# Without Mise there is no trust to revoke, unless a grant is left in its
# state. That grant applies again once Mise is back, and only Mise revokes it.
if omarchy-cmd-missing mise && has_work_record "$mise_state_dir/trusted-configs"; then
echo "Mise is needed to revoke its trust for $work_dir. Install it and update again:" >&2
echo " omarchy pkg add mise-bin" >&2
exit 1
fi
revoke_work_trust
fi
fi

if [[ -f $mise_config ]]; then
if [[ ! -L $mise_config && $(sha256sum "$mise_config" | cut -d ' ' -f 1) == $stock_sha ]]; then
rm -f -- "$mise_config"
else
unsafe_env_paths=$(sed -n -E "\\%$env_section%,\\%$any_section% { \\%$unsafe_path%p; }" "$mise_config")
if [[ -n $unsafe_env_paths ]]; then
backup=$(mktemp "$mise_config.bak.XXXXXX")
cp -p -- "$mise_config" "$backup"
sed --follow-symlinks -i -E "\\%$env_section%,\\%$any_section% { \\%$unsafe_path%d; }" "$mise_config"

printf '\n%s\n' \
"Automatic project bin directories were removed from your Mise PATH." \
"Your other Mise settings were preserved."
printf '\nBackup saved to:\n %s\n' "$backup"
fi
fi
fi

if [[ -f $mise_config ]]; then
if [[ $was_ignored == "true" ]]; then
printf '\n%s\n' "This custom config remains ignored by Mise."
else
printf '\n%s\n %s\n' \
"Mise trust for this custom config was revoked. Review it before trusting it again:" \
"mise trust $mise_config"
fi
fi

if [[ $remove_empty_work_dir == "true" ]]; then
rmdir "$work_dir" 2>/dev/null || true
fi
101 changes: 4 additions & 97 deletions migrations/1789095456.sh
Original file line number Diff line number Diff line change
@@ -1,99 +1,6 @@
echo "Remove automatic project bin directories from PATH"

work_dir="$HOME/Work"
mise_config="$work_dir/.mise.toml"
# install/user/mise-work.sh as shipped in Omarchy 4.0.3.
stock_sha="bd04f191d63bbde86920f44f76f0989fad980afc84e268e8474c201ec7149245"
cwd_bin='\{\{[[:space:]]*cwd[[:space:]]*\}\}/bin'
unsafe_path="^[[:space:]]*_[.]path[[:space:]]*=[[:space:]]*(\"$cwd_bin\"|'$cwd_bin')[[:space:]]*(#.*)?$"
env_section='^[[:space:]]*\[[[:space:]]*env[[:space:]]*\][[:space:]]*(#.*)?$'
any_section='^[[:space:]]*\[\[?.*\]\]?[[:space:]]*(#.*)?$'

remove_empty_work_dir=false
if [[ ! -e $work_dir ]]; then
mkdir -p "$work_dir"
remove_empty_work_dir=true
fi

was_ignored=false
if [[ -d $work_dir ]]; then
mise_state_dir=${MISE_STATE_DIR:-${XDG_STATE_HOME:-$HOME/.local/state}/mise}
ignored_configs_dir="$mise_state_dir/ignored-configs"
work_target=$(readlink -m "$work_dir")
config_path_target="$work_target/.mise.toml"
config_target=$(readlink -m "$mise_config")

if [[ -d $ignored_configs_dir ]]; then
for ignored_entry in "$ignored_configs_dir"/*; do
[[ -L $ignored_entry ]] || continue
ignored_target=$(readlink "$ignored_entry")
if [[ $ignored_target == $work_target || $ignored_target == $config_path_target || $ignored_target == $config_target ]]; then
was_ignored=true
break
fi
done
fi

if [[ $was_ignored == "false" ]]; then
# Normal Mise trust is recorded against the config-root directory, while
# paranoid trust is recorded against the file and its contents. Stage an
# empty, inert config when the legacy file is gone so either trust mode can
# resolve and revoke the original grant.
remove_empty_mise_config=false
if [[ ! -e $mise_config && ! -L $mise_config ]]; then
if (set -o noclobber; : >"$mise_config") 2>/dev/null; then
remove_empty_mise_config=true
fi
fi

untrust_target="$work_dir"
if [[ -f $mise_config ]]; then
untrust_target="$mise_config"
fi

if mise trust --untrust "$untrust_target"; then
:
else
if [[ $remove_empty_mise_config == "true" ]]; then
rm -f -- "$mise_config"
fi
exit 1
fi

if [[ $remove_empty_mise_config == "true" ]]; then
rm -f -- "$mise_config"
fi
fi
fi

if [[ -f $mise_config ]]; then
if [[ ! -L $mise_config && $(sha256sum "$mise_config" | cut -d ' ' -f 1) == $stock_sha ]]; then
rm -f -- "$mise_config"
else
unsafe_env_paths=$(sed -n -E "\\%$env_section%,\\%$any_section% { \\%$unsafe_path%p; }" "$mise_config")
if [[ -n $unsafe_env_paths ]]; then
backup=$(mktemp "$mise_config.bak.XXXXXX")
cp -p -- "$mise_config" "$backup"
sed --follow-symlinks -i -E "\\%$env_section%,\\%$any_section% { \\%$unsafe_path%d; }" "$mise_config"

printf '\n%s\n' \
"Automatic project bin directories were removed from your Mise PATH." \
"Your other Mise settings were preserved."
printf '\nBackup saved to:\n %s\n' "$backup"
fi
fi
fi

if [[ -f $mise_config ]]; then
if [[ $was_ignored == "true" ]]; then
printf '\n%s\n' "This custom config remains ignored by Mise."
else
printf '\n%s\n %s\n' \
"Mise trust for this custom config was revoked. Review it before trusting it again:" \
"mise trust $mise_config"
fi
fi

if [[ $remove_empty_work_dir == "true" ]]; then
rmdir "$work_dir" 2>/dev/null || true
fi
# The repair lives in omarchy-mise-remove-work-bin-path so a machine without
# Mise still finishes it. A leftover Work trust grant keeps this migration
# pending until Mise can revoke it.
omarchy-mise-remove-work-bin-path
107 changes: 107 additions & 0 deletions test/shell.d/mise-work-path-test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
#!/bin/bash

# The Work PATH cleanup lives in omarchy-mise-remove-work-bin-path, which the
# migration runs. These cases cover a machine where Mise is gone: finish when
# nothing trusted ~/Work, and stay pending when a grant is left.

set -euo pipefail

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

require_command mise

test_dir=$(mktemp -d)
trap 'rm -rf "$test_dir"' EXIT
cleanup="$ROOT/bin/omarchy-mise-remove-work-bin-path"

run_cleanup() {
local test_home="$1"

env -i \
HOME="$test_home" \
XDG_CACHE_HOME="$test_home/.cache" \
XDG_CONFIG_HOME="$test_home/.config" \
XDG_DATA_HOME="$test_home/.local/share" \
XDG_STATE_HOME="$test_home/.local/state" \
MISE_PARANOID="${OMARCHY_TEST_MISE_PARANOID:-false}" \
${OMARCHY_TEST_MISE_STATE_DIR:+"MISE_STATE_DIR=$OMARCHY_TEST_MISE_STATE_DIR"} \
PATH="$ROOT/bin:${OMARCHY_TEST_PATH:-/usr/local/bin:/usr/bin}" \
"$cleanup"
}

run_mise() {
local test_home="$1"
shift

env -i \
HOME="$test_home" \
XDG_CACHE_HOME="$test_home/.cache" \
XDG_CONFIG_HOME="$test_home/.config" \
XDG_DATA_HOME="$test_home/.local/share" \
XDG_STATE_HOME="$test_home/.local/state" \
MISE_PARANOID="${OMARCHY_TEST_MISE_PARANOID:-false}" \
${OMARCHY_TEST_MISE_STATE_DIR:+"MISE_STATE_DIR=$OMARCHY_TEST_MISE_STATE_DIR"} \
PATH="/usr/local/bin:/usr/bin" \
mise "$@"
}

mise_environment() {
local test_home="$1"
local project="$2"

(
cd "$project"
run_mise "$test_home" env -s bash
)
}

mise_path_active() {
local test_home="$1"
local project="$2"
local output

if ! output=$(mise_environment "$test_home" "$project" 2>/dev/null); then
return 1
fi

grep -F "$project/bin" <<<"$output" >/dev/null
}

no_mise_path="$test_dir/usr-bin-without-mise"
mkdir "$no_mise_path"
ln -s "$(realpath /usr/bin)"/* "$no_mise_path/"
rm -f "$no_mise_path/mise"
! PATH="$no_mise_path" command -v mise >/dev/null 2>&1 || fail "the PATH built for a machine without mise still finds one"

removed_home="$test_dir/removed-home"
removed_config="$removed_home/Work/.mise.toml"
removed_project="$removed_home/Work/tries/untrusted-repository"
mkdir -p "$removed_project/bin"
printf '[env]\n_.path = "{{ cwd }}/bin"\n' >"$removed_config"
OMARCHY_TEST_PATH="$no_mise_path" run_cleanup "$removed_home" >/dev/null || fail "cleanup without Mise or a Work trust grant completes"
[[ ! -e $removed_config ]] || fail "cleanup without Mise removes the stock Work config"
pass "without Mise and without a Work trust grant the cleanup completes"

printf '[env]\n_.path = "{{ cwd }}/bin"\n' >"$removed_config"
run_mise "$removed_home" trust "$removed_config" >/dev/null
mise_path_active "$removed_home" "$removed_project" || fail "trusted legacy config prepends the repository bin directory"
OMARCHY_TEST_PATH="$no_mise_path" run_cleanup "$removed_home" >"$test_dir/removed-output" 2>&1 && fail "cleanup without Mise completes while a Work trust grant remains"
grep -F "omarchy pkg add mise-bin" "$test_dir/removed-output" >/dev/null || fail "cleanup without Mise names how to install it" "$(cat "$test_dir/removed-output")"
run_cleanup "$removed_home" >/dev/null
printf '[env]\n_.path = "{{ cwd }}/bin"\n' >"$removed_config"
if mise_path_active "$removed_home" "$removed_project"; then
fail "Work trust grant left without Mise is revoked once Mise is back"
fi
pass "without Mise the cleanup stays pending while a Work trust grant remains"

tilde_home="$test_dir/tilde-home"
tilde_config="$tilde_home/Work/.mise.toml"
mkdir -p "$tilde_home/Work"
printf '[env]\n_.path = "{{ cwd }}/bin"\n' >"$tilde_config"
OMARCHY_TEST_MISE_STATE_DIR='~/mise-state' run_mise "$tilde_home" trust "$tilde_config" >/dev/null
[[ -d $tilde_home/mise-state/trusted-configs ]] || fail "Mise expands a leading ~/ in MISE_STATE_DIR"
if OMARCHY_TEST_MISE_STATE_DIR='~/mise-state' OMARCHY_TEST_PATH="$no_mise_path" run_cleanup "$tilde_home" >"$test_dir/tilde-output" 2>&1; then
fail "cleanup without Mise finds a Work trust grant under a MISE_STATE_DIR that starts with ~/"
fi
grep -F "Mise is needed to revoke its trust" "$test_dir/tilde-output" >/dev/null || fail "cleanup without Mise stops for the Work trust grant under a MISE_STATE_DIR that starts with ~/" "$(cat "$test_dir/tilde-output")"
pass "without Mise the cleanup finds a Work trust grant under a MISE_STATE_DIR that starts with ~/"