From 6192cbebacae5944fdd3cc58438f0697cc1993f9 Mon Sep 17 00:00:00 2001 From: Qyriad Date: Wed, 19 Nov 2025 12:28:50 +0100 Subject: [PATCH] libexpr: refactor fallibly doing stuff on debug traces (NFC) Change-Id: I009ce2ea424938507223fc6b3cf3b1236a6a6964 --- lix/libexpr/eval.cc | 9 +++++++++ lix/libexpr/eval.hh | 3 +++ lix/libexpr/primops.cc | 13 +++++++------ lix/libexpr/settings/debugger-on-trace.md | 2 +- 4 files changed, 20 insertions(+), 7 deletions(-) diff --git a/lix/libexpr/eval.cc b/lix/libexpr/eval.cc index d532d5da9..7f12867c9 100644 --- a/lix/libexpr/eval.cc +++ b/lix/libexpr/eval.cc @@ -2924,6 +2924,15 @@ Expr & Evaluator::parseStdin() ); } +std::optional Evaluator::nextDebugTrace() const +{ + if (!debug) { + return std::nullopt; + } + + return debug->traces().next(); +} + kj::Promise>> EvalPaths::findFile(const std::string_view path) diff --git a/lix/libexpr/eval.hh b/lix/libexpr/eval.hh index 22fa12723..942d322b3 100644 --- a/lix/libexpr/eval.hh +++ b/lix/libexpr/eval.hh @@ -568,6 +568,9 @@ public: */ void evalLazily(Expr & e, Value & v); + /** If debugging is enabled, returns the next trace. Otherwise, std::nullopt. */ + std::optional nextDebugTrace() const; + private: Expr * parse( char * text, diff --git a/lix/libexpr/primops.cc b/lix/libexpr/primops.cc index 3f73b370c..7340c2347 100644 --- a/lix/libexpr/primops.cc +++ b/lix/libexpr/primops.cc @@ -629,7 +629,7 @@ static void prim_genericClosure(EvalState & state, Value * * args, Value & v) static void prim_break(EvalState & state, Value * * args, Value & v) { - if (auto trace = state.ctx.debug ? state.ctx.debug->traces().next() : std::nullopt) { + if (auto const trace = state.ctx.nextDebugTrace()) { auto error = EvalError(ErrorInfo { .level = lvlInfo, .msg = HintFmt("breakpoint reached"), @@ -758,12 +758,13 @@ static void prim_trace(EvalState & state, Value * * args, Value & v) printError("trace: %1%", Uncolored(args[0]->str())); else printError("trace: %1%", Uncolored(ValuePrinter(state, *args[0]))); - if (auto last = evalSettings.builtinsTraceDebugger && state.ctx.debug - ? state.ctx.debug->traces().next() - : std::nullopt) - { - state.ctx.debug->onEvalError(nullptr, (*last)->env, (*last)->expr); + + if (evalSettings.debuggerOnTrace) { + if (auto const trace = state.ctx.nextDebugTrace()) { + state.ctx.debug->onEvalError(nullptr, (*trace)->env, (*trace)->expr); + } } + state.forceValue(*args[1], noPos); v = *args[1]; } diff --git a/lix/libexpr/settings/debugger-on-trace.md b/lix/libexpr/settings/debugger-on-trace.md index afecabfec..ba80d5b5f 100644 --- a/lix/libexpr/settings/debugger-on-trace.md +++ b/lix/libexpr/settings/debugger-on-trace.md @@ -1,6 +1,6 @@ --- name: debugger-on-trace -internalName: builtinsTraceDebugger +internalName: debuggerOnTrace type: bool default: false ---