Skip to content

Commit

Permalink
Throw a pretty error if the hooks cannot be sorted
Browse files Browse the repository at this point in the history
If the hooks form a cycle, we now throw an error with the hooks in
question and show their `before` and `after` fields to help identify the
cycle.
  • Loading branch information
sandydoo committed Dec 15, 2024
1 parent 4d56242 commit 96209c1
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion modules/pre-commit.nix
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,32 @@ let
(a: b: builtins.elem b.id a.before || builtins.elem a.id b.after)
(builtins.attrValues enabledHooks);
in
builtins.map (value: value.raw) sortedHooks.result;
if sortedHooks ? result then
builtins.map (value: value.raw) sortedHooks.result
else
let
getIds = builtins.map (value: value.id);

prettyPrintCycle = opts: cycle:
lib.pipe cycle [
(builtins.map (hook:
lib.nameValuePair hook.id { before = hook.before; after = hook.after; }
))
lib.listToAttrs
(lib.generators.toPretty opts)
];
in
throw ''
The hooks can't be sorted because of a cycle in the dependency graph:
${concatStringsSep " -> " (getIds sortedHooks.cycle)}
which leads to a loop back to: ${concatStringsSep ", " (getIds sortedHooks.loops)}
Try removing the conflicting hook ids from the `before` and `after` attributes of these hooks:
${prettyPrintCycle { indent = " "; } sortedHooks.cycle}
'';

configFile =
performAssertions (
Expand Down

0 comments on commit 96209c1

Please sign in to comment.