nix search: Ignore top-level eval errors

$NIX_PATH may contain elements that don't evaluate to an attrset (like
"nixos-config"), so ignore those.
This commit is contained in:
Eelco Dolstra 2017-07-19 16:06:10 +02:00
parent fc3568e263
commit 57a30e101b
No known key found for this signature in database
GPG key ID: 8170B4726D7198DE
2 changed files with 13 additions and 2 deletions

View file

@ -16,6 +16,8 @@ Value * SourceExprCommand::getSourceExpr(EvalState & state)
{ {
if (vSourceExpr) return vSourceExpr; if (vSourceExpr) return vSourceExpr;
auto sToplevel = state.symbols.create("_toplevel");
vSourceExpr = state.allocValue(); vSourceExpr = state.allocValue();
if (file != "") { if (file != "") {
@ -29,7 +31,9 @@ Value * SourceExprCommand::getSourceExpr(EvalState & state)
auto searchPath = state.getSearchPath(); auto searchPath = state.getSearchPath();
state.mkAttrs(*vSourceExpr, searchPath.size()); state.mkAttrs(*vSourceExpr, searchPath.size() + 1);
mkBool(*state.allocAttr(*vSourceExpr, sToplevel), true);
std::unordered_set<std::string> seen; std::unordered_set<std::string> seen;

View file

@ -54,6 +54,8 @@ struct CmdSearch : SourceExprCommand, MixJSON
auto jsonOut = json ? std::make_unique<JSONObject>(std::cout, true) : nullptr; auto jsonOut = json ? std::make_unique<JSONObject>(std::cout, true) : nullptr;
auto sToplevel = state->symbols.create("_toplevel");
doExpr = [&](Value * v, std::string attrPath, bool toplevel) { doExpr = [&](Value * v, std::string attrPath, bool toplevel) {
debug("at attribute %s", attrPath); debug("at attribute %s", attrPath);
@ -123,7 +125,7 @@ struct CmdSearch : SourceExprCommand, MixJSON
if (j == attrs->end() || !state->forceBool(*j->value, *j->pos)) return; if (j == attrs->end() || !state->forceBool(*j->value, *j->pos)) return;
} }
Bindings::iterator j = v->attrs->find(state->symbols.create("_toplevel")); Bindings::iterator j = v->attrs->find(sToplevel);
bool toplevel2 = j != v->attrs->end() && state->forceBool(*j->value, *j->pos); bool toplevel2 = j != v->attrs->end() && state->forceBool(*j->value, *j->pos);
for (auto & i : *v->attrs) { for (auto & i : *v->attrs) {
@ -134,6 +136,11 @@ struct CmdSearch : SourceExprCommand, MixJSON
} }
} catch (AssertionError & e) { } catch (AssertionError & e) {
} catch (Error & e) {
if (!toplevel) {
e.addPrefix(fmt("While evaluating the attribute %s:\n", attrPath));
throw;
}
} }
}; };