From 1b028cbc25c54c8846b17f793330932ceefd08f7 Mon Sep 17 00:00:00 2001 From: Bastien Chanot Date: Tue, 23 Jun 2026 17:30:09 +0200 Subject: [PATCH] fix(install): MAGIC_API_KEY false-negative when repo/.env symlink missing MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The magic check + link_env grep'd `^MAGIC_API_KEY=` on $REPO/.env, but on a fresh machine ~/.claude/.env is often created AFTER link.sh runs, so the repo/.env symlink (which toggle-external.sh sources) is never made — the key looks absent though it's set, and the warning misleadingly points at ~/.claude/.env. - install-plugins.sh: self-heal — if ~/.claude/.env exists but repo/.env is missing, create the symlink before checking. Accurate message. - Both: tolerate optional `export ` + leading whitespace and require a non-empty value (regex sanity-tested), so common .env formats match. Immediate fix for an affected machine: `make link`. Co-Authored-By: Claude Opus 4.8 (1M context) Claude-Session: https://claude.ai/code/session_01UyNYwD4UccVw9ZCFZyJX55 --- install-plugins.sh | 15 +++++++++++++-- link.sh | 2 +- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/install-plugins.sh b/install-plugins.sh index 693e727..fefa89a 100644 --- a/install-plugins.sh +++ b/install-plugins.sh @@ -707,8 +707,19 @@ if [ -x "$REPO/lib/toggle-external.sh" ]; then else ok "magic MCP disabled (default)" fi - if [ ! -f "$REPO/.env" ] || ! grep -q '^MAGIC_API_KEY=' "$REPO/.env" 2>/dev/null; then - warn "MAGIC_API_KEY not found in ~/.claude/.env — copy .env.example there and set your key before enabling" + # The key lives in ~/.claude/.env (canonical, BDR-026), reached via the + # repo/.env symlink that toggle-external.sh sources. Self-heal the common + # fresh-machine case: ~/.claude/.env was created AFTER link.sh ran, so the + # symlink is missing and the key looks absent though it's set. + HOME_ENV="$HOME/.claude/.env" + if [ ! -e "$REPO/.env" ] && [ -f "$HOME_ENV" ]; then + ln -sf "$HOME_ENV" "$REPO/.env" 2>/dev/null \ + && info "Linked repo/.env → ~/.claude/.env (was missing)" + fi + # Tolerate optional `export ` and leading whitespace; require a value. + MAGIC_KEY_RE='^[[:space:]]*(export[[:space:]]+)?MAGIC_API_KEY=.' + if [ ! -f "$REPO/.env" ] || ! grep -qE "$MAGIC_KEY_RE" "$REPO/.env" 2>/dev/null; then + warn "MAGIC_API_KEY not set in ~/.claude/.env — add it (and run 'make link') before enabling magic" fi else warn "lib/toggle-external.sh not found or not executable — skipping" diff --git a/link.sh b/link.sh index 0e9e035..8ae222a 100644 --- a/link.sh +++ b/link.sh @@ -117,7 +117,7 @@ link_env() { echo " cp \"$REPO/.env.example\" \"$home_env\" && \"\${EDITOR:-nano}\" \"$home_env\"" return fi - grep -q '^MAGIC_API_KEY=' "$home_env" 2>/dev/null \ + grep -qE '^[[:space:]]*(export[[:space:]]+)?MAGIC_API_KEY=.' "$home_env" 2>/dev/null \ || echo "⚠️ $home_env has no MAGIC_API_KEY line — magic won't enable until added." if [ -L "$repo_env" ]; then [ "$(readlink "$repo_env")" = "$home_env" ] && return