Skip to content

Commit 5ef887e

Browse files
saravmajesticclaude
andcommitted
fix: the workspace-rules gate finds a link keyed by a symlinked path, and never matches an empty key
- `linked` tries the physical path, then the path as the host reached it (a symlink, `/tmp`), still bounded by the repository root. - An empty key (a `cygpath` that prints nothing) no longer matches an empty string in the cache. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent a0b0581 commit 5ef887e

2 files changed

Lines changed: 35 additions & 5 deletions

File tree

‎plugins/altimate-code/hooks/altimate-code-workspace-rules.sh‎

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,20 +47,32 @@ ROOT="$(git rev-parse --show-toplevel 2>/dev/null || pwd)"
4747
# repository root. This mirrors the CLI, which looks there and never below the directory it runs in. Keys are native
4848
# paths written as JSON strings, so each folder is converted (`cygpath -w` on Windows), JSON-escaped, then escaped for
4949
# the regular expression. It is matched with both quotes and the colon after them: `/x/app` never matches `/x/app-old`,
50-
# and a folder that appears only as a value (a row's `projectPath`) is not a key.
50+
# and a folder that appears only as a value (a row's `projectPath`) is not a key. The physical path is tried first, then
51+
# the path as the host reached it (through a symlink, or `/tmp` for `/private/tmp`): an older CLI wrote that form, and
52+
# rewrites it only when it reads the cache itself.
5153
linked() {
5254
local cache="${XDG_STATE_HOME:-$HOME/.local/state}/altimate-code/altimate-workspace-bindings.json"
53-
local top dir key
55+
local top
5456
[ -f "$cache" ] || return 1
5557
top="$(cd "$ROOT" 2>/dev/null && pwd -P)"
56-
dir="$(pwd -P)"
57-
[ -n "$top" ] && [ -n "$dir" ] || return 1
58+
[ -n "$top" ] || return 1
59+
linked_from "$cache" "$top" "$(pwd -P)" && return 0
60+
[ -n "${PWD:-}" ] && [ "$PWD" != "$(pwd -P)" ] && linked_from "$cache" "$top" "$PWD"
61+
}
62+
63+
# linked_from CACHE TOP DIR: the walk up from DIR, ending at the folder whose physical path is TOP.
64+
linked_from() {
65+
local cache="$1" top="$2" dir="$3" key
66+
[ -n "$dir" ] || return 1
5867
while :; do
5968
key="$dir"
60-
command -v cygpath >/dev/null 2>&1 && key="$(cygpath -w "$dir")"
69+
command -v cygpath >/dev/null 2>&1 && key="$(cygpath -w "$dir" 2>/dev/null)"
70+
# An empty key would match any empty string in the cache.
71+
[ -n "$key" ] || return 1
6172
key="$(printf '%s' "$key" | sed -e 's/\\/\\\\/g' -e 's/"/\\"/g' -e 's/[][\.*^$+?(){}|]/\\&/g')"
6273
grep -qE "\"$key\"[[:space:]]*:" "$cache" 2>/dev/null && return 0
6374
case "$dir" in "$top"|/) return 1 ;; esac
75+
[ "$(cd "$dir" 2>/dev/null && pwd -P)" = "$top" ] && return 1
6476
dir="$(dirname "$dir")"
6577
done
6678
}

‎tests/workspace-rules-hook.sh‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,24 @@ reset
145145
[ "$(hook "$WORK/project" session-start "$WORK/winbin:$BIN:$SYS")" = "0|" ] && ! started \
146146
|| fail "session start at the repository root ran the CLI with only a child folder linked (Windows paths)"
147147

148+
# A `cygpath` that prints nothing gives no key, which must not match an empty string in the cache.
149+
printf '#!/bin/sh\nexit 1\n' > "$WORK/winbin/cygpath"
150+
python3 -c 'import json; print(json.dumps({"bindings": {"": {"datamateId": 1}}}))' > "$CACHE"
151+
reset
152+
[ "$(hook "$WORK/project" session-start "$WORK/winbin:$BIN:$SYS")" = "0|" ] && ! started \
153+
|| fail "an empty key from cygpath matched an empty string in the link cache"
154+
# A folder reached through a symlink, linked by that path (as an older CLI wrote it): found by the path the host used.
155+
ln -s "$WORK/project" "$WORK/via-link"
156+
link "$WORK/via-link/dbt"
157+
reset
158+
[ "$(hook "$WORK/via-link/dbt/models" session-start "$BIN:$SYS")" = "$JSON" ] \
159+
|| fail "a link keyed by the symlinked path was not found"
160+
# The walk by that path still ends at the repository root.
161+
link "$WORK"
162+
reset
163+
[ "$(hook "$WORK/via-link/dbt" session-start "$BIN:$SYS")" = "0|" ] && ! started \
164+
|| fail "the walk by the symlinked path went above the repository root"
165+
148166
link "$WORK/project"
149167
[ "$(hook "$WORK/project" session-start "$BIN:$SYS" help)" = "0|" ] || fail "a CLI without \`learn hook\` leaks its output"
150168
[ "$(hook "$WORK/project" session-start "$BIN:$SYS" fail)" = "0|" ] || fail "a failing CLI fails the hook or leaks its error"

0 commit comments

Comments
 (0)