After years of successfully using cspell as a pre-commit hook, two common annoyances seem to be present.
Due to this, recently I found an alternative way to run cspell hook which I want to share with you, hoping that this will become an official hook.
- repo: https://github.com/streetsidesoftware/cspell-cli
rev: v9.0.1
hooks:
- id: cspell
entry: sh
pass_filenames: false
args:
[
"-c",
"cspell-cli . --relative --no-progress --no-summary && cspell . --disable-dictionary=words --words-only --quiet --unique | sort --ignore-case > .config/dictionary.txt",
]
name: Spell check with cspell and remove unused dictionary entries
As seen above this will:
- run spell once on entire codebase (as opposed to only modified files). This address bug where cspell will not run if its own configuration is changed or if the custom dictionary is modified, failing to spot issues.
- if the run is successful, it will reorder the custom dictionary.
- this keeps pre-commit hook cspell behavior in sync with external cspell calls (vscode extension for example).
Downsides:
- Current implementation relies on knowing the name and location of the in-repo custom dictionary. Maybe some modifications to the tool itself might remove the need for these and solely rely on its config file:
dictionaryDefinitions:
- name: words
path: .config/dictionary.txt
addWords: true