From 047890b4a452c104e7928cf0df428d0255bf23ef Mon Sep 17 00:00:00 2001 From: Farid Zakaria Date: Sat, 13 Jul 2024 20:29:04 -0700 Subject: [PATCH] fix: path-info can return 1 when used with json `nix path-info` would always return 0 previously even in the case of invalid paths. This change makes `nix path-info` return 1 when atore paths do not exist and need to be built which when used with `--json`. This matches the non-json equivalent. fixes #10886 --- src/nix/path-info.cc | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/nix/path-info.cc b/src/nix/path-info.cc index 47f9baee5..2f4301380 100644 --- a/src/nix/path-info.cc +++ b/src/nix/path-info.cc @@ -3,6 +3,7 @@ #include "store-api.hh" #include "common-args.hh" #include "nar-info.hh" +#include "exit.hh" #include #include @@ -34,12 +35,13 @@ static uint64_t getStoreObjectsTotalSize(Store & store, const StorePathSet & clo * @param showClosureSize If true, the closure size of each path is * included. */ -static json pathInfoToJSON( +static std::pair pathInfoToJSON( Store & store, const StorePathSet & storePaths, bool showClosureSize) { json::object_t jsonAllObjects = json::object(); + int resultCode = 0; for (auto & storePath : storePaths) { json jsonObject; @@ -78,11 +80,12 @@ static json pathInfoToJSON( } catch (InvalidPath &) { jsonObject = nullptr; + resultCode = 1; } jsonAllObjects[printedStorePath] = std::move(jsonObject); } - return jsonAllObjects; + return std::make_pair(jsonAllObjects, resultCode); } @@ -152,11 +155,15 @@ struct CmdPathInfo : StorePathsCommand, MixJSON pathLen = std::max(pathLen, store->printStorePath(storePath).size()); if (json) { - std::cout << pathInfoToJSON( + auto pair = pathInfoToJSON( *store, // FIXME: preserve order? StorePathSet(storePaths.begin(), storePaths.end()), - showClosureSize).dump(); + showClosureSize); + auto json = pair.first; + auto resultCode = pair.second; + std::cout << json.dump(); + throw Exit(resultCode); } else {