Loading...
Automatically runs go mod tidy when Go files or go.mod are modified to keep dependencies clean
{
"hookConfig": {
"hooks": {
"postToolUse": {
"script": "./.claude/hooks/go-module-tidy.sh",
"matchers": [
"write",
"edit"
]
}
}
},
"scriptContent": "#!/usr/bin/env bash\n\n# Read the tool input from stdin\nINPUT=$(cat)\nTOOL_NAME=$(echo \"$INPUT\" | jq -r '.tool_name')\nFILE_PATH=$(echo \"$INPUT\" | jq -r '.tool_input.file_path // .tool_input.path // \"\"')\n\nif [ -z \"$FILE_PATH\" ]; then\n exit 0\nfi\n\n# Check if this is a Go-related file\nif [[ \"$FILE_PATH\" == *.go ]] || [[ \"$FILE_PATH\" == *go.mod ]] || [[ \"$FILE_PATH\" == *go.sum ]] || [[ \"$FILE_PATH\" == *go.work* ]]; then\n echo \"๐ง Go Module Maintenance for: $(basename \"$FILE_PATH\")\" >&2\n \n # Find the Go module root\n MODULE_DIR=\"$(dirname \"$FILE_PATH\")\"\n \n # Walk up the directory tree to find go.mod\n while [ \"$MODULE_DIR\" != \"/\" ] && [ ! -f \"$MODULE_DIR/go.mod\" ]; do\n MODULE_DIR=\"$(dirname \"$MODULE_DIR\")\"\n done\n \n if [ ! -f \"$MODULE_DIR/go.mod\" ]; then\n echo \"โ ๏ธ No go.mod found - not a Go module\" >&2\n exit 0\n fi\n \n echo \"๐ Go module root: $MODULE_DIR\" >&2\n cd \"$MODULE_DIR\"\n \n # Check if Go is installed\n if ! command -v go &> /dev/null; then\n echo \"โ Go is not installed or not in PATH\" >&2\n exit 1\n fi\n \n GO_VERSION=$(go version | cut -d' ' -f3 2>/dev/null || echo \"unknown\")\n echo \"๐น Go version: $GO_VERSION\" >&2\n \n # Initialize maintenance counters\n ERRORS=0\n WARNINGS=0\n FIXED=0\n \n # Function to report issues\n report_issue() {\n local level=\"$1\"\n local message=\"$2\"\n \n case \"$level\" in\n \"ERROR\")\n echo \"โ ERROR: $message\" >&2\n ERRORS=$((ERRORS + 1))\n ;;\n \"WARNING\")\n echo \"โ ๏ธ WARNING: $message\" >&2\n WARNINGS=$((WARNINGS + 1))\n ;;\n \"FIXED\")\n echo \"โ
FIXED: $message\" >&2\n FIXED=$((FIXED + 1))\n ;;\n \"INFO\")\n echo \"โน๏ธ INFO: $message\" >&2\n ;;\n esac\n }\n \n # 1. Pre-tidy Module Analysis\n echo \"๐ Analyzing module state...\" >&2\n \n # Check go.mod syntax\n if ! go mod edit -json > /dev/null 2>&1; then\n report_issue \"ERROR\" \"go.mod has syntax errors\"\n exit 1\n else\n echo \" โ
go.mod syntax is valid\" >&2\n fi\n \n # Get current dependencies before tidy\n DEPS_BEFORE=$(go list -m all 2>/dev/null | wc -l | xargs || echo \"0\")\n echo \" ๐ฆ Dependencies before tidy: $DEPS_BEFORE\" >&2\n \n # Check for any build errors\n if go list ./... > /dev/null 2>&1; then\n echo \" โ
Module builds successfully\" >&2\n else\n report_issue \"WARNING\" \"Module has build issues that may affect dependency resolution\"\n fi\n \n # 2. Run go mod tidy\n echo \"๐งน Running go mod tidy...\" >&2\n \n if go mod tidy; then\n report_issue \"FIXED\" \"go mod tidy completed successfully\"\n \n # Check dependencies after tidy\n DEPS_AFTER=$(go list -m all 2>/dev/null | wc -l | xargs || echo \"0\")\n DEPS_CHANGE=$((DEPS_AFTER - DEPS_BEFORE))\n \n if [ \"$DEPS_CHANGE\" -gt 0 ]; then\n echo \" ๐ Added $DEPS_CHANGE dependencies\" >&2\n elif [ \"$DEPS_CHANGE\" -lt 0 ]; then\n echo \" ๐ Removed $((DEPS_CHANGE * -1)) dependencies\" >&2\n else\n echo \" ๐ฆ No dependency changes\" >&2\n fi\n \n else\n report_issue \"ERROR\" \"go mod tidy failed\"\n fi\n \n # 3. Verify go.sum integrity\n echo \"๐ Verifying module checksums...\" >&2\n \n if go mod verify; then\n echo \" โ
All module checksums verified\" >&2\n else\n report_issue \"ERROR\" \"Module checksum verification failed\"\n fi\n \n # 4. Check for vulnerabilities (if govulncheck is available)\n if command -v govulncheck &> /dev/null; then\n echo \"๐ก๏ธ Scanning for vulnerabilities...\" >&2\n \n if govulncheck ./... 2>/dev/null; then\n echo \" โ
No known vulnerabilities found\" >&2\n else\n report_issue \"WARNING\" \"Potential vulnerabilities detected - run 'govulncheck ./...' for details\"\n fi\n else\n echo \" ๐ก Install govulncheck for vulnerability scanning: go install golang.org/x/vuln/cmd/govulncheck@latest\" >&2\n fi\n \n # 5. Run go vet for Go source files\n if [[ \"$FILE_PATH\" == *.go ]]; then\n echo \"๐ Running go vet...\" >&2\n \n if go vet ./...; then\n echo \" โ
go vet passed - no issues found\" >&2\n else\n report_issue \"WARNING\" \"go vet found potential issues\"\n fi\n \n # Check for common Go issues\n echo \"๐ Additional Go code analysis...\" >&2\n \n # Check for gofmt issues\n UNFORMATTED=$(find . -name '*.go' -not -path './vendor/*' -exec gofmt -l {} \\; 2>/dev/null)\n if [ -n \"$UNFORMATTED\" ]; then\n report_issue \"WARNING\" \"Some files are not gofmt formatted\"\n echo \"$UNFORMATTED\" | head -5 | while read file; do\n echo \" $file\" >&2\n done\n else\n echo \" โ
All Go files are properly formatted\" >&2\n fi\n \n # Check imports with goimports if available\n if command -v goimports &> /dev/null; then\n IMPORT_ISSUES=$(find . -name '*.go' -not -path './vendor/*' -exec goimports -l {} \\; 2>/dev/null)\n if [ -n \"$IMPORT_ISSUES\" ]; then\n report_issue \"WARNING\" \"Some files have import formatting issues\"\n else\n echo \" โ
All imports are properly formatted\" >&2\n fi\n fi\n fi\n \n # 6. Module cleanup suggestions\n echo \"๐งน Module optimization check...\" >&2\n \n # Check for indirect dependencies that could be direct\n INDIRECT_COUNT=$(go list -m all | grep -c '// indirect' || echo \"0\")\n if [ \"$INDIRECT_COUNT\" -gt 0 ]; then\n echo \" ๐ Indirect dependencies: $INDIRECT_COUNT\" >&2\n echo \" ๐ก Review if any indirect deps should be direct\" >&2\n fi\n \n # Check for replace directives\n REPLACE_COUNT=$(grep -c '^replace ' go.mod 2>/dev/null || echo \"0\")\n if [ \"$REPLACE_COUNT\" -gt 0 ]; then\n echo \" ๐ Replace directives: $REPLACE_COUNT\" >&2\n echo \" ๐ก Review replace directives for production readiness\" >&2\n fi\n \n # 7. Workspace support\n if [ -f \"go.work\" ]; then\n echo \"๐ข Go workspace detected\" >&2\n \n if go work sync; then\n echo \" โ
Workspace synced successfully\" >&2\n else\n report_issue \"WARNING\" \"Workspace sync issues detected\"\n fi\n fi\n \n # 8. Module cache suggestions\n if [ \"$DEPS_AFTER\" -gt 50 ]; then\n echo \"๐ก Large dependency count - consider 'go clean -modcache' if disk space is low\" >&2\n fi\n \n # 9. Generate Summary Report\n echo \"\" >&2\n echo \"๐ Go Module Maintenance Summary:\" >&2\n echo \"================================\" >&2\n echo \" ๐ Module: $(basename \"$(pwd)\")\" >&2\n echo \" ๐น Go: $GO_VERSION\" >&2\n echo \" ๐ฆ Dependencies: $DEPS_AFTER\" >&2\n echo \" โ
Fixed: $FIXED\" >&2\n echo \" โ ๏ธ Warnings: $WARNINGS\" >&2\n echo \" โ Errors: $ERRORS\" >&2\n \n if [ \"$ERRORS\" -eq 0 ] && [ \"$WARNINGS\" -eq 0 ]; then\n echo \" ๐ Status: EXCELLENT - Module is clean and optimized\" >&2\n elif [ \"$ERRORS\" -eq 0 ]; then\n echo \" โ
Status: GOOD - Minor warnings to review\" >&2\n else\n echo \" โ Status: NEEDS ATTENTION - Errors require fixing\" >&2\n fi\n \n echo \"\" >&2\n echo \"๐ก Go Module Best Practices:\" >&2\n echo \" โข Run 'go mod tidy' regularly to keep dependencies clean\" >&2\n echo \" โข Use 'go mod why <module>' to understand dependency reasons\" >&2\n echo \" โข Update dependencies with 'go get -u ./...' carefully\" >&2\n echo \" โข Consider using 'go mod graph' for dependency visualization\" >&2\n echo \" โข Pin important dependencies to specific versions\" >&2\n \n # Exit with error if there are critical issues\n if [ \"$ERRORS\" -gt 0 ]; then\n echo \"โ ๏ธ Go module maintenance completed with errors\" >&2\n exit 1\n fi\n \nelse\n # Not a Go file, exit silently\n exit 0\nfi\n\nexit 0"
}.claude/hooks/~/.claude/hooks/{
"hooks": {
"postToolUse": {
"script": "./.claude/hooks/go-module-tidy.sh",
"matchers": [
"write",
"edit"
]
}
}
}#!/usr/bin/env bash
# Read the tool input from stdin
INPUT=$(cat)
TOOL_NAME=$(echo "$INPUT" | jq -r '.tool_name')
FILE_PATH=$(echo "$INPUT" | jq -r '.tool_input.file_path // .tool_input.path // ""')
if [ -z "$FILE_PATH" ]; then
exit 0
fi
# Check if this is a Go-related file
if [[ "$FILE_PATH" == *.go ]] || [[ "$FILE_PATH" == *go.mod ]] || [[ "$FILE_PATH" == *go.sum ]] || [[ "$FILE_PATH" == *go.work* ]]; then
echo "๐ง Go Module Maintenance for: $(basename "$FILE_PATH")" >&2
# Find the Go module root
MODULE_DIR="$(dirname "$FILE_PATH")"
# Walk up the directory tree to find go.mod
while [ "$MODULE_DIR" != "/" ] && [ ! -f "$MODULE_DIR/go.mod" ]; do
MODULE_DIR="$(dirname "$MODULE_DIR")"
done
if [ ! -f "$MODULE_DIR/go.mod" ]; then
echo "โ ๏ธ No go.mod found - not a Go module" >&2
exit 0
fi
echo "๐ Go module root: $MODULE_DIR" >&2
cd "$MODULE_DIR"
# Check if Go is installed
if ! command -v go &> /dev/null; then
echo "โ Go is not installed or not in PATH" >&2
exit 1
fi
GO_VERSION=$(go version | cut -d' ' -f3 2>/dev/null || echo "unknown")
echo "๐น Go version: $GO_VERSION" >&2
# Initialize maintenance counters
ERRORS=0
WARNINGS=0
FIXED=0
# Function to report issues
report_issue() {
local level="$1"
local message="$2"
case "$level" in
"ERROR")
echo "โ ERROR: $message" >&2
ERRORS=$((ERRORS + 1))
;;
"WARNING")
echo "โ ๏ธ WARNING: $message" >&2
WARNINGS=$((WARNINGS + 1))
;;
"FIXED")
echo "โ
FIXED: $message" >&2
FIXED=$((FIXED + 1))
;;
"INFO")
echo "โน๏ธ INFO: $message" >&2
;;
esac
}
# 1. Pre-tidy Module Analysis
echo "๐ Analyzing module state..." >&2
# Check go.mod syntax
if ! go mod edit -json > /dev/null 2>&1; then
report_issue "ERROR" "go.mod has syntax errors"
exit 1
else
echo " โ
go.mod syntax is valid" >&2
fi
# Get current dependencies before tidy
DEPS_BEFORE=$(go list -m all 2>/dev/null | wc -l | xargs || echo "0")
echo " ๐ฆ Dependencies before tidy: $DEPS_BEFORE" >&2
# Check for any build errors
if go list ./... > /dev/null 2>&1; then
echo " โ
Module builds successfully" >&2
else
report_issue "WARNING" "Module has build issues that may affect dependency resolution"
fi
# 2. Run go mod tidy
echo "๐งน Running go mod tidy..." >&2
if go mod tidy; then
report_issue "FIXED" "go mod tidy completed successfully"
# Check dependencies after tidy
DEPS_AFTER=$(go list -m all 2>/dev/null | wc -l | xargs || echo "0")
DEPS_CHANGE=$((DEPS_AFTER - DEPS_BEFORE))
if [ "$DEPS_CHANGE" -gt 0 ]; then
echo " ๐ Added $DEPS_CHANGE dependencies" >&2
elif [ "$DEPS_CHANGE" -lt 0 ]; then
echo " ๐ Removed $((DEPS_CHANGE * -1)) dependencies" >&2
else
echo " ๐ฆ No dependency changes" >&2
fi
else
report_issue "ERROR" "go mod tidy failed"
fi
# 3. Verify go.sum integrity
echo "๐ Verifying module checksums..." >&2
if go mod verify; then
echo " โ
All module checksums verified" >&2
else
report_issue "ERROR" "Module checksum verification failed"
fi
# 4. Check for vulnerabilities (if govulncheck is available)
if command -v govulncheck &> /dev/null; then
echo "๐ก๏ธ Scanning for vulnerabilities..." >&2
if govulncheck ./... 2>/dev/null; then
echo " โ
No known vulnerabilities found" >&2
else
report_issue "WARNING" "Potential vulnerabilities detected - run 'govulncheck ./...' for details"
fi
else
echo " ๐ก Install govulncheck for vulnerability scanning: go install golang.org/x/vuln/cmd/govulncheck@latest" >&2
fi
# 5. Run go vet for Go source files
if [[ "$FILE_PATH" == *.go ]]; then
echo "๐ Running go vet..." >&2
if go vet ./...; then
echo " โ
go vet passed - no issues found" >&2
else
report_issue "WARNING" "go vet found potential issues"
fi
# Check for common Go issues
echo "๐ Additional Go code analysis..." >&2
# Check for gofmt issues
UNFORMATTED=$(find . -name '*.go' -not -path './vendor/*' -exec gofmt -l {} \; 2>/dev/null)
if [ -n "$UNFORMATTED" ]; then
report_issue "WARNING" "Some files are not gofmt formatted"
echo "$UNFORMATTED" | head -5 | while read file; do
echo " $file" >&2
done
else
echo " โ
All Go files are properly formatted" >&2
fi
# Check imports with goimports if available
if command -v goimports &> /dev/null; then
IMPORT_ISSUES=$(find . -name '*.go' -not -path './vendor/*' -exec goimports -l {} \; 2>/dev/null)
if [ -n "$IMPORT_ISSUES" ]; then
report_issue "WARNING" "Some files have import formatting issues"
else
echo " โ
All imports are properly formatted" >&2
fi
fi
fi
# 6. Module cleanup suggestions
echo "๐งน Module optimization check..." >&2
# Check for indirect dependencies that could be direct
INDIRECT_COUNT=$(go list -m all | grep -c '// indirect' || echo "0")
if [ "$INDIRECT_COUNT" -gt 0 ]; then
echo " ๐ Indirect dependencies: $INDIRECT_COUNT" >&2
echo " ๐ก Review if any indirect deps should be direct" >&2
fi
# Check for replace directives
REPLACE_COUNT=$(grep -c '^replace ' go.mod 2>/dev/null || echo "0")
if [ "$REPLACE_COUNT" -gt 0 ]; then
echo " ๐ Replace directives: $REPLACE_COUNT" >&2
echo " ๐ก Review replace directives for production readiness" >&2
fi
# 7. Workspace support
if [ -f "go.work" ]; then
echo "๐ข Go workspace detected" >&2
if go work sync; then
echo " โ
Workspace synced successfully" >&2
else
report_issue "WARNING" "Workspace sync issues detected"
fi
fi
# 8. Module cache suggestions
if [ "$DEPS_AFTER" -gt 50 ]; then
echo "๐ก Large dependency count - consider 'go clean -modcache' if disk space is low" >&2
fi
# 9. Generate Summary Report
echo "" >&2
echo "๐ Go Module Maintenance Summary:" >&2
echo "================================" >&2
echo " ๐ Module: $(basename "$(pwd)")" >&2
echo " ๐น Go: $GO_VERSION" >&2
echo " ๐ฆ Dependencies: $DEPS_AFTER" >&2
echo " โ
Fixed: $FIXED" >&2
echo " โ ๏ธ Warnings: $WARNINGS" >&2
echo " โ Errors: $ERRORS" >&2
if [ "$ERRORS" -eq 0 ] && [ "$WARNINGS" -eq 0 ]; then
echo " ๐ Status: EXCELLENT - Module is clean and optimized" >&2
elif [ "$ERRORS" -eq 0 ]; then
echo " โ
Status: GOOD - Minor warnings to review" >&2
else
echo " โ Status: NEEDS ATTENTION - Errors require fixing" >&2
fi
echo "" >&2
echo "๐ก Go Module Best Practices:" >&2
echo " โข Run 'go mod tidy' regularly to keep dependencies clean" >&2
echo " โข Use 'go mod why <module>' to understand dependency reasons" >&2
echo " โข Update dependencies with 'go get -u ./...' carefully" >&2
echo " โข Consider using 'go mod graph' for dependency visualization" >&2
echo " โข Pin important dependencies to specific versions" >&2
# Exit with error if there are critical issues
if [ "$ERRORS" -gt 0 ]; then
echo "โ ๏ธ Go module maintenance completed with errors" >&2
exit 1
fi
else
# Not a Go file, exit silently
exit 0
fi
exit 0Hook fails to find go.mod in nested module subdirectories
Script walks up directories but may hit root before finding go.mod. Ensure MODULE_DIR search starts from FILE_PATH directory: cd $(dirname "$FILE_PATH") before the while loop to guarantee proper traversal.
Go mod tidy hangs when network unavailable for dependency downloads
Add timeout to go commands: timeout 30s go mod tidy to prevent infinite hangs. Set GOPROXY=off to use only local cache, or configure module cache directory with GOMODCACHE for offline operation.
Workspace sync errors when go.work references missing modules
Script runs 'go work sync' without validation. Add existence checks: go work edit -json | jq -r '.Use[].DiskPath' | while read dir; do [ -d "$dir" ] || echo "Missing: $dir"; done before syncing.
PostToolUse timing causes stale go.sum checksums on rapid changes
Hook runs after each write but go.sum updates may lag. Add explicit go.sum validation: go mod verify before tidy: if verification fails, run go mod tidy -v to refresh checksums and rebuild module graph.
Context lost when cd changes directory breaking relative path access
Script changes to MODULE_DIR but hook execution happens per-file. Store original: ORIG_DIR=$(pwd) and restore after: cd "$ORIG_DIR" or use absolute paths: FILE_ABS=$(realpath "$FILE_PATH") throughout script.
Loading reviews...
Join our community of Claude power users. No spam, unsubscribe anytime.
Automated accessibility testing and compliance checking for web applications following WCAG guidelines
Automatically generates or updates API documentation when endpoint files are modified
Automatically formats code files after Claude writes or edits them using Prettier, Black, or other formatters