Pack Installation Failing After #260 Fix - Environment Variable Sourcing Incomplete
Issue: Pack Installation Still Failing - #260 Fix Incomplete
Summary
Following up on Daniel's fix for issue #260 (commit 197f082, Dec 29): The environment variable sourcing and pack installation still fails despite the attempted fix. The .env file is created correctly, but Claude Code doesn't read the variables during pack installation, causing pack installation to fail with validation errors.
Environment
- Platform: WSL2 Ubuntu 22.04 LTS
- Node Runtime: Bun
- Claude Code: Latest Pro
- PAI Version: Latest (as of Dec 30, 20:58 UTC)
- Commits: Based on current HEAD including 197f082 fix
What Daniel Fixed (Dec 29 - Commit 197f082)
Issue #260 addressed environment variable sourcing between wizard and packs:
// Create .env file for packs to read
const envFileContent = `DA="${config.daName}" ...`
await Bun.write(`${paiDir}/.env`, envFileContent);
// Export to current process for immediate use
process.env.DA = config.daName;
process.env.TIME_ZONE = config.timeZone;
The fix assumed: Variables would be available to Claude Code during pack installation.
The Problem Still Exists
Fresh installation (PAI4) completed successfully with .env file properly created:
✓ Created /home/elidev/.claude/.env
File contents verified:
DA="Pai4"
TIME_ZONE="America/New_York"
ELEVENLABS_API_KEY="[set]"
ELEVENLABS_VOICE_ID="[set]"
However, when Claude Code attempts to install kai-hook-system.md, it checks environment variables and finds them unset:
Environment variables (checked during pack installation):
DA: Pai # Expected: Pai4 (missing the "4")
TIME_ZONE: 'NOT SET' # Expected: America/New_York
PAI_SOURCE_APP: 'NOT SET'
Root Cause Analysis
The .env file is created correctly in ~/.claude/.env, but:
- It's not being read by Claude Code — The file exists but Claude Code doesn't load it into its environment
- Process export in install.ts only affects the installer process — It doesn't propagate to Claude Code's session
- No mechanism loads .env into Claude Code — The fix creates the file but not the sourcing mechanism
The Gap in #260's Fix
The fix created .env but assumed it would be:
- Automatically sourced into Claude Code's environment
- Available during pack installation
Neither assumption holds true. Claude Code needs explicit sourcing of .env.
Attempted Pack Installation Failure
When user provides kai-hook-system.md to Claude Code with instruction "Install this pack into my system":
● Bash(echo "Environment variables:" && echo " DA: ${DA:-'NOT SET'}" ...)
⎿ Environment variables:
DA: Pai # Missing "4"
TIME_ZONE: 'NOT SET' # Should be America/New_York
PAI_SOURCE_APP: 'NOT SET'
● Write(~/.claude/hooks/update-tab-titles.ts)
⎿ User rejected write to ../.claude/hooks/update-tab-titles.ts
The write rejection happens because:
- Claude Code can't find required environment variables
- Validation/pre-commit hook rejects incomplete configuration
- Pack installation halts before any hooks are created
Installation Flow Issues
The complete picture:
- ✅ Kai wizard creates
.envfile correctly - ❌ Wizard doesn't source
.envinto Claude Code's runtime - ✅
.envfile exists with correct values - ❌ Claude Code can't access those values during session
- ❌ Pack installation fails due to missing environment
- ❌ Chicken-and-egg: Can't install hooks that would handle this sourcing
What Needs to Happen
Option 1: Source .env in Wizard
After creating .env, the wizard should ensure Claude Code loads it:
# In Bundles/Kai/install.ts
await Bun.write(`${paiDir}/.env`, envFileContent);
// Also update .bashrc to source .env at session start
const bashrcEntry = `[ -f "$HOME/.claude/.env" ] && export $(cat "$HOME/.claude/.env" | xargs)`;
Option 2: Have kai-core-install Bootstrap Environment
The kai-core-install.md pack could handle environment setup as part of installation.
Option 3: Create settings.json During Wizard
Instead of relying on .env sourcing, create settings.json with configuration that Claude Code reads directly.
Test Reproduction
# Fresh WSL2 Ubuntu
git clone https://github.com/danielmiessler/PAI.git PAI-test
cd PAI-test/Bundles/Kai
bun run install.ts
# Respond: Eli, Pai4, America/New_York, No voice
# Verify .env exists
cat ~/.claude/.env
# Should show: DA="Pai4", TIME_ZONE="America/New_York"
# Now try to install pack in Claude Code
# cat ~/PAI-test/Packs/kai-hook-system.md
# [Provide to Claude Code with: "Install this pack into my system"]
# Observe: Environment variables report as unset despite .env file existing
Related Issues
- #260 - Original environment variable sourcing issue (attempted fix)
- #259 - Pack installation clarity
- #261 - Pack manifest documentation
Impact
Severity: HIGH Blocker: Yes — Cannot proceed with pack installation after base setup Affected: All users following Quick Start instructions for fresh installations Workaround: None that maintains proper configuration
Questions
- Is
.envsupposed to be sourced automatically into Claude Code's session? - Should the wizard add sourcing logic to
.bashrcor shell profile? - Should
settings.jsonbe created during wizard with configuration, rather than relying on environment variable sourcing? - Is there a hook that should run early in session initialization to load
.env?
Additional Context
This is post-fix testing on the same codebase Daniel committed to 2 days ago. The issue persists despite the attempted fix, suggesting the sourcing mechanism isn't complete or something else is preventing environment variable propagation to Claude Code.
Testing done on WSL2 provides a clean environment for validation.
Files for Reference
- Installation successful:
.envcreated at~/.claude/.envwith correct values - Pack installation attempted:
kai-hook-system.mdfromPacks/ - Failure point: Claude Code cannot read environment variables despite file existing
Fix Implemented ✅
Thank you for the thorough analysis! You correctly identified the root cause: .env and shell profile updates don't propagate to Claude Code's running session.
The Fix
The installer now creates ~/.claude/settings.json with environment variables using Claude Code's native env var support:
{
"env": {
"DA": "YourAI",
"TIME_ZONE": "Your/Timezone",
"PAI_DIR": "~/.claude",
"PAI_SOURCE_APP": "YourAI"
}
}
Changes Made
- Added settings.json generation - Claude Code reads env vars from this file immediately, no shell sourcing needed
- Merge with existing settings - If settings.json already exists (e.g., with hooks), the env vars are merged in
- Fixed .env quote formatting - Removed quotes from values (
.envformat standard)
Why This Works
Claude Code's settings.json supports an env object that sets environment variables for the session. Unlike shell profile sourcing, these are available immediately when Claude Code starts.
For Existing Installations
If you've already run the installer, you can manually add to your ~/.claude/settings.json:
{
"env": {
"DA": "Pai4",
"TIME_ZONE": "America/New_York",
"PAI_DIR": "/home/elidev/.claude",
"PAI_SOURCE_APP": "Pai4"
}
}
Then restart Claude Code.
Thank you for the excellent debugging and proposed solutions - you were spot-on about needing settings.json!