Skip to content

Commit 91aeb2a

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 e35aed5 commit 91aeb2a

File tree

1 file changed

+50
-7
lines changed

1 file changed

+50
-7
lines changed

modules/hooks.nix

Lines changed: 50 additions & 7 deletions
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,12 +1444,15 @@ 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-
example = ".typos.toml";
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;
1455+
example = "./.typos.toml";
14531456
};
14541457

14551458
diff =
@@ -3300,6 +3303,45 @@ 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 Nix string and not as Nix
3330+
# Path, we can't read it from Nix, unfortunately.
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+
# The "files.extend-exclude" key comes from
3339+
# https://github.com/crate-ci/typos/blob/master/docs/reference.md
3340+
(toml.files or { }).extend-exclude or [ ]
3341+
)
3342+
else
3343+
[ ];
3344+
in
33033345
{
33043346
name = "typos";
33053347
description = "Source code spell checker";
@@ -3314,8 +3356,8 @@ lib.escapeShellArgs (lib.concatMap (ext: [ "--ghc-opt" "-X${ext}" ]) hooks.ormol
33143356
(with hooks.typos.settings; [
33153357
[ binary "--binary" ]
33163358
[ (color != "auto") "--color ${color}" ]
3317-
[ (configuration != "") "--config ${configFile}" ]
3318-
[ (configPath != "" && configuration == "") "--config ${configPath}" ]
3359+
# Config file always exists (we generate one if not).
3360+
[ true "--config ${pathToConfigFile}" ]
33193361
[ diff "--diff" ]
33203362
[ (exclude != "") "--exclude ${exclude} --force-exclude" ]
33213363
[ (format != "long") "--format ${format}" ]
@@ -3331,6 +3373,7 @@ lib.escapeShellArgs (lib.concatMap (ext: [ "--ghc-opt" "-X${ext}" ]) hooks.ormol
33313373
in
33323374
"${hooks.typos.package}/bin/typos ${cmdArgs}";
33333375
types = [ "text" ];
3376+
excludes = excludesFromConfig;
33343377
};
33353378
typstfmt = {
33363379
name = "typstfmt";

0 commit comments

Comments
 (0)