mirror of
https://github.com/privatevoid-net/nix-super.git
synced 2024-11-22 14:06:16 +02:00
Add builtins.warn
This commit is contained in:
parent
d07cdbd9c2
commit
2d4c9d8f4a
4 changed files with 78 additions and 4 deletions
|
@ -48,6 +48,10 @@ EvalSettings::EvalSettings()
|
||||||
{
|
{
|
||||||
auto var = getEnv("NIX_PATH");
|
auto var = getEnv("NIX_PATH");
|
||||||
if (var) nixPath = parseNixPath(*var);
|
if (var) nixPath = parseNixPath(*var);
|
||||||
|
|
||||||
|
var = getEnv("NIX_ABORT_ON_WARN");
|
||||||
|
if (var && (var == "1" || var == "yes" || var == "true"))
|
||||||
|
builtinsAbortOnWarn = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
Strings EvalSettings::getDefaultNixPath()
|
Strings EvalSettings::getDefaultNixPath()
|
||||||
|
|
|
@ -158,13 +158,39 @@ struct EvalSettings : Config
|
||||||
|
|
||||||
Setting<bool> builtinsTraceDebugger{this, false, "debugger-on-trace",
|
Setting<bool> builtinsTraceDebugger{this, false, "debugger-on-trace",
|
||||||
R"(
|
R"(
|
||||||
If set to true and the `--debugger` flag is given,
|
If set to true and the `--debugger` flag is given, the following functions
|
||||||
[`builtins.trace`](@docroot@/language/builtins.md#builtins-trace) will
|
will enter the debugger like [`builtins.break`](@docroot@/language/builtins.md#builtins-break).
|
||||||
enter the debugger like
|
|
||||||
[`builtins.break`](@docroot@/language/builtins.md#builtins-break).
|
* [`builtins.trace`](@docroot@/language/builtins.md#builtins-trace)
|
||||||
|
* [`builtins.traceVerbose`](@docroot@/language/builtins.md#builtins-traceVerbose)
|
||||||
|
if [`trace-verbose`](#conf-trace-verbose) is set to true.
|
||||||
|
* [`builtins.warn`](@docroot@/language/builtins.md#builtins-warn)
|
||||||
|
|
||||||
This is useful for debugging warnings in third-party Nix code.
|
This is useful for debugging warnings in third-party Nix code.
|
||||||
)"};
|
)"};
|
||||||
|
|
||||||
|
Setting<bool> builtinsDebuggerOnWarn{this, false, "debugger-on-warn",
|
||||||
|
R"(
|
||||||
|
If set to true and the `--debugger` flag is given, [`builtins.warn`](@docroot@/language/builtins.md#builtins-warn)
|
||||||
|
will enter the debugger like [`builtins.break`](@docroot@/language/builtins.md#builtins-break).
|
||||||
|
|
||||||
|
This is useful for debugging warnings in third-party Nix code.
|
||||||
|
|
||||||
|
Use [`debugger-on-trace`](#conf-debugger-on-trace) to also enter the debugger on legacy warnings that are logged with [`builtins.trace`](@docroot@/language/builtins.md#builtins-trace).
|
||||||
|
)"};
|
||||||
|
|
||||||
|
Setting<bool> builtinsAbortOnWarn{this, false, "abort-on-warn",
|
||||||
|
R"(
|
||||||
|
If set to true, [`builtins.warn`](@docroot@/language/builtins.md#builtins-warn) will throw an error when logging a warning.
|
||||||
|
|
||||||
|
This will give you a stack trace that leads to the location of the warning.
|
||||||
|
|
||||||
|
This is useful for finding information about warnings in third-party Nix code when you can not start the interactive debugger, such as when Nix is called from a non-interactive script. See [`debugger-on-warn`](#conf-debugger-on-warn).
|
||||||
|
|
||||||
|
Currently, a stack trace can only be produced when the debugger is enabled, or when evaluation is aborted.
|
||||||
|
|
||||||
|
This option can be enabled by setting `NIX_ABORT_ON_WARN=1` in the environment.
|
||||||
|
)"};
|
||||||
};
|
};
|
||||||
|
|
||||||
extern EvalSettings evalSettings;
|
extern EvalSettings evalSettings;
|
||||||
|
|
|
@ -1043,6 +1043,43 @@ static RegisterPrimOp primop_trace({
|
||||||
.fun = prim_trace,
|
.fun = prim_trace,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
static void prim_warn(EvalState & state, const PosIdx pos, Value * * args, Value & v)
|
||||||
|
{
|
||||||
|
state.forceValue(*args[0], pos);
|
||||||
|
if (args[0]->type() == nString)
|
||||||
|
printMsg(lvlWarn, ANSI_WARNING "warning:" ANSI_NORMAL " %1%", args[0]->string_view());
|
||||||
|
else
|
||||||
|
printMsg(lvlWarn, ANSI_WARNING "warning:" ANSI_NORMAL " %1%", ValuePrinter(state, *args[0]));
|
||||||
|
|
||||||
|
if (evalSettings.builtinsAbortOnWarn) {
|
||||||
|
state.error<Abort>("aborting to reveal stack trace of warning, as abort-on-warn is set").debugThrow();
|
||||||
|
}
|
||||||
|
if ((evalSettings.builtinsTraceDebugger || evalSettings.builtinsDebuggerOnWarn) && state.debugRepl && !state.debugTraces.empty()) {
|
||||||
|
const DebugTrace & last = state.debugTraces.front();
|
||||||
|
state.runDebugRepl(nullptr, last.env, last.expr);
|
||||||
|
}
|
||||||
|
state.forceValue(*args[1], pos);
|
||||||
|
v = *args[1];
|
||||||
|
}
|
||||||
|
|
||||||
|
static RegisterPrimOp primop_warn({
|
||||||
|
.name = "__warn",
|
||||||
|
.args = {"e1", "e2"},
|
||||||
|
.doc = R"(
|
||||||
|
Evaluate *e1*, which must be a string and print iton standard error as a warning.
|
||||||
|
Then return *e2*.
|
||||||
|
This function is useful for non-critical situations where attention is advisable.
|
||||||
|
|
||||||
|
If the
|
||||||
|
[`debugger-on-trace`](@docroot@/command-ref/conf-file.md#conf-debugger-on-trace)
|
||||||
|
or [`debugger-on-warn`](@docroot@/command-ref/conf-file.md#conf-debugger-on-warn)
|
||||||
|
option is set to `true` and the `--debugger` flag is given, the
|
||||||
|
interactive debugger will be started when `warn` is called (like
|
||||||
|
[`break`](@docroot@/language/builtins.md#builtins-break)).
|
||||||
|
)",
|
||||||
|
.fun = prim_warn,
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
/* Takes two arguments and evaluates to the second one. Used as the
|
/* Takes two arguments and evaluates to the second one. Used as the
|
||||||
* builtins.traceVerbose implementation when --trace-verbose is not enabled
|
* builtins.traceVerbose implementation when --trace-verbose is not enabled
|
||||||
|
|
|
@ -36,6 +36,13 @@ nix-instantiate --eval -E 'let x = builtins.trace { x = x; } true; in x' \
|
||||||
nix-instantiate --eval -E 'let x = { repeating = x; tracing = builtins.trace x true; }; in x.tracing'\
|
nix-instantiate --eval -E 'let x = { repeating = x; tracing = builtins.trace x true; }; in x.tracing'\
|
||||||
2>&1 | grepQuiet -F 'trace: { repeating = «repeated»; tracing = «potential infinite recursion»; }'
|
2>&1 | grepQuiet -F 'trace: { repeating = «repeated»; tracing = «potential infinite recursion»; }'
|
||||||
|
|
||||||
|
nix-instantiate --eval -E 'builtins.warn "Hello" 123' 2>&1 | grepQuiet 'warning: Hello'
|
||||||
|
nix-instantiate --eval -E 'builtins.addErrorContext "while doing ${"something"} interesting" (builtins.warn "Hello" 123)' 2>/dev/null | grepQuiet 123
|
||||||
|
nix-instantiate --eval -E 'let x = builtins.warn { x = x; } true; in x' \
|
||||||
|
2>&1 | grepQuiet -E 'warning: { x = «potential infinite recursion»; }'
|
||||||
|
expectStderr 1 nix-instantiate --eval --abort-on-warn -E 'builtins.warn "Hello" 123' | grepQuiet Hello
|
||||||
|
NIX_ABORT_ON_WARN=1 expectStderr 1 nix-instantiate --eval -E 'builtins.addErrorContext "while doing ${"something"} interesting" (builtins.warn "Hello" 123)' | grepQuiet "while doing something interesting"
|
||||||
|
|
||||||
set +x
|
set +x
|
||||||
|
|
||||||
badDiff=0
|
badDiff=0
|
||||||
|
|
Loading…
Reference in a new issue