Repository navigation
Open
Description
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:
Line 139 in 3ce793d
| jAttrs->attrs.emplace(ad.first, ad.second); |
Non-recursive attrsets are implicitly created here:
Line 113 in 3ce793d
| 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
Reacted by éclairevoyant
Activity
nix::parser::State::addAttr: Forbid mixed-rec merges
nix::parser::State::addAttr: Forbid mixed-rec merges
The front end builds no tree: one parse lowers to the nix dialect (Ge…
The scan runs where the parser asks: a band is scanned when the names…