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

Add Module System error contexts for imports and module argument infinite recursions #370967

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

roberth
Copy link
Member

@roberth roberth commented Jan 4, 2025

Apologies for the combined PR. They intersect at checkConfigNotError. Can split if desired nonetheless.

This adds two new error contexts, and a little addition to the test runner.

Loading imports of module

       … while loading imports of module `/home/user/nixpkgs/lib/tests/modules/import-configuration.nix`
  • only for the latest ok-ish module, not the whole chain
  • see commit message

Infinite recursion through module argument

       … while calling 'config''
         at /home/user/h/nixpkgs/lib/modules.nix:509:9:
          508|       config' =
          509|         name:
             |         ^
          510|           builtins.addErrorContext

       … *risky* while evaluating the module fixpoint, `config`

       Note that the module structure and `imports` fundamentally can not depend on the module fixpoint!
       An argument `someUnknownArg` was not provided externally through .specialArgs., so we try to load it from the module fixpoint...
       If the following message is "infinite recursion", this was the cause, and possible solutions include:
       - Perhaps someUnknownArg was misspelled.
       - Make sure that the module was loaded into the right context.
       - Provide the argument externally through `specialArgs`.
       - Structure the module such that the top level attribute set and special attributes like `config`, `imports` and `config._module.args` do not refer to `someUnknownArg`.
         (Except perhaps in a deeper structure, like a more deeply nested attribute, or a `mkIf` condition.)

       The above explanation is new. If it is inaccurate or out of place, please report a minimal example in a Nixpkgs issue and ping the Module System maintainers.


       error: infinite recursion encountered

for an example like

let
  b =
    { someUnknownArg, ... }: {
      imports = [ someUnknownArg ];
    };
in
{
  _file = "a";
  imports = [ b ];
}

This has to be the fanciest error message. I am pretty confident about this error message, but I don't have proof, hence the question to report back, which self-destructs in about a year.

Everyone with an up to date Nix will see these traces, even without --show-trace.

Dump traces

DUMP_TRACES=1 now lets us see all error traces in the test suite, including successful ones.


Things done

  • Built on platform(s)
    • x86_64-linux
    • aarch64-linux
    • x86_64-darwin
    • aarch64-darwin
  • For non-Linux: Is sandboxing enabled in nix.conf? (See Nix manual)
    • sandbox = relaxed
    • sandbox = true
  • Tested, as applicable:
  • Tested compilation of all packages that depend on this change using nix-shell -p nixpkgs-review --run "nixpkgs-review rev HEAD". Note: all changes have to be committed, also see nixpkgs-review usage
  • Tested basic functionality of all binary files (usually in ./result/bin/)
  • 25.05 Release Notes (or backporting 24.11 and 25.05 Release notes)
    • (Package updates) Added a release notes entry if the change is major or breaking
    • (Module updates) Added a release notes entry if the change is significant
    • (Module addition) Added a release notes entry if adding a new NixOS module
  • Fits CONTRIBUTING.md.

Add a 👍 reaction to pull requests you find important.

This will appear for errors during the loading and syntax checking of
modules, but not when it has started processing declarations.

It does not form long traces containing the whole chain of imports.
This is good, because only the tip tends to be relevant.
Note that the parent's filename is propagated into anonymous modules.
@github-actions github-actions bot added 6.topic: module system About "NixOS" module system internals 6.topic: lib The Nixpkgs function library labels Jan 4, 2025
@nix-owners nix-owners bot requested a review from infinisil January 4, 2025 20:44
module = checkModule (loadModule args parentFile "${parentKey}:anon-${toString n}" x);
module =
builtins.addErrorContext
"while loading imports of module `${parentFile}`"
Copy link
Member Author

@roberth roberth Jan 4, 2025

Choose a reason for hiding this comment

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

Nix would compulsively start its own trace items with while evaluating, but does very little except reduce the signal to noise ratio. We can use a different verb that actually conveys information.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
6.topic: lib The Nixpkgs function library 6.topic: module system About "NixOS" module system internals 10.rebuild-darwin: 1-10 10.rebuild-linux: 1-10
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant