Skip to content

[BUG] CLAUDE_ENV_FILE not sourced after /clear creates new session #14433

@priyanshujain

Description

@priyanshujain

Preflight Checklist

  • I have searched existing issues and this hasn't been reported yet
  • This is a single bug report (please file separate reports for different bugs)
  • I am using the latest version of Claude Code

What's Wrong?

When /clear is executed, a new session is created and the SessionStart hook runs correctly, writing to the new $CLAUDE_ENV_FILE. However, subsequent Bash tool calls
do not have access to the environment variables from this file - they still use the environment from the original session startup.

The docs state: "Claude Code will source this file before each Bash command, making the environment persistent across all commands."

  • Hook runs correctly (verified via debug logs)
  • New env file is created at new path: ~/.claude/session-env/{new-session-id}/hook-0.sh
  • But Bash tool calls still use environment from original session startup
  • New env file is never sourced

What Should Happen?

After /clear, $CLAUDE_SESSION_ID should reflect the new session ID (e.g., bbb-222) since:

  • SessionStart hook runs with source: "clear"
  • Hook writes new session ID to the new $CLAUDE_ENV_FILE
  • Docs say env file is sourced before each Bash command

Error Messages/Logs

Steps to Reproduce

  1. Create a SessionStart hook that captures the session ID:
// hooks/hooks.json
{
  "hooks": {
    "SessionStart": [
      {
        "hooks": [
          {
            "type": "command",
            "command": "echo \"export CLAUDE_SESSION_ID='$(echo | jq -Rr 'fromjson? | .session_id // empty' <<< \"$(cat)\")''\" >> \"$CLAUDE_ENV_FILE\""
          }
        ]
      }
    ]
  }
}
  1. Start a fresh Claude Code session
  2. Run: echo $CLAUDE_SESSION_ID → outputs correct session ID (e.g., aaa-111)
  3. Run /clear command
  4. Run: echo $CLAUDE_SESSION_ID → still outputs old session ID aaa-111

Claude Model

Opus

Is this a regression?

I don't know

Last Working Version

No response

Claude Code Version

2.0.72

Platform

Anthropic API

Operating System

macOS

Terminal/Shell

Other

Additional Information

I am using latest version of Ghostty terminal.

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
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions