Skip to content

Commit

Permalink
libexpr: show expression in assertion errors
Browse files Browse the repository at this point in the history
Includes the expression of the condition in the assertion message if
the assertion failed, making assertions much easier to debug. eg.

    error: assertion (withPython -> (python2Packages != null)) failed at pkgs/tools/security/nmap/default.nix:11:1
  • Loading branch information
LnL7 committed Jan 11, 2020
1 parent 6f046fa commit 307bcb9
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions src/libexpr/eval.cc
Expand Up @@ -531,9 +531,9 @@ LocalNoInlineNoReturn(void throwTypeError(const char * s, const ExprLambda & fun
throw TypeError(format(s) % fun.showNamePos() % s2 % pos);
}

LocalNoInlineNoReturn(void throwAssertionError(const char * s, const Pos & pos))
LocalNoInlineNoReturn(void throwAssertionError(const char * s, const string & s1, const Pos & pos))
{
throw AssertionError(format(s) % pos);
throw AssertionError(format(s) % s1 % pos);
}

LocalNoInlineNoReturn(void throwUndefinedVarError(const char * s, const string & s1, const Pos & pos))
Expand Down Expand Up @@ -1262,8 +1262,11 @@ void ExprIf::eval(EvalState & state, Env & env, Value & v)

void ExprAssert::eval(EvalState & state, Env & env, Value & v)
{
if (!state.evalBool(env, cond, pos))
throwAssertionError("assertion failed at %1%", pos);
if (!state.evalBool(env, cond, pos)) {
std::ostringstream out;
cond->show(out);
throwAssertionError("assertion %1% failed at %2%", out.str(), pos);
}
body->eval(state, env, v);
}

Expand Down

0 comments on commit 307bcb9

Please sign in to comment.