From 0e49de6a2b1a751c744c9e608968e9e32a042db8 Mon Sep 17 00:00:00 2001 From: Ben Burdette Date: Thu, 21 May 2020 14:28:45 -0600 Subject: [PATCH] position for stdin, string; (string) for trace; fix tests --- Makefile | 3 +- src/error-demo/error-demo.cc | 8 ++--- src/libexpr/nixexpr.cc | 17 ++++++++++- src/libutil/error.cc | 59 ++++++++++++++++++------------------ tests/misc.sh | 11 +++++-- 5 files changed, 60 insertions(+), 38 deletions(-) diff --git a/Makefile b/Makefile index 0ba011e2a..05c4bdc8a 100644 --- a/Makefile +++ b/Makefile @@ -17,7 +17,8 @@ makefiles = \ misc/upstart/local.mk \ doc/manual/local.mk \ tests/local.mk \ - tests/plugins/local.mk + tests/plugins/local.mk \ + src/error-demo/local.mk -include Makefile.config diff --git a/src/error-demo/error-demo.cc b/src/error-demo/error-demo.cc index 0216092e3..8c73a8c83 100644 --- a/src/error-demo/error-demo.cc +++ b/src/error-demo/error-demo.cc @@ -118,7 +118,7 @@ int main() "yellow", "values"), .nixCode = NixCode { - .errPos = Pos(problem_file, 40, 13), + .errPos = Pos(foFile, problem_file, 40, 13), .prevLineOfCode = std::nullopt, .errLineOfCode = "this is the problem line of code", .nextLineOfCode = std::nullopt @@ -132,7 +132,7 @@ int main() "yellow", "values"), .nixCode = NixCode { - .errPos = Pos(problem_file, 40, 13), + .errPos = Pos(foFile, problem_file, 40, 13), .prevLineOfCode = "previous line of code", .errLineOfCode = "this is the problem line of code", .nextLineOfCode = "next line of code", @@ -147,7 +147,7 @@ int main() "yellow", "values"), .nixCode = NixCode { - .errPos = Pos(problem_file, 40, 13) + .errPos = Pos(foFile, problem_file, 40, 13) }}); // Error with only hint and name.. @@ -155,7 +155,7 @@ int main() ErrorInfo { .name = "error name", .hint = hintfmt("hint %1%", "only"), .nixCode = NixCode { - .errPos = Pos(problem_file, 40, 13) + .errPos = Pos(foFile, problem_file, 40, 13) }}); return 0; diff --git a/src/libexpr/nixexpr.cc b/src/libexpr/nixexpr.cc index 91a508305..6ab36dd35 100644 --- a/src/libexpr/nixexpr.cc +++ b/src/libexpr/nixexpr.cc @@ -197,7 +197,22 @@ std::ostream & operator << (std::ostream & str, const Pos & pos) if (!pos) str << "undefined position"; else - str << (format(ANSI_BOLD "%1%" ANSI_NORMAL ":%2%:%3%") % (string) pos.file % pos.line % pos.column).str(); + { + auto f = format(ANSI_BOLD "%1%" ANSI_NORMAL ":%2%:%3%"); + switch (pos.origin) { + case foFile: + f % (string) pos.file; + break; + case foStdin: + case foString: + f % "(string)"; + break; + default: + throw Error("unhandled Pos origin!"); + } + str << (f % pos.line % pos.column).str(); + } + return str; } diff --git a/src/libutil/error.cc b/src/libutil/error.cc index ee415e18c..585059f22 100644 --- a/src/libutil/error.cc +++ b/src/libutil/error.cc @@ -58,34 +58,35 @@ void getCodeLines(NixCode &nixCode) if (nixCode.errPos.line <= 0) return; - // check this magic value! if (nixCode.errPos.origin == foFile) { try { AutoCloseFD fd = open(nixCode.errPos.file.c_str(), O_RDONLY | O_CLOEXEC); if (!fd) - throw SysError("opening file '%1%'", nixCode.errPos.file); - - // count the newlines. - int count = 0; - string line; - int pl = nixCode.errPos.line - 1; - do + logError(SysError("opening file '%1%'", nixCode.errPos.file).info()); + else { - line = readLine(fd.get()); - ++count; - if (count < pl) + // count the newlines. + int count = 0; + string line; + int pl = nixCode.errPos.line - 1; + do { - ; - } - else if (count == pl) { - nixCode.prevLineOfCode = line; - } else if (count == pl + 1) { - nixCode.errLineOfCode = line; - } else if (count == pl + 2) { - nixCode.nextLineOfCode = line; - break; - } - } while (true); + line = readLine(fd.get()); + ++count; + if (count < pl) + { + ; + } + else if (count == pl) { + nixCode.prevLineOfCode = line; + } else if (count == pl + 1) { + nixCode.errLineOfCode = line; + } else if (count == pl + 2) { + nixCode.nextLineOfCode = line; + break; + } + } while (true); + } } catch (EndOfFile &eof) { ; @@ -103,7 +104,6 @@ void getCodeLines(NixCode &nixCode) do { std::getline(iss, line); - // std::cout << "getline result: " << std::getline(iss, line) << std::endl; ++count; if (count < pl) { @@ -261,12 +261,12 @@ std::ostream& operator<<(std::ostream &out, const ErrorInfo &einfo) break; } case foString: { - out << fmt("%1%from command line argument", prefix) << std::endl; + out << fmt("%1%from command line argument %2%", prefix, showErrPos(einfo.nixCode->errPos)) << std::endl; out << prefix << std::endl; break; } case foStdin: { - out << fmt("%1%from stdin", prefix) << std::endl; + out << fmt("%1%from stdin %2%", prefix, showErrPos(einfo.nixCode->errPos)) << std::endl; out << prefix << std::endl; break; } @@ -291,11 +291,12 @@ std::ostream& operator<<(std::ostream &out, const ErrorInfo &einfo) printCodeLines(out, prefix, nixcode); out << prefix << std::endl; } + } - // hint - out << prefix << *einfo.hint << std::endl; - out << prefix << std::endl; - + // hint + if (einfo.hint.has_value()) { + out << prefix << *einfo.hint << std::endl; + out << prefix << std::endl; } return out; diff --git a/tests/misc.sh b/tests/misc.sh index fd4908e25..0804622ea 100644 --- a/tests/misc.sh +++ b/tests/misc.sh @@ -16,6 +16,11 @@ nix-env --foo 2>&1 | grep "no operation" nix-env -q --foo 2>&1 | grep "unknown flag" # Eval Errors. -eval_res=$(nix-instantiate --eval -E 'let a = {} // a; in a.foo' 2>&1 || true) -echo $eval_res | grep "(string) (1:15)" -echo $eval_res | grep "infinite recursion encountered" +eval_arg_res=$(nix-instantiate --eval -E 'let a = {} // a; in a.foo' 2>&1 || true) +echo $eval_arg_res | grep "command line argument (1:15)" +echo $eval_arg_res | grep "infinite recursion encountered" + +eval_stdin_res=$(echo 'let a = {} // a; in a.foo' | nix-instantiate --eval -E - 2>&1 || true) +echo $eval_stdin_res | grep "stdin (1:15)" +echo $eval_stdin_res | grep "infinite recursion encountered" +