Skip to content

Commit

Permalink
findCycles: revert to original file
Browse files Browse the repository at this point in the history
  • Loading branch information
DavHau authored and hsjobeki committed Oct 17, 2023
1 parent c80a656 commit 96610f4
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 112 deletions.
98 changes: 98 additions & 0 deletions lib/internal/findCycles.nix
Original file line number Diff line number Diff line change
@@ -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
93 changes: 0 additions & 93 deletions lib/internal/graphUtils.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 3 additions & 6 deletions modules/dream2nix/nodejs-granular-v3/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
{lib ? import <nixpkgs/lib>, ...}: let
utils = import ../../../lib/internal/graphUtils.nix {inherit lib;};
{
pkgs ? import <nixpkgs> {},
lib ? import <nixpkgs/lib>,
...
}: let
findCycles = import ../../../lib/internal/findCycles.nix {inherit lib;};
in {
test_simple = {
expr = utils.findCycles {
expr = findCycles {
dependencyGraph = {
a.v1 = [
{
Expand Down Expand Up @@ -32,7 +36,7 @@ in {
};

test_cycle_length_3 = {
expr = utils.findCycles {
expr = findCycles {
dependencyGraph = {
a.v1 = [
{
Expand Down Expand Up @@ -68,7 +72,7 @@ in {
};

test_two_roots_both_chosen = {
expr = utils.findCycles {
expr = findCycles {
dependencyGraph = {
a.v1 = [
{
Expand Down Expand Up @@ -117,7 +121,7 @@ in {
};

test_two_roots_one_chosen = {
expr = utils.findCycles {
expr = findCycles {
dependencyGraph = {
a.v1 = [
{
Expand Down Expand Up @@ -159,7 +163,7 @@ in {
};

test_c_visited_twice_no_cycle = {
expr = utils.findCycles {
expr = findCycles {
dependencyGraph = {
a.v1 = [
{
Expand All @@ -184,7 +188,7 @@ in {
};

test_two_cycles_one_root = {
expr = utils.findCycles {
expr = findCycles {
dependencyGraph = {
a.v1 = [
{
Expand Down
7 changes: 2 additions & 5 deletions tests/nix-unit/test_graph_utils/default.nix
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
{
lib ? import <nixpkgs/lib>,
...
}: let
{lib ? import <nixpkgs/lib>, ...}: let
sanitizeGraphTests = import ./sanitizeGraph.nix {inherit lib;};
in {
inherit sanitizeGraphTests;
inherit sanitizeGraphTests;
}

0 comments on commit 96610f4

Please sign in to comment.