lib/modules: Throw earlier when module function does not return attrs

`m` must always be an attrset at this point. It is basically always
evaluated. This will make it throw when any of the attrs is accessed,
rather than just `config`. We assume that this will improve the error
message in more scenarios.
This commit is contained in:
Robert Hensing 2022-06-14 23:12:46 +02:00
parent dfd98a5da2
commit 3c4a49f506
3 changed files with 23 additions and 3 deletions

View File

@ -462,14 +462,13 @@ rec {
config = addFreeformType (addMeta (m.config or {})); config = addFreeformType (addMeta (m.config or {}));
} }
else else
lib.throwIfNot (isAttrs m) "module ${file} (${key}) does not look like a module."
{ _file = toString m._file or file; { _file = toString m._file or file;
key = toString m.key or key; key = toString m.key or key;
disabledModules = m.disabledModules or []; disabledModules = m.disabledModules or [];
imports = m.require or [] ++ m.imports or []; imports = m.require or [] ++ m.imports or [];
options = {}; options = {};
config = config = addFreeformType (addMeta (removeAttrs m ["_file" "key" "disabledModules" "require" "imports" "freeformType"]));
lib.throwIfNot (isAttrs m) "module ${file} (${key}) does not look like a module."
addFreeformType (addMeta (removeAttrs m ["_file" "key" "disabledModules" "require" "imports" "freeformType"]));
}; };
applyModuleArgsIfFunction = key: f: args@{ config, options, lib, ... }: if isFunction f then applyModuleArgsIfFunction = key: f: args@{ config, options, lib, ... }: if isFunction f then

View File

@ -199,6 +199,7 @@ checkConfigOutput '^true$' config.submodule.enable ./declare-submoduleWith-path.
checkConfigOutput '"beta"' config.nodes.foo.settingsDict.c ./deferred-module.nix checkConfigOutput '"beta"' config.nodes.foo.settingsDict.c ./deferred-module.nix
# errors from the default module are reported with accurate location # errors from the default module are reported with accurate location
checkConfigError 'In `the-file-that-contains-the-bad-config.nix, via option default'\'': "bogus"' config.nodes.foo.bottom ./deferred-module.nix checkConfigError 'In `the-file-that-contains-the-bad-config.nix, via option default'\'': "bogus"' config.nodes.foo.bottom ./deferred-module.nix
checkConfigError '.*lib/tests/modules/deferred-module-error.nix, via option deferred [(]:anon-1:anon-1:anon-1[)] does not look like a module.' config.result ./deferred-module-error.nix
# Check the file location information is propagated into submodules # Check the file location information is propagated into submodules
checkConfigOutput the-file.nix config.submodule.internalFiles.0 ./submoduleFiles.nix checkConfigOutput the-file.nix config.submodule.internalFiles.0 ./submoduleFiles.nix

View File

@ -0,0 +1,20 @@
{ config, lib, ... }:
let
inherit (lib) types mkOption setDefaultModuleLocation evalModules;
inherit (types) deferredModule lazyAttrsOf submodule str raw enum;
in
{
options = {
deferred = mkOption {
type = deferredModule;
};
result = mkOption {
default = (evalModules { modules = [ config.deferred ]; }).config.result;
};
};
config = {
deferred = { ... }:
# this should be an attrset, so this fails
true;
};
}