Created
December 6, 2025 08:07
-
-
Save cruftyoldsysadmin/e8b86ed24b0305a027e9cfeabcc10af6 to your computer and use it in GitHub Desktop.
SessionStartHook to set persistent variables for getting around Claude Code's inability to remember its project work dir.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "hooks": { | |
| "SessionStart": [ | |
| { | |
| "hooks": [ | |
| { | |
| "type": "command", | |
| "command": "${CLAUDE_PLUGIN_ROOT}/scripts/workdir-persistence.sh", | |
| "timeout": 10 | |
| } | |
| ] | |
| } | |
| ] | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # workdir-persistence.sh | |
| # Persists PROJECT_WORK_DIR and additional directories as environment variables | |
| # Called by SessionStart hook | |
| # | |
| # Exports: | |
| # PROJECT_WORK_DIR - Main project directory ($CLAUDE_PROJECT_DIR) | |
| # ADDITIONAL_DIR_1, ADDITIONAL_DIR_2, etc. - From additionalDirectories in settings.local.json | |
| # SHARED_DIR_100, SHARED_DIR_101, etc. - From additionalDirectories in settings.json | |
| if [ -z "$CLAUDE_ENV_FILE" ]; then | |
| exit 0 # Not in a Claude session context | |
| fi | |
| # Always set PROJECT_WORK_DIR to the main project directory | |
| echo "export PROJECT_WORK_DIR=\"$CLAUDE_PROJECT_DIR\"" >> "$CLAUDE_ENV_FILE" | |
| # Parse additionalDirectories from .claude/settings.local.json | |
| # Export them as ADDITIONAL_DIR_1, ADDITIONAL_DIR_2, etc. | |
| SETTINGS_FILE="${CLAUDE_PROJECT_DIR}/.claude/settings.local.json" | |
| if [ -f "$SETTINGS_FILE" ]; then | |
| python3 -c " | |
| import json | |
| try: | |
| with open('$SETTINGS_FILE') as f: | |
| data = json.load(f) | |
| dirs = data.get('permissions', {}).get('additionalDirectories', []) | |
| for i, d in enumerate(dirs, 1): | |
| print(f'export ADDITIONAL_DIR_{i}=\"{d}\"') | |
| except: | |
| pass | |
| " >> "$CLAUDE_ENV_FILE" | |
| fi | |
| # Also check shared settings.json | |
| SHARED_SETTINGS="${CLAUDE_PROJECT_DIR}/.claude/settings.json" | |
| if [ -f "$SHARED_SETTINGS" ]; then | |
| python3 -c " | |
| import json | |
| try: | |
| with open('$SHARED_SETTINGS') as f: | |
| data = json.load(f) | |
| dirs = data.get('permissions', {}).get('additionalDirectories', []) | |
| # Start numbering from 100 to avoid conflicts with local settings | |
| for i, d in enumerate(dirs, 100): | |
| print(f'export SHARED_DIR_{i}=\"{d}\"') | |
| except: | |
| pass | |
| " >> "$CLAUDE_ENV_FILE" | |
| fi | |
| # Output context message (shown to Claude via SessionStart additionalContext) | |
| echo "Environment variables persisted for session:" | |
| echo " PROJECT_WORK_DIR=$CLAUDE_PROJECT_DIR" | |
| exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment