Skip to content

libexpr: implictly ignoring rec modifier while merging two attrsets #9020

Open

Description

@inclyc

Describe the bug

We are merging two attrsets in the parser1, this process implictly discards the latter rec modifier.

The following examples might be confusing for our users:

{
  foo = {
    # this attribute set is not recursive
    bar = 1;
  };
  foo = rec {
    # merging this attrset to foo.bar, discards the `rec` modifier
    x = 1;
    y = x;
  };
}
❯ nix-instantiate --parse  ./test.nix
error: undefined variable 'x'

       at /home/lyc/workspace/CS/OS/NixOS/nixd/test.nix:9:9:

            8|     x = 1;
            9|     y = x;
             |         ^
           10|   };

This might be more confusing for implicitly created non-recursive attrset, by selecting the attrpath2.

{
  foo.bar = 1;
  #   ^~~  we are implicitly creating a non-recursive attrset
  foo = rec {
    # merging this attrset to foo.bar, discards the `rec` modifier
    x = 1;
    y = x;
  };
}

Expected behavior

Maybe we should report a warning for this case, since it is hard to determine the merged attrset is recursive or not.

Additional context

Related source code

We are merging attrsets here:

jAttrs->attrs.emplace(ad.first, ad.second);

Non-recursive attrsets are implicitly created here:

ExprAttrs * nested = new ExprAttrs;

Static analysis for nix language

Language servers, for example, nil/nixd, already have this diagnostic to warn the user.

rec {
  foo.bar = 1;
  foo = rec {
    x = 1;
    y = 2;
  };
}
❯ nixd/tools/nixd-lint/nixd-lint /tmp/tmp.uo3dM4AfPK/rec-1.nix
warning: merging two attributes with different `rec` modifiers, the latter will be implicitly ignored

at /tmp/tmp.uo3dM4AfPK/rec-1.nix:3:9:

     2|   foo.bar = 1;
     3|   foo = rec {
      |         ^
     4|     x = 1;

note: this attribute set is not recursive

at /tmp/tmp.uo3dM4AfPK/rec-1.nix:2:3:

     1| rec {
     2|   foo.bar = 1;
      |   ^~^
     3|   foo = rec {

note: while this attribute set is marked as recursive, it will be considered as non-recursive

at /tmp/tmp.uo3dM4AfPK/rec-1.nix:3:9:

     2|   foo.bar = 1;
     3|   foo = rec {
      |         ^
     4|     x = 1;
{
  foo = rec {
    x = 1;
    y = 2;
  };
  foo.bar = 1;
}
❯ nixd/tools/nixd-lint/nixd-lint /tmp/tmp.uo3dM4AfPK/rec-2.nix
warning: merging two attributes with different `rec` modifiers, the latter will be implicitly ignored

at /tmp/tmp.uo3dM4AfPK/rec-2.nix:6:7:

     5|   };
     6|   foo.bar = 1;
      |       ^~^
     7| }

note: this attribute set is recursive

at /tmp/tmp.uo3dM4AfPK/rec-2.nix:2:9:

     1| {
     2|   foo = rec {
      |         ^
     3|     x = 1;

note: while this attribute set is marked as non-recursive, it will be considered as recursive

at /tmp/tmp.uo3dM4AfPK/rec-2.nix:6:7:

     5|   };
     6|   foo.bar = 1;
      |       ^~^
     7| }

Priorities

Add 👍 to issues you find important.

Footnotes

  1. they are merged here ↩

  2. it is created here, while selecting the attrpath, it might be merged latter ↩

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    buglanguage

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions