Skip to content

Commit 913b2bc

Browse files
committed
typos: fix behaviour
1 parent 5df5a70 commit 913b2bc

File tree

1 file changed

+68
-6
lines changed

1 file changed

+68
-6
lines changed

modules/hooks.nix

Lines changed: 68 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1115,10 +1115,24 @@ in
11151115
extend-glob = []
11161116
'';
11171117
};
1118+
# Recommended way to specifcy a config, as this way, exludes can be
1119+
# taken into account by pre-commit when running
1120+
# `$ pre-commit run --all-files`.
1121+
# We provide configFile and configPath for API compatibility with
1122+
# previous versions and coherence with other hooks defined here.
1123+
configFile =
1124+
mkOption {
1125+
type = types.path;
1126+
description = lib.mdDoc "Path to a typos config file (in Nix store).";
1127+
default = null;
1128+
example = "./.typos.toml";
1129+
};
1130+
# It is recommended to use configFile instead, as it provides more
1131+
# flexibility and a more correct/expected behaviour.
11181132
configPath =
11191133
mkOption {
11201134
type = types.str;
1121-
description = lib.mdDoc "Path to a custom config file.";
1135+
description = lib.mdDoc "Path to a typos config file (not in Nix store).";
11221136
default = "";
11231137
example = ".typos.toml";
11241138
};
@@ -2456,21 +2470,68 @@ in
24562470
entry = "${settings.treefmt.package}/bin/treefmt --fail-on-change";
24572471
};
24582472
typos =
2473+
let
2474+
# Since we don't set "pass_filenames = false" for typos (see
2475+
# upstream discussions [0]), we must ensure that pre-commit never
2476+
# passes the files specified as excludes in `.typos.toml` to
2477+
# `typos` as argument, if possible. Otherwise,
2478+
# `$ pre-commit run typos --all-files` has another behaviour than
2479+
# `$ typos .`, which is confusing and wrong.
2480+
#
2481+
# To not break anything, and to be compliant with the existing
2482+
# API, we encourage users to provide the `.typos.toml` as Nix
2483+
# path, so the excludes can be read via evaluation time.
2484+
#
2485+
# The `configAsFile` variable is the path where the configuration
2486+
# file can be found by the `typos` utility when it executes in the
2487+
# user environment. Not necessarily in Nix store.
2488+
#
2489+
# [0]: https://github.com/cachix/pre-commit-hooks.nix/pull/387#issuecomment-1893600631
2490+
configAsFile =
2491+
# Highest precedence.
2492+
if settings.typos.configFile != null
2493+
then (toString settings.typos.configFile)
2494+
# Secondary precedence.
2495+
else if settings.typos.configPath != ""
2496+
then settings.typos.configPath
2497+
# Lowest precedence.
2498+
else
2499+
builtins.toFile "config.toml"
2500+
# Concatenate config in config file with section for ignoring words
2501+
# generated from list of words to ignore
2502+
("${settings.typos.config}" +
2503+
lib.strings.optionalString (settings.typos.ignored-words != [ ]) "\n\[default.extend-words\]" +
2504+
lib.strings.concatMapStrings (x: "\n${x} = \"${x}\"") settings.typos.ignored-words
2505+
)
2506+
;
2507+
# If the config file path is passed as Nix string and not as Nix
2508+
# Path, we can't read it from Nix, unfortunately.
2509+
excludesFromConfig =
2510+
if settings.typos.configPath == "" # passed directly or as Path
2511+
then
2512+
(
2513+
let
2514+
toml = builtins.fromTOML (builtins.readFile configAsFile);
2515+
in
2516+
# The "files.extend-exclude" key comes from
2517+
# https://github.com/crate-ci/typos/blob/master/docs/reference.md
2518+
(toml.files or { }).extend-exclude or [ ]
2519+
)
2520+
else
2521+
[ ];
2522+
in
24592523
{
24602524
name = "typos";
24612525
description = "Source code spell checker";
24622526
entry =
24632527
let
2464-
# Concatenate config in config file with section for ignoring words generated from list of words to ignore
2465-
config = "${settings.typos.config}" + lib.strings.optionalString (settings.typos.ignored-words != [ ]) "\n\[default.extend-words\]" + lib.strings.concatMapStrings (x: "\n${x} = \"${x}\"") settings.typos.ignored-words;
2466-
configFile = builtins.toFile "typos-config.toml" config;
24672528
cmdArgs =
24682529
mkCmdArgs
24692530
(with settings.typos; [
2531+
# Config file always exists.
2532+
[ true "--config ${configAsFile}" ]
24702533
[ binary "--binary" ]
24712534
[ (color != "auto") "--color ${color}" ]
2472-
[ (config != "") "--config ${configFile}" ]
2473-
[ (configPath != "" && config == "") "--config ${configPath}" ]
24742535
[ diff "--diff" ]
24752536
[ (exclude != "") "--exclude ${exclude} --force-exclude" ]
24762537
[ (format != "long") "--format ${format}" ]
@@ -2486,6 +2547,7 @@ in
24862547
in
24872548
"${tools.typos}/bin/typos ${cmdArgs}";
24882549
types = [ "text" ];
2550+
excludes = excludesFromConfig;
24892551
};
24902552
typstfmt = {
24912553
name = "typstfmt";

0 commit comments

Comments
 (0)