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
47 changes: 46 additions & 1 deletion bin/omarchy-mise-install
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,52 @@ case "$command" in
esac

mkdir -p "$HOME/.local/bin"
target="$HOME/.local/bin/$command"

# Every form this script has written, rebuilt from the package the file names
# and the bin being installed, which no caller changes. Anything else is the
# user's own.
is_mise_wrapper() {
local contents named_package named_bin exec_line form
# One argument as this script quotes it: in double quotes, or as %q leaves it.
local token='^("[^"$`\\!]*"|([A-Za-z0-9_@%+=:,./-]|\\.)+)$'

[[ -f $1 ]] && (($(stat -c%s "$1") <= 4096)) || return 1
contents=$(<"$1")

named_package=$(sed -n 's/^mise use -g \(--quiet \)\{0,1\}\(.*\)$/\2/p' <<<"$contents")
named_package=${named_package% || exit 1}
exec_line=$(sed -n 's/^exec \(.*\) "\$@"$/\1/p' <<<"$contents")
named_bin=${exec_line#mise x "$named_package" -- }
named_bin=${named_bin#mise exec "$named_package" -- }
[[ $named_package =~ $token ]] || return 1
[[ $named_bin == "\"$bin\"" || $named_bin == "$(printf '%q' "$bin")" ]] || return 1
Comment on lines +42 to +48

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 Pinned wrappers can be replaced A user can pin a version by changing the package in a generated wrapper to claude@2.0.1 while keeping its format. This check reads the package from that file but compares only the binary name with the install request. It then treats the wrapper as Omarchy-owned and replaces the user's pinned version with the requested package.

Knowledge Base Used: System updates and migrations

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!


for form in \
"export MISE_MINIMUM_RELEASE_AGE=0"$'\n'"mise use -g --quiet $named_package || exit 1"$'\n'"exec mise x $named_package -- $named_bin \"\$@\"" \
"export MISE_MINIMUM_RELEASE_AGE=0"$'\n'"mise use -g $named_package || exit 1"$'\n'"exec mise x $named_package -- $named_bin \"\$@\"" \
"mise use -g $named_package || exit 1"$'\n'"exec mise x $named_package -- $named_bin \"\$@\"" \
"mise use -g $named_package"$'\n'"exec mise exec $named_package -- $named_bin \"\$@\"" \
"mise use -g $named_package"$'\n'"exec $named_bin \"\$@\""; do
[[ $contents == "#!/bin/bash"$'\n'"$form" ]] && return 0
done

return 1
}

# `cat >` would write through a symlink into the real binary behind it (issue
# #6618), so a link is dropped rather than followed. A user's own command at the
# same path keeps precedence, and leaving it is a success, so a migration
# installing this command does not stop every migration after it.
if [[ -L $target ]] || is_mise_wrapper "$target"; then
rm -f "$target"
elif [[ -f $target ]]; then
echo "omarchy-mise-install: leaving $target alone, it is not a mise wrapper" >&2
exit 0

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 Skipped installs report success When a user-owned file occupies the command path, this branch leaves it untouched but exits successfully without installing the requested wrapper. A migration that relies on the exit status can then be marked complete even though the install was skipped. The PR description also says this case should exit nonzero.

Suggested change
exit 0
exit 1

Knowledge Base Used: System updates and migrations

elif [[ -e $target ]]; then
echo "omarchy-mise-install: $target is in the way and is not a file" >&2
exit 1
fi

# The heredoc below is unquoted, so whatever these hold is written into the
# wrapper as shell source. Quote them the way omarchy-install-and-launch does, so
Expand All @@ -48,7 +94,6 @@ esac
# hold a new version back for days after it ships. Exported rather than set on
# the install line alone, so resolving the version to execute agrees with the
# one just installed.
rm -f "$HOME/.local/bin/$command"
cat >"$HOME/.local/bin/$command" <<EOF
#!/bin/bash
export MISE_MINIMUM_RELEASE_AGE=0
Expand Down
65 changes: 65 additions & 0 deletions test/shell.d/mise-install-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,68 @@ fi
fail "an escaping command name removes nothing outside ~/.local/bin"

pass "an escaping command name removes nothing outside ~/.local/bin"

# A wrapper the user wrote at the same path is theirs, whatever it calls. Leaving
# it is a success, or a migration installing that command stops every one after it.
user_wrappers=(
"an unquoted exec" $'#!/bin/sh\nexport ANTHROPIC_BASE_URL=https://my-proxy\nexec /opt/claude/bin/claude "$@"\n'
"a quoted exec" $'#!/bin/bash\nexport ANTHROPIC_BASE_URL=https://my-proxy\nexec "/opt/claude/bin/claude" "$@"\n'
"a pinned mise x" $'#!/bin/bash\nexport ANTHROPIC_BASE_URL=https://my-proxy\nexec mise x claude@2.0.1 -- claude "$@"\n'
"an extra argument" $'#!/bin/bash\nmise use -g "claude" || exit 1\nexec mise x "claude" -- "claude" --model opus "$@"\n'
"an extra command" $'#!/bin/bash\nmise use -g "claude"; export ANTHROPIC_BASE_URL=https://my-proxy\nexec mise x "claude" -- "claude" "$@"\n'
"a redirect" $'#!/bin/bash\nexport MISE_MINIMUM_RELEASE_AGE=0\nmise use -g --quiet claude || exit 1\nexec mise x claude -- claude</dev/null "$@"\n'
"a binary of its own" $'#!/bin/bash\nmise use -g "claude" || exit 1\nexec mise x "claude" -- "/opt/claude/bin/claude" "$@"\n'
)

for (( i = 0; i < ${#user_wrappers[@]}; i += 2 )); do
label=${user_wrappers[i]}
printf '%s' "${user_wrappers[i + 1]}" >"$home/.local/bin/claude"

install_wrapper claude >/dev/null 2>"$tmpdir/err" ||
fail "a user's wrapper with $label is left without failing" "$(cat "$tmpdir/err")"
[[ $(cat "$home/.local/bin/claude") == "$(printf '%s' "${user_wrappers[i + 1]}")" ]] ||
fail "a user's wrapper with $label is left as it was" "$(cat "$home/.local/bin/claude")"
done

pass "a user's own wrapper is left alone"

# Every form a previous install or migration wrote is still regenerated.
generated_wrappers=(
"the current form" "$(cat "$home/.local/bin/playwright")"
"the cooldown form" $'#!/bin/bash\nexport MISE_MINIMUM_RELEASE_AGE=0\nmise use -g "npm:playwright" || exit 1\nexec mise x "npm:playwright" -- "playwright" "$@"'
"the bail-on-failure form" $'#!/bin/bash\nmise use -g "npm:playwright" || exit 1\nexec mise x "npm:playwright" -- "playwright" "$@"'
"the mise exec form" $'#!/bin/bash\nmise use -g "npm:playwright"\nexec mise exec "npm:playwright" -- "playwright" "$@"'
"the bare exec form" $'#!/bin/bash\nmise use -g "npm:playwright"\nexec "playwright" "$@"'
)

for (( i = 0; i < ${#generated_wrappers[@]}; i += 2 )); do
label=${generated_wrappers[i]}
printf '%s' "${generated_wrappers[i + 1]}" >"$home/.local/bin/playwright"

install_wrapper npm:playwright playwright >/dev/null
grep -Fqx 'mise use -g --quiet "npm:playwright" || exit 1' "$home/.local/bin/playwright" ||
fail "a wrapper in $label is regenerated" "$(cat "$home/.local/bin/playwright")"
done

install_wrapper 'npm:pkg$(touch '"$tmpdir"'/PWNED)end' hostile >/dev/null 2>"$tmpdir/err"
[[ ! -s $tmpdir/err ]] ||
fail "a wrapper with %q-quoted arguments is regenerated" "$(cat "$tmpdir/err")"

pass "a wrapper in any generated form is regenerated"

# A link is dropped, never written through into the binary it points at.
printf 'real binary\n' >"$tmpdir/real-binary"
ln -sfn "$tmpdir/real-binary" "$home/.local/bin/linked"
install_wrapper somepkg linked >/dev/null
[[ ! -L $home/.local/bin/linked && $(cat "$tmpdir/real-binary") == "real binary" ]] ||
fail "a symlink is replaced without touching its target"

pass "a symlink is replaced without touching its target"

# A directory in the way leaves no command at all, so that is still a failure.
mkdir -p "$home/.local/bin/blocked"
if install_wrapper somepkg blocked >/dev/null 2>&1; then
fail "a directory in the way is reported as a failure"
fi

pass "a directory in the way is reported as a failure"