Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

Stop the installer rolling back a successful install when a shell profile cannot be written (#9269)

Long Yixinga5105f94f70f
diff --git a/install.sh b/install.sh
index 0a95c653240..9b18137d7d7 100755
--- a/install.sh
+++ b/install.sh
@@ -5724,6 +5724,9 @@ else
fi
if [ "$_SETUP_EXIT" -eq 0 ]; then
+ # First: until this runs, anything that fails below reaches the exit trap, which would
+ # restore the previous environment over the one just installed.
+ _commit_studio_venv_replacement
tauri_clear_install_error "studio setup completed"
fi
@@ -5741,7 +5744,17 @@ if [ -d "$_shim_path" ] && [ ! -L "$_shim_path" ]; then
fi
# why: -sfn is atomic and -n prevents descent into a symlink-to-directory at
# the shim path (the directory guard above already rejects a real directory).
-ln -sfn "$VENV_DIR/bin/unsloth" "$_shim_path"
+if ! ln -sfn "$VENV_DIR/bin/unsloth" "$_shim_path" 2>/dev/null; then
+ # A reinstall rebuilds the environment at the same path, so an entry already resolving to
+ # this executable is the shim we were about to write: not a failed install.
+ if [ "$_shim_path" -ef "$VENV_DIR/bin/unsloth" ] 2>/dev/null; then
+ substep "kept the existing shim at $_shim_path ($_LOCAL_BIN is not writable)"
+ else
+ echo "ERROR: could not create the shim at $_shim_path." >&2
+ echo " Make $_LOCAL_BIN writable, or run '$VENV_DIR/bin/unsloth' directly." >&2
+ exit 1
+ fi
+fi
# Is $2 one of the colon-separated entries of $1? Field splitting also globs, so pathname
# expansion is off for the walk and restored afterwards: a directory holding *, ? or [ would
@@ -5777,9 +5790,14 @@ _persist_fish_path_dir() {
# The exact line we would write, not any occurrence of the directory: /opt/uv-old must not
# pass for /opt/uv, and fish reads none of the POSIX files that would otherwise cover it.
if ! grep -v '^[[:space:]]*#' "$_pfp_file" 2>/dev/null | grep -qxF "fish_add_path '$_pfp_quoted'"; then
- echo "# Added by Unsloth installer" >> "$_pfp_file"
- echo "fish_add_path '$_pfp_quoted'" >> "$_pfp_file"
- step "path" "added $_pfp_label to PATH in $_pfp_file"
+ if {
+ echo "# Added by Unsloth installer"
+ echo "fish_add_path '$_pfp_quoted'"
+ } 2>/dev/null >> "$_pfp_file"; then
+ step "path" "added $_pfp_label to PATH in $_pfp_file"
+ else
+ step "path" "could not write $_pfp_file; add $_pfp_label to PATH yourself" "$C_WARN"
+ fi
fi
}
@@ -5823,10 +5841,17 @@ _persist_login_path_dir() {
# shell with no uv at all.
if ! grep -v '^[[:space:]]*#' "$_SHELL_PROFILE" 2>/dev/null \
| grep -E "$_PATH_LINE_RE" | grep -qE "$_plp_pattern"; then
- echo '' >> "$_SHELL_PROFILE"
- echo '# Added by Unsloth installer' >> "$_SHELL_PROFILE"
- echo "export PATH=\"$_plp_literal:\$PATH\"" >> "$_SHELL_PROFILE"
- step "path" "added $_plp_label to PATH in $_SHELL_PROFILE"
+ # One redirect, so an unwritable profile leaves no half-written entry; a warning rather
+ # than a failure, because under set -e an unguarded append would end the install.
+ if {
+ echo ''
+ echo '# Added by Unsloth installer'
+ echo "export PATH=\"$_plp_literal:\$PATH\""
+ } 2>/dev/null >> "$_SHELL_PROFILE"; then
+ step "path" "added $_plp_label to PATH in $_SHELL_PROFILE"
+ else
+ step "path" "could not write $_SHELL_PROFILE; add $_plp_label to PATH yourself" "$C_WARN"
+ fi
fi
}
@@ -5902,8 +5927,6 @@ if [ "$_SETUP_EXIT" -ne 0 ]; then
exit "$_SETUP_EXIT"
fi
-_commit_studio_venv_replacement
-
# ── Tauri mode: done, skip shortcuts and auto-launch ──
if [ "$TAURI_MODE" = true ]; then
tauri_log "DONE" ""
diff --git a/tests/sh/test_install_rollback_lifecycle.sh b/tests/sh/test_install_rollback_lifecycle.sh
index b0e6183fb49..048334d1f79 100644
--- a/tests/sh/test_install_rollback_lifecycle.sh
+++ b/tests/sh/test_install_rollback_lifecycle.sh
@@ -179,6 +179,200 @@ else
bad "stale cleanup mutated a rollback symlink target"
fi
+echo "=== install.sh commits before the post-setup tail ==="
+# The environment is final once studio setup returns, so nothing in the wiring below it may
+# reach the exit trap that restores the previous environment.
+_commit_calls=$(grep -c '^[[:space:]]*_commit_studio_venv_replacement$' "$INSTALL_SH")
+_commit_at=$(grep -n '^[[:space:]]*_commit_studio_venv_replacement$' "$INSTALL_SH" | head -1 | cut -d: -f1)
+_setup_gate_at=$(grep -n '^if \[ "\$_SETUP_EXIT" -eq 0 \]; then$' "$INSTALL_SH" | head -1 | cut -d: -f1)
+# The first thing install.sh mutates outside the venv once setup has returned.
+_shim_at=$(grep -n '^mkdir -p "\$_LOCAL_BIN"$' "$INSTALL_SH" | head -1 | cut -d: -f1)
+if [ "$_commit_calls" -eq 1 ] && [ -n "$_commit_at" ] && [ -n "$_setup_gate_at" ] && [ -n "$_shim_at" ] \
+ && [ "$_setup_gate_at" -lt "$_commit_at" ] && [ "$_commit_at" -lt "$_shim_at" ]; then
+ ok "the replacement is committed inside the setup-succeeded gate, before anything is wired"
+else
+ bad "the replacement is committed too late (gate=$_setup_gate_at commit=$_commit_at shim=$_shim_at calls=$_commit_calls)"
+fi
+# ...and first in that gate: anything added ahead of it is one more command inside the window.
+_gate_first=$(sed -n "$((_setup_gate_at + 1)),\$p" "$INSTALL_SH" \
+ | grep -vE '^[[:space:]]*(#|$)' | head -1 | sed 's/^[[:space:]]*//')
+if [ "$_gate_first" = "_commit_studio_venv_replacement" ]; then
+ ok "nothing runs between studio setup succeeding and the commit"
+else
+ bad "the setup-succeeded gate runs something before the commit ($_gate_first)"
+fi
+
+# install.sh's own tail, gate to gate, so the cases below run its real commit call site. Both
+# anchors are checked: without the closing one sed would run to EOF and carry unrelated code.
+_tail_ends=$(grep -c '^if \[ "\$_SETUP_EXIT" -ne 0 \]; then$' "$INSTALL_SH")
+TAIL_BLOCK=$(sed -n '/^if \[ "\$_SETUP_EXIT" -eq 0 \]; then$/,/^if \[ "\$_SETUP_EXIT" -ne 0 \]; then$/p' "$INSTALL_SH" \
+ | sed '$d')
+if [ "$_tail_ends" -ne 1 ] \
+ || ! printf '%s\n' "$TAIL_BLOCK" | grep -q '^_persist_login_path_dir() {'; then
+ echo " FAIL: could not extract the post-setup tail from install.sh"
+ exit 1
+fi
+
+# The state the tail inherits: a replacement in flight and a new environment on disk.
+write_tail_harness() { # case dir, login shell, "no-exe" to leave the shim's target absent
+ {
+ printf '%s\n' 'set -e'
+ printf '%s\n' 'substep() { printf "%s\n" "$1" >> "$STUDIO_HOME/steps.log"; }'
+ printf '%s\n' 'rollback_substep() { substep "$@"; }'
+ printf '%s\n' 'step() { printf "%s\n" "$2" >> "$STUDIO_HOME/steps.log"; }'
+ printf '%s\n' 'tauri_clear_install_error() { :; }'
+ # The tail ends with this call; writing a launcher is not what these cases are about.
+ printf '%s\n' 'create_studio_shortcuts() { return 0; }'
+ printf '%s\n' 'TAURI_MODE=false'
+ printf '%s\n' 'OS=linux'
+ printf '%s\n' 'C_WARN=""'
+ printf "STUDIO_HOME='%s'\n" "$1"
+ printf "VENV_DIR='%s/unsloth_studio'\n" "$1"
+ printf '%s\n' "$ROLLBACK_BLOCK"
+ printf '%s\n' '_start_studio_venv_replacement "$VENV_DIR"'
+ printf '%s\n' 'mkdir -p "$VENV_DIR/bin"'
+ if [ "${3:-}" != no-exe ]; then
+ printf '%s\n' 'printf "#!/bin/sh\\n" > "$VENV_DIR/bin/unsloth"'
+ printf '%s\n' 'chmod +x "$VENV_DIR/bin/unsloth"'
+ fi
+ printf '%s\n' 'VENV_ABS_BIN="$VENV_DIR/bin"'
+ printf '%s\n' 'printf "new\n" > "$VENV_DIR/generation"'
+ printf '%s\n' '_SETUP_EXIT=0'
+ printf "HOME='%s/home'\n" "$1"
+ printf '%s\n' 'export HOME'
+ printf "SHELL='%s'\n" "$2"
+ printf '%s\n' 'export SHELL'
+ # The tail reads all four from the environment; an exported ZDOTDIR would send the
+ # fixture's write to the developer's own ~/.zshrc.
+ printf '%s\n' 'unset ZDOTDIR ZSH_VERSION UV_NO_MODIFY_PATH UV_UNMANAGED_INSTALL'
+ printf '%s\n' '_LOCAL_BIN="$HOME/.local/bin"'
+ printf '%s\n' '_STUDIO_HOME_REDIRECT=default'
+ printf '%s\n' '_UNSLOTH_LOGIN_PATH="/usr/bin:/bin"'
+ printf '%s\n' '_UNSLOTH_UV_BIN_DIR="$HOME/.local/uvbin"'
+ printf '%s\n' "$TAIL_BLOCK"
+ } > "$1/harness.sh"
+}
+
+# An unwritable profile costs the PATH entry it would have added, not the install.
+run_readonly_profile_case() {
+ _case="$1"
+ _case_dir="$WORK/readonly-$_case"
+ _case_home="$_case_dir/home"
+ mkdir -p "$_case_dir/unsloth_studio" "$_case_home/.local/bin" \
+ "$_case_home/$(dirname "$3")"
+ printf 'old\n' > "$_case_dir/unsloth_studio/generation"
+ printf '# unwritable\n' > "$_case_home/$3"
+ chmod 444 "$_case_home/$3"
+ if true 2>/dev/null >> "$_case_home/$3"; then
+ bad "unwritable $_case profile could not be set up (this user appends to it regardless)"
+ return 0
+ fi
+ write_tail_harness "$_case_dir" "$2"
+
+ set +e
+ sh "$_case_dir/harness.sh" >/dev/null 2>&1
+ _status=$?
+ set -e
+ if [ "$_status" -eq 0 ]; then
+ ok "an unwritable $_case profile does not fail the install"
+ else
+ bad "an unwritable $_case profile failed the install (exit $_status)"
+ fi
+ if [ "$(cat "$_case_home/$3")" = "# unwritable" ]; then
+ ok "an unwritable $_case profile is left as it was"
+ else
+ bad "an unwritable $_case profile was modified"
+ fi
+ if grep -q "could not write $_case_home/$3; add ~/.local/bin" \
+ "$_case_dir/steps.log" 2>/dev/null; then
+ ok "an unwritable $_case profile is reported to the user"
+ else
+ bad "an unwritable $_case profile is silently skipped"
+ fi
+ # One unwritable profile must not cost the other five the uv loop writes.
+ if grep -q '/.local/uvbin' "$_case_home/.profile" 2>/dev/null; then
+ ok "the profiles that can be written still get their PATH entry ($_case)"
+ else
+ bad "one unwritable $_case profile stopped the remaining profiles from being written"
+ fi
+}
+
+run_readonly_profile_case zsh /bin/zsh .zshrc
+run_readonly_profile_case fish /usr/bin/fish .config/fish/conf.d/unsloth.fish
+
+# The tail can still refuse outright: a real directory at the shim path is user data it will not
+# delete. That refusal must cost the shim, not the environment.
+REFUSE_DIR="$WORK/tail-refusal"
+mkdir -p "$REFUSE_DIR/unsloth_studio" "$REFUSE_DIR/home/.local/bin/unsloth"
+printf 'old\n' > "$REFUSE_DIR/unsloth_studio/generation"
+write_tail_harness "$REFUSE_DIR" /bin/bash
+set +e
+sh "$REFUSE_DIR/harness.sh" >/dev/null 2>&1
+_refusal_status=$?
+set -e
+if [ "$_refusal_status" -eq 1 ]; then
+ ok "a directory at the shim path still refuses the install"
+else
+ bad "a directory at the shim path no longer refuses the install (exit $_refusal_status)"
+fi
+if [ "$(cat "$REFUSE_DIR/unsloth_studio/generation" 2>/dev/null)" = "new" ]; then
+ ok "a refused shim keeps the environment just installed"
+else
+ bad "a refused shim rolled back the environment just installed"
+fi
+if ! find "$REFUSE_DIR" -maxdepth 1 -name 'unsloth_studio.rollback.*' -print -quit | grep -q .; then
+ ok "a refused shim leaves no rollback copy"
+else
+ bad "a refused shim left a rollback copy"
+fi
+
+# An unwritable bin directory is only a failed install when what it holds is not this run's shim.
+run_readonly_bin_case() { # name, what the existing entry points at, expected status, [no-exe]
+ _bin_dir="$WORK/readonly-bin-$1"
+ _bin_home="$_bin_dir/home"
+ mkdir -p "$_bin_dir/unsloth_studio" "$_bin_home/.local/bin"
+ printf 'old\n' > "$_bin_dir/unsloth_studio/generation"
+ # The harness writes the executable; the entry already there either resolves to it or not.
+ ln -sfn "$2" "$_bin_home/.local/bin/unsloth"
+ chmod 555 "$_bin_home/.local/bin"
+ if true 2>/dev/null > "$_bin_home/.local/bin/probe"; then
+ rm -f "$_bin_home/.local/bin/probe"
+ chmod 755 "$_bin_home/.local/bin"
+ bad "unwritable bin directory holding $1 could not be set up (this user writes it anyway)"
+ return 0
+ fi
+ write_tail_harness "$_bin_dir" /bin/bash "${4:-}"
+ set +e
+ sh "$_bin_dir/harness.sh" >/dev/null 2>"$_bin_dir/stderr"
+ _bin_status=$?
+ set -e
+ chmod 755 "$_bin_home/.local/bin"
+ if [ "$_bin_status" -eq "$3" ]; then
+ ok "an unwritable bin directory holding $1 exits $3"
+ else
+ bad "an unwritable bin directory holding $1 exits $3 (got $_bin_status)"
+ fi
+ if [ "$3" -eq 0 ]; then
+ if grep -q "kept the existing shim" "$_bin_dir/steps.log" 2>/dev/null; then
+ ok "keeping the existing shim is reported rather than passed over in silence"
+ else
+ bad "keeping the existing shim is not reported"
+ fi
+ elif grep -qF "run '$_bin_dir/unsloth_studio/bin/unsloth' directly" "$_bin_dir/stderr"; then
+ ok "refusing $1 says how to start Unsloth without the shim"
+ else
+ bad "refusing $1 does not say how to start Unsloth without the shim"
+ fi
+}
+
+# Absolute as install.sh writes it, relative as something else might: both resolve to it.
+run_readonly_bin_case absolute-shim "$WORK/readonly-bin-absolute-shim/unsloth_studio/bin/unsloth" 0
+run_readonly_bin_case relative-shim ../../../unsloth_studio/bin/unsloth 0
+# One resolving elsewhere, one naming the exact path install.sh writes but resolving nowhere.
+run_readonly_bin_case another-command /bin/false 1
+run_readonly_bin_case dangling-shim \
+ "$WORK/readonly-bin-dangling-shim/unsloth_studio/bin/unsloth" 1 no-exe
+
echo "=== install.ps1 rollback wiring ==="
if grep -q '^ function Remove-StaleStudioVenvRollbacks {' "$INSTALL_PS1" \
&& grep -q '^ Remove-StaleStudioVenvRollbacks$' "$INSTALL_PS1"; then