Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Submodule refactor #108833

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/tests/modules.sh
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,10 @@ checkConfigOutput "true" config.submodule.outer ./declare-submoduleWith-modules.
## Paths should be allowed as values and work as expected
checkConfigOutput "true" config.submodule.enable ./declare-submoduleWith-path.nix

# Check the file location information is propagated into submodules
checkConfigOutput the-file.nix config.submodule.internalFiles.0 ./submoduleFiles.nix


# Check that disabledModules works recursively and correctly
checkConfigOutput "true" config.enable ./disable-recursive/main.nix
checkConfigOutput "true" config.enable ./disable-recursive/{main.nix,disable-foo.nix}
Expand Down
21 changes: 21 additions & 0 deletions lib/tests/modules/submoduleFiles.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{ lib, ... }: {
options.submodule = lib.mkOption {
default = {};
type = lib.types.submoduleWith {
modules = [ ({ options, ... }: {
options.value = lib.mkOption {};

options.internalFiles = lib.mkOption {
default = options.value.files;
};
})];
};
};

imports = [
{
_file = "the-file.nix";
submodule.value = 10;
}
];
}
11 changes: 3 additions & 8 deletions lib/types.nix
Original file line number Diff line number Diff line change
Expand Up @@ -467,15 +467,10 @@ rec {
let
inherit (lib.modules) evalModules;

coerce = unify: value: if isFunction value
then setFunctionArgs (args: unify (value args)) (functionArgs value)
else unify (if shorthandOnlyDefinesConfig then { config = value; } else value);

allModules = defs: modules ++ imap1 (n: { value, file }:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
allModules = defs: modules ++ imap1 (n: { value, file }:
allModules = defs: modules ++ map ({ value, file }:

if isAttrs value || isFunction value then
# Annotate the value with the location of its definition for better error messages
coerce (lib.modules.unifyModuleSyntax file "${toString file}-${toString n}") value
else value
if isAttrs value && shorthandOnlyDefinesConfig
then { _file = file; config = value; }
else { _file = file; imports = [ value ]; }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • n is now unused.
  • Usually an extra imports indirection doesn't matter, but some error messages print a path through the import graph. Those will be a bit harder to understand after this change

So it doesn't seem to be purely refactoring.

In all fairness those messages aren't great, but this change is a small step back.

Perhaps we could revisit those error messages and make them report an imports "trace" with actual file/line/column positions.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Boy was I wrong.

This seems to be a solution to the same problem that #177157 solves.

  • n is now unused.

n was a technicality of a previous solution for adding _file. Nothing more, nothing less.

Usually an extra imports indirection doesn't matter, but some error messages print a path through the import graph.

I don't think these occur frequently, and if they do they're a problem that can be solved separately. Adding more _file, like this code does, may prevent such messages in the first place.


off-topic section 🐢

So it doesn't seem to be purely refactoring.

Picking nits...

Perhaps we could revisit those error messages and make them report an imports "trace" with actual file/line/column positions.

Something with unsafeGetAttrPos seems like a better path for improving location reporting.

) defs;

freeformType = (evalModules {
Expand Down