Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 22 additions & 10 deletions src/libexpr/primops.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1022,20 +1022,32 @@ static RegisterPrimOp primop_throw(

static void prim_addErrorContext(EvalState & state, const PosIdx pos, Value ** args, Value & v)
{
std::exception_ptr error;
try {
state.forceValue(*args[1], pos);
v = *args[1];
return;
} catch (Error &) {
error = std::current_exception();
}

/* Evaluate the message outside of the catch handler: it may force
thunks and thereby suspend the current fiber, which is not
allowed while an exception is being handled. */
NixStringContext context;
auto message = state
.coerceToString(
pos,
*args[0],
context,
"while evaluating the error message passed to builtins.addErrorContext",
false,
false)
.toOwned();

try {
std::rethrow_exception(error);
} catch (Error & e) {
NixStringContext context;
auto message = state
.coerceToString(
pos,
*args[0],
context,
"while evaluating the error message passed to builtins.addErrorContext",
false,
false)
.toOwned();
e.addTrace(nullptr, HintFmt(message), TracePrint::Always);
throw;
}
Expand Down
1 change: 1 addition & 0 deletions tests/functional/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ suites = [
'optimise-store.sh',
'otel.sh',
'output-normalization.sh',
'parallel-eval.sh',
'pass-as-file.sh',
'path-from-hash-part.sh',
'path-info.sh',
Expand Down
22 changes: 22 additions & 0 deletions tests/functional/parallel-eval.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/usr/bin/env bash

# Tests for the multi-threaded evaluator (`eval-cores`).

source common.sh

# `builtins.addErrorContext` evaluates its message after the body has
# thrown. If the message depends on a thunk that another fiber is
# evaluating, the current fiber has to wait for it. That must not
# happen inside the exception handler (which the fiber cannot suspend
# in), so the message is evaluated after leaving the handler.
cat > "$TEST_ROOT/add-error-context.nix" <<'NIX'
let
big = builtins.foldl' (a: b: a + b) 0 (builtins.genList (x: x) 3000000);
in
{
a = big;
b = builtins.addErrorContext "context mentioning ${toString big}" (throw "boom");
}
NIX

expectStderr 1 nix eval --eval-cores 4 --json --file "$TEST_ROOT/add-error-context.nix" | grepQuiet "context mentioning 4499998500000"
Loading