From 96610f40d20235f6534a62170afcb6706f825f07 Mon Sep 17 00:00:00 2001 From: DavHau Date: Tue, 17 Oct 2023 21:39:45 +0100 Subject: [PATCH] findCycles: revert to original file --- lib/internal/findCycles.nix | 98 +++++++++++++++++++ lib/internal/graphUtils.nix | 93 ------------------ .../dream2nix/nodejs-granular-v3/default.nix | 9 +- .../default.nix} | 20 ++-- tests/nix-unit/test_graph_utils/default.nix | 7 +- 5 files changed, 115 insertions(+), 112 deletions(-) create mode 100644 lib/internal/findCycles.nix rename tests/nix-unit/{test_graph_utils/findCycles.nix => test_findCycles/default.nix} (93%) diff --git a/lib/internal/findCycles.nix b/lib/internal/findCycles.nix new file mode 100644 index 0000000000..a0d5f53ffc --- /dev/null +++ b/lib/internal/findCycles.nix @@ -0,0 +1,98 @@ +# This is currently only used for legacy modules ported to v1. +# The dream-lock concept might be deprecated together with this module at some +# point. +{lib, ...}: let + l = builtins // lib; + + nameVersionPair = name: version: { + name = name; + version = version; + }; + + findCycles = { + dependencyGraph, + roots, + }: let + depGraphWithFakeRoot = + l.recursiveUpdate + dependencyGraph + { + __fake-entry.__fake-version = + l.mapAttrsToList + nameVersionPair + roots; + }; + + findCycles_ = node: prevNodes: cycles: let + children = + depGraphWithFakeRoot."${node.name}"."${node.version}"; + + cyclicChildren = + l.filter + (child: prevNodes ? "${child.name}#${child.version}") + children; + + nonCyclicChildren = + l.filter + (child: ! prevNodes ? "${child.name}#${child.version}") + children; + + cycles' = + cycles + ++ (l.map (child: { + from = node; + to = child; + }) + cyclicChildren); + + # use set for efficient lookups + prevNodes' = + prevNodes + // {"${node.name}#${node.version}" = null;}; + in + if nonCyclicChildren == [] + then cycles' + else + l.flatten + (l.map + (child: findCycles_ child prevNodes' cycles') + nonCyclicChildren); + + cyclesList = + findCycles_ + ( + nameVersionPair + "__fake-entry" + "__fake-version" + ) + {} + []; + in + l.foldl' + (cycles: cycle: ( + let + existing = + cycles."${cycle.from.name}"."${cycle.from.version}" + or []; + + reverse = + cycles."${cycle.to.name}"."${cycle.to.version}" + or []; + in + # if edge or reverse edge already in cycles, do nothing + if + l.elem cycle.from reverse + || l.elem cycle.to existing + then cycles + else + l.recursiveUpdate + cycles + { + "${cycle.from.name}"."${cycle.from.version}" = + existing ++ [cycle.to]; + } + )) + {} + cyclesList; +in + findCycles diff --git a/lib/internal/graphUtils.nix b/lib/internal/graphUtils.nix index 33713ebab2..8619e87bc4 100644 --- a/lib/internal/graphUtils.nix +++ b/lib/internal/graphUtils.nix @@ -153,104 +153,11 @@ A collection of tools needed to interact with graphs (i.e. A dependencyTree) in res // {${name} = res.${name} or {} // {${version} = res.${name}.${version} or [] ++ map identToNameVersionPair deps;};} ) {} (l.attrNames graph); - ########################################## # Currently only used for legacy modules ported to v1. - - nameVersionPair = name: version: { - name = name; - version = version; - }; - - findCycles = { - dependencyGraph, - roots, - }: let - depGraphWithFakeRoot = - l.recursiveUpdate - dependencyGraph - { - __fake-entry.__fake-version = - l.mapAttrsToList - nameVersionPair - roots; - }; - - findCycles_ = node: prevNodes: cycles: let - children = - depGraphWithFakeRoot."${node.name}"."${node.version}"; - - cyclicChildren = - l.filter - (child: prevNodes ? "${child.name}#${child.version}") - children; - - nonCyclicChildren = - l.filter - (child: ! prevNodes ? "${child.name}#${child.version}") - children; - - cycles' = - cycles - ++ (l.map (child: { - from = node; - to = child; - }) - cyclicChildren); - - # use set for efficient lookups - prevNodes' = - prevNodes - // {"${node.name}#${node.version}" = null;}; - in - if nonCyclicChildren == [] - then cycles' - else - l.flatten - (l.map - (child: findCycles_ child prevNodes' cycles') - nonCyclicChildren); - - cyclesList = - findCycles_ - ( - nameVersionPair - "__fake-entry" - "__fake-version" - ) - {} - []; - in - l.foldl' - (cycles: cycle: ( - let - existing = - cycles."${cycle.from.name}"."${cycle.from.version}" - or []; - - reverse = - cycles."${cycle.to.name}"."${cycle.to.version}" - or []; - in - # if edge or reverse edge already in cycles, do nothing - if - l.elem cycle.from reverse - || l.elem cycle.to existing - then cycles - else - l.recursiveUpdate - cycles - { - "${cycle.from.name}"."${cycle.from.version}" = - existing ++ [cycle.to]; - } - )) - {} - cyclesList; in { inherit sanitizeGraph - findCycles fromDependencyGraph identToNameVersionPair fromNameVersionPair diff --git a/modules/dream2nix/nodejs-granular-v3/default.nix b/modules/dream2nix/nodejs-granular-v3/default.nix index 92ad4c2fd7..8bb96bf30a 100644 --- a/modules/dream2nix/nodejs-granular-v3/default.nix +++ b/modules/dream2nix/nodejs-granular-v3/default.nix @@ -13,12 +13,9 @@ inherit (config.deps.stdenv) mkDerivation; }; - inherit - (import ../../../lib/internal/graphUtils.nix { - inherit lib; - }) - findCycles - ; + findCycles = import ../../../lib/internal/findCycles.nix { + inherit lib; + }; # pdefs.${name}.${version} :: { # // all dependency entries of that package. diff --git a/tests/nix-unit/test_graph_utils/findCycles.nix b/tests/nix-unit/test_findCycles/default.nix similarity index 93% rename from tests/nix-unit/test_graph_utils/findCycles.nix rename to tests/nix-unit/test_findCycles/default.nix index 8735098cb9..f0f98399a6 100644 --- a/tests/nix-unit/test_graph_utils/findCycles.nix +++ b/tests/nix-unit/test_findCycles/default.nix @@ -1,8 +1,12 @@ -{lib ? import , ...}: let - utils = import ../../../lib/internal/graphUtils.nix {inherit lib;}; +{ + pkgs ? import {}, + lib ? import , + ... +}: let + findCycles = import ../../../lib/internal/findCycles.nix {inherit lib;}; in { test_simple = { - expr = utils.findCycles { + expr = findCycles { dependencyGraph = { a.v1 = [ { @@ -32,7 +36,7 @@ in { }; test_cycle_length_3 = { - expr = utils.findCycles { + expr = findCycles { dependencyGraph = { a.v1 = [ { @@ -68,7 +72,7 @@ in { }; test_two_roots_both_chosen = { - expr = utils.findCycles { + expr = findCycles { dependencyGraph = { a.v1 = [ { @@ -117,7 +121,7 @@ in { }; test_two_roots_one_chosen = { - expr = utils.findCycles { + expr = findCycles { dependencyGraph = { a.v1 = [ { @@ -159,7 +163,7 @@ in { }; test_c_visited_twice_no_cycle = { - expr = utils.findCycles { + expr = findCycles { dependencyGraph = { a.v1 = [ { @@ -184,7 +188,7 @@ in { }; test_two_cycles_one_root = { - expr = utils.findCycles { + expr = findCycles { dependencyGraph = { a.v1 = [ { diff --git a/tests/nix-unit/test_graph_utils/default.nix b/tests/nix-unit/test_graph_utils/default.nix index 2119586ae2..e96f1bd014 100644 --- a/tests/nix-unit/test_graph_utils/default.nix +++ b/tests/nix-unit/test_graph_utils/default.nix @@ -1,8 +1,5 @@ -{ - lib ? import , - ... -}: let +{lib ? import , ...}: let sanitizeGraphTests = import ./sanitizeGraph.nix {inherit lib;}; in { - inherit sanitizeGraphTests; + inherit sanitizeGraphTests; }