From fb028ae701b34b3687600386e6556058d865124d Mon Sep 17 00:00:00 2001 From: John Ericson Date: Thu, 13 Feb 2025 00:52:05 -0500 Subject: [PATCH] Don't do store operations in `*DerivedPath::toJSON` It is inappropriate to do "real work" when converting plain old data to JSON. --- src/libstore/derived-path.cc | 47 ++++++------------- .../include/nix/store/derived-path.hh | 8 ++-- tests/functional/build-dry.sh | 15 ++---- 3 files changed, 22 insertions(+), 48 deletions(-) diff --git a/src/libstore/derived-path.cc b/src/libstore/derived-path.cc index 6186f0582..658be5801 100644 --- a/src/libstore/derived-path.cc +++ b/src/libstore/derived-path.cc @@ -41,49 +41,30 @@ nlohmann::json DerivedPath::Opaque::toJSON(const StoreDirConfig & store) const return store.printStorePath(path); } -nlohmann::json SingleDerivedPath::Built::toJSON(Store & store) const { - nlohmann::json res; - res["drvPath"] = 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 - const auto outputMap = store.queryPartialDerivationOutputMap(resolveDerivedPath(store, *drvPath)); - res["output"] = output; - auto outputPathIter = outputMap.find(output); - if (outputPathIter == outputMap.end()) - res["outputPath"] = nullptr; - else if (std::optional p = outputPathIter->second) - res["outputPath"] = store.printStorePath(*p); - else - res["outputPath"] = nullptr; - return res; +nlohmann::json SingleDerivedPath::Built::toJSON(const StoreDirConfig & store) const +{ + return nlohmann::json{ + {"drvPath", drvPath->toJSON(store)}, + {"output", output}, + }; } -nlohmann::json DerivedPath::Built::toJSON(Store & store) const { - nlohmann::json res; - res["drvPath"] = 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 - const auto outputMap = store.queryPartialDerivationOutputMap(resolveDerivedPath(store, *drvPath)); - for (const auto & [output, outputPathOpt] : outputMap) { - if (!outputs.contains(output)) continue; - if (outputPathOpt) - res["outputs"][output] = store.printStorePath(*outputPathOpt); - else - res["outputs"][output] = nullptr; - } - return res; +nlohmann::json DerivedPath::Built::toJSON(const StoreDirConfig & store) const +{ + return nlohmann::json{ + {"drvPath", drvPath->toJSON(store)}, + {"outputs", outputs}, + }; } -nlohmann::json SingleDerivedPath::toJSON(Store & store) const +nlohmann::json SingleDerivedPath::toJSON(const StoreDirConfig & store) const { return std::visit([&](const auto & buildable) { return buildable.toJSON(store); }, raw()); } -nlohmann::json DerivedPath::toJSON(Store & store) const +nlohmann::json DerivedPath::toJSON(const StoreDirConfig & store) const { return std::visit([&](const auto & buildable) { return buildable.toJSON(store); diff --git a/src/libstore/include/nix/store/derived-path.hh b/src/libstore/include/nix/store/derived-path.hh index 64189bd41..18a722ba4 100644 --- a/src/libstore/include/nix/store/derived-path.hh +++ b/src/libstore/include/nix/store/derived-path.hh @@ -77,7 +77,7 @@ struct SingleDerivedPathBuilt { const StoreDirConfig & store, ref drvPath, OutputNameView outputs, const ExperimentalFeatureSettings & xpSettings = experimentalFeatureSettings); - nlohmann::json toJSON(Store & store) const; + nlohmann::json toJSON(const StoreDirConfig & store) const; bool operator == (const SingleDerivedPathBuilt &) const noexcept; std::strong_ordering operator <=> (const SingleDerivedPathBuilt &) const noexcept; @@ -151,7 +151,7 @@ struct SingleDerivedPath : _SingleDerivedPathRaw { const StoreDirConfig & store, std::string_view, const ExperimentalFeatureSettings & xpSettings = experimentalFeatureSettings); - nlohmann::json toJSON(Store & store) const; + nlohmann::json toJSON(const StoreDirConfig & store) const; }; static inline ref makeConstantStorePathRef(StorePath drvPath) @@ -204,7 +204,7 @@ struct DerivedPathBuilt { const StoreDirConfig & store, ref, std::string_view, const ExperimentalFeatureSettings & xpSettings = experimentalFeatureSettings); - nlohmann::json toJSON(Store & store) const; + nlohmann::json toJSON(const StoreDirConfig & store) const; bool operator == (const DerivedPathBuilt &) const noexcept; // TODO libc++ 16 (used by darwin) missing `std::set::operator <=>`, can't do yet. @@ -285,7 +285,7 @@ struct DerivedPath : _DerivedPathRaw { */ static DerivedPath fromSingle(const SingleDerivedPath &); - nlohmann::json toJSON(Store & store) const; + nlohmann::json toJSON(const StoreDirConfig & store) const; }; typedef std::vector DerivedPaths; diff --git a/tests/functional/build-dry.sh b/tests/functional/build-dry.sh index cff0f9a49..7d09c7f45 100755 --- a/tests/functional/build-dry.sh +++ b/tests/functional/build-dry.sh @@ -58,14 +58,7 @@ clearCache RES=$(nix build -f dependencies.nix --dry-run --json) -if [[ -z "${NIX_TESTS_CA_BY_DEFAULT-}" ]]; then - echo "$RES" | jq '.[0] | [ - (.drvPath | test("'"$NIX_STORE_DIR"'.*\\.drv")), - (.outputs.out | test("'"$NIX_STORE_DIR"'")) - ] | all' -else - echo "$RES" | jq '.[0] | [ - (.drvPath | test("'"$NIX_STORE_DIR"'.*\\.drv")), - .outputs.out == null - ] | all' -fi +echo "$RES" | jq '.[0] | [ + (.drvPath | test("'"$NIX_STORE_DIR"'.*\\.drv")), + .outputs == [ "out" ] +] | all'