Combining nested attribute shorthand and rec causes weird scoping behavior				#329
		
		
	
	
		Labels
		
	
	
	
	No labels
	
		
			
	
	Affects/CppNix
		
			Affects/Nightly
		
			Affects/Only nightly
		
			Affects/Stable
		
			Area/build-packaging
		
			Area/cli
		
			Area/evaluator
		
			Area/fetching
		
			Area/flakes
		
			Area/language
		
			Area/lix ci
		
			Area/nix-eval-jobs
		
			Area/profiles
		
			Area/protocol
		
			Area/releng
		
			Area/remote-builds
		
			Area/repl
		
			Area/repl/debugger
		
			Area/store
		
			bug
		
			Context
contributors
		
			Context
drive-by
		
			Context
maintainers
		
			Context
RFD
		
			crash 💥
		
			Cross Compilation
		
			devx
		
			docs
		
			Downstream Dependents
		
			E/easy
		
			E/hard
		
			E/help wanted
		
			E/reproducible
		
			E/requires rearchitecture
		
			Feature/S3
		
			imported
		
			Language/Bash
		
			Language/C++
		
			Language/NixLang
		
			Language/Python
		
			Language/Rust
		
			Needs Langver
		
			OS/Linux
		
			OS/macOS
		
			performance
		
			regression
		
			release-blocker
		
			stability
		
			Status
blocked
		
			Status
invalid
		
			Status
postponed
		
			Status
wontfix
		
			testing
		
			testing/flakey
		
			Topic/Large Scale Installations
		
			ux
		
		
	
		No milestone
		
			
		
	No project
	
		
	
	
	
	
		No assignees
		
	
	
		
			
		
	
	
	
		3 participants
	
	
		
		
	Notifications
	
		
	
	
	
		
	
	
	Due date
No due date set.
	
		Dependencies
		
		
	
	
	No dependencies set.
	
	
		
	
	
		
			Reference
		
	
	
		
	
	
			lix-project/lix#329
			
		
	
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	
	No description provided.
		
		Delete branch "%!s()"
	 
	Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Describe the bug
When an attrset is declared with one element which is itself a
recattrset, and another element nested within that attrset using thea.b=csyntax, the value ("c") is evaluated in the scope of the recursive attrset.Steps To Reproduce
This results in
{a = {b = 5; c = 5;};}, despiteblooking like it shouldn't be in scope.Expected behavior
error: undefined variable 'b', becausebisn't in scope at the declaration ofa.cIf, god forbid, someone depends on the current behavior, deprecate it and print a warning.
nix --versionoutputnix (Lix, like Nix) 2.90.0-beta.1-lixpre20240506-b6799abAdditional context
Nix bug: https://github.com/NixOS/nix/issues/6251
there's actually no reasonable way to implement your expected behavior in the current version of the language because this is merely syntactic sugar. rec sets in general are very confusing (and even buggy, in some cases); we::horrors'd much rather deprecate the desugaring for rec sets specifically instead of changing how scoping works.
consider
{ a = rec { b = c; }; a.c = 5; }. desugaring makes this work, but againclooks to not be in scope. should we apply the outer scope to thisctoo? if we do we make the language extremely sensitive to code motion. in fact, swapping the two breaks this snippet due to a parser bug that we can't really fix anymore without risking breakages.consider
let d = 1; in { a = rec { b = rec { c = d; }; d = 2; }; a.b.d = 3; }. what should this evaluate to?recsets are a bit of an anti-pattern in themselves, making them non-mergable would be the least surprising and corner-case-y way forward. it's so much easier to explain "attrset merging does not apply torec" than adding more scoping rules that can be extremely surprising when thing silently evaluate to something else just by moving some code around that.Honestly this example seems less cursed to me; you can sensibly define rec as "
withthe attrset I'm making right now, including any attributes that get merged in from elsewhere". In other words, in this example,d's scoping is affected by the fact that it's in a rec block; in my example, something's scope being affected by the presence of a block, despite being outside that block.That being said:
Yes, this seems like by far the best move.
😱
as in,
rec { ... }being rewritten tolet __hidden = with __hidden; { ... }; in __hidden? with scoping doesn't work like that (withscopes are searched after all other scopes, not in syntactic order) and cannot be changed without breaking things. it'd need an entirely new kind of scope for this to work at all, and that would be quite a lot of complexity for a feature that doesn't even seem to be used very often. deleting the feature does seem to be the only sound option 🫠Ah yes, very good points.
I'm going to mark this as wontfix, and open a new issue for disallowing attr merging for recursive attrsets
--arg/--argstr#496