Skip to content

CLAUDE_ENV_FILE not provided to SessionStart hooks #15840

Closed as not planned
Closed as not planned

Description

@excessivereason

Environment

  • Claude Code version: 2.0.76
  • OS: macOS (Darwin 25.1.0)
  • Shell: bash

Description

According to the hooks documentation, SessionStart hooks should receive CLAUDE_ENV_FILE - a path to a file where export statements can be written to persist environment variables to subsequent Bash tool calls.

However, CLAUDE_ENV_FILE is an empty string when the hook runs, so the documented mechanism doesn't work.

Steps to Reproduce

  1. Create a SessionStart hook in ~/.claude/hooks/session-start.sh:
#!/bin/bash

# Try to use CLAUDE_ENV_FILE as documented
if [ -n "$CLAUDE_ENV_FILE" ]; then
    echo "export MY_SESSION_VAR=\"test\"" >> "$CLAUDE_ENV_FILE"
fi

# Debug: log the actual value
echo "CLAUDE_ENV_FILE='$CLAUDE_ENV_FILE'" >> /tmp/claude-hook-debug.log
  1. Register it in ~/.claude/settings.json:
{
  "hooks": {
    "SessionStart": [
      {
        "matcher": "*",
        "hooks": [
          {
            "type": "command",
            "command": "~/.claude/hooks/session-start.sh"
          }
        ]
      }
    ]
  }
}
  1. Start a new Claude Code session

  2. Run a Bash command to check environment:

echo "MY_SESSION_VAR: $MY_SESSION_VAR"

Expected Behavior

  • CLAUDE_ENV_FILE should contain a path to a file
  • Variables written to that file should be available in subsequent Bash commands

Actual Behavior

  • CLAUDE_ENV_FILE is an empty string (exists but has no value)
  • The [ -n "$CLAUDE_ENV_FILE" ] check fails
  • No environment variables are persisted

Additional Context

From within a Bash tool call in the session:

$ env | grep -i claude
CLAUDE_CODE_ENTRYPOINT=cli
CLAUDECODE=1

CLAUDE_ENV_FILE is not set or is empty - not provided with a path as documented.

The hook itself runs correctly (other parts of the hook execute fine, like creating files), confirming the issue is specifically with CLAUDE_ENV_FILE not being populated.

Activity

  1. excessivereason commented on Dec 31, 2025

    @excessivereason
    Author

    @claude You can start work on this. thanks!

  2. piercelamb commented on Jan 28, 2026

    @piercelamb

    Bumping this thread, would also like to see this fixed

  3. Oloremo commented on Jan 29, 2026

    @Oloremo

    Really needed to extend oTel metrics.

  4. hb-man commented on Feb 11, 2026

    @hb-man

    still a problem :c

  5. ryan-dyer-sp commented on Feb 18, 2026

    @ryan-dyer-sp

    Doesnt seem to be an issue with Claude CLI version 2.1.45

  6. acushner-rippling commented on Mar 11, 2026

    @acushner-rippling

    /clear causes CLAUDE_ENV_FILE to point to the wrong session

    Version: 2.1.70 (macOS)

    Finding

    CLAUDE_ENV_FILE works correctly on fresh startup but after /clear, it still points to the previous session's env file path. The hook receives the correct new session_id in stdin JSON, but writes it to the old session's env file — which the new session never sources.

    Evidence

    Added debug logging to a SessionStart hook:

    # Fresh launch — CLAUDE_ENV_FILE matches session_id ✓
    session_id=35a91082-... CLAUDE_ENV_FILE='~/.claude/session-env/35a91082-.../sessionstart-hook-0.sh'
    
    # Another fresh launch — correct ✓
    session_id=bdc8ef9e-... CLAUDE_ENV_FILE='~/.claude/session-env/bdc8ef9e-.../sessionstart-hook-0.sh'
    
    # /clear on that session — CLAUDE_ENV_FILE still points to OLD session ✗
    session_id=ce5fa79d-... CLAUDE_ENV_FILE='~/.claude/session-env/bdc8ef9e-.../sessionstart-hook-0.sh'
    

    Impact

    Any SessionStart hook that writes to CLAUDE_ENV_FILE (e.g., exporting CLAUDE_SESSION_ID) will write the correct value to the wrong file. Subsequent Bash tool calls retain the stale env from the pre-/clear session.

    Repro

    1. Configure a SessionStart hook that logs $CLAUDE_ENV_FILE and stdin session_id
    2. Launch claude fresh — observe matching paths
    3. Run /clear — observe CLAUDE_ENV_FILE path still contains the old session ID

    This is consistent with #14433 (closed as stale). The SessionStart event fires correctly after /clear, but the env file path is not regenerated for the new session.

  7. github-actions commented on Apr 9, 2026

    @github-actions

    Closing for now — inactive for too long. Please open a new issue if this is still relevant.

  8. github-actions commented on Apr 17, 2026

    @github-actions

    This issue has been automatically locked since it was closed and has not had any activity for 7 days. If you're experiencing a similar issue, please file a new issue and reference this one if it's relevant.

  9. locked as resolved and limited conversation to collaborators on Apr 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:corearea:toolsbugSomething isn't workinghas reproHas detailed reproduction stepsplatform:macosIssue specifically occurs on macOSstaleIssue is inactive

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions