aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEelco Dolstra <edolstra@gmail.com>2022-01-03 20:41:59 +0100
committerGitHub <noreply@github.com>2022-01-03 20:41:59 +0100
commit70dfcbbb37c114550fe1228748d30fd60520fe10 (patch)
tree7c8b72b7d7f48ba2d68a575d00c28752ff9020d5
parentd7b286fc775b419206a3bc30bebf974ca1b36377 (diff)
parenta26351da02689d1e3ee562a6d89d93c12816e476 (diff)
Merge pull request #5840 from tweag/balsoft/nix-repl-show-trace
nix repl: fix --show-trace and add the ability to set trace display
-rw-r--r--doc/manual/src/release-notes/rl-next.md2
-rw-r--r--src/libutil/error.cc2
-rw-r--r--src/nix/repl.cc15
-rw-r--r--tests/repl.sh10
-rw-r--r--tests/undefined-variable.nix1
5 files changed, 27 insertions, 3 deletions
diff --git a/doc/manual/src/release-notes/rl-next.md b/doc/manual/src/release-notes/rl-next.md
index ac662c132..2a67a39b1 100644
--- a/doc/manual/src/release-notes/rl-next.md
+++ b/doc/manual/src/release-notes/rl-next.md
@@ -2,3 +2,5 @@
* The TOML parser used by `builtins.fromTOML` has been replaced by [a
more compliant one](https://github.com/ToruNiina/toml11).
+* Added `:st`/`:show-trace` commands to nix repl, which are used to
+ set or toggle display of error traces.
diff --git a/src/libutil/error.cc b/src/libutil/error.cc
index 203d79087..dd9678ee7 100644
--- a/src/libutil/error.cc
+++ b/src/libutil/error.cc
@@ -25,7 +25,7 @@ const string & BaseError::calcWhat() const
err.name = sname();
std::ostringstream oss;
- showErrorInfo(oss, err, false);
+ showErrorInfo(oss, err, loggerSettings.showTrace);
what_ = oss.str();
return *what_;
diff --git a/src/nix/repl.cc b/src/nix/repl.cc
index 39a5a31de..a62ad66c2 100644
--- a/src/nix/repl.cc
+++ b/src/nix/repl.cc
@@ -430,7 +430,8 @@ bool NixRepl::processLine(string line)
<< " :t <expr> Describe result of evaluation\n"
<< " :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";
+ << " :log <expr> Show logs for a derivation\n"
+ << " :st [bool] Enable, disable or toggle showing traces for errors\n";
}
else if (command == ":a" || command == ":add") {
@@ -572,6 +573,18 @@ bool NixRepl::processLine(string line)
throw Error("value does not have documentation");
}
+ else if (command == ":st" || command == ":show-trace") {
+ if (arg == "false" || (arg == "" && loggerSettings.showTrace)) {
+ std::cout << "not showing error traces\n";
+ loggerSettings.showTrace = false;
+ } else if (arg == "true" || (arg == "" && !loggerSettings.showTrace)) {
+ std::cout << "showing error traces\n";
+ loggerSettings.showTrace = true;
+ } else {
+ throw Error("unexpected argument '%s' to %s", arg, command);
+ };
+ }
+
else if (command != "")
throw Error("unknown command '%1%'", command);
diff --git a/tests/repl.sh b/tests/repl.sh
index 995db869c..0e23a98db 100644
--- a/tests/repl.sh
+++ b/tests/repl.sh
@@ -13,6 +13,10 @@ failing = import ./simple-failing.nix
:log failing
"
+replUndefinedVariable="
+import ./undefined-variable.nix
+"
+
testRepl () {
local nixArgs=("$@")
local replOutput="$(nix repl "${nixArgs[@]}" <<< "$replCmds")"
@@ -22,10 +26,14 @@ testRepl () {
nix path-info "${nixArgs[@]}" "$outPath"
# simple.nix prints a PATH during build
echo "$replOutput" | grep -qs 'PATH=' || fail "nix repl :log doesn't output logs"
- local replOutput="$(nix repl "${nixArgs[@]}" <<< "$replFailingCmds")"
+ local replOutput="$(nix repl "${nixArgs[@]}" <<< "$replFailingCmds" 2>&1)"
echo "$replOutput"
echo "$replOutput" | grep -qs 'This should fail' \
|| fail "nix repl :log doesn't output logs for a failed derivation"
+ local replOutput="$(nix repl --show-trace "${nixArgs[@]}" <<< "$replUndefinedVariable" 2>&1)"
+ echo "$replOutput"
+ echo "$replOutput" | grep -qs "while evaluating the file" \
+ || fail "nix repl --show-trace doesn't show the trace"
}
# Simple test, try building a drv
diff --git a/tests/undefined-variable.nix b/tests/undefined-variable.nix
new file mode 100644
index 000000000..579985497
--- /dev/null
+++ b/tests/undefined-variable.nix
@@ -0,0 +1 @@
+let f = builtins.toFile "test-file.nix" "asd"; in import f