2020-05-09 03:18:28 +03:00
|
|
|
#include "error.hh"
|
2023-10-25 07:43:36 +03:00
|
|
|
#include "environment-variables.hh"
|
|
|
|
#include "signals.hh"
|
|
|
|
#include "terminal.hh"
|
2020-03-22 20:25:47 +02:00
|
|
|
|
|
|
|
#include <iostream>
|
2020-03-25 18:52:03 +02:00
|
|
|
#include <optional>
|
2020-04-18 00:07:44 +03:00
|
|
|
#include "serialise.hh"
|
2020-05-14 21:28:18 +03:00
|
|
|
#include <sstream>
|
2020-03-22 20:25:47 +02:00
|
|
|
|
2020-04-29 06:06:08 +03:00
|
|
|
namespace nix {
|
2020-03-22 20:25:47 +02:00
|
|
|
|
2023-01-19 14:23:04 +02:00
|
|
|
void BaseError::addTrace(std::shared_ptr<AbstractPos> && e, hintformat hint, bool frame)
|
2020-06-19 00:25:26 +03:00
|
|
|
{
|
2023-01-19 14:23:04 +02:00
|
|
|
err.traces.push_front(Trace { .pos = std::move(e), .hint = hint, .frame = frame });
|
2020-06-19 00:25:26 +03:00
|
|
|
}
|
|
|
|
|
2023-08-11 18:22:55 +03:00
|
|
|
void throwExceptionSelfCheck(){
|
|
|
|
// This is meant to be caught in initLibUtil()
|
|
|
|
throw SysError("C++ exception handling is broken. This would appear to be a problem with the way Nix was compiled and/or linked and/or loaded.");
|
|
|
|
}
|
|
|
|
|
2020-06-04 20:53:19 +03:00
|
|
|
// c++ std::exception descendants must have a 'const char* what()' function.
|
|
|
|
// This stringifies the error and caches it for use by what(), or similarly by msg().
|
2022-02-25 17:00:00 +02:00
|
|
|
const std::string & BaseError::calcWhat() const
|
2020-05-14 21:28:18 +03:00
|
|
|
{
|
|
|
|
if (what_.has_value())
|
|
|
|
return *what_;
|
|
|
|
else {
|
|
|
|
std::ostringstream oss;
|
2021-12-28 14:53:21 +02:00
|
|
|
showErrorInfo(oss, err, loggerSettings.showTrace);
|
2020-05-14 21:28:18 +03:00
|
|
|
what_ = oss.str();
|
|
|
|
return *what_;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-25 17:00:00 +02:00
|
|
|
std::optional<std::string> ErrorInfo::programName = std::nullopt;
|
2020-03-22 20:25:47 +02:00
|
|
|
|
2022-12-13 01:48:04 +02:00
|
|
|
std::ostream & operator <<(std::ostream & os, const hintformat & hf)
|
2020-04-15 19:09:43 +03:00
|
|
|
{
|
|
|
|
return os << hf.str();
|
|
|
|
}
|
|
|
|
|
2022-12-13 01:48:04 +02:00
|
|
|
std::ostream & operator <<(std::ostream & str, const AbstractPos & pos)
|
2020-03-22 20:25:47 +02:00
|
|
|
{
|
2022-12-13 01:48:04 +02:00
|
|
|
pos.print(str);
|
|
|
|
str << ":" << pos.line;
|
|
|
|
if (pos.column > 0)
|
|
|
|
str << ":" << pos.column;
|
|
|
|
return str;
|
2020-03-22 20:25:47 +02:00
|
|
|
}
|
|
|
|
|
2022-12-13 01:48:04 +02:00
|
|
|
std::optional<LinesOfCode> AbstractPos::getCodeLines() const
|
2020-05-21 02:25:02 +03:00
|
|
|
{
|
2022-12-13 01:48:04 +02:00
|
|
|
if (line == 0)
|
2020-06-24 17:33:53 +03:00
|
|
|
return std::nullopt;
|
2020-05-21 02:25:02 +03:00
|
|
|
|
2022-12-13 01:48:04 +02:00
|
|
|
if (auto source = getSource()) {
|
|
|
|
|
|
|
|
std::istringstream iss(*source);
|
2020-05-21 02:25:02 +03:00
|
|
|
// count the newlines.
|
|
|
|
int count = 0;
|
2022-12-13 01:48:04 +02:00
|
|
|
std::string curLine;
|
|
|
|
int pl = line - 1;
|
2020-06-24 17:33:53 +03:00
|
|
|
|
|
|
|
LinesOfCode loc;
|
2020-07-06 20:54:53 +03:00
|
|
|
|
2022-12-13 01:48:04 +02:00
|
|
|
do {
|
|
|
|
std::getline(iss, curLine);
|
2020-05-21 02:25:02 +03:00
|
|
|
++count;
|
2020-06-19 00:25:26 +03:00
|
|
|
if (count < pl)
|
|
|
|
;
|
2020-05-21 02:25:02 +03:00
|
|
|
else if (count == pl) {
|
2022-12-13 01:48:04 +02:00
|
|
|
loc.prevLineOfCode = curLine;
|
2020-05-21 02:25:02 +03:00
|
|
|
} else if (count == pl + 1) {
|
2022-12-13 01:48:04 +02:00
|
|
|
loc.errLineOfCode = curLine;
|
2020-05-21 02:25:02 +03:00
|
|
|
} else if (count == pl + 2) {
|
2022-12-13 01:48:04 +02:00
|
|
|
loc.nextLineOfCode = curLine;
|
2020-05-21 02:25:02 +03:00
|
|
|
break;
|
|
|
|
}
|
2020-05-21 07:18:26 +03:00
|
|
|
|
|
|
|
if (!iss.good())
|
|
|
|
break;
|
2020-05-21 02:25:02 +03:00
|
|
|
} while (true);
|
2020-06-24 17:33:53 +03:00
|
|
|
|
|
|
|
return loc;
|
2020-05-21 02:25:02 +03:00
|
|
|
}
|
2022-12-13 01:48:04 +02:00
|
|
|
|
|
|
|
return std::nullopt;
|
2020-05-21 02:25:02 +03:00
|
|
|
}
|
|
|
|
|
2020-07-01 07:05:21 +03:00
|
|
|
// print lines of code to the ostream, indicating the error column.
|
2020-10-07 17:33:19 +03:00
|
|
|
void printCodeLines(std::ostream & out,
|
2022-02-25 17:00:00 +02:00
|
|
|
const std::string & prefix,
|
2022-12-13 01:48:04 +02:00
|
|
|
const AbstractPos & errPos,
|
2020-10-07 17:33:19 +03:00
|
|
|
const LinesOfCode & loc)
|
2020-03-22 20:25:47 +02:00
|
|
|
{
|
2020-04-08 18:48:21 +03:00
|
|
|
// previous line of code.
|
2020-06-24 17:33:53 +03:00
|
|
|
if (loc.prevLineOfCode.has_value()) {
|
2020-06-15 15:12:39 +03:00
|
|
|
out << std::endl
|
2020-06-03 23:47:00 +03:00
|
|
|
<< fmt("%1% %|2$5d|| %3%",
|
2020-06-19 00:25:26 +03:00
|
|
|
prefix,
|
2020-06-24 17:33:53 +03:00
|
|
|
(errPos.line - 1),
|
|
|
|
*loc.prevLineOfCode);
|
2020-04-08 18:48:21 +03:00
|
|
|
}
|
2020-04-03 01:02:40 +03:00
|
|
|
|
2020-06-24 17:33:53 +03:00
|
|
|
if (loc.errLineOfCode.has_value()) {
|
2020-05-09 03:18:28 +03:00
|
|
|
// line of code containing the error.
|
2020-06-03 23:47:00 +03:00
|
|
|
out << std::endl
|
|
|
|
<< fmt("%1% %|2$5d|| %3%",
|
2020-06-19 00:25:26 +03:00
|
|
|
prefix,
|
2020-06-24 17:33:53 +03:00
|
|
|
(errPos.line),
|
|
|
|
*loc.errLineOfCode);
|
2020-05-09 03:18:28 +03:00
|
|
|
// error arrows for the column range.
|
2020-06-24 17:33:53 +03:00
|
|
|
if (errPos.column > 0) {
|
|
|
|
int start = errPos.column;
|
2020-05-09 03:18:28 +03:00
|
|
|
std::string spaces;
|
|
|
|
for (int i = 0; i < start; ++i) {
|
|
|
|
spaces.append(" ");
|
|
|
|
}
|
2020-03-22 20:25:47 +02:00
|
|
|
|
2020-05-09 03:18:28 +03:00
|
|
|
std::string arrows("^");
|
2020-04-02 01:20:20 +03:00
|
|
|
|
2020-06-03 23:47:00 +03:00
|
|
|
out << std::endl
|
|
|
|
<< fmt("%1% |%2%" ANSI_RED "%3%" ANSI_NORMAL,
|
2020-06-19 00:25:26 +03:00
|
|
|
prefix,
|
|
|
|
spaces,
|
|
|
|
arrows);
|
2020-05-09 03:18:28 +03:00
|
|
|
}
|
2020-03-22 20:25:47 +02:00
|
|
|
}
|
|
|
|
|
2020-04-08 18:48:21 +03:00
|
|
|
// next line of code.
|
2020-06-24 17:33:53 +03:00
|
|
|
if (loc.nextLineOfCode.has_value()) {
|
2020-06-03 23:47:00 +03:00
|
|
|
out << std::endl
|
|
|
|
<< fmt("%1% %|2$5d|| %3%",
|
2020-06-19 00:25:26 +03:00
|
|
|
prefix,
|
2020-06-24 17:33:53 +03:00
|
|
|
(errPos.line + 1),
|
|
|
|
*loc.nextLineOfCode);
|
2020-04-08 18:48:21 +03:00
|
|
|
}
|
2020-03-22 20:25:47 +02:00
|
|
|
}
|
|
|
|
|
2021-01-21 01:27:36 +02:00
|
|
|
static std::string indent(std::string_view indentFirst, std::string_view indentRest, std::string_view s)
|
2020-03-22 20:25:47 +02:00
|
|
|
{
|
2021-01-21 01:27:36 +02:00
|
|
|
std::string res;
|
|
|
|
bool first = true;
|
|
|
|
|
|
|
|
while (!s.empty()) {
|
|
|
|
auto end = s.find('\n');
|
|
|
|
if (!first) res += "\n";
|
2021-01-21 01:49:29 +02:00
|
|
|
res += chomp(std::string(first ? indentFirst : indentRest) + std::string(s.substr(0, end)));
|
2021-01-21 01:27:36 +02:00
|
|
|
first = false;
|
|
|
|
if (end == s.npos) break;
|
|
|
|
s = s.substr(end + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
2020-03-22 20:25:47 +02:00
|
|
|
|
2023-09-03 15:08:03 +03:00
|
|
|
/**
|
|
|
|
* A development aid for finding missing positions, to improve error messages. Example use:
|
|
|
|
*
|
|
|
|
* NIX_DEVELOPER_SHOW_UNKNOWN_LOCATIONS=1 _NIX_TEST_ACCEPT=1 make tests/lang.sh.test
|
|
|
|
* git diff -U20 tests
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
static bool printUnknownLocations = getEnv("_NIX_DEVELOPER_SHOW_UNKNOWN_LOCATIONS").has_value();
|
|
|
|
|
2023-09-03 15:00:55 +03:00
|
|
|
/**
|
|
|
|
* Print a position, if it is known.
|
|
|
|
*
|
|
|
|
* @return true if a position was printed.
|
|
|
|
*/
|
|
|
|
static bool printPosMaybe(std::ostream & oss, std::string_view indent, const std::shared_ptr<AbstractPos> & pos) {
|
|
|
|
bool hasPos = pos && *pos;
|
|
|
|
if (hasPos) {
|
|
|
|
oss << "\n" << indent << ANSI_BLUE << "at " ANSI_WARNING << *pos << ANSI_NORMAL << ":";
|
|
|
|
|
|
|
|
if (auto loc = pos->getCodeLines()) {
|
|
|
|
oss << "\n";
|
|
|
|
printCodeLines(oss, "", *pos, *loc);
|
|
|
|
oss << "\n";
|
|
|
|
}
|
2023-09-03 15:08:03 +03:00
|
|
|
} else if (printUnknownLocations) {
|
|
|
|
oss << "\n" << indent << ANSI_BLUE << "at " ANSI_RED << "UNKNOWN LOCATION" << ANSI_NORMAL << "\n";
|
2023-09-03 15:00:55 +03:00
|
|
|
}
|
|
|
|
return hasPos;
|
|
|
|
}
|
|
|
|
|
2021-01-21 01:27:36 +02:00
|
|
|
std::ostream & showErrorInfo(std::ostream & out, const ErrorInfo & einfo, bool showTrace)
|
|
|
|
{
|
|
|
|
std::string prefix;
|
2020-04-03 01:02:40 +03:00
|
|
|
switch (einfo.level) {
|
2020-05-09 03:18:28 +03:00
|
|
|
case Verbosity::lvlError: {
|
2021-01-21 01:27:36 +02:00
|
|
|
prefix = ANSI_RED "error";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Verbosity::lvlNotice: {
|
|
|
|
prefix = ANSI_RED "note";
|
2020-05-09 03:18:28 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Verbosity::lvlWarn: {
|
2021-09-14 11:38:10 +03:00
|
|
|
prefix = ANSI_WARNING "warning";
|
2020-05-09 03:18:28 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Verbosity::lvlInfo: {
|
2021-01-21 01:27:36 +02:00
|
|
|
prefix = ANSI_GREEN "info";
|
2020-05-09 03:18:28 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Verbosity::lvlTalkative: {
|
2021-01-21 01:27:36 +02:00
|
|
|
prefix = ANSI_GREEN "talk";
|
2020-05-09 03:18:28 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Verbosity::lvlChatty: {
|
2021-01-21 01:27:36 +02:00
|
|
|
prefix = ANSI_GREEN "chat";
|
2020-05-09 03:18:28 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Verbosity::lvlVomit: {
|
2021-01-21 01:27:36 +02:00
|
|
|
prefix = ANSI_GREEN "vomit";
|
2020-05-09 03:18:28 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Verbosity::lvlDebug: {
|
2021-09-14 11:38:10 +03:00
|
|
|
prefix = ANSI_WARNING "debug";
|
2020-05-09 03:18:28 +03:00
|
|
|
break;
|
|
|
|
}
|
2021-01-21 01:27:36 +02:00
|
|
|
default:
|
|
|
|
assert(false);
|
2020-03-22 20:25:47 +02:00
|
|
|
}
|
|
|
|
|
2021-01-21 01:27:36 +02:00
|
|
|
// FIXME: show the program name as part of the trace?
|
|
|
|
if (einfo.programName && einfo.programName != ErrorInfo::programName)
|
|
|
|
prefix += fmt(" [%s]:" ANSI_NORMAL " ", einfo.programName.value_or(""));
|
2020-04-25 21:05:26 +03:00
|
|
|
else
|
2021-01-21 01:27:36 +02:00
|
|
|
prefix += ":" ANSI_NORMAL " ";
|
2020-03-22 20:25:47 +02:00
|
|
|
|
2021-01-21 01:27:36 +02:00
|
|
|
std::ostringstream oss;
|
2020-06-19 00:25:26 +03:00
|
|
|
|
2023-01-19 14:23:04 +02:00
|
|
|
/*
|
|
|
|
* Traces
|
|
|
|
* ------
|
|
|
|
*
|
|
|
|
* The semantics of traces is a bit weird. We have only one option to
|
|
|
|
* print them and to make them verbose (--show-trace). In the code they
|
|
|
|
* are always collected, but they are not printed by default. The code
|
|
|
|
* also collects more traces when the option is on. This means that there
|
|
|
|
* is no way to print the simplified traces at all.
|
|
|
|
*
|
|
|
|
* I (layus) designed the code to attach positions to a restricted set of
|
|
|
|
* messages. This means that we have a lot of traces with no position at
|
|
|
|
* all, including most of the base error messages. For example "type
|
|
|
|
* error: found a string while a set was expected" has no position, but
|
|
|
|
* will come with several traces detailing it's precise relation to the
|
|
|
|
* closest know position. This makes erroring without printing traces
|
|
|
|
* quite useless.
|
|
|
|
*
|
|
|
|
* This is why I introduced the idea to always print a few traces on
|
|
|
|
* error. The number 3 is quite arbitrary, and was selected so as not to
|
|
|
|
* clutter the console on error. For the same reason, a trace with an
|
|
|
|
* error position takes more space, and counts as two traces towards the
|
|
|
|
* limit.
|
|
|
|
*
|
|
|
|
* The rest is truncated, unless --show-trace is passed. This preserves
|
|
|
|
* the same bad semantics of --show-trace to both show the trace and
|
|
|
|
* augment it with new data. Not too sure what is the best course of
|
|
|
|
* action.
|
|
|
|
*
|
|
|
|
* The issue is that it is fundamentally hard to provide a trace for a
|
|
|
|
* lazy language. The trace will only cover the current spine of the
|
|
|
|
* evaluation, missing things that have been evaluated before. For
|
|
|
|
* example, most type errors are hard to inspect because there is not
|
|
|
|
* trace for the faulty value. These errors should really print the faulty
|
|
|
|
* value itself.
|
|
|
|
*
|
|
|
|
* In function calls, the --show-trace flag triggers extra traces for each
|
|
|
|
* function invocation. These work as scopes, allowing to follow the
|
|
|
|
* current spine of the evaluation graph. Without that flag, the error
|
|
|
|
* trace should restrict itself to a restricted prefix of that trace,
|
|
|
|
* until the first scope. If we ever get to such a precise error
|
|
|
|
* reporting, there would be no need to add an arbitrary limit here. We
|
|
|
|
* could always print the full trace, and it would just be small without
|
|
|
|
* the flag.
|
|
|
|
*
|
|
|
|
* One idea I had is for XxxError.addTrace() to perform nothing if one
|
|
|
|
* scope has already been traced. Alternatively, we could stop here when
|
|
|
|
* we encounter such a scope instead of after an arbitrary number of
|
|
|
|
* traces. This however requires to augment traces with the notion of
|
|
|
|
* "scope".
|
|
|
|
*
|
|
|
|
* This is particularly visible in code like evalAttrs(...) where we have
|
|
|
|
* to make a decision between the two following options.
|
|
|
|
*
|
|
|
|
* ``` long traces
|
|
|
|
* inline void EvalState::evalAttrs(Env & env, Expr * e, Value & v, const Pos & pos, std::string_view errorCtx)
|
|
|
|
* {
|
|
|
|
* try {
|
|
|
|
* e->eval(*this, env, v);
|
|
|
|
* if (v.type() != nAttrs)
|
|
|
|
* throwTypeError("value is %1% while a set was expected", v);
|
|
|
|
* } catch (Error & e) {
|
|
|
|
* e.addTrace(pos, errorCtx);
|
|
|
|
* throw;
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* ``` short traces
|
|
|
|
* inline void EvalState::evalAttrs(Env & env, Expr * e, Value & v, const Pos & pos, std::string_view errorCtx)
|
|
|
|
* {
|
|
|
|
* e->eval(*this, env, v);
|
|
|
|
* try {
|
|
|
|
* if (v.type() != nAttrs)
|
|
|
|
* throwTypeError("value is %1% while a set was expected", v);
|
|
|
|
* } catch (Error & e) {
|
|
|
|
* e.addTrace(pos, errorCtx);
|
|
|
|
* throw;
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* The second example can be rewritten more concisely, but kept in this
|
|
|
|
* form to highlight the symmetry. The first option adds more information,
|
|
|
|
* because whatever caused an error down the line, in the generic eval
|
|
|
|
* function, will get annotated with the code location that uses and
|
|
|
|
* required it. The second option is less verbose, but does not provide
|
|
|
|
* any context at all as to where and why a failing value was required.
|
|
|
|
*
|
|
|
|
* Scopes would fix that, by adding context only when --show-trace is
|
|
|
|
* passed, and keeping the trace terse otherwise.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Enough indent to align with with the `... `
|
|
|
|
// prepended to each element of the trace
|
|
|
|
auto ellipsisIndent = " ";
|
|
|
|
|
|
|
|
bool frameOnly = false;
|
|
|
|
if (!einfo.traces.empty()) {
|
|
|
|
size_t count = 0;
|
2022-12-09 19:36:25 +02:00
|
|
|
for (const auto & trace : einfo.traces) {
|
2023-03-21 23:16:44 +02:00
|
|
|
if (trace.hint.str().empty()) continue;
|
|
|
|
if (frameOnly && !trace.frame) continue;
|
|
|
|
|
2023-01-19 14:23:04 +02:00
|
|
|
if (!showTrace && count > 3) {
|
|
|
|
oss << "\n" << ANSI_WARNING "(stack trace truncated; use '--show-trace' to show the full trace)" ANSI_NORMAL << "\n";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
count++;
|
|
|
|
frameOnly = trace.frame;
|
|
|
|
|
2022-12-09 19:36:25 +02:00
|
|
|
oss << "\n" << "… " << trace.hint.str() << "\n";
|
2020-07-06 19:51:48 +03:00
|
|
|
|
2023-09-03 15:00:55 +03:00
|
|
|
if (printPosMaybe(oss, ellipsisIndent, trace.pos))
|
2023-01-19 14:23:04 +02:00
|
|
|
count++;
|
2020-06-23 18:36:58 +03:00
|
|
|
}
|
2022-11-22 18:45:58 +02:00
|
|
|
oss << "\n" << prefix;
|
|
|
|
}
|
|
|
|
|
2021-01-21 01:27:36 +02:00
|
|
|
oss << einfo.msg << "\n";
|
|
|
|
|
2023-09-03 15:00:55 +03:00
|
|
|
printPosMaybe(oss, "", einfo.errPos);
|
2020-05-21 02:25:02 +03:00
|
|
|
|
2022-03-03 11:50:35 +02:00
|
|
|
auto suggestions = einfo.suggestions.trim();
|
2022-12-13 01:48:04 +02:00
|
|
|
if (!suggestions.suggestions.empty()) {
|
2022-03-03 11:50:35 +02:00
|
|
|
oss << "Did you mean " <<
|
2022-03-07 11:04:57 +02:00
|
|
|
suggestions.trim() <<
|
2022-03-03 11:50:35 +02:00
|
|
|
"?" << std::endl;
|
2020-06-23 18:36:58 +03:00
|
|
|
}
|
|
|
|
|
2021-01-21 01:27:36 +02:00
|
|
|
out << indent(prefix, std::string(filterANSIEscapes(prefix, true).size(), ' '), chomp(oss.str()));
|
|
|
|
|
2020-04-18 00:07:44 +03:00
|
|
|
return out;
|
|
|
|
}
|
2020-03-22 20:25:47 +02:00
|
|
|
}
|