fix(install): MAGIC_API_KEY false-negative when repo/.env symlink missing

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) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01UyNYwD4UccVw9ZCFZyJX55
This commit is contained in:
Bastien Chanot 2026-06-23 17:30:09 +02:00
parent 735b62a002
commit 1b028cbc25
2 changed files with 14 additions and 3 deletions

View File

@ -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"

View File

@ -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