Skip to content

Commit 6b35e30

Browse files
mohsin-wiserclaude
andcommitted
fix: use terraform local_file for .claude.json creation
This commit replaces the bash provisioner approach with Terraform's local_file resource to create .claude.json. This ensures the file exists during Terraform's planning phase, before Docker evaluates mount points. Changes: - Added local_file resource for .claude.json creation - Made local_file depend on host_directories provisioner - Removed .claude.json creation from bash provisioner - Added local_file to docker container's depends_on Why this fixes the issue: - local_file creates the file during Terraform plan/apply - Docker evaluates mounts AFTER Terraform resources are created - No race condition between provisioner and Docker mount evaluation - File is guaranteed to exist before Docker tries to mount it 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
1 parent a57feda commit 6b35e30

1 file changed

Lines changed: 18 additions & 19 deletions

File tree

.coder/template.tf

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,20 @@ provider "docker" {
4545
host = "unix:///var/run/docker.sock"
4646
}
4747

48+
# Create .claude.json file using Terraform (ensures it exists before Docker mounts)
49+
resource "local_file" "claude_config" {
50+
filename = "/home/coder/.coder-mount/${data.coder_workspace_owner.me.name}/claude/.claude.json"
51+
content = "{}"
52+
53+
# Only create if doesn't exist, don't overwrite user's config
54+
lifecycle {
55+
ignore_changes = [content]
56+
}
57+
58+
# Ensure parent directory exists first
59+
depends_on = [null_resource.host_directories]
60+
}
61+
4862
# Create host directories for bind mounts before container starts
4963
resource "null_resource" "host_directories" {
5064
# Re-run when workspace is rebuilt
@@ -71,24 +85,8 @@ resource "null_resource" "host_directories" {
7185
mkdir -p "$BASE_DIR/bash_history"
7286
mkdir -p "$BASE_DIR/gitconfig"
7387
74-
# Create .claude.json as a file (not directory) - CRITICAL FIX
75-
# Docker creates missing mount paths as directories, so we must ensure this exists as a file first
76-
if [ -d "$BASE_DIR/claude/.claude.json" ]; then
77-
rm -rf "$BASE_DIR/claude/.claude.json"
78-
echo "⚠️ Removed .claude.json directory (was incorrectly created as directory)"
79-
fi
80-
# Always ensure it's a file, not a directory
81-
if [ ! -f "$BASE_DIR/claude/.claude.json" ]; then
82-
touch "$BASE_DIR/claude/.claude.json"
83-
echo '{}' > "$BASE_DIR/claude/.claude.json"
84-
chmod 644 "$BASE_DIR/claude/.claude.json"
85-
echo "✅ Created .claude.json as file with proper permissions"
86-
fi
87-
# Verify it's a file (safety check)
88-
if [ ! -f "$BASE_DIR/claude/.claude.json" ]; then
89-
echo "❌ ERROR: .claude.json could not be created as file!"
90-
exit 1
91-
fi
88+
# NOTE: .claude.json is now created by Terraform local_file resource
89+
# This ensures it exists before Docker tries to mount it
9290
9391
# Create .gemini/config.json if it doesn't exist
9492
if [ ! -f "$BASE_DIR/gemini/.gemini/config.json" ]; then
@@ -524,7 +522,8 @@ resource "docker_container" "workspace" {
524522
depends_on = [
525523
docker_container.postgres,
526524
docker_container.redis,
527-
null_resource.host_directories
525+
null_resource.host_directories,
526+
local_file.claude_config # Ensure .claude.json exists before mounting
528527
]
529528

530529
# Auto-restart on failure

0 commit comments

Comments
 (0)