Skip to main content

CSharpier Auto-Formatter Hook

handbook-dotnet

Automatically format C# files after Claude creates or edits them using CSharpier.

This PostToolUse hook integrates CSharpier formatting into your Claude Code workflow, ensuring consistent code style without manual intervention.

How It Works​

The hook triggers after Claude uses the Write or Edit tools:

  1. Detects C# files: Only processes files with .cs extension
  2. Runs CSharpier: Executes dotnet csharpier format <file> with optimized flags
  3. Non-blocking: Shows warnings if formatting fails, but doesn't interrupt Claude's workflow

Hook Configuration​

{
"description": "Automatic CSharpier formatting for C# files",
"hooks": {
"PostToolUse": [
{
"matcher": "Write|Edit",
"hooks": [
{
"type": "command",
"command": "bash \"${CLAUDE_PLUGIN_ROOT}/hooks/format-csharp.sh\"",
"timeout": 30
}
]
}
]
}
}

CSharpier Options Used​

  • --skip-validation: Skip validation for better performance
  • --compilation-errors-as-warnings: Format code even with compilation errors

Prerequisites​

CSharpier must be installed on your system:

# Install globally
dotnet tool install -g csharpier

# Verify installation
dotnet csharpier --version

Formatting Fails​

Debug manually:

dotnet csharpier format path/to/file.cs --skip-validation --compilation-errors-as-warnings

Or make sure jq is installed. The hook script requires jq for JSON parsing.

Configuration​

Environment Variables​

CC_HANDBOOK_DOTNET_DISABLE_HOOKS

Disable the automatic CSharpier formatting hook.

# Disable globally in your shell
export CC_HANDBOOK_DOTNET_DISABLE_HOOKS=true

# Or disable for a single Claude Code session
CC_HANDBOOK_DOTNET_DISABLE_HOOKS=true claude

This is useful when:

  • You want to temporarily disable formatting
  • You're working with generated code that shouldn't be formatted
  • You're debugging formatting issues

Custom CSharpier Config​

If you have a .csharpierrc or .csharpierrc.json file in your project, CSharpier will automatically use it.

Performance Considerations​

  • --skip-validation: Skips re-parsing formatted code for significant speed improvements
  • Timeout: 30 seconds (configurable in hooks.json)
  • Parallel execution: Hook runs asynchronously, doesn't block Claude

See Also​