Skip to content

Commit 6d71e46

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 6d71e46

File tree

1 file changed

+51
-7
lines changed

1 file changed

+51
-7
lines changed

Diff for: modules/hooks.nix

+51-7
Original file line numberDiff line numberDiff line change
@@ -1427,10 +1427,11 @@ in
14271427
description = lib.mdDoc "When to use generate output.";
14281428
default = "auto";
14291429
};
1430+
# It is recommended to use `configPath`.
14301431
configuration =
14311432
mkOption {
14321433
type = types.str;
1433-
description = lib.mdDoc "Multiline-string configuration passed as config file. If set, config set in `typos.settings.configPath` gets ignored.";
1434+
description = lib.mdDoc "Multiline-string configuration passed as config file. If set, config set in `typos.settings.config{File|Path}` gets ignored.";
14341435
default = "";
14351436
example = ''
14361437
[files]
@@ -1444,12 +1445,15 @@ in
14441445
'';
14451446
};
14461447

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

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

0 commit comments

Comments
 (0)