diff --git a/lix/libcmd/built-path.cc b/lix/libcmd/built-path.cc index e05b16ae2..703396925 100644 --- a/lix/libcmd/built-path.cc +++ b/lix/libcmd/built-path.cc @@ -80,38 +80,47 @@ SingleDerivedPath SingleBuiltPath::discardOutputPath() const ); } -nlohmann::json BuiltPath::Built::toJSON(const Store & store) const -{ +kj::Promise> BuiltPath::Built::toJSON(const Store & store) const +try { nlohmann::json res; - res["drvPath"] = drvPath->toJSON(store); + res["drvPath"] = TRY_AWAIT(drvPath->toJSON(store)); for (const auto & [outputName, outputPath] : outputs) { res["outputs"][outputName] = store.printStorePath(outputPath); } - return res; + co_return res; +} catch (...) { + co_return result::current_exception(); } -nlohmann::json SingleBuiltPath::Built::toJSON(const Store & store) const -{ +kj::Promise> SingleBuiltPath::Built::toJSON(const Store & store) const +try { nlohmann::json res; - res["drvPath"] = drvPath->toJSON(store); + res["drvPath"] = TRY_AWAIT(drvPath->toJSON(store)); auto & [outputName, outputPath] = output; res["output"] = outputName; res["outputPath"] = store.printStorePath(outputPath); - return res; + co_return res; +} catch (...) { + co_return result::current_exception(); } -nlohmann::json SingleBuiltPath::toJSON(const Store & store) const -{ - return std::visit([&](const auto & buildable) { +kj::Promise> SingleBuiltPath::toJSON(const Store & store) const +try { + co_return TRY_AWAIT(std::visit([&](const auto & buildable) { return buildable.toJSON(store); - }, raw()); + }, raw())); +} catch (...) { + co_return result::current_exception(); } -nlohmann::json BuiltPath::toJSON(const Store & store) const -{ - return std::visit([&](const auto & buildable) { + +kj::Promise> BuiltPath::toJSON(const Store & store) const +try { + co_return TRY_AWAIT(std::visit([&](const auto & buildable) { return buildable.toJSON(store); - }, raw()); + }, raw())); +} catch (...) { + co_return result::current_exception(); } kj::Promise> BuiltPath::toRealisedPaths(Store & store) const diff --git a/lix/libcmd/built-path.hh b/lix/libcmd/built-path.hh index e3c015c00..0ddf0f5e7 100644 --- a/lix/libcmd/built-path.hh +++ b/lix/libcmd/built-path.hh @@ -17,7 +17,7 @@ struct SingleBuiltPathBuilt { std::string to_string(const Store & store) const; static SingleBuiltPathBuilt parse(const Store & store, std::string_view, std::string_view); - nlohmann::json toJSON(const Store & store) const; + kj::Promise> toJSON(const Store & store) const; DECLARE_CMP(SingleBuiltPathBuilt); }; @@ -45,7 +45,7 @@ struct SingleBuiltPath : built_path::detail::SingleBuiltPathRaw { SingleDerivedPath discardOutputPath() const; static SingleBuiltPath parse(const Store & store, std::string_view); - nlohmann::json toJSON(const Store & store) const; + kj::Promise> toJSON(const Store & store) const; }; static inline ref staticDrv(StorePath drvPath) @@ -64,7 +64,7 @@ struct BuiltPathBuilt { std::string to_string(const Store & store) const; static BuiltPathBuilt parse(const Store & store, std::string_view, std::string_view); - nlohmann::json toJSON(const Store & store) const; + kj::Promise> toJSON(const Store & store) const; DECLARE_CMP(BuiltPathBuilt); }; @@ -94,7 +94,7 @@ struct BuiltPath : built_path::detail::BuiltPathRaw { StorePathSet outPaths() const; kj::Promise> toRealisedPaths(Store & store) const; - nlohmann::json toJSON(const Store & store) const; + kj::Promise> toJSON(const Store & store) const; }; typedef std::vector BuiltPaths; diff --git a/lix/libstore/derived-path.cc b/lix/libstore/derived-path.cc index f042f234b..592938e40 100644 --- a/lix/libstore/derived-path.cc +++ b/lix/libstore/derived-path.cc @@ -28,14 +28,17 @@ CMP(SingleDerivedPath, DerivedPathBuilt, outputs) #undef CMP #undef CMP_ONE -nlohmann::json DerivedPath::Opaque::toJSON(const Store & store) const -{ - return store.printStorePath(path); +kj::Promise> DerivedPath::Opaque::toJSON(const Store & store) const +try { + return {store.printStorePath(path)}; +} catch (...) { + return {result::current_exception()}; } -nlohmann::json SingleDerivedPath::Built::toJSON(Store & store) const { +kj::Promise> SingleDerivedPath::Built::toJSON(Store & store) const +try { nlohmann::json res; - res["drvPath"] = drvPath->toJSON(store); + res["drvPath"] = TRY_AWAIT(drvPath->toJSON(store)); // Fallback for the input-addressed derivation case: We expect to always be // able to print the output paths, so let’s do it // FIXME try-resolve on drvPath @@ -48,12 +51,15 @@ nlohmann::json SingleDerivedPath::Built::toJSON(Store & store) const { res["outputPath"] = store.printStorePath(*p); else res["outputPath"] = nullptr; - return res; + co_return res; +} catch (...) { + co_return result::current_exception(); } -nlohmann::json DerivedPath::Built::toJSON(Store & store) const { +kj::Promise> DerivedPath::Built::toJSON(Store & store) const +try { nlohmann::json res; - res["drvPath"] = drvPath->toJSON(store); + res["drvPath"] = TRY_AWAIT(drvPath->toJSON(store)); // Fallback for the input-addressed derivation case: We expect to always be // able to print the output paths, so let’s do it // FIXME try-resolve on drvPath @@ -65,21 +71,27 @@ nlohmann::json DerivedPath::Built::toJSON(Store & store) const { else res["outputs"][output] = nullptr; } - return res; + co_return res; +} catch (...) { + co_return result::current_exception(); } -nlohmann::json SingleDerivedPath::toJSON(Store & store) const -{ - return std::visit([&](const auto & buildable) { +kj::Promise> SingleDerivedPath::toJSON(Store & store) const +try { + co_return TRY_AWAIT(std::visit([&](const auto & buildable) { return buildable.toJSON(store); - }, raw()); + }, raw())); +} catch (...) { + co_return result::current_exception(); } -nlohmann::json DerivedPath::toJSON(Store & store) const -{ - return std::visit([&](const auto & buildable) { +kj::Promise> DerivedPath::toJSON(Store & store) const +try { + co_return TRY_AWAIT(std::visit([&](const auto & buildable) { return buildable.toJSON(store); - }, raw()); + }, raw())); +} catch (...) { + co_return result::current_exception(); } std::string DerivedPath::Opaque::to_string(const Store & store) const diff --git a/lix/libstore/derived-path.hh b/lix/libstore/derived-path.hh index d110ce434..e9994f181 100644 --- a/lix/libstore/derived-path.hh +++ b/lix/libstore/derived-path.hh @@ -6,7 +6,9 @@ #include "lix/libstore/outputs-spec.hh" #include "lix/libutil/comparator.hh" #include "lix/libutil/ref.hh" +#include "lix/libutil/result.hh" +#include #include #include @@ -27,7 +29,7 @@ struct DerivedPathOpaque { std::string to_string(const Store & store) const; static DerivedPathOpaque parse(const Store & store, std::string_view); - nlohmann::json toJSON(const Store & store) const; + kj::Promise> toJSON(const Store & store) const; GENERATE_CMP(DerivedPathOpaque, me->path); }; @@ -74,7 +76,7 @@ struct SingleDerivedPathBuilt { const Store & store, ref drvPath, OutputNameView outputs, const ExperimentalFeatureSettings & xpSettings = experimentalFeatureSettings); - nlohmann::json toJSON(Store & store) const; + kj::Promise> toJSON(Store & store) const; DECLARE_CMP(SingleDerivedPathBuilt); }; @@ -146,7 +148,7 @@ struct SingleDerivedPath : derived_path::detail::SingleDerivedPathRaw { const Store & store, std::string_view, const ExperimentalFeatureSettings & xpSettings = experimentalFeatureSettings); - nlohmann::json toJSON(Store & store) const; + kj::Promise> toJSON(Store & store) const; }; static inline ref makeConstantStorePathRef(StorePath drvPath) @@ -199,7 +201,7 @@ struct DerivedPathBuilt { const Store & store, ref, std::string_view, const ExperimentalFeatureSettings & xpSettings = experimentalFeatureSettings); - nlohmann::json toJSON(Store & store) const; + kj::Promise> toJSON(Store & store) const; DECLARE_CMP(DerivedPathBuilt); }; @@ -276,7 +278,7 @@ struct DerivedPath : derived_path::detail::DerivedPathRaw { */ static DerivedPath fromSingle(const SingleDerivedPath &); - nlohmann::json toJSON(Store & store) const; + kj::Promise> toJSON(Store & store) const; }; typedef std::vector DerivedPaths; diff --git a/lix/nix/build.cc b/lix/nix/build.cc index 7602f719f..432698fba 100644 --- a/lix/nix/build.cc +++ b/lix/nix/build.cc @@ -9,23 +9,25 @@ using namespace nix; -static nlohmann::json derivedPathsToJSON(const DerivedPaths & paths, Store & store) +static nlohmann::json derivedPathsToJSON(AsyncIoRoot & aio, const DerivedPaths & paths, Store & store) { auto res = nlohmann::json::array(); for (auto & t : paths) { std::visit([&](const auto & t) { - res.push_back(t.toJSON(store)); + res.push_back(aio.blockOn(t.toJSON(store))); }, t.raw()); } return res; } -static nlohmann::json builtPathsWithResultToJSON(const std::vector & buildables, const Store & store) +static nlohmann::json builtPathsWithResultToJSON( + AsyncIoRoot & aio, const std::vector & buildables, const Store & store +) { auto res = nlohmann::json::array(); for (auto & b : buildables) { std::visit([&](const auto & t) { - auto j = t.toJSON(store); + auto j = aio.blockOn(t.toJSON(store)); if (b.result) { if (b.result->startTime) j["startTime"] = b.result->startTime; @@ -132,7 +134,7 @@ struct CmdBuild : InstallablesCommand, MixDryRun, MixJSON, MixProfile printMissing(store, pathsToBuild, lvlError); if (json) - logger->cout("%s", derivedPathsToJSON(pathsToBuild, *store).dump()); + logger->cout("%s", derivedPathsToJSON(aio(), pathsToBuild, *store).dump()); return; } @@ -143,7 +145,7 @@ struct CmdBuild : InstallablesCommand, MixDryRun, MixJSON, MixProfile installables, repair ? bmRepair : buildMode); - if (json) logger->cout("%s", builtPathsWithResultToJSON(buildables, *store).dump()); + if (json) logger->cout("%s", builtPathsWithResultToJSON(aio(), buildables, *store).dump()); if (outLink != "") if (auto store2 = store.dynamic_pointer_cast())