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.

fix(studio): anchor relative PATH entries during setup (#13606)

Sahacha813e3c37145d
diff --git a/studio/setup.sh b/studio/setup.sh
index 7babbf18356..c2b47685dec 100755
--- a/studio/setup.sh
+++ b/studio/setup.sh
@@ -10,6 +10,29 @@ _SETUP_SELF="$SCRIPT_DIR/$(basename -- "${BASH_SOURCE[0]}")"
_SETUP_SELF_SUM=$(cksum < "$_SETUP_SELF" 2>/dev/null || true)
_SETUP_ARGV=("$@")
_SETUP_START_PWD=$PWD
+# Bash caches PATH entries across cd, so anchor relative entries before the first directory change.
+_setup_anchor_path() {
+ local remaining=${PATH-} entry anchored="" last=false
+ while :; do
+ case "$remaining" in
+ *:*) entry=${remaining%%:*}; remaining=${remaining#*:} ;;
+ *) entry=$remaining; last=true ;;
+ esac
+ # Bash expands a leading ~ in PATH itself, so a literal ~/bin is not relative.
+ case "$entry" in
+ /*|\~*) ;;
+ ""|.) entry=$_SETUP_START_PWD ;;
+ *) entry="${_SETUP_START_PWD%/}/${entry#./}" ;;
+ esac
+ anchored="${anchored:+$anchored:}$entry"
+ [ "$last" = true ] && break
+ done
+ PATH=$anchored
+ export PATH
+ hash -r 2>/dev/null || true
+}
+_setup_anchor_path
+unset -f _setup_anchor_path
REPO_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
RULE=$(printf '\342\224\200%.0s' {1..52})
diff --git a/tests/sh/test_setup_relative_path.sh b/tests/sh/test_setup_relative_path.sh
new file mode 100644
index 00000000000..123550466cb
--- /dev/null
+++ b/tests/sh/test_setup_relative_path.sh
@@ -0,0 +1,76 @@
+#!/usr/bin/env bash
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+set -euo pipefail
+
+SCRIPT_DIR=$(CDPATH='' cd -- "$(dirname "$0")" && pwd)
+SETUP_SH="$SCRIPT_DIR/../../studio/setup.sh"
+WORK=$(mktemp -d)
+SYSTEM_RM=$(command -v rm)
+trap '"$SYSTEM_RM" -rf "$WORK"' EXIT
+
+sed -n '/^_setup_anchor_path() {/,/^}/p' "$SETUP_SH" > "$WORK/anchor_path.sh"
+if ! grep -q '^_setup_anchor_path() {' "$WORK/anchor_path.sh"; then
+ echo 'FAIL: setup.sh has no _setup_anchor_path function' >&2
+ exit 1
+fi
+if ! grep -Eq '^_setup_anchor_path([[:space:]]|$)' "$SETUP_SH"; then
+ echo 'FAIL: setup.sh does not call _setup_anchor_path' >&2
+ exit 1
+fi
+
+# shellcheck disable=SC1090,SC1091
+. "$WORK/anchor_path.sh"
+
+mkdir -p "$WORK/start/bin" "$WORK/start/relative/bin" "$WORK/other" "$WORK/home/bin"
+cat > "$WORK/start/bin/rm" <<'EOF'
+#!/bin/sh
+printf '%s\n' 'fake rm'
+EOF
+cat > "$WORK/home/bin/home-tool" <<'EOF'
+#!/bin/sh
+printf '%s\n' 'home tool'
+EOF
+chmod +x "$WORK/start/bin/rm" "$WORK/home/bin/home-tool"
+
+(
+ cd "$WORK/start"
+ _SETUP_START_PWD=$PWD
+ HOME="$WORK/home"
+ # shellcheck disable=SC2147
+ PATH='./bin::relative/bin:~/bin:/usr/bin'
+ hash -p ./bin/rm rm
+ _setup_anchor_path
+
+ expected="$WORK/start/bin:$WORK/start:$WORK/start/relative/bin:~/bin:/usr/bin"
+ if [ "$PATH" != "$expected" ]; then
+ printf 'FAIL: PATH after anchoring\nexpected: %s\nactual: %s\n' "$expected" "$PATH" >&2
+ exit 1
+ fi
+
+ cd "$WORK/other"
+ actual=$(rm)
+ if [ "$actual" != 'fake rm' ]; then
+ printf 'FAIL: rm after cd returned %s\n' "$actual" >&2
+ exit 1
+ fi
+ actual=$(home-tool)
+ if [ "$actual" != 'home tool' ]; then
+ printf 'FAIL: ~/bin tool after cd returned %s\n' "$actual" >&2
+ exit 1
+ fi
+)
+
+(
+ cd /
+ _SETUP_START_PWD=$PWD
+ PATH='./bin:/usr/bin'
+ _setup_anchor_path
+ if [ "$PATH" != '/bin:/usr/bin' ]; then
+ printf 'FAIL: PATH anchored at / is %s\n' "$PATH" >&2
+ exit 1
+ fi
+)
+
+echo 'PASS: relative and empty PATH entries stay anchored after cd'