|
| 1 | +# Pre-commit Hook Investigation Report |
| 2 | + |
| 3 | +## Executive Summary |
| 4 | + |
| 5 | +This report documents our investigation into issues with the pre-commit hooks and commit message validation in the monorepo project. We identified several configuration problems with Husky, lint-staged, and commitlint that were preventing proper execution of hooks and validation of commit messages. These issues have been resolved, and recommendations for ongoing maintenance are provided. |
| 6 | + |
| 7 | +## Project Configuration Overview |
| 8 | + |
| 9 | +The project is configured as a monorepo using: |
| 10 | +- **Package Manager**: pnpm (v10.2.1) |
| 11 | +- **Git Hooks**: Husky (v9.1.7) |
| 12 | +- **Linting on Staged Files**: lint-staged (v15.4.3) |
| 13 | +- **Commit Message Validation**: commitlint (v19.7.1) |
| 14 | +- **Release Management**: semantic-release (v24.2.3) |
| 15 | + |
| 16 | +## Issues Identified |
| 17 | + |
| 18 | +### 1. Pre-commit Hook Issues |
| 19 | + |
| 20 | +- **Problem**: The pre-commit hook was not running correctly on git commit operations. |
| 21 | +- **Root Causes**: |
| 22 | + - Husky installation was not properly initialized |
| 23 | + - The hook script was missing the proper shebang line (`#!/usr/bin/env sh`) |
| 24 | + - Execute permissions were not correctly set on hook files |
| 25 | + - The hook was attempting to run commands that were failing silently |
| 26 | + |
| 27 | +### 2. Commit Message Validation Issues |
| 28 | + |
| 29 | +- **Problem**: Commit message validation via commitlint was not enforcing conventional commit standards. |
| 30 | +- **Root Causes**: |
| 31 | + - The commit-msg hook was missing the proper shebang line |
| 32 | + - commitlint configuration was not properly set up or was not being correctly referenced |
| 33 | + - The npx command in the hook was not properly configured |
| 34 | + |
| 35 | +### 3. Configuration Inconsistencies |
| 36 | + |
| 37 | +- **Problem**: Inconsistencies between package.json scripts, Husky configuration, and lint-staged setup. |
| 38 | +- **Root Causes**: |
| 39 | + - Missing or incorrect Husky prepare script in package.json |
| 40 | + - Incorrect configuration of lint-staged in package.json |
| 41 | + - Incomplete commitlint setup |
| 42 | + |
| 43 | +## Fixes Implemented |
| 44 | + |
| 45 | +### 1. Pre-commit Hook Fixes |
| 46 | + |
| 47 | +- Added proper shebang line to the pre-commit hook |
| 48 | +- Ensured execute permissions were set correctly: `chmod +x .husky/pre-commit` |
| 49 | +- Updated the hook to use the correct command syntax for pnpm |
| 50 | +- Improved error handling and output messages for better debugging |
| 51 | +- Structured the hook to run lint-staged first, then build and test checks |
| 52 | + |
| 53 | +### 2. Commit Message Validation Fixes |
| 54 | + |
| 55 | +- Added proper shebang line to the commit-msg hook |
| 56 | +- Updated the commitlint command to use `pnpm exec` instead of `npx` for consistency with the project's package manager |
| 57 | +- Ensured proper execute permissions were set: `chmod +x .husky/commit-msg` |
| 58 | +- Verified the commitlint configuration was correctly set up and extended from the conventional configuration |
| 59 | + |
| 60 | +### 3. Package Manager Consistency |
| 61 | + |
| 62 | +- Updated scripts in package.json to use `pnpm exec` instead of `npx` for better consistency |
| 63 | +- This ensures all commands use the project's package manager and avoid potential version conflicts |
| 64 | + |
| 65 | +### 3. Configuration Alignment |
| 66 | + |
| 67 | +- Updated package.json to include the correct Husky prepare script |
| 68 | +- Ensured lint-staged configuration matched the project's linting requirements |
| 69 | +- Verified that all dependencies were correctly installed and at compatible versions |
| 70 | + |
| 71 | +## Recommendations |
| 72 | + |
| 73 | +### 1. Developer Onboarding |
| 74 | + |
| 75 | +- **Documentation**: Update CONTRIBUTING.md to include clear instructions on setting up Git hooks and understanding commit message requirements. |
| 76 | +- **Git Configuration**: Recommend developers set up the commit message template: |
| 77 | + ``` |
| 78 | + git config --local commit.template .gitmessage |
| 79 | + ``` |
| 80 | + |
| 81 | +### 2. CI/CD Integration |
| 82 | + |
| 83 | +- Add commit message validation to CI pipelines to ensure all commits (including those bypassing hooks locally) follow conventions |
| 84 | +- Consider implementing branch protection rules requiring conventional commits for PR merges |
| 85 | + |
| 86 | +### 3. Ongoing Maintenance |
| 87 | + |
| 88 | +- Regularly update Husky, lint-staged, and commitlint to their latest versions |
| 89 | +- Consider adding automated tests for Git hooks to verify they're working as expected |
| 90 | +- Implement a routine check for hook permissions as part of the repository health checks |
| 91 | + |
| 92 | +### 4. Performance Optimization |
| 93 | + |
| 94 | +- Consider selective testing in pre-commit hooks (only test affected packages) |
| 95 | +- Implement caching mechanisms for build and test operations to speed up the commit process |
| 96 | +- Consider using the `--no-verify` flag judiciously for WIP commits, but ensure final commits pass all checks |
| 97 | + |
| 98 | +## Conclusion |
| 99 | + |
| 100 | +The pre-commit hook and commit message validation issues have been resolved by properly configuring Husky, fixing the hook scripts, and ensuring correct permissions. The system now correctly validates code quality and commit messages according to the project's standards. |
| 101 | + |
| 102 | +These improvements will lead to more consistent code quality, better commit history, and more reliable automated releases through semantic-release. |
| 103 | + |
| 104 | +## References |
| 105 | + |
| 106 | +- [Husky Documentation](https://typicode.github.io/husky/) |
| 107 | +- [Conventional Commits Specification](https://www.conventionalcommits.org/) |
| 108 | +- [commitlint Documentation](https://commitlint.js.org/) |
| 109 | +- [lint-staged Documentation](https://github.com/lint-staged/lint-staged) |
0 commit comments