Skip to content

Commit f826a04

Browse files
committed
typos: fix unexpected behaviour
Enable typos.configPath to be of type path. This way, we can use excludes from .typos.toml when running `pre-commit run typos --all-files`. Otherwise, the execution differs from a normal invocation of typos, which is unexpected and leads to wrong results. To not break anything, and to be compliant with the existing API, I modified configPath to be either a Nix path or a string. The main motivation for this change is a repository on my machine where pre-commit ignores directory foobar (which is excluded by .typos.toml) but passes all 65,000 files of foobar as argument to typos. In these situation, typos goes nuts and often the system freezes. So with this change, I prevent that possibly tens of thousands of files that should not be checked in any case are passed to typos, which results in a smooth experience. [0]: cachix#387 (comment)
1 parent 40e6053 commit f826a04

File tree

1 file changed

+51
-6
lines changed

1 file changed

+51
-6
lines changed

modules/hooks.nix

Lines changed: 51 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1436,7 +1436,7 @@ in
14361436
configuration =
14371437
mkOption {
14381438
type = types.str;
1439-
description = lib.mdDoc "Multiline-string configuration passed as config file. If set, config set in `typos.settings.configPath` gets ignored.";
1439+
description = lib.mdDoc "Multiline-string configuration passed as config file. It is recommended to use `configPath` instead for a more natural experience of typos.";
14401440
default = "";
14411441
example = ''
14421442
[files]
@@ -1450,11 +1450,14 @@ in
14501450
'';
14511451
};
14521452

1453+
# It is recommended to use a Nix path here as this way, the excludes
1454+
# from the config file can be taken into account by pre-commit when
1455+
# running `$ pre-commit run --all-files`.
14531456
configPath =
14541457
mkOption {
1455-
type = types.str;
1456-
description = lib.mdDoc "Path to a custom config file.";
1457-
default = "";
1458+
type = types.nullOr (types.either types.str types.path);
1459+
description = lib.mdDoc "[Path](https://nixos.org/manual/nix/stable/language/values#type-path) to a typos config file (recommended) or a string (deprecated)";
1460+
default = null;
14581461
example = ".typos.toml";
14591462
};
14601463

@@ -3314,6 +3317,47 @@ lib.escapeShellArgs (lib.concatMap (ext: [ "--ghc-opt" "-X${ext}" ]) hooks.ormol
33143317
entry = "${hooks.treefmt.package}/bin/treefmt --fail-on-change";
33153318
};
33163319
typos =
3320+
let
3321+
# Path to config file. May be in Nix store but can also be a relative
3322+
# path to a system-dependent file.
3323+
#
3324+
# After various upstream discussions [0]), the best solution is to
3325+
# provide the config file as Nix path but keep the string passing
3326+
# as fall-back.
3327+
#
3328+
# [0]: https://github.com/cachix/pre-commit-hooks.nix/pull/387#issuecomment-1893600631
3329+
pathToConfigFile =
3330+
if hooks.typos.settings.configPath != null
3331+
# Important: If passed as typeOf == "path", this is in Nix store
3332+
# which we in fact encourage.
3333+
then hooks.typos.settings.configPath
3334+
else
3335+
builtins.toFile "config.toml"
3336+
# Concatenate config in config file with section for ignoring words
3337+
# generated from list of words to ignore
3338+
(hooks.typos.settings.configuration +
3339+
lib.strings.optionalString (hooks.typos.settings.ignored-words != [ ]) "\n\[default.extend-words\]" +
3340+
lib.strings.concatMapStrings (x: "\n${x} = \"${x}\"") hooks.typos.settings.ignored-words
3341+
)
3342+
;
3343+
# If the config file path is passed as string and not as path, we
3344+
# can't read it from Nix.
3345+
excludesFromConfig =
3346+
if builtins.typeOf hooks.typos.settings.configPath == "path" # passed directly or as Path
3347+
then
3348+
(
3349+
let
3350+
toml = builtins.fromTOML (builtins.readFile pathToConfigFile);
3351+
in
3352+
/*
3353+
The "files.extend-exclude" key comes from
3354+
https://github.com/crate-ci/typos/blob/master/docs/reference.md
3355+
*/
3356+
(toml.files or { }).extend-exclude or [ ]
3357+
)
3358+
else
3359+
[ ];
3360+
in
33173361
{
33183362
name = "typos";
33193363
description = "Source code spell checker";
@@ -3328,8 +3372,8 @@ lib.escapeShellArgs (lib.concatMap (ext: [ "--ghc-opt" "-X${ext}" ]) hooks.ormol
33283372
(with hooks.typos.settings; [
33293373
[ binary "--binary" ]
33303374
[ (color != "auto") "--color ${color}" ]
3331-
[ (configuration != "") "--config ${configFile}" ]
3332-
[ (configPath != "" && configuration == "") "--config ${configPath}" ]
3375+
# Config file always exists (we generate one if not).
3376+
[ true "--config ${pathToConfigFile}" ]
33333377
[ diff "--diff" ]
33343378
[ (exclude != "") "--exclude ${exclude} --force-exclude" ]
33353379
[ (format != "long") "--format ${format}" ]
@@ -3345,6 +3389,7 @@ lib.escapeShellArgs (lib.concatMap (ext: [ "--ghc-opt" "-X${ext}" ]) hooks.ormol
33453389
in
33463390
"${hooks.typos.package}/bin/typos ${cmdArgs}";
33473391
types = [ "text" ];
3392+
excludes = excludesFromConfig;
33483393
};
33493394
typstfmt = {
33503395
name = "typstfmt";

0 commit comments

Comments
 (0)