From 6110a6009fc77348e9c47e0801a9b884dc76c730 Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Fri, 2 Dec 2022 10:49:10 +0000 Subject: [PATCH] lib/modules: Add context to the "option does not exist" error Add trace items that provide context for a failed definition that can not be caught within the Nix language. This also adds a test for the `tryEval` behavior of `showDefs`. --- lib/modules.nix | 13 ++++++++++++- lib/tests/modules.sh | 3 +++ lib/tests/modules/define-enable-abort.nix | 3 +++ lib/tests/modules/define-enable-throw.nix | 3 +++ 4 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 lib/tests/modules/define-enable-abort.nix create mode 100644 lib/tests/modules/define-enable-throw.nix diff --git a/lib/modules.nix b/lib/modules.nix index 204a2cc1ac12..23e531de5c3a 100644 --- a/lib/modules.nix +++ b/lib/modules.nix @@ -284,7 +284,18 @@ rec { if config._module.check && config._module.freeformType == null && merged.unmatchedDefns != [] then let firstDef = head merged.unmatchedDefns; - baseMsg = "The option `${showOption (prefix ++ firstDef.prefix)}' does not exist. Definition values:${showDefs [ firstDef ]}"; + baseMsg = + let + optText = showOption (prefix ++ firstDef.prefix); + defText = + builtins.addErrorContext + "while evaluating the error message for definitions for `${optText}', which is an option that does not exist" + (builtins.addErrorContext + "while evaluating a definition from `${firstDef.file}'" + ( showDefs [ firstDef ]) + ); + in + "The option `${optText}' does not exist. Definition values:${defText}"; in if attrNames options == [ "_module" ] then diff --git a/lib/tests/modules.sh b/lib/tests/modules.sh index 6d2eb24db55c..75b316c97212 100755 --- a/lib/tests/modules.sh +++ b/lib/tests/modules.sh @@ -64,6 +64,9 @@ checkConfigOutput '^"one two"$' config.result ./shorthand-meta.nix # Check boolean option. checkConfigOutput '^false$' config.enable ./declare-enable.nix checkConfigError 'The option .* does not exist. Definition values:\n\s*- In .*: true' config.enable ./define-enable.nix +checkConfigError 'The option .* does not exist. Definition values:\n\s*- In .*' config.enable ./define-enable-throw.nix +checkConfigError 'while evaluating a definition from `.*/define-enable-abort.nix' config.enable ./define-enable-abort.nix +checkConfigError 'while evaluating the error message for definitions for .enable., which is an option that does not exist' config.enable ./define-enable-abort.nix checkConfigOutput '^1$' config.bare-submodule.nested ./declare-bare-submodule.nix ./declare-bare-submodule-nested-option.nix checkConfigOutput '^2$' config.bare-submodule.deep ./declare-bare-submodule.nix ./declare-bare-submodule-deep-option.nix diff --git a/lib/tests/modules/define-enable-abort.nix b/lib/tests/modules/define-enable-abort.nix new file mode 100644 index 000000000000..85b58a567cad --- /dev/null +++ b/lib/tests/modules/define-enable-abort.nix @@ -0,0 +1,3 @@ +{ + config.enable = abort "oops"; +} diff --git a/lib/tests/modules/define-enable-throw.nix b/lib/tests/modules/define-enable-throw.nix new file mode 100644 index 000000000000..16a59b781dc5 --- /dev/null +++ b/lib/tests/modules/define-enable-throw.nix @@ -0,0 +1,3 @@ +{ + config.enable = throw "oops"; +}