Skip to content

Commit 7e6fcae

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. [0]: #387 (comment)
1 parent 70f5040 commit 7e6fcae

File tree

1 file changed

+51
-6
lines changed

1 file changed

+51
-6
lines changed

modules/hooks.nix

+51-6
Original file line numberDiff line numberDiff line change
@@ -1430,7 +1430,7 @@ in
14301430
configuration =
14311431
mkOption {
14321432
type = types.str;
1433-
description = lib.mdDoc "Multiline-string configuration passed as config file. If set, config set in `typos.settings.configPath` gets ignored.";
1433+
description = lib.mdDoc "Multiline-string configuration passed as config file. It is recommended to use `configPath` instead for a more natural experience of typos.";
14341434
default = "";
14351435
example = ''
14361436
[files]
@@ -1444,11 +1444,14 @@ in
14441444
'';
14451445
};
14461446

1447+
# It is recommended to use a Nix path here as this way, the excludes
1448+
# from the config file can be taken into account by pre-commit when
1449+
# running `$ pre-commit run --all-files`.
14471450
configPath =
14481451
mkOption {
1449-
type = types.str;
1450-
description = lib.mdDoc "Path to a custom config file.";
1451-
default = "";
1452+
type = types.nullOr (types.either types.str types.path);
1453+
description = lib.mdDoc "Path to a typos config file (recommended) or a string (deprecated).";
1454+
default = null;
14521455
example = ".typos.toml";
14531456
};
14541457

@@ -3300,6 +3303,47 @@ lib.escapeShellArgs (lib.concatMap (ext: [ "--ghc-opt" "-X${ext}" ]) hooks.ormol
33003303
entry = "${hooks.treefmt.package}/bin/treefmt --fail-on-change";
33013304
};
33023305
typos =
3306+
let
3307+
# Path to config file. May be in Nix store but can also be a relative
3308+
# path to a system-dependent file.
3309+
#
3310+
# After various upstream discussions [0]), the best solution is to
3311+
# provide the config file as Nix path but keep the string passing
3312+
# as fall-back.
3313+
#
3314+
# [0]: https://github.com/cachix/pre-commit-hooks.nix/pull/387#issuecomment-1893600631
3315+
pathToConfigFile =
3316+
if hooks.typos.settings.configPath != null
3317+
# Important: If passed as typeOf == "path", this is in Nix store
3318+
# which we in fact encourage.
3319+
then hooks.typos.settings.configPath
3320+
else
3321+
builtins.toFile "config.toml"
3322+
# Concatenate config in config file with section for ignoring words
3323+
# generated from list of words to ignore
3324+
(hooks.typos.settings.configuration +
3325+
lib.strings.optionalString (hooks.typos.settings.ignored-words != [ ]) "\n\[default.extend-words\]" +
3326+
lib.strings.concatMapStrings (x: "\n${x} = \"${x}\"") hooks.typos.settings.ignored-words
3327+
)
3328+
;
3329+
# If the config file path is passed as string and not as path, we
3330+
# can't read it from Nix.
3331+
excludesFromConfig =
3332+
if builtins.typeOf hooks.typos.settings.configPath == "path" # passed directly or as Path
3333+
then
3334+
(
3335+
let
3336+
toml = builtins.fromTOML (builtins.readFile pathToConfigFile);
3337+
in
3338+
/*
3339+
The "files.extend-exclude" key comes from
3340+
https://github.com/crate-ci/typos/blob/master/docs/reference.md
3341+
*/
3342+
(toml.files or { }).extend-exclude or [ ]
3343+
)
3344+
else
3345+
[ ];
3346+
in
33033347
{
33043348
name = "typos";
33053349
description = "Source code spell checker";
@@ -3314,8 +3358,8 @@ lib.escapeShellArgs (lib.concatMap (ext: [ "--ghc-opt" "-X${ext}" ]) hooks.ormol
33143358
(with hooks.typos.settings; [
33153359
[ binary "--binary" ]
33163360
[ (color != "auto") "--color ${color}" ]
3317-
[ (configuration != "") "--config ${configFile}" ]
3318-
[ (configPath != "" && configuration == "") "--config ${configPath}" ]
3361+
# Config file always exists (we generate one if not).
3362+
[ true "--config ${pathToConfigFile}" ]
33193363
[ diff "--diff" ]
33203364
[ (exclude != "") "--exclude ${exclude} --force-exclude" ]
33213365
[ (format != "long") "--format ${format}" ]
@@ -3331,6 +3375,7 @@ lib.escapeShellArgs (lib.concatMap (ext: [ "--ghc-opt" "-X${ext}" ]) hooks.ormol
33313375
in
33323376
"${hooks.typos.package}/bin/typos ${cmdArgs}";
33333377
types = [ "text" ];
3378+
excludes = excludesFromConfig;
33343379
};
33353380
typstfmt = {
33363381
name = "typstfmt";

0 commit comments

Comments
 (0)