aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/libcmd/command.cc58
-rw-r--r--src/libcmd/command.hh8
-rw-r--r--src/libcmd/local.mk8
-rw-r--r--src/libcmd/repl.cc (renamed from src/nix/repl.cc)225
-rw-r--r--src/libexpr/eval-cache.cc19
-rw-r--r--src/libexpr/eval-inline.hh28
-rw-r--r--src/libexpr/eval.cc433
-rw-r--r--src/libexpr/eval.hh38
-rw-r--r--src/libexpr/nixexpr.cc141
-rw-r--r--src/libexpr/nixexpr.hh39
-rw-r--r--src/libexpr/parser.y12
-rw-r--r--src/libexpr/primops.cc211
-rw-r--r--src/libexpr/primops/fetchTree.cc34
-rw-r--r--src/libexpr/value-to-json.cc4
-rw-r--r--src/libutil/error.hh9
15 files changed, 987 insertions, 280 deletions
diff --git a/src/libcmd/command.cc b/src/libcmd/command.cc
index a53b029b7..5cb8728e9 100644
--- a/src/libcmd/command.cc
+++ b/src/libcmd/command.cc
@@ -86,6 +86,11 @@ ref<Store> CopyCommand::getDstStore()
EvalCommand::EvalCommand()
{
+ addFlag({
+ .longName = "debugger",
+ .description = "start an interactive environment if evaluation fails",
+ .handler = {&startReplOnEvalErrors, true},
+ });
}
EvalCommand::~EvalCommand()
@@ -103,7 +108,7 @@ ref<Store> EvalCommand::getEvalStore()
ref<EvalState> EvalCommand::getEvalState()
{
- if (!evalState)
+ if (!evalState) {
evalState =
#if HAVE_BOEHMGC
std::allocate_shared<EvalState>(traceable_allocator<EvalState>(),
@@ -113,6 +118,57 @@ ref<EvalState> EvalCommand::getEvalState()
searchPath, getEvalStore(), getStore())
#endif
;
+ if (startReplOnEvalErrors)
+ debuggerHook = [evalState{ref<EvalState>(evalState)}](const Error * error, const Env & env, const Expr & expr) {
+ // clear the screen.
+ // std::cout << "\033[2J\033[1;1H";
+
+ auto dts =
+ error && expr.getPos() ?
+ std::unique_ptr<DebugTraceStacker>(
+ new DebugTraceStacker(
+ *evalState,
+ DebugTrace
+ {.pos = (error->info().errPos ? *error->info().errPos : *expr.getPos()),
+ .expr = expr,
+ .env = env,
+ .hint = error->info().msg,
+ .is_error = true
+ }))
+ : nullptr;
+
+
+ if (error)
+ printError("%s\n\n" ANSI_BOLD "Starting REPL to allow you to inspect the current state of the evaluator.\n" ANSI_NORMAL, error->what());
+
+ // else
+ // {
+ // auto iter = evalState->debugTraces.begin();
+ // if (iter != evalState->debugTraces.end()) {
+ // std::cout << "\n" << "… " << iter->hint.str() << "\n";
+
+ // if (iter->pos.has_value() && (*iter->pos)) {
+ // auto pos = iter->pos.value();
+ // std::cout << "\n";
+ // printAtPos(pos, std::cout);
+
+ // auto loc = getCodeLines(pos);
+ // if (loc.has_value()) {
+ // std::cout << "\n";
+ // printCodeLines(std::cout, "", pos, *loc);
+ // std::cout << "\n";
+ // }
+ // }
+ // }
+ // }
+
+ if (expr.staticenv)
+ {
+ std::unique_ptr<valmap> vm(mapStaticEnvBindings(*expr.staticenv.get(), env));
+ runRepl(evalState, error, expr, *vm);
+ }
+ };
+ }
return ref<EvalState>(evalState);
}
diff --git a/src/libcmd/command.hh b/src/libcmd/command.hh
index 0f6125f11..94ad80210 100644
--- a/src/libcmd/command.hh
+++ b/src/libcmd/command.hh
@@ -57,6 +57,8 @@ struct CopyCommand : virtual StoreCommand
struct EvalCommand : virtual StoreCommand, MixEvalArgs
{
+ bool startReplOnEvalErrors = false;
+
EvalCommand();
~EvalCommand();
@@ -269,4 +271,10 @@ void printClosureDiff(
const StorePath & afterPath,
std::string_view indent);
+
+void runRepl(
+ ref<EvalState> evalState,
+ const Error *debugError,
+ const Expr &expr,
+ const std::map<std::string, Value *> & extraEnv);
}
diff --git a/src/libcmd/local.mk b/src/libcmd/local.mk
index 7a2f83cc7..a12837ce5 100644
--- a/src/libcmd/local.mk
+++ b/src/libcmd/local.mk
@@ -6,10 +6,12 @@ libcmd_DIR := $(d)
libcmd_SOURCES := $(wildcard $(d)/*.cc)
-libcmd_CXXFLAGS += -I src/libutil -I src/libstore -I src/libexpr -I src/libmain -I src/libfetchers
+libcmd_CXXFLAGS += -I src/libutil -I src/libstore -I src/libexpr -I src/libmain -I src/libfetchers -I src/nix
-libcmd_LDFLAGS += $(LOWDOWN_LIBS) -pthread
+# libcmd_LDFLAGS += -llowdown -pthread
+libcmd_LDFLAGS = $(EDITLINE_LIBS) -llowdown -pthread
+# libcmd_LDFLAGS += $(LOWDOWN_LIBS) -pthread
-libcmd_LIBS = libstore libutil libexpr libmain libfetchers
+libcmd_LIBS = libstore libutil libexpr libmain libfetchers libnix
$(eval $(call install-file-in, $(d)/nix-cmd.pc, $(libdir)/pkgconfig, 0644))
diff --git a/src/nix/repl.cc b/src/libcmd/repl.cc
index 1f9d4fb4e..3dd55e104 100644
--- a/src/nix/repl.cc
+++ b/src/libcmd/repl.cc
@@ -47,20 +47,23 @@ struct NixRepl
#endif
{
std::string curDir;
- std::unique_ptr<EvalState> state;
+ ref<EvalState> state;
Bindings * autoArgs;
+ const Error *debugError;
+ int debugTraceIndex;
+
Strings loadedFiles;
const static int envSize = 32768;
- StaticEnv staticEnv;
+ std::shared_ptr<StaticEnv> staticEnv;
Env * env;
int displ;
StringSet varNames;
const Path historyFile;
- NixRepl(const Strings & searchPath, nix::ref<Store> store);
+ NixRepl(ref<EvalState> state);
~NixRepl();
void mainLoop(const std::vector<std::string> & files);
StringSet completePrefix(const std::string & prefix);
@@ -70,15 +73,17 @@ struct NixRepl
void loadFile(const Path & path);
void loadFlake(const std::string & flakeRef);
void initEnv();
+ void loadFiles();
void reloadFiles();
void addAttrsToScope(Value & attrs);
void addVarToScope(const Symbol & name, Value & v);
Expr * parseString(std::string s);
void evalString(std::string s, Value & v);
+ void loadDebugTraceEnv(DebugTrace &dt);
typedef std::set<Value *> ValuesSeen;
- std::ostream & printValue(std::ostream & str, Value & v, unsigned int maxDepth);
- std::ostream & printValue(std::ostream & str, Value & v, unsigned int maxDepth, ValuesSeen & seen);
+ std::ostream & printValue(std::ostream & str, Value & v, unsigned int maxDepth);
+ std::ostream & printValue(std::ostream & str, Value & v, unsigned int maxDepth, ValuesSeen & seen);
};
@@ -91,9 +96,10 @@ std::string removeWhitespace(std::string s)
}
-NixRepl::NixRepl(const Strings & searchPath, nix::ref<Store> store)
- : state(std::make_unique<EvalState>(searchPath, store))
- , staticEnv(false, &state->staticBaseEnv)
+NixRepl::NixRepl(ref<EvalState> state)
+ : state(state)
+ , debugTraceIndex(0)
+ , staticEnv(new StaticEnv(false, state->staticBaseEnv.get()))
, historyFile(getDataDir() + "/nix/repl-history")
{
curDir = absPath(".");
@@ -197,15 +203,41 @@ namespace {
}
}
+std::ostream& showDebugTrace(std::ostream &out, const DebugTrace &dt)
+{
+ if (dt.is_error)
+ out << ANSI_RED "error: " << ANSI_NORMAL;
+ out << dt.hint.str() << "\n";
+
+ // prefer direct pos, but if noPos then try the expr.
+ auto pos = (*dt.pos ? *dt.pos :
+ (dt.expr.getPos() ? *dt.expr.getPos() : noPos));
+
+ if (pos) {
+ printAtPos(pos, out);
+
+ auto loc = getCodeLines(pos);
+ if (loc.has_value()) {
+ out << "\n";
+ printCodeLines(out, "", pos, *loc);
+ out << "\n";
+ }
+ }
+
+ return out;
+}
+
void NixRepl::mainLoop(const std::vector<std::string> & files)
{
std::string error = ANSI_RED "error:" ANSI_NORMAL " ";
notice("Welcome to Nix " + nixVersion + ". Type :? for help.\n");
- for (auto & i : files)
- loadedFiles.push_back(i);
+ if (!files.empty()) {
+ for (auto & i : files)
+ loadedFiles.push_back(i);
+ }
- reloadFiles();
+ loadFiles();
if (!loadedFiles.empty()) notice("");
// Allow nix-repl specific settings in .inputrc
@@ -227,8 +259,12 @@ void NixRepl::mainLoop(const std::vector<std::string> & files)
// When continuing input from previous lines, don't print a prompt, just align to the same
// number of chars as the prompt.
if (!getLine(input, input.empty() ? "nix-repl> " : " "))
+ {
+ // ctrl-D should exit the debugger.
+ state->debugStop = false;
+ state->debugQuit = true;
break;
-
+ }
try {
if (!removeWhitespace(input).empty() && !processLine(input)) return;
} catch (ParseError & e) {
@@ -239,6 +275,14 @@ void NixRepl::mainLoop(const std::vector<std::string> & files)
} else {
printMsg(lvlError, e.msg());
}
+ } catch (EvalError & e) {
+ // in debugger mode, an EvalError should trigger another repl session.
+ // when that session returns the exception will land here. No need to show it again;
+ // show the error for this repl session instead.
+ if (debuggerHook && !this->state->debugTraces.empty())
+ showDebugTrace(std::cout, this->state->debugTraces.front());
+ else
+ printMsg(lvlError, e.msg());
} catch (Error & e) {
printMsg(lvlError, e.msg());
} catch (Interrupted & e) {
@@ -393,6 +437,24 @@ StorePath NixRepl::getDerivationPath(Value & v) {
return *drvPath;
}
+void NixRepl::loadDebugTraceEnv(DebugTrace &dt)
+{
+ if (dt.expr.staticenv)
+ {
+ initEnv();
+
+ auto vm = std::make_unique<valmap>(*(mapStaticEnvBindings(*dt.expr.staticenv.get(), dt.env)));
+
+ // add staticenv vars.
+ for (auto & [name, value] : *(vm.get())) {
+ this->addVarToScope(this->state->symbols.create(name), *value);
+ }
+ }
+ else
+ {
+ initEnv();
+ }
+}
bool NixRepl::processLine(std::string line)
{
@@ -432,7 +494,84 @@ bool NixRepl::processLine(std::string line)
<< " :u <expr> Build derivation, then start nix-shell\n"
<< " :doc <expr> Show documentation of a builtin function\n"
<< " :log <expr> Show logs for a derivation\n"
- << " :st [bool] Enable, disable or toggle showing traces for errors\n";
+ << " :st [bool] Enable, disable or toggle showing traces for errors\n"
+ << " :d <cmd> Debug mode commands\n"
+ << " :d stack Show call stack\n"
+ << " :d env Show env stack\n"
+ << " :d show <idx> Show current trace, or change to call stack index\n"
+ << " :d go Go until end of program, exception, or builtins.break().\n"
+ << " :d step Go one step\n"
+ ;
+
+ }
+
+ else if (command == ":d" || command == ":debug") {
+ if (arg == "stack") {
+ int idx = 0;
+ for (auto iter = this->state->debugTraces.begin();
+ iter != this->state->debugTraces.end();
+ ++iter, ++idx) {
+ std::cout << "\n" << ANSI_BLUE << idx << ANSI_NORMAL << ": ";
+ showDebugTrace(std::cout, *iter);
+ }
+ } else if (arg == "env") {
+ int idx = 0;
+ for (auto iter = this->state->debugTraces.begin();
+ iter != this->state->debugTraces.end();
+ ++iter, ++idx) {
+ if (idx == this->debugTraceIndex)
+ {
+ printEnvBindings(iter->expr, iter->env);
+ break;
+ }
+ }
+ }
+ else if (arg.compare(0,4,"show") == 0) {
+ try {
+ // change the DebugTrace index.
+ debugTraceIndex = stoi(arg.substr(4));
+ }
+ catch (...)
+ {
+ }
+
+ int idx = 0;
+ for (auto iter = this->state->debugTraces.begin();
+ iter != this->state->debugTraces.end();
+ ++iter, ++idx) {
+ if (idx == this->debugTraceIndex)
+ {
+ std::cout << "\n" << ANSI_BLUE << idx << ANSI_NORMAL << ": ";
+ showDebugTrace(std::cout, *iter);
+ std::cout << std::endl;
+ printEnvBindings(iter->expr, iter->env);
+ loadDebugTraceEnv(*iter);
+ break;
+ }
+ // std::cout << "\n" << ANSI_BLUE << idx << ANSI_NORMAL << ": ";
+ // showDebugTrace(std::cout, *iter);
+ }
+ }
+ else if (arg == "error") {
+ // TODO: remove, along with debugError.
+ if (this->debugError) {
+ showErrorInfo(std::cout, debugError->info(), true);
+ }
+ else
+ {
+ notice("error information not available");
+ }
+ }
+ else if (arg == "step") {
+ // set flag to stop at next DebugTrace; exit repl.
+ state->debugStop = true;
+ return false;
+ }
+ else if (arg == "go") {
+ // set flag to run to next breakpoint or end of program; exit repl.
+ state->debugStop = false;
+ return false;
+ }
}
else if (command == ":a" || command == ":add") {
@@ -555,8 +694,11 @@ bool NixRepl::processLine(std::string line)
printValue(std::cout, v, 1000000000) << std::endl;
}
- else if (command == ":q" || command == ":quit")
+ else if (command == ":q" || command == ":quit") {
+ state->debugStop = false;
+ state->debugQuit = true;
return false;
+ }
else if (command == ":doc") {
Value v;
@@ -657,10 +799,10 @@ void NixRepl::initEnv()
env = &state->allocEnv(envSize);
env->up = &state->baseEnv;
displ = 0;
- staticEnv.vars.clear();
+ staticEnv->vars.clear();
varNames.clear();
- for (auto & i : state->staticBaseEnv.vars)
+ for (auto & i : state->staticBaseEnv->vars)
varNames.insert(i.first);
}
@@ -669,6 +811,12 @@ void NixRepl::reloadFiles()
{
initEnv();
+ loadFiles();
+}
+
+
+void NixRepl::loadFiles()
+{
Strings old = loadedFiles;
loadedFiles.clear();
@@ -689,12 +837,12 @@ void NixRepl::addAttrsToScope(Value & attrs)
throw Error("environment full; cannot add more variables");
for (auto & i : *attrs.attrs) {
- staticEnv.vars.emplace_back(i.name, displ);
+ staticEnv->vars.emplace_back(i.name, displ);
env->values[displ++] = i.value;
varNames.insert((std::string) i.name);
}
- staticEnv.sort();
- staticEnv.deduplicate();
+ staticEnv->sort();
+ staticEnv->deduplicate();
notice("Added %1% variables.", attrs.attrs->size());
}
@@ -703,10 +851,10 @@ void NixRepl::addVarToScope(const Symbol & name, Value & v)
{
if (displ >= envSize)
throw Error("environment full; cannot add more variables");
- if (auto oldVar = staticEnv.find(name); oldVar != staticEnv.vars.end())
- staticEnv.vars.erase(oldVar);
- staticEnv.vars.emplace_back(name, displ);
- staticEnv.sort();
+ if (auto oldVar = staticEnv->find(name); oldVar != staticEnv->vars.end())
+ staticEnv->vars.erase(oldVar);
+ staticEnv->vars.emplace_back(name, displ);
+ staticEnv->sort();
env->values[displ++] = &v;
varNames.insert((std::string) name);
}
@@ -874,6 +1022,31 @@ std::ostream & NixRepl::printValue(std::ostream & str, Value & v, unsigned int m
return str;
}
+void runRepl(
+ ref<EvalState> evalState,
+ const Error *debugError,
+ const Expr &expr,
+ const std::map<std::string, Value *> & extraEnv)
+{
+ auto repl = std::make_unique<NixRepl>(evalState);
+
+ repl->debugError = debugError;
+
+ repl->initEnv();
+
+ // add 'extra' vars.
+ // std::set<std::string> names;
+ for (auto & [name, value] : extraEnv) {
+ // names.insert(ANSI_BOLD + name + ANSI_NORMAL);
+ // names.insert(name);
+ repl->addVarToScope(repl->state->symbols.create(name), *value);
+ }
+
+ // printError(hintfmt("The following extra variables are in scope: %s\n", concatStringsSep(", ", names)).str());
+
+ repl->mainLoop({});
+}
+
struct CmdRepl : StoreCommand, MixEvalArgs
{
std::vector<std::string> files;
@@ -902,8 +1075,12 @@ struct CmdRepl : StoreCommand, MixEvalArgs
void run(ref<Store> store) override
{
evalSettings.pureEval = false;
- auto repl = std::make_unique<NixRepl>(searchPath, openStore());
+
+ auto evalState = make_ref<EvalState>(searchPath, store);
+
+ auto repl = std::make_unique<NixRepl>(evalState);
repl->autoArgs = getAutoArgs(*repl->state);
+ repl->initEnv();
repl->mainLoop(files);
}
};
diff --git a/src/libexpr/eval-cache.cc b/src/libexpr/eval-cache.cc
index 54fa9b741..9f6152561 100644
--- a/src/libexpr/eval-cache.cc
+++ b/src/libexpr/eval-cache.cc
@@ -528,14 +528,14 @@ std::string AttrCursor::getString()
debug("using cached string attribute '%s'", getAttrPathStr());
return s->first;
} else
- throw TypeError("'%s' is not a string", getAttrPathStr());
+ root->state.debug_throw(TypeError("'%s' is not a string", getAttrPathStr()));
}
}
auto & v = forceValue();
if (v.type() != nString && v.type() != nPath)
- throw TypeError("'%s' is not a string but %s", getAttrPathStr(), showType(v.type()));
+ root->state.debug_throw(TypeError("'%s' is not a string but %s", getAttrPathStr(), showType(v.type())));
return v.type() == nString ? v.string.s : v.path;
}
@@ -559,7 +559,7 @@ string_t AttrCursor::getStringWithContext()
return *s;
}
} else
- throw TypeError("'%s' is not a string", getAttrPathStr());
+ root->state.debug_throw(TypeError("'%s' is not a string", getAttrPathStr()));
}
}
@@ -570,7 +570,10 @@ string_t AttrCursor::getStringWithContext()
else if (v.type() == nPath)
return {v.path, {}};
else
- throw TypeError("'%s' is not a string but %s", getAttrPathStr(), showType(v.type()));
+ {
+ root->state.debug_throw(TypeError("'%s' is not a string but %s", getAttrPathStr(), showType(v.type())));
+ return {v.path, {}}; // should never execute
+ }
}
bool AttrCursor::getBool()
@@ -583,14 +586,14 @@ bool AttrCursor::getBool()
debug("using cached Boolean attribute '%s'", getAttrPathStr());
return *b;
} else
- throw TypeError("'%s' is not a Boolean", getAttrPathStr());
+ root->state.debug_throw(TypeError("'%s' is not a Boolean", getAttrPathStr()));
}
}
auto & v = forceValue();
if (v.type() != nBool)
- throw TypeError("'%s' is not a Boolean", getAttrPathStr());
+ root->state.debug_throw(TypeError("'%s' is not a Boolean", getAttrPathStr()));
return v.boolean;
}
@@ -605,14 +608,14 @@ std::vector<Symbol> AttrCursor::getAttrs()
debug("using cached attrset attribute '%s'", getAttrPathStr());
return *attrs;
} else
- throw TypeError("'%s' is not an attribute set", getAttrPathStr());
+ root->state.debug_throw(TypeError("'%s' is not an attribute set", getAttrPathStr()));
}
}
auto & v = forceValue();
if (v.type() != nAttrs)
- throw TypeError("'%s' is not an attribute set", getAttrPathStr());
+ root->state.debug_throw(TypeError("'%s' is not an attribute set", getAttrPathStr()));
std::vector<Symbol> attrs;
for (auto & attr : *getValue().attrs)
diff --git a/src/libexpr/eval-inline.hh b/src/libexpr/eval-inline.hh
index 08a419923..9b0073822 100644
--- a/src/libexpr/eval-inline.hh
+++ b/src/libexpr/eval-inline.hh
@@ -7,20 +7,34 @@
namespace nix {
-LocalNoInlineNoReturn(void throwEvalError(const Pos & pos, const char * s))
+LocalNoInlineNoReturn(void throwEvalError(const Pos & pos, const char * s, EvalState &evalState))
{
- throw EvalError({
+ auto error = EvalError({
.msg = hintfmt(s),
.errPos = pos
});
+
+ if (debuggerHook && !evalState.debugTraces.empty()) {
+ DebugTrace &last = evalState.debugTraces.front();
+ debuggerHook(&error, last.env, last.expr);
+ }
+
+ throw error;
}
-LocalNoInlineNoReturn(void throwTypeError(const Pos & pos, const char * s, const Value & v))
+LocalNoInlineNoReturn(void throwTypeError(const Pos & pos, const char * s, const Value & v, EvalState &evalState))
{
- throw TypeError({
+ auto error = TypeError({
.msg = hintfmt(s, showType(v)),
.errPos = pos
});
+
+ if (debuggerHook && !evalState.debugTraces.empty()) {
+ DebugTrace &last = evalState.debugTraces.front();
+ debuggerHook(&error, last.env, last.expr);
+ }
+
+ throw error;
}
@@ -123,7 +137,7 @@ void EvalState::forceValue(Value & v, Callable getPos)
else if (v.isApp())
callFunction(*v.app.left, *v.app.right, v, noPos);
else if (v.isBlackhole())
- throwEvalError(getPos(), "infinite recursion encountered");
+ throwEvalError(getPos(), "infinite recursion encountered", *this);
}
@@ -140,7 +154,7 @@ inline void EvalState::forceAttrs(Value & v, Callable getPos)
{
forceValue(v, getPos);
if (v.type() != nAttrs)
- throwTypeError(getPos(), "value is %1% while a set was expected", v);
+ throwTypeError(getPos(), "value is %1% while a set was expected", v, *this);
}
@@ -149,7 +163,7 @@ inline void EvalState::forceList(Value & v, const Pos & pos)
{
forceValue(v, pos);
if (!v.isList())
- throwTypeError(pos, "value is %1% while a list was expected", v);
+ throwTypeError(pos, "value is %1% while a list was expected", v, *this);
}
diff --git a/src/libexpr/eval.cc b/src/libexpr/eval.cc
index b87e06ef5..5ad7e546c 100644
--- a/src/libexpr/eval.cc
+++ b/src/libexpr/eval.cc
@@ -18,6 +18,7 @@
#include <sys/resource.h>
#include <iostream>
#include <fstream>
+#include <functional>
#include <sys/resource.h>
@@ -36,7 +37,6 @@
namespace nix {
-
static char * allocString(size_t size)
{
char * t;
@@ -453,6 +453,8 @@ EvalState::EvalState(
, emptyBindings(0)
, store(store)
, buildStore(buildStore ? buildStore : store)
+ , debugStop(false)
+ , debugQuit(false)
, regexCache(makeRegexCache())
#if HAVE_BOEHMGC
, valueAllocCache(std::allocate_shared<void *>(traceable_allocator<void *>(), nullptr))
@@ -462,7 +464,7 @@ EvalState::EvalState(
, env1AllocCache(std::make_shared<void *>(nullptr))
#endif
, baseEnv(allocEnv(128))
- , staticBaseEnv(false, 0)
+ , staticBaseEnv(new StaticEnv(false, 0))
{
countCalls = getEnv("NIX_COUNT_CALLS").value_or("0") != "0";
@@ -628,7 +630,7 @@ Value * EvalState::addConstant(const std::string & name, Value & v)
void EvalState::addConstant(const std::string & name, Value * v)
{
- staticBaseEnv.vars.emplace_back(symbols.create(name), baseEnvDispl);
+ staticBaseEnv->vars.emplace_back(symbols.create(name), baseEnvDispl);
baseEnv.values[baseEnvDispl++] = v;
auto name2 = name.substr(0, 2) == "__" ? name.substr(2) : name;
baseEnv.values[0]->attrs->push_back(Attr(symbols.create(name2), v));
@@ -653,7 +655,7 @@ Value * EvalState::addPrimOp(const std::string & name,
Value * v = allocValue();
v->mkPrimOp(new PrimOp { .fun = primOp, .arity = arity, .name = sym });
- staticBaseEnv.vars.emplace_back(symbols.create(name), baseEnvDispl);
+ staticBaseEnv->vars.emplace_back(symbols.create(name), baseEnvDispl);
baseEnv.values[baseEnvDispl++] = v;
baseEnv.values[0]->attrs->push_back(Attr(sym, v));
return v;
@@ -679,7 +681,7 @@ Value * EvalState::addPrimOp(PrimOp && primOp)
Value * v = allocValue();
v->mkPrimOp(new PrimOp(std::move(primOp)));
- staticBaseEnv.vars.emplace_back(envName, baseEnvDispl);
+ staticBaseEnv->vars.emplace_back(envName, baseEnvDispl);
baseEnv.values[baseEnvDispl++] = v;
baseEnv.values[0]->attrs->push_back(Attr(primOp.name, v));
return v;
@@ -709,108 +711,333 @@ std::optional<EvalState::Doc> EvalState::getDoc(Value & v)
}
+// just for the current level of StaticEnv, not the whole chain.
+void printStaticEnvBindings(const StaticEnv &se)
+{
+ std::cout << ANSI_MAGENTA;
+ for (auto i = se.vars.begin(); i != se.vars.end(); ++i)
+ {
+ std::cout << i->first << " ";
+ }
+ std::cout << ANSI_NORMAL;
+ std::cout << std::endl;
+}
+
+// just for the current level of Env, not the whole chain.
+void printWithBindings(const Env &env)
+{
+ if (env.type == Env::HasWithAttrs)
+ {
+ std::cout << "with: ";
+ std::cout << ANSI_MAGENTA;
+ Bindings::iterator j = env.values[0]->attrs->begin();
+ while (j != env.values[0]->attrs->end()) {
+ std::cout << j->name << " ";
+ ++j;
+ }
+ std::cout << ANSI_NORMAL;
+ std::cout << std::endl;
+ }
+}
+
+void printEnvBindings(const StaticEnv &se, const Env &env, int lvl)
+{
+ std::cout << "Env level " << lvl << std::endl;
+
+ if (se.up && env.up) {
+ std::cout << "static: ";
+ printStaticEnvBindings(se);
+ printWithBindings(env);
+ std::cout << std::endl;
+ printEnvBindings(*se.up, *env.up, ++lvl);
+ }
+ else
+ {
+ std::cout << ANSI_MAGENTA;
+ // for the top level, don't print the double underscore ones; they are in builtins.
+ for (auto i = se.vars.begin(); i != se.vars.end(); ++i)
+ {
+ if (((std::string)i->first).substr(0,2) != "__")
+ std::cout << i->first << " ";
+ }
+ std::cout << ANSI_NORMAL;
+ std::cout << std::endl;
+ printWithBindings(env); // probably nothing there for the top level.
+ std::cout << std::endl;
+
+ }
+}
+
+// TODO: add accompanying env for With stuff.
+void printEnvBindings(const Expr &expr, const Env &env)
+{
+ // just print the names for now
+ if (expr.staticenv)
+ {
+ printEnvBindings(*expr.staticenv.get(), env, 0);
+ }
+}
+
+void mapStaticEnvBindings(const StaticEnv &se, const Env &env, valmap & vm)
+{
+ // add bindings for the next level up first, so that the bindings for this level
+ // override the higher levels.
+ // The top level bindings (builtins) are skipped since they are added for us by initEnv()
+ if (env.up && se.up) {
+ mapStaticEnvBindings(*se.up, *env.up,vm);
+
+ auto map = valmap();
+ if (env.type == Env::HasWithAttrs)
+ {
+ Bindings::iterator j = env.values[0]->attrs->begin();
+ while (j != env.values[0]->attrs->end()) {
+ map[j->name] = j->value;
+ ++j;
+ }
+ }
+ else
+ {
+ // iterate through staticenv bindings and add them.
+ for (auto iter = se.vars.begin(); iter != se.vars.end(); ++iter)
+ {
+ map[iter->first] = env.values[iter->second];
+ }
+ }
+
+ vm.merge(map);
+ }
+}
+
+valmap * mapStaticEnvBindings(const StaticEnv &se, const Env &env)
+{
+ auto vm = new valmap();
+ mapStaticEnvBindings(se, env, *vm);
+ return vm;
+}
+
+
/* Every "format" object (even temporary) takes up a few hundred bytes
of stack space, which is a real killer in the recursive
evaluator. So here are some helper functions for throwing
exceptions. */
-LocalNoInlineNoReturn(void throwEvalError(const char * s, const std::string & s2))
+LocalNoInlineNoReturn(void throwEvalError(const char * s, const std::string & s2, EvalState &evalState))
{
- throw EvalError(s, s2);
+ auto error = EvalError(s, s2);
+
+ if (debuggerHook && !evalState.debugTraces.empty()) {
+ DebugTrace &last = evalState.debugTraces.front();
+ debuggerHook(&error, last.env, last.expr);
+ }
+
+ throw error;
}
-LocalNoInlineNoReturn(void throwEvalError(const Pos & pos, const Suggestions & suggestions, const char * s, const std::string & s2))
+void EvalState::debug_throw(Error e) {
+ // call this in the situation where Expr and Env are inaccessible. The debugger will start in the last context
+ // that's in the DebugTrace stack.
+
+ if (debuggerHook && !debugTraces.empty()) {
+ DebugTrace &last = debugTraces.front();
+ debuggerHook(&e, last.env, last.expr);
+ }
+ throw e;
+}
+
+LocalNoInlineNoReturn(void throwEvalError(const Pos & pos, const Suggestions & suggestions, const char * s, const std::string & s2, Env & env, Expr &expr))
{
- throw EvalError(ErrorInfo {
+ auto error = EvalError({
.msg = hintfmt(s, s2),
.errPos = pos,
.suggestions = suggestions,
});
+
+ if (debuggerHook)
+ debuggerHook(&error, env, expr);
+
+ throw error;
}
-LocalNoInlineNoReturn(void throwEvalError(const Pos & pos, const char * s, const std::string & s2))
+LocalNoInlineNoReturn(void throwEvalError(const Pos & pos, const char * s, const std::string & s2, EvalState &evalState))
{
- throw EvalError(ErrorInfo {
+ auto error = EvalError({
.msg = hintfmt(s, s2),
.errPos = pos
});
+
+ if (debuggerHook && !evalState.debugTraces.empty()) {
+ DebugTrace &last = evalState.debugTraces.front();
+ debuggerHook(&error, last.env, last.expr);
+ }
+
+ throw error;
}
-LocalNoInlineNoReturn(void throwEvalError(const char * s, const std::string & s2, const std::string & s3))
+LocalNoInlineNoReturn(void throwEvalError(const Pos & pos, const char * s, Env & env, Expr &expr))
{
- throw EvalError(s, s2, s3);
+ auto error = EvalError({
+ .msg = hintfmt(s),
+ .errPos = pos
+ });
+
+ if (debuggerHook)
+ debuggerHook(&error, env, expr);
+
+ throw error;
}
-LocalNoInlineNoReturn(void throwEvalError(const Pos & pos, const char * s, const std::string & s2, const std::string & s3))
+LocalNoInlineNoReturn(void throwEvalError(const Pos & pos, const char * s, const std::string & s2, Env & env, Expr &expr))
{
- throw EvalError({
+ auto error = EvalError({
+ .msg = hintfmt(s, s2),
+ .errPos = pos
+ });
+
+ if (debuggerHook)
+ debuggerHook(&error, env, expr);
+
+ throw error;
+}
+
+LocalNoInlineNoReturn(void throwEvalError(const Pos & pos, const char * s, const std::string & s2, const std::string & s3, EvalState &evalState))
+{
+ auto error = EvalError({
.msg = hintfmt(s, s2, s3),
.errPos = pos
});
+
+ if (debuggerHook && !evalState.debugTraces.empty()) {
+ DebugTrace &last = evalState.debugTraces.front();
+ debuggerHook(&error, last.env, last.expr);
+ }
+
+ throw error;
+}
+
+LocalNoInlineNoReturn(void throwEvalError(const char * s, const std::string & s2, const std::string & s3, EvalState &evalState))
+{
+ auto error = EvalError({
+ .msg = hintfmt(s, s2, s3),
+ .errPos = noPos
+ });
+
+ if (debuggerHook && !evalState.debugTraces.empty()) {
+ DebugTrace &last = evalState.debugTraces.front();
+ debuggerHook(&error, last.env, last.expr);
+ }
+
+ throw error;
}
-LocalNoInlineNoReturn(void throwEvalError(const Pos & p1, const char * s, const Symbol & sym, const Pos & p2))
+LocalNoInlineNoReturn(void throwEvalError(const Pos & p1, const char * s, const Symbol & sym, const Pos & p2, Env & env, Expr &expr))
{
// p1 is where the error occurred; p2 is a position mentioned in the message.
- throw EvalError({
+ auto error = EvalError({
.msg = hintfmt(s, sym, p2),
.errPos = p1
});
+
+ if (debuggerHook)
+ debuggerHook(&error, env, expr);
+
+ throw error;
}
-LocalNoInlineNoReturn(void throwTypeError(const Pos & pos, const char * s))
+LocalNoInlineNoReturn(void throwTypeError(const Pos & pos, const char * s, EvalState &evalState))
{
- throw TypeError({
+ auto error = TypeError({
.msg = hintfmt(s),
.errPos = pos
});
+
+ if (debuggerHook && !evalState.debugTraces.empty()) {
+ DebugTrace &last = evalState.debugTraces.front();
+ debuggerHook(&error, last.env, last.expr);
+ }
+
+ throw error;
}
-LocalNoInlineNoReturn(void throwTypeError(const Pos & pos, const char * s, const ExprLambda & fun, const Symbol & s2))
+
+LocalNoInlineNoReturn(void throwTypeError(const Pos & pos, const char * s, const Value & v, Env & env, Expr &expr))
{
- throw TypeError({
- .msg = hintfmt(s, fun.showNamePos(), s2),
+ auto error = TypeError({
+ .msg = hintfmt(s, v),
.errPos = pos
});
+
+ if (debuggerHook)
+ debuggerHook(&error, env, expr);
+
+ throw error;
}
-LocalNoInlineNoReturn(void throwTypeError(const Pos & pos, const Suggestions & suggestions, const char * s, const ExprLambda & fun, const Symbol & s2))
+// LocalNoInlineNoReturn(void throwTypeError(const char * s, const Value & v));
+
+LocalNoInlineNoReturn(void throwTypeError(const Pos & pos, const char * s, const ExprLambda & fun, const Symbol & s2, Env & env, Expr &expr))
{
- throw TypeError(ErrorInfo {
+ auto error = TypeError({
.msg = hintfmt(s, fun.showNamePos(), s2),
.errPos = pos,
- .suggestions = suggestions,
});
-}
+ if (debuggerHook)
+ debuggerHook(&error, env, expr);
-LocalNoInlineNoReturn(void throwTypeError(const char * s, const Value & v))
+ throw error;
+}
+
+LocalNoInlineNoReturn(void throwTypeError(const Pos & pos, const Suggestions & suggestions, const char * s, const ExprLambda & fun, const Symbol & s2, Env & env, Expr &expr))
{
- throw TypeError(s, showType(v));
+ auto error = TypeError({
+ .msg = hintfmt(s, fun.showNamePos(), s2),
+ .errPos = pos,
+ .suggestions = suggestions,
+ });
+
+ if (debuggerHook)
+ debuggerHook(&error, env, expr);
+
+ throw error;
}
-LocalNoInlineNoReturn(void throwAssertionError(const Pos & pos, const char * s, const std::string & s1))
+LocalNoInlineNoReturn(void throwAssertionError(const Pos & pos, const char * s, const std::string & s1, Env & env, Expr &expr))
{
- throw AssertionError({
+ auto error = AssertionError({
.msg = hintfmt(s, s1),
.errPos = pos
});
+
+ if (debuggerHook)
+ debuggerHook(&error, env, expr);
+
+ throw error;
}
-LocalNoInlineNoReturn(void throwUndefinedVarError(const Pos & pos, const char * s, const std::string & s1))
+LocalNoInlineNoReturn(void throwUndefinedVarError(const Pos & pos, const char * s, const std::string & s1, Env & env, const Expr &expr))
{
- throw UndefinedVarError({
+ auto error = UndefinedVarError({
.msg = hintfmt(s, s1),
.errPos = pos
});
+
+ if (debuggerHook)
+ debuggerHook(&error, env, expr);
+
+ throw error;
}
-LocalNoInlineNoReturn(void throwMissingArgumentError(const Pos & pos, const char * s, const std::string & s1))
+LocalNoInlineNoReturn(void throwMissingArgumentError(const Pos & pos, const char * s, const std::string & s1, Env & env, Expr &expr))
{
- throw MissingArgumentError({
+ auto error = MissingArgumentError({
.msg = hintfmt(s, s1),
.errPos = pos
});
+
+ if (debuggerHook)
+ debuggerHook(&error, env, expr);
+
+ throw error;
}
LocalNoInline(void addErrorTrace(Error & e, const char * s, const std::string & s2))
@@ -823,6 +1050,28 @@ LocalNoInline(void addErrorTrace(Error & e, const Pos & pos, const char * s, con
e.addTrace(pos, s, s2);
}
+LocalNoInline(std::unique_ptr<DebugTraceStacker>
+ makeDebugTraceStacker(EvalState &state, Expr &expr, Env &env, std::optional<ErrPos> pos, const char * s, const std::string & s2))
+{
+ return std::unique_ptr<DebugTraceStacker>(
+ new DebugTraceStacker(
+ state,
+ DebugTrace
+ {.pos = pos,
+ .expr = expr,
+ .env = env,
+ .hint = hintfmt(s, s2),
+ .is_error = false
+ }));
+}
+
+DebugTraceStacker::DebugTraceStacker(EvalState &evalState, DebugTrace t)
+:evalState(evalState), trace(t)
+{
+ evalState.debugTraces.push_front(t);
+ if (evalState.debugStop && debuggerHook)
+ debuggerHook(0, t.env, t.expr);
+}
void Value::mkString(std::string_view s)
{
@@ -880,13 +1129,13 @@ inline Value * EvalState::lookupVar(Env * env, const ExprVar & var, bool noEval)
if (countCalls) attrSelects[*j->pos]++;
return j->value;
}
- if (!env->prevWith)
- throwUndefinedVarError(var.pos, "undefined variable '%1%'", var.name);
+ if (!env->prevWith) {
+ throwUndefinedVarError(var.pos, "undefined variable '%1%'", var.name, *env, var);
+ }
for (size_t l = env->prevWith; l; --l, env = env->up) ;
}
}
-
void EvalState::mkList(Value & v, size_t size)
{
v.mkList(size);
@@ -1018,6 +1267,16 @@ void EvalState::cacheFile(
fileParseCache[resolvedPath] = e;
try {
+ auto dts =
+ debuggerHook ?
+ makeDebugTraceStacker(
+ *this,
+ *e,
+ this->baseEnv,
+ (e->getPos() ? std::optional(ErrPos(*e->getPos())) : std::nullopt),
+ "while evaluating the file '%1%':", resolvedPath)
+ : nullptr;
+
// Enforce that 'flake.nix' is a direct attrset, not a
// computation.
if (mustBeTrivial &&
@@ -1045,7 +1304,7 @@ inline bool EvalState::evalBool(Env & env, Expr * e)
Value v;
e->eval(*this, env, v);
if (v.type() != nBool)
- throwTypeError("value is %1% while a Boolean was expected", v);
+ throwTypeError(noPos, "value is %1% while a Boolean was expected", v, env, *e);
return v.boolean;
}
@@ -1055,7 +1314,7 @@ inline bool EvalState::evalBool(Env & env, Expr * e, const Pos & pos)
Value v;
e->eval(*this, env, v);
if (v.type() != nBool)
- throwTypeError(pos, "value is %1% while a Boolean was expected", v);
+ throwTypeError(pos, "value is %1% while a Boolean was expected", v, env, *e);
return v.boolean;
}
@@ -1064,7 +1323,7 @@ inline void EvalState::evalAttrs(Env & env, Expr * e, Value & v)
{
e->eval(*this, env, v);
if (v.type() != nAttrs)
- throwTypeError("value is %1% while a set was expected", v);
+ throwTypeError(noPos, "value is %1% while a set was expected", v, env, *e);
}
@@ -1169,7 +1428,7 @@ void ExprAttrs::eval(EvalState & state, Env & env, Value & v)
Symbol nameSym = state.symbols.create(nameVal.string.s);
Bindings::iterator j = v.attrs->find(nameSym);
if (j != v.attrs->end())
- throwEvalError(i.pos, "dynamic attribute '%1%' already defined at %2%", nameSym, *j->pos);
+ throwEvalError(i.pos, "dynamic attribute '%1%' already defined at %2%", nameSym, *j->pos, env, *this);
i.valueExpr->setName(nameSym);
/* Keep sorted order so find can catch duplicates */
@@ -1241,6 +1500,16 @@ void ExprSelect::eval(EvalState & state, Env & env, Value & v)
e->eval(state, env, vTmp);
try {
+ auto dts =
+ debuggerHook ?
+ makeDebugTraceStacker(
+ state,
+ *this,
+ env,
+ *pos2,
+ "while evaluating the attribute '%1%'",
+ showAttrPath(state, env, attrPath))
+ : nullptr;
for (auto & i : attrPath) {
state.nrLookups++;
@@ -1263,7 +1532,7 @@ void ExprSelect::eval(EvalState & state, Env & env, Value & v)
throwEvalError(
pos,
Suggestions::bestMatches(allAttrNames, name),
- "attribute '%1%' missing", name);
+ "attribute '%1%' missing", name, env, *this);
}
}
vAttrs = j->value;
@@ -1351,7 +1620,6 @@ void EvalState::callFunction(Value & fun, size_t nrArgs, Value * * args, Value &
if (!lambda.hasFormals())
env2.values[displ++] = args[0];
-
else {
forceAttrs(*args[0], pos);
@@ -1366,7 +1634,7 @@ void EvalState::callFunction(Value & fun, size_t nrArgs, Value * * args, Value &
auto j = args[0]->attrs->get(i.name);
if (!j) {
if (!i.def) throwTypeError(pos, "%1% called without required argument '%2%'",
- lambda, i.name);
+ lambda, i.name, *fun.lambda.env, lambda);
env2.values[displ++] = i.def->maybeThunk(*this, env2);
} else {
attrsUsed++;
@@ -1388,8 +1656,7 @@ void EvalState::callFunction(Value & fun, size_t nrArgs, Value * * args, Value &
pos,
Suggestions::bestMatches(formalNames, i.name),
"%1% called with unexpected argument '%2%'",
- lambda,
- i.name);
+ lambda, i.name, *fun.lambda.env, lambda);
}
abort(); // can't happen
}
@@ -1400,6 +1667,15 @@ void EvalState::callFunction(Value & fun, size_t nrArgs, Value * * args, Value &
/* Evaluate the body. */
try {
+ auto dts =
+ debuggerHook ?
+ makeDebugTraceStacker(*this, *lambda.body, env2, lambda.pos,
+ "while evaluating %s",
+ (lambda.name.set()
+ ? "'" + (std::string) lambda.name + "'"
+ : "anonymous lambda"))
+ : nullptr;
+
lambda.body->eval(*this, env2, vCur);
} catch (Error & e) {
if (loggerSettings.showTrace.get()) {
@@ -1485,7 +1761,7 @@ void EvalState::callFunction(Value & fun, size_t nrArgs, Value * * args, Value &
}
else
- throwTypeError(pos, "attempt to call something which is not a function but %1%", vCur);
+ throwTypeError(pos, "attempt to call something which is not a function but %1%", vCur, *this);
}
vRes = vCur;
@@ -1554,8 +1830,9 @@ void EvalState::autoCallFunction(Bindings & args, Value & fun, Value & res)
Nix attempted to evaluate a function as a top level expression; in
this case it must have its arguments supplied either by default
values, or passed explicitly with '--arg' or '--argstr'. See
-https://nixos.org/manual/nix/stable/#ss-functions.)", i.name);
-
+https://nixos.org/manual/nix/stable/#ss-functions.)",
+ i.name,
+ *fun.lambda.env, *fun.lambda.fun);
}
}
}
@@ -1587,7 +1864,7 @@ void ExprAssert::eval(EvalState & state, Env & env, Value & v)
if (!state.evalBool(env, cond, pos)) {
std::ostringstream out;
cond->show(out);
- throwAssertionError(pos, "assertion '%1%' failed", out.str());
+ throwAssertionError(pos, "assertion '%1%' failed", out.str(), env, *this);
}
body->eval(state, env, v);
}
@@ -1763,15 +2040,16 @@ void ExprConcatStrings::eval(EvalState & state, Env & env, Value & v)
firstType = nFloat;
nf = n;
nf += vTmp.fpoint;
- } else
- throwEvalError(i_pos, "cannot add %1% to an integer", showType(vTmp));
+ } else {
+ throwEvalError(i_pos, "cannot add %1% to an integer", showType(vTmp), env, *this);
+ }
} else if (firstType == nFloat) {
if (vTmp.type() == nInt) {
nf += vTmp.integer;
} else if (vTmp.type() == nFloat) {
nf += vTmp.fpoint;
} else
- throwEvalError(i_pos, "cannot add %1% to a float", showType(vTmp));
+ throwEvalError(i_pos, "cannot add %1% to a float", showType(vTmp), env, *this);
} else {
if (s.empty()) s.reserve(es->size());
/* skip canonization of first path, which would only be not
@@ -1791,7 +2069,7 @@ void ExprConcatStrings::eval(EvalState & state, Env & env, Value & v)
v.mkFloat(nf);
else if (firstType == nPath) {
if (!context.empty())
- throwEvalError(pos, "a string that refers to a store path cannot be appended to a path");
+ throwEvalError(pos, "a string that refers to a store path cannot be appended to a path", env, *this);
v.mkPath(canonPath(str()));
} else
v.mkStringMove(c_str(), context);
@@ -1818,6 +2096,16 @@ void EvalState::forceValueDeep(Value & v)
if (v.type() == nAttrs) {
for (auto & i : *v.attrs)
try {
+
+ auto dts =
+ debuggerHook ?
+ // if the value is a thunk, we're evaling. otherwise no trace necessary.
+ (i.value->isThunk() ?
+ makeDebugTraceStacker(*this, *v.thunk.expr, *v.thunk.env, *i.pos,
+ "while evaluating the attribute '%1%'", i.name)
+ : nullptr)
+ : nullptr;
+
recurse(*i.value);
} catch (Error & e) {
addErrorTrace(e, *i.pos, "while evaluating the attribute '%1%'", i.name);
@@ -1839,7 +2127,8 @@ NixInt EvalState::forceInt(Value & v, const Pos & pos)
{
forceValue(v, pos);
if (v.type() != nInt)
- throwTypeError(pos, "value is %1% while an integer was expected", v);
+ throwTypeError(pos, "value is %1% while an integer was expected", v, *this);
+
return v.integer;
}
@@ -1850,7 +2139,8 @@ NixFloat EvalState::forceFloat(Value & v, const Pos & pos)
if (v.type() == nInt)
return v.integer;
else if (v.type() != nFloat)
- throwTypeError(pos, "value is %1% while a float was expected", v);
+ throwTypeError(pos, "value is %1% while a float was expected", v,
+ *this);
return v.fpoint;
}
@@ -1859,7 +2149,8 @@ bool EvalState::forceBool(Value & v, const Pos & pos)
{
forceValue(v, pos);
if (v.type() != nBool)
- throwTypeError(pos, "value is %1% while a Boolean was expected", v);
+ throwTypeError(pos, "value is %1% while a Boolean was expected", v,
+ *this);
return v.boolean;
}
@@ -1874,7 +2165,8 @@ void EvalState::forceFunction(Value & v, const Pos & pos)
{
forceValue(v, pos);
if (v.type() != nFunction && !isFunctor(v))
- throwTypeError(pos, "value is %1% while a function was expected", v);
+ throwTypeError(pos, "value is %1% while a function was expected", v,
+ *this);
}
@@ -1882,10 +2174,8 @@ std::string_view EvalState::forceString(Value & v, const Pos & pos)
{
forceValue(v, pos);
if (v.type() != nString) {
- if (pos)
- throwTypeError(pos, "value is %1% while a string was expected", v);
- else
- throwTypeError("value is %1% while a string was expected", v);
+ throwTypeError(pos, "value is %1% while a string was expected", v,
+ *this);
}
return v.string.s;
}
@@ -1945,10 +2235,10 @@ std::string_view EvalState::forceStringNoCtx(Value & v, const Pos & pos)
if (v.string.context) {
if (pos)
throwEvalError(pos, "the string '%1%' is not allowed to refer to a store path (such as '%2%')",
- v.string.s, v.string.context[0]);
+ v.string.s, v.string.context[0], *this);
else
throwEvalError("the string '%1%' is not allowed to refer to a store path (such as '%2%')",
- v.string.s, v.string.context[0]);
+ v.string.s, v.string.context[0], *this);
}
return s;
}
@@ -2002,7 +2292,8 @@ BackedStringView EvalState::coerceToString(const Pos & pos, Value & v, PathSet &
if (maybeString)
return std::move(*maybeString);
auto i = v.attrs->find(sOutPath);
- if (i == v.attrs->end()) throwTypeError(pos, "cannot coerce a set to a string");
+ if (i == v.attrs->end())
+ throwTypeError(pos, "cannot coerce a set to a string", *this);
return coerceToString(pos, *i->value, context, coerceMore, copyToStore);
}
@@ -2010,7 +2301,6 @@ BackedStringView EvalState::coerceToString(const Pos & pos, Value & v, PathSet &
return v.external->coerceToString(pos, context, coerceMore, copyToStore);
if (coerceMore) {
-
/* Note that `false' is represented as an empty string for
shell scripting convenience, just like `null'. */
if (v.type() == nBool && v.boolean) return "1";
@@ -2032,14 +2322,14 @@ BackedStringView EvalState::coerceToString(const Pos & pos, Value & v, PathSet &
}
}
- throwTypeError(pos, "cannot coerce %1% to a string", v);
+ throwTypeError(pos, "cannot coerce %1% to a string", v, *this);
}
std::string EvalState::copyPathToStore(PathSet & context, const Path & path)
{
if (nix::isDerivation(path))
- throwEvalError("file names are not allowed to end in '%1%'", drvExtension);
+ throwEvalError("file names are not allowed to end in '%1%'", drvExtension, *this);
Path dstPath;
auto i = srcToStore.find(path);
@@ -2064,7 +2354,7 @@ Path EvalState::coerceToPath(const Pos & pos, Value & v, PathSet & context)
{
auto path = coerceToString(pos, v, context, false, false).toOwned();
if (path == "" || path[0] != '/')
- throwEvalError(pos, "string '%1%' doesn't represent an absolute path", path);
+ throwEvalError(pos, "string '%1%' doesn't represent an absolute path", path, *this);
return path;
}
@@ -2155,7 +2445,10 @@ bool EvalState::eqValues(Value & v1, Value & v2)
return v1.fpoint == v2.fpoint;
default:
- throwEvalError("cannot compare %1% with %2%", showType(v1), showType(v2));
+ throwEvalError("cannot compare %1% with %2%",
+ showType(v1),
+ showType(v2),
+ *this);
}
}
diff --git a/src/libexpr/eval.hh b/src/libexpr/eval.hh
index 7ed376e8d..2ced5bea9 100644
--- a/src/libexpr/eval.hh
+++ b/src/libexpr/eval.hh
@@ -25,6 +25,8 @@ enum RepairFlag : bool;
typedef void (* PrimOpFun) (EvalState & state, const Pos & pos, Value * * args, Value & v);
+void printEnvBindings(const Expr &expr, const Env &env);
+void printEnvBindings(const StaticEnv &se, const Env &env, int lvl = 0);
struct PrimOp
{
@@ -35,6 +37,7 @@ struct PrimOp
const char * doc = nullptr;
};
+typedef std::map<std::string, Value *> valmap;
struct Env
{
@@ -44,6 +47,7 @@ struct Env
Value * values[0];
};
+valmap * mapStaticEnvBindings(const StaticEnv &se, const Env &env);
void copyContext(const Value & v, PathSet & context);
@@ -68,6 +72,13 @@ struct RegexCache;
std::shared_ptr<RegexCache> makeRegexCache();
+struct DebugTrace {
+ std::optional<ErrPos> pos;
+ const Expr &expr;
+ const Env &env;
+ hintformat hint;
+ bool is_error;
+};
class EvalState
{
@@ -104,6 +115,12 @@ public:
RootValue vCallFlake = nullptr;
RootValue vImportedDrvToDerivation = nullptr;
+ /* Debugger */
+ bool debugStop;
+ bool debugQuit;
+ std::list<DebugTrace> debugTraces;
+ void debug_throw(Error e);
+
private:
SrcToStore srcToStore;
@@ -180,10 +197,10 @@ public:
/* Parse a Nix expression from the specified file. */
Expr * parseExprFromFile(const Path & path);
- Expr * parseExprFromFile(const Path & path, StaticEnv & staticEnv);
+ Expr * parseExprFromFile(const Path & path, std::shared_ptr<StaticEnv> & staticEnv);
/* Parse a Nix expression from the specified string. */
- Expr * parseExprFromString(std::string s, const Path & basePath, StaticEnv & staticEnv);
+ Expr * parseExprFromString(std::string s, const Path & basePath, std::shared_ptr<StaticEnv> & staticEnv);
Expr * parseExprFromString(std::string s, const Path & basePath);
Expr * parseStdin();
@@ -193,7 +210,7 @@ public:
trivial (i.e. doesn't require arbitrary computation). */
void evalFile(const Path & path, Value & v, bool mustBeTrivial = false);
- /* Like `cacheFile`, but with an already parsed expression. */
+ /* Like `evalFile`, but with an already parsed expression. */
void cacheFile(
const Path & path,
const Path & resolvedPath,
@@ -281,7 +298,7 @@ public:
Env & baseEnv;
/* The same, but used during parsing to resolve variables. */
- StaticEnv staticBaseEnv; // !!! should be private
+ std::shared_ptr<StaticEnv> staticBaseEnv; // !!! should be private
private:
@@ -322,7 +339,7 @@ private:
friend struct ExprLet;
Expr * parse(char * text, size_t length, FileOrigin origin, const PathView path,
- const PathView basePath, StaticEnv & staticEnv);
+ const PathView basePath, std::shared_ptr<StaticEnv> & staticEnv);
public:
@@ -417,6 +434,17 @@ private:
friend struct Value;
};
+class DebugTraceStacker {
+ public:
+ DebugTraceStacker(EvalState &evalState, DebugTrace 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'. */
std::string_view showType(ValueType type);
diff --git a/src/libexpr/nixexpr.cc b/src/libexpr/nixexpr.cc
index a2def65a6..51b05de60 100644
--- a/src/libexpr/nixexpr.cc
+++ b/src/libexpr/nixexpr.cc
@@ -4,9 +4,11 @@
#include <cstdlib>
-
namespace nix {
+/* Launch the nix debugger */
+
+std::function<void(const Error * error, const Env & env, const Expr & expr)> debuggerHook;
/* Displaying abstract syntax trees. */
@@ -254,35 +256,46 @@ Pos noPos;
/* Computing levels/displacements for variables. */
-void Expr::bindVars(const StaticEnv & env)
+void Expr::bindVars(const std::shared_ptr<const StaticEnv> &env)
{
abort();
}
-void ExprInt::bindVars(const StaticEnv & env)
+void ExprInt::bindVars(const std::shared_ptr<const StaticEnv> &env)
{
+ if (debuggerHook)
+ staticenv = env;
}
-void ExprFloat::bindVars(const StaticEnv & env)
+void ExprFloat::bindVars(const std::shared_ptr<const StaticEnv> &env)
{
+ if (debuggerHook)
+ staticenv = env;
}
-void ExprString::bindVars(const StaticEnv & env)
+void ExprString::bindVars(const std::shared_ptr<const StaticEnv> &env)
{
+ if (debuggerHook)
+ staticenv = env;
}
-void ExprPath::bindVars(const StaticEnv & env)
+void ExprPath::bindVars(const std::shared_ptr<const StaticEnv> &env)
{
+ if (debuggerHook)
+ staticenv = env;
}
-void ExprVar::bindVars(const StaticEnv & env)
+void ExprVar::bindVars(const std::shared_ptr<const StaticEnv> &env)
{
+ if (debuggerHook)
+ staticenv = env;
+
/* Check whether the variable appears in the environment. If so,
set its level and displacement. */
const StaticEnv * curEnv;
Level level;
int withLevel = -1;
- for (curEnv = &env, level = 0; curEnv; curEnv = curEnv->up, level++) {
+ for (curEnv = env.get(), level = 0; curEnv; curEnv = curEnv->up, level++) {
if (curEnv->isWith) {
if (withLevel == -1) withLevel = level;
} else {
@@ -299,17 +312,22 @@ void ExprVar::bindVars(const StaticEnv & env)
/* Otherwise, the variable must be obtained from the nearest
enclosing `with'. If there is no `with', then we can issue an
"undefined variable" error now. */
- if (withLevel == -1)
+ if (withLevel == -1)
+ {
throw UndefinedVarError({
.msg = hintfmt("undefined variable '%1%'", name),
.errPos = pos
});
+ }
fromWith = true;
this->level = withLevel;
}
-void ExprSelect::bindVars(const StaticEnv & env)
+void ExprSelect::bindVars(const std::shared_ptr<const StaticEnv> &env)
{
+ if (debuggerHook)
+ staticenv = env;
+
e->bindVars(env);
if (def) def->bindVars(env);
for (auto & i : attrPath)
@@ -317,64 +335,79 @@ void ExprSelect::bindVars(const StaticEnv & env)
i.expr->bindVars(env);
}
-void ExprOpHasAttr::bindVars(const StaticEnv & env)
+void ExprOpHasAttr::bindVars(const std::shared_ptr<const StaticEnv> &env)
{
+ if (debuggerHook)
+ staticenv = env;
+
e->bindVars(env);
for (auto & i : attrPath)
if (!i.symbol.set())
i.expr->bindVars(env);
}
-void ExprAttrs::bindVars(const StaticEnv & env)
+void ExprAttrs::bindVars(const std::shared_ptr<const StaticEnv> &env)
{
- const StaticEnv * dynamicEnv = &env;
- StaticEnv newEnv(false, &env, recursive ? attrs.size() : 0);
+ if (debuggerHook)
+ staticenv = env;
if (recursive) {
- dynamicEnv = &newEnv;
+ auto newEnv = std::shared_ptr<StaticEnv>(new StaticEnv(false, env.get(), recursive ? attrs.size() : 0));
Displacement displ = 0;
for (auto & i : attrs)
- newEnv.vars.emplace_back(i.first, i.second.displ = displ++);
+ newEnv->vars.emplace_back(i.first, i.second.displ = displ++);
// No need to sort newEnv since attrs is in sorted order.
for (auto & i : attrs)
i.second.e->bindVars(i.second.inherited ? env : newEnv);
- }
- else
+ for (auto & i : dynamicAttrs) {
+ i.nameExpr->bindVars(newEnv);
+ i.valueExpr->bindVars(newEnv);
+ }
+ }
+ else {
for (auto & i : attrs)
i.second.e->bindVars(env);
- for (auto & i : dynamicAttrs) {
- i.nameExpr->bindVars(*dynamicEnv);
- i.valueExpr->bindVars(*dynamicEnv);
+ for (auto & i : dynamicAttrs) {
+ i.nameExpr->bindVars(env);
+ i.valueExpr->bindVars(env);
+ }
}
}
-void ExprList::bindVars(const StaticEnv & env)
+void ExprList::bindVars(const std::shared_ptr<const StaticEnv> &env)
{
+ if (debuggerHook)
+ staticenv = env;
+
for (auto & i : elems)
i->bindVars(env);
}
-void ExprLambda::bindVars(const StaticEnv & env)
+void ExprLambda::bindVars(const std::shared_ptr<const StaticEnv> &env)
{
- StaticEnv newEnv(
- false, &env,
- (hasFormals() ? formals->formals.size() : 0) +
- (arg.empty() ? 0 : 1));
+ if (debuggerHook)
+ staticenv = env;
+
+ auto newEnv = std::shared_ptr<StaticEnv>(
+ new StaticEnv(
+ false, env.get(),
+ (hasFormals() ? formals->formals.size() : 0) +
+ (arg.empty() ? 0 : 1)));
Displacement displ = 0;
- if (!arg.empty()) newEnv.vars.emplace_back(arg, displ++);
+ if (!arg.empty()) newEnv->vars.emplace_back(arg, displ++);
if (hasFormals()) {
for (auto & i : formals->formals)
- newEnv.vars.emplace_back(i.name, displ++);
+ newEnv->vars.emplace_back(i.name, displ++);
- newEnv.sort();
+ newEnv->sort();
for (auto & i : formals->formals)
if (i.def) i.def->bindVars(newEnv);
@@ -383,20 +416,26 @@ void ExprLambda::bindVars(const StaticEnv & env)
body->bindVars(newEnv);
}
-void ExprCall::bindVars(const StaticEnv & env)
+void ExprCall::bindVars(const std::shared_ptr<const StaticEnv> &env)
{
+ if (debuggerHook)
+ staticenv = env;
+
fun->bindVars(env);
for (auto e : args)
e->bindVars(env);
}
-void ExprLet::bindVars(const StaticEnv & env)
+void ExprLet::bindVars(const std::shared_ptr<const StaticEnv> &env)
{
- StaticEnv newEnv(false, &env, attrs->attrs.size());
+ if (debuggerHook)
+ staticenv = env;
+
+ auto newEnv = std::shared_ptr<StaticEnv>(new StaticEnv(false, env.get(), attrs->attrs.size()));
Displacement displ = 0;
for (auto & i : attrs->attrs)
- newEnv.vars.emplace_back(i.first, i.second.displ = displ++);
+ newEnv->vars.emplace_back(i.first, i.second.displ = displ++);
// No need to sort newEnv since attrs->attrs is in sorted order.
@@ -406,51 +445,69 @@ void ExprLet::bindVars(const StaticEnv & env)
body->bindVars(newEnv);
}
-void ExprWith::bindVars(const StaticEnv & env)
+void ExprWith::bindVars(const std::shared_ptr<const StaticEnv> &env)
{
+ if (debuggerHook)
+ staticenv = env;
+
/* Does this `with' have an enclosing `with'? If so, record its
level so that `lookupVar' can look up variables in the previous
`with' if this one doesn't contain the desired attribute. */
const StaticEnv * curEnv;
Level level;
prevWith = 0;
- for (curEnv = &env, level = 1; curEnv; curEnv = curEnv->up, level++)
+ for (curEnv = env.get(), level = 1; curEnv; curEnv = curEnv->up, level++)
if (curEnv->isWith) {
prevWith = level;
break;
}
attrs->bindVars(env);
- StaticEnv newEnv(true, &env);
+ auto newEnv = std::shared_ptr<StaticEnv>(new StaticEnv(true, env.get()));
body->bindVars(newEnv);
}
-void ExprIf::bindVars(const StaticEnv & env)
+void ExprIf::bindVars(const std::shared_ptr<const StaticEnv> &env)
{
+ if (debuggerHook)
+ staticenv = env;
+
cond->bindVars(env);
then->bindVars(env);
else_->bindVars(env);
}
-void ExprAssert::bindVars(const StaticEnv & env)
+void ExprAssert::bindVars(const std::shared_ptr<const StaticEnv> &env)
{
+ if (debuggerHook)
+ staticenv = env;
+
cond->bindVars(env);
body->bindVars(env);
}
-void ExprOpNot::bindVars(const StaticEnv & env)
+void ExprOpNot::bindVars(const std::shared_ptr<const StaticEnv> &env)
{
+ if (debuggerHook)
+ staticenv = env;
+
e->bindVars(env);
}
-void ExprConcatStrings::bindVars(const StaticEnv & env)
+void ExprConcatStrings::bindVars(const std::shared_ptr<const StaticEnv> &env)
{
+ if (debuggerHook)
+ staticenv = env;
+
for (auto & i : *es)
i.second->bindVars(env);
}
-void ExprPos::bindVars(const StaticEnv & env)
+void ExprPos::bindVars(const std::shared_ptr<const StaticEnv> &env)
{
+ if (debuggerHook)
+ staticenv = env;
+
}
diff --git a/src/libexpr/nixexpr.hh b/src/libexpr/nixexpr.hh
index 4dbe31510..db210e07b 100644
--- a/src/libexpr/nixexpr.hh
+++ b/src/libexpr/nixexpr.hh
@@ -18,6 +18,7 @@ MakeError(UndefinedVarError, Error);
MakeError(MissingArgumentError, EvalError);
MakeError(RestrictedPathError, Error);
+extern std::function<void(const Error * error, const Env & env, const Expr & expr)> debuggerHook;
/* Position objects. */
@@ -79,10 +80,13 @@ struct Expr
{
virtual ~Expr() { };
virtual void show(std::ostream & str) const;
- virtual void bindVars(const StaticEnv & env);
+ virtual void bindVars(const std::shared_ptr<const StaticEnv> & env);
virtual void eval(EvalState & state, Env & env, Value & v);
virtual Value * maybeThunk(EvalState & state, Env & env);
virtual void setName(Symbol & name);
+
+ std::shared_ptr<const StaticEnv> staticenv;
+ virtual const Pos* getPos() const = 0;
};
std::ostream & operator << (std::ostream & str, const Expr & e);
@@ -90,15 +94,16 @@ std::ostream & operator << (std::ostream & str, const Expr & e);
#define COMMON_METHODS \
void show(std::ostream & str) const; \
void eval(EvalState & state, Env & env, Value & v); \
- void bindVars(const StaticEnv & env);
+ void bindVars(const std::shared_ptr<const StaticEnv> & env);
struct ExprInt : Expr
{
NixInt n;
Value v;
ExprInt(NixInt n) : n(n) { v.mkInt(n); };
- COMMON_METHODS
Value * maybeThunk(EvalState & state, Env & env);
+ const Pos* getPos() const { return 0; }
+ COMMON_METHODS
};
struct ExprFloat : Expr
@@ -106,8 +111,9 @@ struct ExprFloat : Expr
NixFloat nf;
Value v;
ExprFloat(NixFloat nf) : nf(nf) { v.mkFloat(nf); };
- COMMON_METHODS
Value * maybeThunk(EvalState & state, Env & env);
+ const Pos* getPos() const { return 0; }
+ COMMON_METHODS
};
struct ExprString : Expr
@@ -115,8 +121,9 @@ struct ExprString : Expr
std::string s;
Value v;
ExprString(std::string s) : s(std::move(s)) { v.mkString(this->s.data()); };
- COMMON_METHODS
Value * maybeThunk(EvalState & state, Env & env);
+ const Pos* getPos() const { return 0; }
+ COMMON_METHODS
};
struct ExprPath : Expr
@@ -124,8 +131,9 @@ struct ExprPath : Expr
std::string s;
Value v;
ExprPath(std::string s) : s(std::move(s)) { v.mkPath(this->s.c_str()); };
- COMMON_METHODS
Value * maybeThunk(EvalState & state, Env & env);
+ const Pos* getPos() const { return 0; }
+ COMMON_METHODS
};
typedef uint32_t Level;
@@ -151,8 +159,9 @@ struct ExprVar : Expr
ExprVar(const Symbol & name) : name(name) { };
ExprVar(const Pos & pos, const Symbol & name) : pos(pos), name(name) { };
- COMMON_METHODS
Value * maybeThunk(EvalState & state, Env & env);
+ const Pos* getPos() const { return &pos; }
+ COMMON_METHODS
};
struct ExprSelect : Expr
@@ -162,6 +171,7 @@ struct ExprSelect : Expr
AttrPath attrPath;
ExprSelect(const Pos & pos, Expr * e, const AttrPath & attrPath, Expr * def) : pos(pos), e(e), def(def), attrPath(attrPath) { };
ExprSelect(const Pos & pos, Expr * e, const Symbol & name) : pos(pos), e(e), def(0) { attrPath.push_back(AttrName(name)); };
+ const Pos* getPos() const { return &pos; }
COMMON_METHODS
};
@@ -170,6 +180,7 @@ struct ExprOpHasAttr : Expr
Expr * e;
AttrPath attrPath;
ExprOpHasAttr(Expr * e, const AttrPath & attrPath) : e(e), attrPath(attrPath) { };
+ const Pos* getPos() const { return e->getPos(); }
COMMON_METHODS
};
@@ -198,6 +209,7 @@ struct ExprAttrs : Expr
DynamicAttrDefs dynamicAttrs;
ExprAttrs(const Pos &pos) : recursive(false), pos(pos) { };
ExprAttrs() : recursive(false), pos(noPos) { };
+ const Pos* getPos() const { return &pos; }
COMMON_METHODS
};
@@ -205,6 +217,7 @@ struct ExprList : Expr
{
std::vector<Expr *> elems;
ExprList() { };
+ const Pos* getPos() const { return 0; }
COMMON_METHODS
};
@@ -253,6 +266,7 @@ struct ExprLambda : Expr
void setName(Symbol & name);
std::string showNamePos() const;
inline bool hasFormals() const { return formals != nullptr; }
+ const Pos* getPos() const { return &pos; }
COMMON_METHODS
};
@@ -264,6 +278,7 @@ struct ExprCall : Expr
ExprCall(const Pos & pos, Expr * fun, std::vector<Expr *> && args)
: fun(fun), args(args), pos(pos)
{ }
+ const Pos* getPos() const { return &pos; }
COMMON_METHODS
};
@@ -272,6 +287,7 @@ struct ExprLet : Expr
ExprAttrs * attrs;
Expr * body;
ExprLet(ExprAttrs * attrs, Expr * body) : attrs(attrs), body(body) { };
+ const Pos* getPos() const { return 0; }
COMMON_METHODS
};
@@ -281,6 +297,7 @@ struct ExprWith : Expr
Expr * attrs, * body;
size_t prevWith;
ExprWith(const Pos & pos, Expr * attrs, Expr * body) : pos(pos), attrs(attrs), body(body) { };
+ const Pos* getPos() const { return &pos; }
COMMON_METHODS
};
@@ -289,6 +306,7 @@ struct ExprIf : Expr
Pos pos;
Expr * cond, * then, * else_;
ExprIf(const Pos & pos, Expr * cond, Expr * then, Expr * else_) : pos(pos), cond(cond), then(then), else_(else_) { };
+ const Pos* getPos() const { return &pos; }
COMMON_METHODS
};
@@ -297,6 +315,7 @@ struct ExprAssert : Expr
Pos pos;
Expr * cond, * body;
ExprAssert(const Pos & pos, Expr * cond, Expr * body) : pos(pos), cond(cond), body(body) { };
+ const Pos* getPos() const { return &pos; }
COMMON_METHODS
};
@@ -304,6 +323,7 @@ struct ExprOpNot : Expr
{
Expr * e;
ExprOpNot(Expr * e) : e(e) { };
+ const Pos* getPos() const { return 0; }
COMMON_METHODS
};
@@ -318,11 +338,12 @@ struct ExprOpNot : Expr
{ \
str << "(" << *e1 << " " s " " << *e2 << ")"; \
} \
- void bindVars(const StaticEnv & env) \
+ void bindVars(const std::shared_ptr<const StaticEnv> & env) \
{ \
e1->bindVars(env); e2->bindVars(env); \
} \
void eval(EvalState & state, Env & env, Value & v); \
+ const Pos* getPos() const { return &pos; } \
};
MakeBinOp(ExprOpEq, "==")
@@ -340,6 +361,7 @@ struct ExprConcatStrings : Expr
std::vector<std::pair<Pos, Expr *> > * es;
ExprConcatStrings(const Pos & pos, bool forceString, std::vector<std::pair<Pos, Expr *> > * es)
: pos(pos), forceString(forceString), es(es) { };
+ const Pos* getPos() const { return &pos; }
COMMON_METHODS
};
@@ -347,6 +369,7 @@ struct ExprPos : Expr
{
Pos pos;
ExprPos(const Pos & pos) : pos(pos) { };
+ const Pos* getPos() const { return &pos; }
COMMON_METHODS
};
diff --git a/src/libexpr/parser.y b/src/libexpr/parser.y
index 919b9cfae..4182f36d5 100644
--- a/src/libexpr/parser.y
+++ b/src/libexpr/parser.y
@@ -23,6 +23,7 @@
#include "nixexpr.hh"
#include "eval.hh"
#include "globals.hh"
+#include <iostream>
namespace nix {
@@ -643,7 +644,7 @@ namespace nix {
Expr * EvalState::parse(char * text, size_t length, FileOrigin origin,
- const PathView path, const PathView basePath, StaticEnv & staticEnv)
+ const PathView path, const PathView basePath, std::shared_ptr<StaticEnv> & staticEnv)
{
yyscan_t scanner;
ParseData data(*this);
@@ -706,7 +707,7 @@ Expr * EvalState::parseExprFromFile(const Path & path)
}
-Expr * EvalState::parseExprFromFile(const Path & path, StaticEnv & staticEnv)
+Expr * EvalState::parseExprFromFile(const Path & path, std::shared_ptr<StaticEnv> & staticEnv)
{
auto buffer = readFile(path);
// readFile should have left some extra space for terminators
@@ -715,7 +716,7 @@ Expr * EvalState::parseExprFromFile(const Path & path, StaticEnv & staticEnv)
}
-Expr * EvalState::parseExprFromString(std::string s, const Path & basePath, StaticEnv & staticEnv)
+Expr * EvalState::parseExprFromString(std::string s, const Path & basePath, std::shared_ptr<StaticEnv> & staticEnv)
{
s.append("\0\0", 2);
return parse(s.data(), s.size(), foString, "", basePath, staticEnv);
@@ -782,13 +783,14 @@ Path EvalState::findFile(SearchPath & searchPath, const std::string_view path, c
if (hasPrefix(path, "nix/"))
return concatStrings(corepkgsPrefix, path.substr(4));
- throw ThrownError({
+ debug_throw(ThrownError({
.msg = hintfmt(evalSettings.pureEval
? "cannot look up '<%s>' in pure evaluation mode (use '--impure' to override)"
: "file '%s' was not found in the Nix search path (add it using $NIX_PATH or -I)",
path),
.errPos = pos
- });
+ }));
+ return Path(); // should never execute due to debug_throw above.
}
diff --git a/src/libexpr/primops.cc b/src/libexpr/primops.cc
index 73817dbdd..c40c54247 100644
--- a/src/libexpr/primops.cc
+++ b/src/libexpr/primops.cc
@@ -46,7 +46,7 @@ StringMap EvalState::realiseContext(const PathSet & context)
auto [ctx, outputName] = decodeContext(*store, i);
auto ctxS = store->printStorePath(ctx);
if (!store->isValidPath(ctx))
- throw InvalidPathError(store->printStorePath(ctx));
+ debug_throw(InvalidPathError(store->printStorePath(ctx)));
if (!outputName.empty() && ctx.isDerivation()) {
drvs.push_back({ctx, {outputName}});
} else {
@@ -57,9 +57,9 @@ StringMap EvalState::realiseContext(const PathSet & context)
if (drvs.empty()) return {};
if (!evalSettings.enableImportFromDerivation)
- throw Error(
+ debug_throw(Error(
"cannot build '%1%' during evaluation because the option 'allow-import-from-derivation' is disabled",
- store->printStorePath(drvs.begin()->drvPath));
+ store->printStorePath(drvs.begin()->drvPath)));
/* Build/substitute the context. */
std::vector<DerivedPath> buildReqs;
@@ -71,8 +71,8 @@ StringMap EvalState::realiseContext(const PathSet & context)
auto outputPaths = store->queryDerivationOutputMap(drvPath);
for (auto & outputName : outputs) {
if (outputPaths.count(outputName) == 0)
- throw Error("derivation '%s' does not have an output named '%s'",
- store->printStorePath(drvPath), outputName);
+ debug_throw(Error("derivation '%s' does not have an output named '%s'",
+ store->printStorePath(drvPath), outputName));
res.insert_or_assign(
downstreamPlaceholder(*store, drvPath, outputName),
store->printStorePath(outputPaths.at(outputName))
@@ -215,11 +215,11 @@ static void import(EvalState & state, const Pos & pos, Value & vPath, Value * vS
Env * env = &state.allocEnv(vScope->attrs->size());
env->up = &state.baseEnv;
- StaticEnv staticEnv(false, &state.staticBaseEnv, vScope->attrs->size());
+ auto staticEnv = std::shared_ptr<StaticEnv>(new StaticEnv(false, state.staticBaseEnv.get(), vScope->attrs->size()));
unsigned int displ = 0;
for (auto & attr : *vScope->attrs) {
- staticEnv.vars.emplace_back(attr.name, displ);
+ staticEnv->vars.emplace_back(attr.name, displ);
env->values[displ++] = attr.value;
}
@@ -318,17 +318,17 @@ void prim_importNative(EvalState & state, const Pos & pos, Value * * args, Value
void *handle = dlopen(path.c_str(), RTLD_LAZY | RTLD_LOCAL);
if (!handle)
- throw EvalError("could not open '%1%': %2%", path, dlerror());
+ state.debug_throw(EvalError("could not open '%1%': %2%", path, dlerror()));
dlerror();
ValueInitializer func = (ValueInitializer) dlsym(handle, sym.c_str());
if(!func) {
char *message = dlerror();
if (message)
- throw EvalError("could not load symbol '%1%' from '%2%': %3%", sym, path, message);
+ state.debug_throw(EvalError("could not load symbol '%1%' from '%2%': %3%", sym, path, message));
else
- throw EvalError("symbol '%1%' from '%2%' resolved to NULL when a function pointer was expected",
- sym, path);
+ state.debug_throw(EvalError("symbol '%1%' from '%2%' resolved to NULL when a function pointer was expected",
+ sym, path));
}
(func)(state, v);
@@ -344,10 +344,10 @@ void prim_exec(EvalState & state, const Pos & pos, Value * * args, Value & v)
auto elems = args[0]->listElems();
auto count = args[0]->listSize();
if (count == 0) {
- throw EvalError({
+ state.debug_throw(EvalError({
.msg = hintfmt("at least one argument to 'exec' required"),
.errPos = pos
- });
+ }));
}
PathSet context;
auto program = state.coerceToString(pos, *elems[0], context, false, false).toOwned();
@@ -358,11 +358,11 @@ void prim_exec(EvalState & state, const Pos & pos, Value * * args, Value & v)
try {
auto _ = state.realiseContext(context); // FIXME: Handle CA derivations
} catch (InvalidPathError & e) {
- throw EvalError({
+ state.debug_throw(EvalError({
.msg = hintfmt("cannot execute '%1%', since path '%2%' is not valid",
program, e.path),
.errPos = pos
- });
+ }));
}
auto output = runProgram(program, true, commandArgs);
@@ -545,7 +545,7 @@ struct CompareValues
if (v1->type() == nInt && v2->type() == nFloat)
return v1->integer < v2->fpoint;
if (v1->type() != v2->type())
- throw EvalError("cannot compare %1% with %2%", showType(*v1), showType(*v2));
+ state.debug_throw(EvalError("cannot compare %1% with %2%", showType(*v1), showType(*v2)));
switch (v1->type()) {
case nInt:
return v1->integer < v2->integer;
@@ -567,7 +567,8 @@ struct CompareValues
}
}
default:
- throw EvalError("cannot compare %1% with %2%", showType(*v1), showType(*v2));
+ state.debug_throw(EvalError("cannot compare %1% with %2%", showType(*v1), showType(*v2)));
+ return false;
}
}
};
@@ -597,10 +598,10 @@ static Bindings::iterator getAttr(
Pos aPos = *attrSet->pos;
if (aPos == noPos) {
- throw TypeError({
+ state.debug_throw(TypeError({
.msg = errorMsg,
.errPos = pos,
- });
+ }));
} else {
auto e = TypeError({
.msg = errorMsg,
@@ -610,7 +611,7 @@ static Bindings::iterator getAttr(
// Adding another trace for the function name to make it clear
// which call received wrong arguments.
e.addTrace(pos, hintfmt("while invoking '%s'", funcName));
- throw e;
+ state.debug_throw(e);
}
}
@@ -664,10 +665,10 @@ static void prim_genericClosure(EvalState & state, const Pos & pos, Value * * ar
Bindings::iterator key =
e->attrs->find(state.sKey);
if (key == e->attrs->end())
- throw EvalError({
+ state.debug_throw(EvalError({
.msg = hintfmt("attribute 'key' required"),
.errPos = pos
- });
+ }));
state.forceValue(*key->value, pos);
if (!doneKeys.insert(key->value).second) continue;
@@ -723,6 +724,40 @@ static RegisterPrimOp primop_genericClosure(RegisterPrimOp::Info {
.fun = prim_genericClosure,
});
+
+static RegisterPrimOp primop_break({
+ .name = "break",
+ .args = {"v"},
+ .doc = R"(
+ In debug mode, pause Nix expression evaluation and enter the repl.
+ )",
+ .fun = [](EvalState & state, const Pos & pos, Value * * args, Value & v)
+ {
+ auto error = Error(ErrorInfo{
+ .level = lvlInfo,
+ .msg = hintfmt("breakpoint reached; value was %1%", *args[0]),
+ .errPos = pos,
+ });
+ if (debuggerHook && !state.debugTraces.empty())
+ {
+ auto &dt = state.debugTraces.front();
+ debuggerHook(&error, dt.env, dt.expr);
+
+ if (state.debugQuit) {
+ // if the user elects to quit the repl, throw an exception.
+ throw Error(ErrorInfo{
+ .level = lvlInfo,
+ .msg = hintfmt("quit from debugger"),
+ .errPos = noPos,
+ });
+ }
+
+ // returning the value we were passed.
+ v = *args[0];
+ }
+ }
+});
+
static RegisterPrimOp primop_abort({
.name = "abort",
.args = {"s"},
@@ -733,7 +768,7 @@ static RegisterPrimOp primop_abort({
{
PathSet context;
auto s = state.coerceToString(pos, *args[0], context).toOwned();
- throw Abort("evaluation aborted with the following error message: '%1%'", s);
+ state.debug_throw(Abort("evaluation aborted with the following error message: '%1%'", s));
}
});
@@ -751,7 +786,7 @@ static RegisterPrimOp primop_throw({
{
PathSet context;
auto s = state.coerceToString(pos, *args[0], context).toOwned();
- throw ThrownError(s);
+ state.debug_throw(ThrownError(s));
}
});
@@ -1006,37 +1041,37 @@ static void prim_derivationStrict(EvalState & state, const Pos & pos, Value * *
if (s == "recursive") ingestionMethod = FileIngestionMethod::Recursive;
else if (s == "flat") ingestionMethod = FileIngestionMethod::Flat;
else
- throw EvalError({
+ state.debug_throw(EvalError({
.msg = hintfmt("invalid value '%s' for 'outputHashMode' attribute", s),
.errPos = posDrvName
- });
+ }));
};
auto handleOutputs = [&](const Strings & ss) {
outputs.clear();
for (auto & j : ss) {
if (outputs.find(j) != outputs.end())
- throw EvalError({
+ state.debug_throw(EvalError({
.msg = hintfmt("duplicate derivation output '%1%'", j),
.errPos = posDrvName
- });
+ }));
/* !!! Check whether j is a valid attribute
name. */
/* Derivations cannot be named ‘drv’, because
then we'd have an attribute ‘drvPath’ in
the resulting set. */
if (j == "drv")
- throw EvalError({
+ state.debug_throw(EvalError({
.msg = hintfmt("invalid derivation output name 'drv'" ),
.errPos = posDrvName
- });
+ }));
outputs.insert(j);
}
if (outputs.empty())
- throw EvalError({
+ state.debug_throw(EvalError({
.msg = hintfmt("derivation cannot have an empty set of outputs"),
.errPos = posDrvName
- });
+ }));
};
try {
@@ -1161,23 +1196,23 @@ static void prim_derivationStrict(EvalState & state, const Pos & pos, Value * *
/* Do we have all required attributes? */
if (drv.builder == "")
- throw EvalError({
+ state.debug_throw(EvalError({
.msg = hintfmt("required attribute 'builder' missing"),
.errPos = posDrvName
- });
+ }));
if (drv.platform == "")
- throw EvalError({
+ state.debug_throw(EvalError({
.msg = hintfmt("required attribute 'system' missing"),
.errPos = posDrvName
- });
+ }));
/* Check whether the derivation name is valid. */
if (isDerivation(drvName))
- throw EvalError({
+ state.debug_throw(EvalError({
.msg = hintfmt("derivation names are not allowed to end in '%s'", drvExtension),
.errPos = posDrvName
- });
+ }));
if (outputHash) {
/* Handle fixed-output derivations.
@@ -1185,10 +1220,10 @@ static void prim_derivationStrict(EvalState & state, const Pos & pos, Value * *
Ignore `__contentAddressed` because fixed output derivations are
already content addressed. */
if (outputs.size() != 1 || *(outputs.begin()) != "out")
- throw Error({
+ state.debug_throw(Error({
.msg = hintfmt("multiple outputs are not supported in fixed-output derivations"),
.errPos = posDrvName
- });
+ }));
auto h = newHashAllowEmpty(*outputHash, parseHashTypeOpt(outputHashAlgo));
@@ -1351,10 +1386,10 @@ static RegisterPrimOp primop_toPath({
static void prim_storePath(EvalState & state, const Pos & pos, Value * * args, Value & v)
{
if (evalSettings.pureEval)
- throw EvalError({
+ state.debug_throw(EvalError({
.msg = hintfmt("'%s' is not allowed in pure evaluation mode", "builtins.storePath"),
.errPos = pos
- });
+ }));
PathSet context;
Path path = state.checkSourcePath(state.coerceToPath(pos, *args[0], context));
@@ -1363,10 +1398,10 @@ static void prim_storePath(EvalState & state, const Pos & pos, Value * * args, V
e.g. nix-push does the right thing. */
if (!state.store->isStorePath(path)) path = canonPath(path, true);
if (!state.store->isInStore(path))
- throw EvalError({
+ state.debug_throw(EvalError({
.msg = hintfmt("path '%1%' is not in the Nix store", path),
.errPos = pos
- });
+ }));
auto path2 = state.store->toStorePath(path).first;
if (!settings.readOnlyMode)
state.store->ensurePath(path2);
@@ -1469,7 +1504,7 @@ static void prim_readFile(EvalState & state, const Pos & pos, Value * * args, Va
auto path = realisePath(state, pos, *args[0]);
auto s = readFile(path);
if (s.find((char) 0) != std::string::npos)
- throw Error("the contents of the file '%1%' cannot be represented as a Nix string", path);
+ state.debug_throw(Error("the contents of the file '%1%' cannot be represented as a Nix string", path));
StorePathSet refs;
if (state.store->isInStore(path)) {
try {
@@ -1521,10 +1556,10 @@ static void prim_findFile(EvalState & state, const Pos & pos, Value * * args, Va
auto rewrites = state.realiseContext(context);
path = rewriteStrings(path, rewrites);
} catch (InvalidPathError & e) {
- throw EvalError({
+ state.debug_throw(EvalError({
.msg = hintfmt("cannot find '%1%', since path '%2%' is not valid", path, e.path),
.errPos = pos
- });
+ }));
}
@@ -1548,10 +1583,10 @@ static void prim_hashFile(EvalState & state, const Pos & pos, Value * * args, Va
auto type = state.forceStringNoCtx(*args[0], pos);
std::optional<HashType> ht = parseHashType(type);
if (!ht)
- throw Error({
+ state.debug_throw(Error({
.msg = hintfmt("unknown hash type '%1%'", type),
.errPos = pos
- });
+ }));
auto path = realisePath(state, pos, *args[1]);
@@ -1788,13 +1823,13 @@ static void prim_toFile(EvalState & state, const Pos & pos, Value * * args, Valu
for (auto path : context) {
if (path.at(0) != '/')
- throw EvalError( {
+ state.debug_throw(EvalError( {
.msg = hintfmt(
"in 'toFile': the file named '%1%' must not contain a reference "
"to a derivation but contains (%2%)",
name, path),
.errPos = pos
- });
+ }));
refs.insert(state.store->parseStorePath(path));
}
@@ -1951,7 +1986,7 @@ static void addPath(
? state.store->computeStorePathForPath(name, path, method, htSHA256, filter).first
: state.store->addToStore(name, path, method, htSHA256, filter, state.repair, refs);
if (expectedHash && expectedStorePath != dstPath)
- throw Error("store path mismatch in (possibly filtered) path added from '%s'", path);
+ state.debug_throw(Error("store path mismatch in (possibly filtered) path added from '%s'", path));
state.allowAndSetStorePathString(dstPath, v);
} else
state.allowAndSetStorePathString(*expectedStorePath, v);
@@ -1969,12 +2004,12 @@ static void prim_filterSource(EvalState & state, const Pos & pos, Value * * args
state.forceValue(*args[0], pos);
if (args[0]->type() != nFunction)
- throw TypeError({
+ state.debug_throw(TypeError({
.msg = hintfmt(
"first argument in call to 'filterSource' is not a function but %1%",
showType(*args[0])),
.errPos = pos
- });
+ }));
addPath(state, pos, std::string(baseNameOf(path)), path, args[0], FileIngestionMethod::Recursive, std::nullopt, v, context);
}
@@ -2058,16 +2093,16 @@ static void prim_path(EvalState & state, const Pos & pos, Value * * args, Value
else if (n == "sha256")
expectedHash = newHashAllowEmpty(state.forceStringNoCtx(*attr.value, *attr.pos), htSHA256);
else
- throw EvalError({
+ state.debug_throw(EvalError({
.msg = hintfmt("unsupported argument '%1%' to 'addPath'", attr.name),
.errPos = *attr.pos
- });
+ }));
}
if (path.empty())
- throw EvalError({
+ state.debug_throw(EvalError({
.msg = hintfmt("'path' required"),
.errPos = pos
- });
+ }));
if (name.empty())
name = baseNameOf(path);
@@ -2438,10 +2473,10 @@ static void prim_functionArgs(EvalState & state, const Pos & pos, Value * * args
return;
}
if (!args[0]->isLambda())
- throw TypeError({
+ state.debug_throw(TypeError({
.msg = hintfmt("'functionArgs' requires a function"),
.errPos = pos
- });
+ }));
if (!args[0]->lambda.fun->hasFormals()) {
v.mkAttrs(&state.emptyBindings);
@@ -2529,7 +2564,7 @@ static void prim_zipAttrsWith(EvalState & state, const Pos & pos, Value * * args
attrsSeen[attr.name].first++;
} catch (TypeError & e) {
e.addTrace(pos, hintfmt("while invoking '%s'", "zipAttrsWith"));
- throw;
+ state.debug_throw(e);
}
}
@@ -2616,10 +2651,10 @@ static void elemAt(EvalState & state, const Pos & pos, Value & list, int n, Valu
{
state.forceList(list, pos);
if (n < 0 || (unsigned int) n >= list.listSize())
- throw Error({
- .msg = hintfmt("list index %1% is out of bounds", n),
+ state.debug_throw(Error({
+ .msg = hintfmt("list index %1% is out of boundz", n),
.errPos = pos
- });
+ }));
state.forceValue(*list.listElems()[n], pos);
v = *list.listElems()[n];
}
@@ -2664,10 +2699,10 @@ static void prim_tail(EvalState & state, const Pos & pos, Value * * args, Value
{
state.forceList(*args[0], pos);
if (args[0]->listSize() == 0)
- throw Error({
+ state.debug_throw(Error({
.msg = hintfmt("'tail' called on an empty list"),
.errPos = pos
- });
+ }));
state.mkList(v, args[0]->listSize() - 1);
for (unsigned int n = 0; n < v.listSize(); ++n)
@@ -2902,10 +2937,10 @@ static void prim_genList(EvalState & state, const Pos & pos, Value * * args, Val
auto len = state.forceInt(*args[1], pos);
if (len < 0)
- throw EvalError({
+ state.debug_throw(EvalError({
.msg = hintfmt("cannot create list of size %1%", len),
.errPos = pos
- });
+ }));
state.mkList(v, len);
@@ -3114,7 +3149,7 @@ static void prim_concatMap(EvalState & state, const Pos & pos, Value * * args, V
state.forceList(lists[n], lists[n].determinePos(args[0]->determinePos(pos)));
} catch (TypeError &e) {
e.addTrace(pos, hintfmt("while invoking '%s'", "concatMap"));
- throw;
+ state.debug_throw(e);
}
len += lists[n].listSize();
}
@@ -3209,10 +3244,10 @@ static void prim_div(EvalState & state, const Pos & pos, Value * * args, Value &
NixFloat f2 = state.forceFloat(*args[1], pos);
if (f2 == 0)
- throw EvalError({
+ state.debug_throw(EvalError({
.msg = hintfmt("division by zero"),
.errPos = pos
- });
+ }));
if (args[0]->type() == nFloat || args[1]->type() == nFloat) {
v.mkFloat(state.forceFloat(*args[0], pos) / state.forceFloat(*args[1], pos));
@@ -3221,10 +3256,10 @@ static void prim_div(EvalState & state, const Pos & pos, Value * * args, Value &
NixInt i2 = state.forceInt(*args[1], pos);
/* Avoid division overflow as it might raise SIGFPE. */
if (i1 == std::numeric_limits<NixInt>::min() && i2 == -1)
- throw EvalError({
+ state.debug_throw(EvalError({
.msg = hintfmt("overflow in integer division"),
.errPos = pos
- });
+ }));
v.mkInt(i1 / i2);
}
@@ -3352,10 +3387,10 @@ static void prim_substring(EvalState & state, const Pos & pos, Value * * args, V
auto s = state.coerceToString(pos, *args[2], context);
if (start < 0)
- throw EvalError({
+ state.debug_throw(EvalError({
.msg = hintfmt("negative start position in 'substring'"),
.errPos = pos
- });
+ }));
v.mkString((unsigned int) start >= s->size() ? "" : s->substr(start, len), context);
}
@@ -3403,10 +3438,10 @@ static void prim_hashString(EvalState & state, const Pos & pos, Value * * args,
auto type = state.forceStringNoCtx(*args[0], pos);
std::optional<HashType> ht = parseHashType(type);
if (!ht)
- throw Error({
+ state.debug_throw(Error({
.msg = hintfmt("unknown hash type '%1%'", type),
.errPos = pos
- });
+ }));
PathSet context; // discarded
auto s = state.forceString(*args[1], context, pos);
@@ -3476,15 +3511,15 @@ void prim_match(EvalState & state, const Pos & pos, Value * * args, Value & v)
} catch (std::regex_error &e) {
if (e.code() == std::regex_constants::error_space) {
// limit is _GLIBCXX_REGEX_STATE_LIMIT for libstdc++
- throw EvalError({
+ state.debug_throw(EvalError({
.msg = hintfmt("memory limit exceeded by regular expression '%s'", re),
.errPos = pos
- });
+ }));
} else {
- throw EvalError({
+ state.debug_throw(EvalError({
.msg = hintfmt("invalid regular expression '%s'", re),
.errPos = pos
- });
+ }));
}
}
}
@@ -3581,15 +3616,15 @@ void prim_split(EvalState & state, const Pos & pos, Value * * args, Value & v)
} catch (std::regex_error &e) {
if (e.code() == std::regex_constants::error_space) {
// limit is _GLIBCXX_REGEX_STATE_LIMIT for libstdc++
- throw EvalError({
+ state.debug_throw(EvalError({
.msg = hintfmt("memory limit exceeded by regular expression '%s'", re),
.errPos = pos
- });
+ }));
} else {
- throw EvalError({
+ state.debug_throw(EvalError({
.msg = hintfmt("invalid regular expression '%s'", re),
.errPos = pos
- });
+ }));
}
}
}
@@ -3666,10 +3701,10 @@ static void prim_replaceStrings(EvalState & state, const Pos & pos, Value * * ar
state.forceList(*args[0], pos);
state.forceList(*args[1], pos);
if (args[0]->listSize() != args[1]->listSize())
- throw EvalError({
+ state.debug_throw(EvalError({
.msg = hintfmt("'from' and 'to' arguments to 'replaceStrings' have different lengths"),
.errPos = pos
- });
+ }));
std::vector<std::string> from;
from.reserve(args[0]->listSize());
@@ -3922,7 +3957,7 @@ void EvalState::createBaseEnv()
because attribute lookups expect it to be sorted. */
baseEnv.values[0]->attrs->sort();
- staticBaseEnv.sort();
+ staticBaseEnv->sort();
/* Note: we have to initialize the 'derivation' constant *after*
building baseEnv/staticBaseEnv because it uses 'builtins'. */
diff --git a/src/libexpr/primops/fetchTree.cc b/src/libexpr/primops/fetchTree.cc
index 42c98e312..bae0fb1e4 100644
--- a/src/libexpr/primops/fetchTree.cc
+++ b/src/libexpr/primops/fetchTree.cc
@@ -108,16 +108,16 @@ static void fetchTree(
if (auto aType = args[0]->attrs->get(state.sType)) {
if (type)
- throw Error({
+ state.debug_throw(EvalError({
.msg = hintfmt("unexpected attribute 'type'"),
.errPos = pos
- });
+ }));
type = state.forceStringNoCtx(*aType->value, *aType->pos);
} else if (!type)
- throw Error({
+ state.debug_throw(EvalError({
.msg = hintfmt("attribute 'type' is missing in call to 'fetchTree'"),
.errPos = pos
- });
+ }));
attrs.emplace("type", type.value());
@@ -138,16 +138,16 @@ static void fetchTree(
else if (attr.value->type() == nInt)
attrs.emplace(attr.name, uint64_t(attr.value->integer));
else
- throw TypeError("fetchTree argument '%s' is %s while a string, Boolean or integer is expected",
- attr.name, showType(*attr.value));
+ state.debug_throw(TypeError("fetchTree argument '%s' is %s while a string, Boolean or integer is expected",
+ attr.name, showType(*attr.value)));
}
if (!params.allowNameArgument)
if (auto nameIter = attrs.find("name"); nameIter != attrs.end())
- throw Error({
- .msg = hintfmt("attribute 'name' isn't supported in call to 'fetchTree'"),
+ state.debug_throw(EvalError({
+ .msg = hintfmt("attribute 'name' isn’t supported in call to 'fetchTree'"),
.errPos = pos
- });
+ }));
input = fetchers::Input::fromAttrs(std::move(attrs));
} else {
@@ -167,7 +167,7 @@ static void fetchTree(
input = lookupInRegistries(state.store, input).first;
if (evalSettings.pureEval && !input.isLocked())
- throw Error("in pure evaluation mode, 'fetchTree' requires a locked input, at %s", pos);
+ state.debug_throw(EvalError("in pure evaluation mode, 'fetchTree' requires a locked input, at %s", pos));
auto [tree, input2] = input.fetch(state.store);
@@ -206,17 +206,17 @@ static void fetch(EvalState & state, const Pos & pos, Value * * args, Value & v,
else if (n == "name")
name = state.forceStringNoCtx(*attr.value, *attr.pos);
else
- throw EvalError({
+ state.debug_throw(EvalError({
.msg = hintfmt("unsupported argument '%s' to '%s'", attr.name, who),
.errPos = *attr.pos
- });
+ }));
}
if (!url)
- throw EvalError({
+ state.debug_throw(EvalError({
.msg = hintfmt("'url' argument required"),
.errPos = pos
- });
+ }));
} else
url = state.forceStringNoCtx(*args[0], pos);
@@ -228,7 +228,7 @@ static void fetch(EvalState & state, const Pos & pos, Value * * args, Value & v,
name = baseNameOf(*url);
if (evalSettings.pureEval && !expectedHash)
- throw Error("in pure evaluation mode, '%s' requires a 'sha256' argument", who);
+ state.debug_throw(EvalError("in pure evaluation mode, '%s' requires a 'sha256' argument", who));
// early exit if pinned and already in the store
if (expectedHash && expectedHash->type == htSHA256) {
@@ -255,8 +255,8 @@ static void fetch(EvalState & state, const Pos & pos, Value * * args, Value & v,
? state.store->queryPathInfo(storePath)->narHash
: hashFile(htSHA256, state.store->toRealPath(storePath));
if (hash != *expectedHash)
- throw Error((unsigned int) 102, "hash mismatch in file downloaded from '%s':\n specified: %s\n got: %s",
- *url, expectedHash->to_string(Base32, true), hash.to_string(Base32, true));
+ state.debug_throw(EvalError((unsigned int) 102, "hash mismatch in file downloaded from '%s':\n specified: %s\n got: %s",
+ *url, expectedHash->to_string(Base32, true), hash.to_string(Base32, true)));
}
state.allowAndSetStorePathString(storePath, v);
diff --git a/src/libexpr/value-to-json.cc b/src/libexpr/value-to-json.cc
index 7b35abca2..d8bf73cd5 100644
--- a/src/libexpr/value-to-json.cc
+++ b/src/libexpr/value-to-json.cc
@@ -85,7 +85,7 @@ void printValueAsJSON(EvalState & state, bool strict,
.errPos = v.determinePos(pos)
});
e.addTrace(pos, hintfmt("message for the trace"));
- throw e;
+ state.debug_throw(e);
}
}
@@ -99,7 +99,7 @@ void printValueAsJSON(EvalState & state, bool strict,
void ExternalValueBase::printValueAsJSON(EvalState & state, bool strict,
JSONPlaceholder & out, PathSet & context) const
{
- throw TypeError("cannot convert %1% to JSON", showType());
+ state.debug_throw(TypeError("cannot convert %1% to JSON", showType()));
}
diff --git a/src/libutil/error.hh b/src/libutil/error.hh
index 6a757f9ad..d17575f47 100644
--- a/src/libutil/error.hh
+++ b/src/libutil/error.hh
@@ -102,6 +102,15 @@ struct ErrPos {
}
};
+std::optional<LinesOfCode> getCodeLines(const ErrPos & errPos);
+void printCodeLines(std::ostream & out,
+ const std::string & prefix,
+ const ErrPos & errPos,
+ const LinesOfCode & loc);
+
+void printAtPos(const ErrPos & pos, std::ostream & out);
+
+
struct Trace {
std::optional<ErrPos> pos;
hintformat hint;