Bug #21334
closedNamespaces and object reference sharing
Description
Implications related to builtin classes and modules¶
As we know, when a namespace is created, builtin classes in the namespace are as in the root one. This applies to all references to them, explicit or implicit.
This is at the core of the current implementation and semantics of namespaces.
However, the feature also allow object references to cross namespaces. For example, via method calls:
ns::Foo.m(ref)
And this has implications that do not seem good to me. Examples:
-
If
ref
has an HTTP client in an attribute, that depends on a parser gem, which depends on a string utils gem, that addsblank?
toString
, that call is broken because the transitive code at some point may perhaps hit ablank?
call, and the user has no way to tell. -
If
ref
is a reference created by a gem managed by Zeitwerk (there are hundreds), the call is broken too. Whenm
usesref
, it might need to lazy load something, and that won't work because the Kernel#require and Module#const_added decorations are gone within the namespace.
Please, note whether a gem loads with Zeitwerk or not is transparent to the user of the gem, by design. How do gems load their code is a private matter that is not, and should not be, documented. So the user does not know.
Implications related to different gem versions¶
In the example above, ref
could be an instance of an object that corresponds to v2.0 of a gem, and perhaps the code under the namespace has a transitive dependency on v1.0. Mixing such references seems like a receipt for a good amount of Paracetamol.
Proposal¶
This is all so brittle, that I would like to ask for a fundamental reconsideration of cross-namespace object reference passing.
The rules are consistent within a namespace, problem is opening that up to cross-namespace communication.
Maybe redefine communication across namespaces in a very restrictive way that make these issues by design. Maybe remove cross-namespace communication in this manner altogether. Or some other option people can think of.