Skip to content

Commit

Permalink
Make 'nix search --json' thread-safe
Browse files Browse the repository at this point in the history
  • Loading branch information
edolstra committed Aug 15, 2024
1 parent ceeb648 commit 8b7d5b4
Showing 1 changed file with 4 additions and 5 deletions.
9 changes: 4 additions & 5 deletions src/nix/search.cc
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ struct CmdSearch : InstallableValueCommand, MixJSON

auto state = getEvalState();

std::optional<nlohmann::json> jsonOut;
if (json) jsonOut = json::object();
std::optional<Sync<nlohmann::json>> jsonOut;
if (json) jsonOut.emplace(json::object());

std::atomic<uint64_t> results = 0;

Expand Down Expand Up @@ -169,9 +169,8 @@ struct CmdSearch : InstallableValueCommand, MixJSON
if (found)
{
results++;
// FIXME: locking
if (json) {
(*jsonOut)[attrPath2] = {
(*jsonOut->lock())[attrPath2] = {
{"pname", name.name},
{"version", name.version},
{"description", description},
Expand Down Expand Up @@ -224,7 +223,7 @@ struct CmdSearch : InstallableValueCommand, MixJSON
futures.finishAll();

if (json)
logger->cout("%s", *jsonOut);
logger->cout("%s", *(jsonOut->lock()));

if (!json && !results)
throw Error("no results for the given search term(s)!");
Expand Down

0 comments on commit 8b7d5b4

Please sign in to comment.