From 9760fa8661f7562e0b8979338200904053cc4631 Mon Sep 17 00:00:00 2001 From: Ben Burdette Date: Mon, 27 Dec 2021 17:35:27 -0700 Subject: [PATCH] add DebugTrace for the current error --- src/libcmd/command.cc | 2 +- src/libcmd/command.hh | 2 ++ src/libcmd/repl.cc | 10 ++++++++++ src/libexpr/eval.cc | 17 +---------------- src/libexpr/eval.hh | 18 ++++++++++++++++-- 5 files changed, 30 insertions(+), 19 deletions(-) diff --git a/src/libcmd/command.cc b/src/libcmd/command.cc index 6c0f84c4b..897d81981 100644 --- a/src/libcmd/command.cc +++ b/src/libcmd/command.cc @@ -84,7 +84,7 @@ ref EvalCommand::getEvalState() if (expr.staticenv) { auto vm = mapStaticEnvBindings(*expr.staticenv.get(), env); - runRepl(evalState, &error, *vm); + runRepl(evalState, &error, expr, *vm); } }; } diff --git a/src/libcmd/command.hh b/src/libcmd/command.hh index e2c72256e..8af9eae27 100644 --- a/src/libcmd/command.hh +++ b/src/libcmd/command.hh @@ -312,8 +312,10 @@ void printClosureDiff( const StorePath & afterPath, std::string_view indent); + void runRepl( ref evalState, const Error *debugError, + const Expr &expr, const std::map & extraEnv); } diff --git a/src/libcmd/repl.cc b/src/libcmd/repl.cc index 3948ede02..4a61d2be4 100644 --- a/src/libcmd/repl.cc +++ b/src/libcmd/repl.cc @@ -897,6 +897,7 @@ std::ostream & NixRepl::printValue(std::ostream & str, Value & v, unsigned int m void runRepl( ref evalState, const Error *debugError, + const Expr &expr, const std::map & extraEnv) { auto repl = std::make_unique(evalState); @@ -905,6 +906,15 @@ void runRepl( repl->initEnv(); + // tack on a final DebugTrace for the error position. + DebugTraceStacker ldts( + *evalState, + DebugTrace + {.pos = debugError->info().errPos, + .expr = expr, + .hint = debugError->info().msg + }); + // add 'extra' vars. std::set names; for (auto & [name, value] : extraEnv) { diff --git a/src/libexpr/eval.cc b/src/libexpr/eval.cc index f1e6cfdf2..4bdcc052f 100644 --- a/src/libexpr/eval.cc +++ b/src/libexpr/eval.cc @@ -38,21 +38,6 @@ namespace nix { std::function debuggerHook; -class DebugTraceStacker { - public: - DebugTraceStacker(EvalState &evalState, DebugTrace t) - :evalState(evalState), trace(t) - { - evalState.debugTraces.push_front(t); - } - ~DebugTraceStacker() { - // assert(evalState.debugTraces.front() == trace); - evalState.debugTraces.pop_front(); - } - EvalState &evalState; - DebugTrace trace; -}; - static char * dupString(const char * s) { char * t; @@ -701,7 +686,7 @@ void printStaticEnvBindings(const StaticEnv &se, int lvl) std::cout << std::endl; if (se.up) { - printStaticEnvBindings(*se.up, ++lvl); + printStaticEnvBindings(*se.up, ++lvl); } } diff --git a/src/libexpr/eval.hh b/src/libexpr/eval.hh index c7a19e100..2f8cc82b0 100644 --- a/src/libexpr/eval.hh +++ b/src/libexpr/eval.hh @@ -76,11 +76,10 @@ std::shared_ptr makeRegexCache(); struct DebugTrace { std::optional pos; - Expr &expr; + const Expr &expr; hintformat hint; }; - class EvalState { public: @@ -406,6 +405,21 @@ private: friend void prim_match(EvalState & state, const Pos & pos, Value * * args, Value & v); }; +class DebugTraceStacker { + public: + DebugTraceStacker(EvalState &evalState, DebugTrace t) + :evalState(evalState), trace(t) + { + evalState.debugTraces.push_front(t); + } + ~DebugTraceStacker() + { + // assert(evalState.debugTraces.front() == trace); + evalState.debugTraces.pop_front(); + } + EvalState &evalState; + DebugTrace trace; +}; /* Return a string representing the type of the value `v'. */ string showType(ValueType type);