From 581f774284fa1b833255218165f4c958fe741500 Mon Sep 17 00:00:00 2001 From: Alexander Bantyev Date: Tue, 28 Dec 2021 15:53:21 +0300 Subject: [PATCH 1/3] BaseError::calcWhat: take loggerSettings.showTrace into account Text representation for errors should include the trace if --show-trace is passed. --- src/libutil/error.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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_; From 2dc29e0d932822baa50dbc3e81ae108382c39062 Mon Sep 17 00:00:00 2001 From: Alexander Bantyev Date: Tue, 28 Dec 2021 15:54:46 +0300 Subject: [PATCH 2/3] Add a test that nix repl --show-trace actually shows the trace --- tests/repl.sh | 10 +++++++++- tests/undefined-variable.nix | 1 + 2 files changed, 10 insertions(+), 1 deletion(-) create mode 100644 tests/undefined-variable.nix 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 From a26351da02689d1e3ee562a6d89d93c12816e476 Mon Sep 17 00:00:00 2001 From: Alexander Bantyev Date: Tue, 28 Dec 2021 16:13:07 +0300 Subject: [PATCH 3/3] Add ability to toggle show-trace from within the repl --- doc/manual/src/release-notes/rl-next.md | 2 ++ src/nix/repl.cc | 15 ++++++++++++++- 2 files changed, 16 insertions(+), 1 deletion(-) 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/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 Describe result of evaluation\n" << " :u Build derivation, then start nix-shell\n" << " :doc Show documentation of a builtin function\n" - << " :log Show logs for a derivation\n"; + << " :log 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);