From cda78765fa3b1cbcae4806a24ed6d1b1a382d686 Mon Sep 17 00:00:00 2001 From: eldritch horrors Date: Sat, 1 Feb 2025 21:24:35 +0100 Subject: [PATCH] libstore: asyncify ParsedDerivation::prepareStructuredAttrs Change-Id: I6c34bb5fc67046a0c25486bf4f2fa5bbaa62eacf --- lix/legacy/nix-build.cc | 2 +- lix/libstore/build/local-derivation-goal.cc | 4 +++- lix/libstore/parsed-derivations.cc | 13 ++++++++----- lix/libstore/parsed-derivations.hh | 3 ++- 4 files changed, 14 insertions(+), 8 deletions(-) diff --git a/lix/legacy/nix-build.cc b/lix/legacy/nix-build.cc index 2d0a64fb9..e924edeeb 100644 --- a/lix/legacy/nix-build.cc +++ b/lix/legacy/nix-build.cc @@ -470,7 +470,7 @@ static void main_nix_build(AsyncIoRoot & aio, std::string programName, Strings a ParsedDerivation parsedDrv(drvInfo.requireDrvPath(*state), drv); - if (auto structAttrs = parsedDrv.prepareStructuredAttrs(*store, inputs)) { + if (auto structAttrs = aio.blockOn(parsedDrv.prepareStructuredAttrs(*store, inputs))) { auto json = structAttrs.value(); structuredAttrsRC = writeStructuredAttrsShell(json); diff --git a/lix/libstore/build/local-derivation-goal.cc b/lix/libstore/build/local-derivation-goal.cc index 82079ef7f..7ae6474a3 100644 --- a/lix/libstore/build/local-derivation-goal.cc +++ b/lix/libstore/build/local-derivation-goal.cc @@ -916,7 +916,9 @@ void LocalDerivationGoal::initEnv() kj::Promise> LocalDerivationGoal::writeStructuredAttrs() try { - if (auto structAttrsJson = parsedDrv->prepareStructuredAttrs(worker.store, inputPaths)) { + if (auto structAttrsJson = + TRY_AWAIT(parsedDrv->prepareStructuredAttrs(worker.store, inputPaths))) + { auto json = structAttrsJson.value(); nlohmann::json rewritten; for (auto & [i, v] : json["outputs"].get()) { diff --git a/lix/libstore/parsed-derivations.cc b/lix/libstore/parsed-derivations.cc index f01c7f28d..70853f937 100644 --- a/lix/libstore/parsed-derivations.cc +++ b/lix/libstore/parsed-derivations.cc @@ -134,10 +134,11 @@ bool ParsedDerivation::useUidRange() const static std::regex shVarName("[A-Za-z_][A-Za-z0-9_]*"); -std::optional ParsedDerivation::prepareStructuredAttrs(Store & store, const StorePathSet & inputPaths) -{ +kj::Promise>> +ParsedDerivation::prepareStructuredAttrs(Store & store, const StorePathSet & inputPaths) +try { auto structuredAttrs = getStructuredAttrs(); - if (!structuredAttrs) return std::nullopt; + if (!structuredAttrs) co_return std::nullopt; auto json = *structuredAttrs; @@ -155,12 +156,14 @@ std::optional ParsedDerivation::prepareStructuredAttrs(Store & s for (auto & p : *i) storePaths.insert(store.toStorePath(p.get()).first); json[i.key()] = store.pathInfoToJSON( - RUN_ASYNC_IN_NEW_THREAD(store.exportReferences(storePaths, inputPaths)), false, true + TRY_AWAIT(store.exportReferences(storePaths, inputPaths)), false, true ); } } - return json; + co_return json; +} catch (...) { + co_return result::current_exception(); } /* As a convenience to bash scripts, write a shell file that diff --git a/lix/libstore/parsed-derivations.hh b/lix/libstore/parsed-derivations.hh index 36621d01d..6c7d928f1 100644 --- a/lix/libstore/parsed-derivations.hh +++ b/lix/libstore/parsed-derivations.hh @@ -41,7 +41,8 @@ public: bool useUidRange() const; - std::optional prepareStructuredAttrs(Store & store, const StorePathSet & inputPaths); + kj::Promise>> + prepareStructuredAttrs(Store & store, const StorePathSet & inputPaths); }; std::string writeStructuredAttrsShell(const nlohmann::json & json);