diff options
author | Robert Hensing <roberth@users.noreply.github.com> | 2021-10-13 22:43:39 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-10-13 22:43:39 +0200 |
commit | a541ce1fe7c7b6b8f699202283f7da5af0af6888 (patch) | |
tree | a7ee1139e39b253aea0d4d878d796a1cfc1b0679 /lib | |
parent | 4f2ff90f7436aa235e789f70107af8b4a54bd0c6 (diff) | |
parent | c9b7cc79e6ce6b38debb28ab0d06051a4fb5b336 (diff) | |
download | nixlib-a541ce1fe7c7b6b8f699202283f7da5af0af6888.tar nixlib-a541ce1fe7c7b6b8f699202283f7da5af0af6888.tar.gz nixlib-a541ce1fe7c7b6b8f699202283f7da5af0af6888.tar.bz2 nixlib-a541ce1fe7c7b6b8f699202283f7da5af0af6888.tar.lz nixlib-a541ce1fe7c7b6b8f699202283f7da5af0af6888.tar.xz nixlib-a541ce1fe7c7b6b8f699202283f7da5af0af6888.tar.zst nixlib-a541ce1fe7c7b6b8f699202283f7da5af0af6888.zip |
Merge pull request #140763 from hercules-ci/abort-on-warn
lib.warn: Add NIX_ABORT_ON_WARN for call traces
Diffstat (limited to 'lib')
-rw-r--r-- | lib/trivial.nix | 21 |
1 files changed, 20 insertions, 1 deletions
diff --git a/lib/trivial.nix b/lib/trivial.nix index 7956ba4bde6f..a389c7cdfacb 100644 --- a/lib/trivial.nix +++ b/lib/trivial.nix @@ -303,7 +303,26 @@ rec { # TODO: figure out a clever way to integrate location information from # something like __unsafeGetAttrPos. - warn = msg: builtins.trace "[1;31mwarning: ${msg}[0m"; + /* + Print a warning before returning the second argument. This function behaves + like `builtins.trace`, but requires a string message and formats it as a + warning, including the `warning: ` prefix. + + To get a call stack trace and abort evaluation, set the environment variable + `NIX_ABORT_ON_WARN=true` and set the Nix options `--option pure-eval false --show-trace` + + Type: string -> a -> a + */ + warn = + if lib.elem (builtins.getEnv "NIX_ABORT_ON_WARN") ["1" "true" "yes"] + then msg: builtins.trace "[1;31mwarning: ${msg}[0m" (abort "NIX_ABORT_ON_WARN=true; warnings are treated as unrecoverable errors.") + else msg: builtins.trace "[1;31mwarning: ${msg}[0m"; + + /* + Like warn, but only warn when the first argument is `true`. + + Type: bool -> string -> a -> a + */ warnIf = cond: msg: if cond then warn msg else id; info = msg: builtins.trace "INFO: ${msg}"; |