Skip to content

A complete AI agency at your fingertips - From frontend wizards to Reddit community ninjas, from whimsy injectors to reality checkers. Each agent is a specialized expert with personality, processes, and proven deliverables.

Shell

fix(install): preserve user files and foreign links in link mode

Rudy Celekli3711d8729ad6
diff --git a/.github/workflows/test-install.yml b/.github/workflows/test-install.yml
index 749b6ebe..4646a0f0 100644
--- a/.github/workflows/test-install.yml
+++ b/.github/workflows/test-install.yml
@@ -29,3 +29,4 @@ jobs:
chmod +x scripts/test-install.sh scripts/install.sh
./scripts/test-install.sh
bash scripts/test-install-openclaw-registration.sh
+ bash scripts/test-install-link-ownership.sh
diff --git a/scripts/install.sh b/scripts/install.sh
index f8ccb7fa..10903d29 100755
--- a/scripts/install.sh
+++ b/scripts/install.sh
@@ -283,26 +283,30 @@ OVERRIDE_PATH="" # --path (single-destination override)
# install_file <src> <dest> — copy, or symlink when --link is set.
install_file() {
+ local target="$2"
+ # Directory destinations have a trailing slash. Do not follow a leaf
+ # symlink to a directory when deciding which file belongs to the installer.
+ if [[ "$target" == */ ]] || { ! $USE_LINK && [[ -d "$target" ]]; }; then
+ target="${target%/}/$(basename "$1")"
+ fi
+ if [[ -L "$target" ]]; then
+ local link_to; link_to="$(readlink "$target")"
+ if [[ "$link_to" == "$REPO_ROOT/"* ]]; then
+ # An installer-owned link may be refreshed or switched to a copy.
+ rm -f -- "$target"
+ else
+ warn "Skipped $target — it is a symlink to $link_to; not overwriting it."
+ [[ -n "${SKIPPED_LOG:-}" ]] && printf '%s -> %s\n' "$target" "$link_to" >> "$SKIPPED_LOG"
+ return 0
+ fi
+ elif $USE_LINK && [[ -e "$target" ]]; then
+ warn "Skipped $target — it already exists; not replacing it with a symlink."
+ [[ -n "${SKIPPED_LOG:-}" ]] && printf '%s (existing file)\n' "$target" >> "$SKIPPED_LOG"
+ return 0
+ fi
if $USE_LINK; then
- ln -sf "$1" "$2"
+ ln -s "$1" "$target"
else
- local target="$2"
- [[ -d "$target" ]] && target="${target%/}/$(basename "$1")"
- if [[ -L "$target" ]]; then
- # cp would follow the link and overwrite whatever it points at.
- local link_to; link_to="$(readlink "$target")"
- if [[ "$link_to" == "$REPO_ROOT/"* ]]; then
- # Our own --link install: switching to a copy is the intended change.
- rm -f -- "$target"
- else
- # Someone else's link: leave it and its target alone, keep installing
- # the rest, and say so in the summary (one stray link must not abort
- # the install halfway through the roster).
- warn "Skipped $target — it is a symlink to $link_to; not overwriting it."
- [[ -n "${SKIPPED_LOG:-}" ]] && printf '%s -> %s\n' "$target" "$link_to" >> "$SKIPPED_LOG"
- return 0
- fi
- fi
cp "$1" "$2"
fi
}
@@ -1708,9 +1712,9 @@ main() {
box_bot
printf "\n"
if [[ -s "$SKIPPED_LOG" ]]; then
- warn "Not installed: $(wc -l < "$SKIPPED_LOG" | tr -d ' ') file(s) whose destination is a symlink to somewhere else:"
+ warn "Not installed: $(wc -l < "$SKIPPED_LOG" | tr -d ' ') file(s) whose destination is an existing user file or foreign symlink:"
sed 's/^/ /' "$SKIPPED_LOG" >&2
- warn "Remove or replace those links, then re-run to install them."
+ warn "Move or remove those destinations, then re-run to install them."
fi
dim " Run ./scripts/convert.sh to regenerate after adding or editing agents."
printf "\n"
diff --git a/scripts/test-install-link-ownership.sh b/scripts/test-install-link-ownership.sh
new file mode 100755
index 00000000..0135e458
--- /dev/null
+++ b/scripts/test-install-link-ownership.sh
@@ -0,0 +1,50 @@
+#!/usr/bin/env bash
+set -euo pipefail
+SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
+scratch="$(mktemp -d)"
+trap 'rm -rf "$scratch"' EXIT
+mkdir -p "$scratch/repo/scripts" "$scratch/repo/engineering" "$scratch/repo/integrations" "$scratch/home"
+cp "$SCRIPT_DIR/install.sh" "$SCRIPT_DIR/lib.sh" "$scratch/repo/scripts/"
+cp "$SCRIPT_DIR/../divisions.json" "$scratch/repo/"
+export HOME="$scratch/home"
+for agent in first second; do
+ printf '%s\n' '---' "name: $agent" 'description: Example agent' 'color: blue' '---' '# Example agent' > "$scratch/repo/engineering/$agent.md"
+done
+run_install() {
+ bash "$scratch/repo/scripts/install.sh" --tool claude-code --division engineering \
+ --no-convert --link --path "$scratch/dest" > "$scratch/result.log" 2>&1
+}
+mkdir -p "$scratch/dest"
+printf 'user content\n' > "$scratch/dest/first.md"
+run_install
+[[ ! -L "$scratch/dest/first.md" ]] && grep -qx 'user content' "$scratch/dest/first.md" || {
+ echo 'FAIL: --link replaced a user-owned regular file' >&2; exit 1;
+}
+[[ -L "$scratch/dest/second.md" ]]
+grep -q 'Not installed: 1 file' "$scratch/result.log"
+for kind in foreign dangling directory; do
+ rm -f "$scratch/dest/first.md" "$scratch/dest/second.md"
+ target="$scratch/$kind"
+ [[ "$kind" == foreign ]] && printf 'outside content\n' > "$target"
+ [[ "$kind" == directory ]] && mkdir -p "$target"
+ ln -s "$target" "$scratch/dest/first.md"
+ run_install
+ [[ "$(readlink "$scratch/dest/first.md")" == "$target" ]]
+ [[ -L "$scratch/dest/second.md" ]]
+ grep -q 'Not installed: 1 file' "$scratch/result.log"
+ [[ "$kind" != foreign ]] || grep -qx 'outside content' "$target"
+ [[ "$kind" != dangling ]] || [[ ! -e "$target" ]]
+ [[ "$kind" != directory ]] || [[ ! -e "$target/first.md" ]]
+done
+rm -f "$scratch/dest/first.md"
+ln -s "$scratch/repo/engineering/old.md" "$scratch/dest/first.md"
+run_install
+[[ "$(readlink "$scratch/dest/first.md")" == "$scratch/repo/engineering/first.md" ]]
+run_install
+[[ "$(readlink "$scratch/dest/first.md")" == "$scratch/repo/engineering/first.md" ]]
+# A config directory itself may be a user's dotfiles link; protect leaf files.
+mv "$scratch/dest" "$scratch/dotfiles"
+ln -s "$scratch/dotfiles" "$scratch/dest"
+run_install
+[[ -L "$scratch/dest" && -L "$scratch/dest/second.md" ]]
+echo 'PASS: --link skips user files and foreign links, replaces its own links, and continues'