1
0
Fork 0
mirror of https://github.com/NixOS/nix synced 2024-09-19 10:50:24 -04:00

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
This commit is contained in:
Farid Zakaria 2024-07-13 20:29:04 -07:00
parent db3e99d9d4
commit 047890b4a4

View file

@ -3,6 +3,7 @@
#include "store-api.hh" #include "store-api.hh"
#include "common-args.hh" #include "common-args.hh"
#include "nar-info.hh" #include "nar-info.hh"
#include "exit.hh"
#include <algorithm> #include <algorithm>
#include <array> #include <array>
@ -34,12 +35,13 @@ static uint64_t getStoreObjectsTotalSize(Store & store, const StorePathSet & clo
* @param showClosureSize If true, the closure size of each path is * @param showClosureSize If true, the closure size of each path is
* included. * included.
*/ */
static json pathInfoToJSON( static std::pair<json, int> pathInfoToJSON(
Store & store, Store & store,
const StorePathSet & storePaths, const StorePathSet & storePaths,
bool showClosureSize) bool showClosureSize)
{ {
json::object_t jsonAllObjects = json::object(); json::object_t jsonAllObjects = json::object();
int resultCode = 0;
for (auto & storePath : storePaths) { for (auto & storePath : storePaths) {
json jsonObject; json jsonObject;
@ -78,11 +80,12 @@ static json pathInfoToJSON(
} catch (InvalidPath &) { } catch (InvalidPath &) {
jsonObject = nullptr; jsonObject = nullptr;
resultCode = 1;
} }
jsonAllObjects[printedStorePath] = std::move(jsonObject); 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()); pathLen = std::max(pathLen, store->printStorePath(storePath).size());
if (json) { if (json) {
std::cout << pathInfoToJSON( auto pair = pathInfoToJSON(
*store, *store,
// FIXME: preserve order? // FIXME: preserve order?
StorePathSet(storePaths.begin(), storePaths.end()), StorePathSet(storePaths.begin(), storePaths.end()),
showClosureSize).dump(); showClosureSize);
auto json = pair.first;
auto resultCode = pair.second;
std::cout << json.dump();
throw Exit(resultCode);
} }
else { else {