From 5ab5f7d96ca26dfdd9c36f0261686fdfe3f26a68 Mon Sep 17 00:00:00 2001 From: Maximilian Bosch Date: Wed, 2 Jul 2025 00:03:15 +0200 Subject: [PATCH] why-depends: add entrypoint for graph builder API The now-introduced genGraphString will be the public API of the libstore helper. Change-Id: Id47d21230d1a30b880f298f307c30ddffdb0e9c7 --- lix/nix/why-depends.cc | 61 +++++++++++++++++++++++------------------- 1 file changed, 33 insertions(+), 28 deletions(-) diff --git a/lix/nix/why-depends.cc b/lix/nix/why-depends.cc index fb1c9ee00..439471b48 100644 --- a/lix/nix/why-depends.cc +++ b/lix/nix/why-depends.cc @@ -3,11 +3,10 @@ #include "lix/libstore/fs-accessor.hh" #include "lix/libmain/shared.hh" #include "lix/libutil/error.hh" -#include "lix/libutil/generator.hh" #include "lix/libutil/result.hh" +#include "lix/libutil/strings.hh" #include "why-depends.hh" -#include #include namespace nix { @@ -285,6 +284,32 @@ static std::map mkGraph( return graph_data; } +static kj::Promise> genGraphString( + const StorePath & start, + const StorePath & to, + const std::map & graphData, + Store & store, + bool all, + bool precise +) +try { + auto graph = mkGraph(start, to, graphData, store, all, precise); + + Strings output; + if (!precise) { + output.push_back(fmt("%s", store.printStorePath(graph.at(start).path))); + } + + try { + TRY_AWAIT(printNode(graph.at(start), "", "", all, precise, store, start, to, graph, output)); + } catch (BailOut &) { + } + + co_return concatStringsSep("\n", output); +} catch (...) { + co_return result::current_exception(); +} + struct CmdWhyDepends : SourceExprCommand, MixOperateOnOptions { std::string _package, _dependency; @@ -377,37 +402,17 @@ struct CmdWhyDepends : SourceExprCommand, MixOperateOnOptions graphData.emplace(path, aio().blockOn(store->queryPathInfo(path))->references); } - auto graph = mkGraph(packagePath, dependencyPath, graphData, *store, all, precise); - /* Print the subgraph of nodes that have 'dependency' in their closure (i.e., that have a non-infinite distance to 'dependency'). Print every edge on a path between `package` and `dependency`. */ RunPager pager; - if (!precise) { - logger->cout("%s", store->printStorePath(graph.at(packagePath).path)); - } - - Strings output; - try { - aio().blockOn(printNode( - graph.at(packagePath), - "", - "", - all, - precise, - *store, - packagePath, - dependencyPath, - graph, - output - )); - } catch (BailOut &) { - } - - for (auto & l : output) { - logger->cout("%s", l); - } + logger->cout( + "%s", + aio().blockOn( + genGraphString(packagePath, dependencyPath, graphData, *store, all, precise) + ) + ); } };