PostToolUse hooks not triggering for Edit operations on .cs files

Resolved 💬 4 comments Opened Aug 7, 2025 by wangkanai Closed Aug 7, 2025

PostToolUse hooks not triggering for Edit operations on .cs files

Summary

PostToolUse hooks configured in .claude/settings.json are not executing after Edit, MultiEdit, or Write operations on C# files, despite having a properly configured matcher pattern and verbose mode enabled.

Environment

  • Claude Code Version: 1.0.70
  • Operating System: macOS 15.6 (24G84)
  • Kernel: Darwin 24.6.0
  • User Setup: claude --dangerously-skip-permissions (aliased)
  • System Integrity Protection: Disabled

Configuration

.claude/settings.json

{
	"enableAllProjectMcpServers": true,
	"verbose": true,
	"hooks": {
		"PostToolUse": [
			{
				"matcher": "Edit|MultiEdit|Write:.*\\.cs$",
				"name": "Format and Build C# Files",
				"description": "Formats the code and builds the project after editing C# files.",
				"type": "command",
				"command": "dotnet format -v normal && dotnet build --no-restore",
				"timeout": 180
			}
		]
	}
}

Problem Description

The hook should trigger after any Edit, MultiEdit, or Write operation on files matching the pattern .*\\.cs$, but it completely fails to execute.

Reproduction Steps

  1. Setup: Create .claude/settings.json with the configuration above
  2. Create test file: Create any .cs file (tested both at project root and within src/ directories)
  3. Edit operation: Use the Edit tool to modify the .cs file
  4. Expected behavior: Hook should execute dotnet format -v normal && dotnet build --no-restore
  5. Actual behavior: No hook execution occurs, no output or error messages

Tested Files

  • /Users/wangkanai/Sources/sathai/test-hook-csharp.cs (project root)
  • /Users/wangkanai/Sources/sathai/src/Web/Controllers/HomeController.cs (within project structure)

Edit Operations Performed

# Test 1: Root level file
Edit tool on test-hook-csharp.cs
Result: Hook did not trigger

# Test 2: Project structure file  
Edit tool on src/Web/Controllers/HomeController.cs
Result: Hook did not trigger

Configuration Analysis

  • Matcher Pattern: Edit|MultiEdit|Write:.*\\.cs$ - Should match any .cs file after Edit/MultiEdit/Write operations
  • Verbose Mode: Enabled ("verbose": true) - Should show hook execution details
  • Timeout: 180 seconds - Sufficient for dotnet operations
  • Command: dotnet format -v normal && dotnet build --no-restore - Valid dotnet commands

Expected Behavior

With verbose mode enabled, we should see:

  1. Hook matching confirmation when editing .cs files
  2. Command execution output from dotnet format -v normal
  3. Command execution output from dotnet build --no-restore
  4. Success/failure status of hook execution

Actual Behavior

  • No output indicating hook matching
  • No command execution
  • No errors or warnings about hook failures
  • Silent failure with no indication of why hooks aren't triggering

Debugging Attempts

  1. Pattern Verification: The regex .*\\.cs$ should match all .cs files
  2. File Path Testing: Tested files at different directory levels
  3. Tool Verification: Confirmed Edit tool successfully modifies files
  4. Configuration Syntax: JSON syntax is valid and follows documented format

Potential Issues

  1. Pattern Matching: The matcher pattern may not be working as expected
  2. Tool Name Recognition: The Edit|MultiEdit|Write: prefix may not be matching correctly
  3. File Path Resolution: Hook system may not be resolving file paths properly
  4. Working Directory: Commands may need specific working directory context
  5. Hook Execution Environment: There may be an issue with the hook execution environment

Additional Context

This is part of a comprehensive hook validation system for a .NET 9.0 project with multiple file types (C#, CSHTML, Razor, SCSS, PowerShell, JSON). The C# hook configuration follows the same pattern as documented examples but fails to execute.

Request

Please investigate why PostToolUse hooks are not triggering for Edit operations on .cs files despite apparently correct configuration. Verbose mode should provide debugging output but currently shows nothing related to hook execution attempts.

View original on GitHub ↗

This issue has 4 comments on GitHub. Read the full discussion on GitHub ↗