ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
* [ruby-core:121950] [Ruby Bug#21316] Namespaces leak with permanent names
@ 2025-05-09 22:05 fxn (Xavier Noria) via ruby-core
  2025-05-10  6:22 ` [ruby-core:121962] " mame (Yusuke Endoh) via ruby-core
                   ` (8 more replies)
  0 siblings, 9 replies; 10+ messages in thread
From: fxn (Xavier Noria) via ruby-core @ 2025-05-09 22:05 UTC (permalink / raw)
  To: ruby-core; +Cc: fxn (Xavier Noria)

Issue #21316 has been reported by fxn (Xavier Noria).

----------------------------------------
Bug #21316: Namespaces leak with permanent names
https://bugs.ruby-lang.org/issues/21316

* Author: fxn (Xavier Noria)
* Status: Open
* Backport: 3.2: UNKNOWN, 3.3: UNKNOWN, 3.4: UNKNOWN
----------------------------------------
Namespaces are not transparent for this program

```ruby
C = Class.new
C.name == 'C'
```

because under a non-main user namespace, the name of `C` has the namespace as a prefix.



-- 
https://bugs.ruby-lang.org/
 ______________________________________________
 ruby-core mailing list -- ruby-core@ml.ruby-lang.org
 To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
 ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [ruby-core:121962] [Ruby Bug#21316] Namespaces leak with permanent names
  2025-05-09 22:05 [ruby-core:121950] [Ruby Bug#21316] Namespaces leak with permanent names fxn (Xavier Noria) via ruby-core
@ 2025-05-10  6:22 ` mame (Yusuke Endoh) via ruby-core
  2025-05-10  8:18 ` [ruby-core:121964] " fxn (Xavier Noria) via ruby-core
                   ` (7 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: mame (Yusuke Endoh) via ruby-core @ 2025-05-10  6:22 UTC (permalink / raw)
  To: ruby-core; +Cc: mame (Yusuke Endoh)

Issue #21316 has been updated by mame (Yusuke Endoh).


This is definitely not ideal. `Class#name` could end up referring to a different constant.

```ruby
# main.rb
NS = Namespace.new
NS.require "./sub"

# sub.rb
class C; end
p C #=> expected: C
    #=> actual: NS::C ← This could refer to a different constant, which is problematic
```

@tagomoris suggested that `Class#name` should behave as follows:

* If the current namespace is at the front, omit it and just return `"C"`
* Otherwise, return something like `"#<Namespace: ...>::C"`

```ruby
# main.rb
NS = Namespace.new
NS.require "./sub"
p NS::C #=> #<Namespace: ...>::C

# sub.rb
class C; end
p C #=> C
```


----------------------------------------
Bug #21316: Namespaces leak with permanent names
https://bugs.ruby-lang.org/issues/21316#change-113078

* Author: fxn (Xavier Noria)
* Status: Open
* Backport: 3.2: UNKNOWN, 3.3: UNKNOWN, 3.4: UNKNOWN
----------------------------------------
Namespaces are not transparent for this program

```ruby
C = Class.new
C.name == 'C'
```

because under a non-main user namespace, the name of `C` has the namespace as a prefix.



-- 
https://bugs.ruby-lang.org/
 ______________________________________________
 ruby-core mailing list -- ruby-core@ml.ruby-lang.org
 To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
 ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [ruby-core:121964] [Ruby Bug#21316] Namespaces leak with permanent names
  2025-05-09 22:05 [ruby-core:121950] [Ruby Bug#21316] Namespaces leak with permanent names fxn (Xavier Noria) via ruby-core
  2025-05-10  6:22 ` [ruby-core:121962] " mame (Yusuke Endoh) via ruby-core
@ 2025-05-10  8:18 ` fxn (Xavier Noria) via ruby-core
  2025-05-10 12:13 ` [ruby-core:121983] " Eregon (Benoit Daloze) via ruby-core
                   ` (6 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: fxn (Xavier Noria) via ruby-core @ 2025-05-10  8:18 UTC (permalink / raw)
  To: ruby-core; +Cc: fxn (Xavier Noria)

Issue #21316 has been updated by fxn (Xavier Noria).


Main problem here is that there are many programs that depend on that name. They may store it somewhere for later `const_get` (for example polymorphic Active Record associations), passed to a callback (for example `on_load` callbacks in Zeitwerk), etc.

That the name matches the constant path of the class or module being defined is a strong property of the language people rely on.

----------------------------------------
Bug #21316: Namespaces leak with permanent names
https://bugs.ruby-lang.org/issues/21316#change-113080

* Author: fxn (Xavier Noria)
* Status: Open
* Backport: 3.2: UNKNOWN, 3.3: UNKNOWN, 3.4: UNKNOWN
----------------------------------------
Namespaces are not transparent for this program

```ruby
C = Class.new
C.name == 'C'
```

because under a non-main user namespace, the name of `C` has the namespace as a prefix.



-- 
https://bugs.ruby-lang.org/
 ______________________________________________
 ruby-core mailing list -- ruby-core@ml.ruby-lang.org
 To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
 ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [ruby-core:121983] [Ruby Bug#21316] Namespaces leak with permanent names
  2025-05-09 22:05 [ruby-core:121950] [Ruby Bug#21316] Namespaces leak with permanent names fxn (Xavier Noria) via ruby-core
  2025-05-10  6:22 ` [ruby-core:121962] " mame (Yusuke Endoh) via ruby-core
  2025-05-10  8:18 ` [ruby-core:121964] " fxn (Xavier Noria) via ruby-core
@ 2025-05-10 12:13 ` Eregon (Benoit Daloze) via ruby-core
  2025-05-13 11:20 ` [ruby-core:122045] " make_now_just (Hiroya Fujinami) via ruby-core
                   ` (5 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: Eregon (Benoit Daloze) via ruby-core @ 2025-05-10 12:13 UTC (permalink / raw)
  To: ruby-core; +Cc: Eregon (Benoit Daloze)

Issue #21316 has been updated by Eregon (Benoit Daloze).


Right I think in the namespace defining the class/module the `Module#name` needs to not have a prefix, or it will break many gems.
OTOH when used outside, it could be very confusing without a prefix.

> suggested that Class#name should behave as follows:

Yeah, that's probably the best trade-off, although of course it means the Module#name for a given Module changes based on which Namespace is the current one.

---

BTW, I think the main namespace constants should also be prefixed when seen in another namespace, currently it's not the case:
```
$ RUBY_NAMESPACE=1 ruby -ve 'main = Namespace.current; ns = Namespace.new; class C; end; ns::MAIN_C = C; File.write "ns.rb", "p MAIN_C; p eval(MAIN_C.name)"; ns.require "./ns"'
ruby 3.5.0dev (2025-05-10T07:50:29Z namespace-on-read-.. bd4f57f96b) +PRISM [x86_64-linux]
ruby: warning: Namespace is experimental, and the behavior may change in the future!
See doc/namespace.md for know issues, etc.
C
(eval at /home/eregon/ns.rb:1):1:in '<top (required)>': uninitialized constant #<Namespace:24,user,optional>::C (NameError)
	from /home/eregon/ns.rb:1:in 'Kernel#eval'
	from /home/eregon/ns.rb:1:in '<top (required)>'
	from -e:1:in 'Namespace#require'
	from -e:1:in '<main>'
```

----------------------------------------
Bug #21316: Namespaces leak with permanent names
https://bugs.ruby-lang.org/issues/21316#change-113099

* Author: fxn (Xavier Noria)
* Status: Open
* Backport: 3.2: UNKNOWN, 3.3: UNKNOWN, 3.4: UNKNOWN
----------------------------------------
Namespaces are not transparent for this program

```ruby
C = Class.new
C.name == 'C'
```

because under a non-main user namespace, the name of `C` has the namespace as a prefix.



-- 
https://bugs.ruby-lang.org/
 ______________________________________________
 ruby-core mailing list -- ruby-core@ml.ruby-lang.org
 To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
 ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [ruby-core:122045] [Ruby Bug#21316] Namespaces leak with permanent names
  2025-05-09 22:05 [ruby-core:121950] [Ruby Bug#21316] Namespaces leak with permanent names fxn (Xavier Noria) via ruby-core
                   ` (2 preceding siblings ...)
  2025-05-10 12:13 ` [ruby-core:121983] " Eregon (Benoit Daloze) via ruby-core
@ 2025-05-13 11:20 ` make_now_just (Hiroya Fujinami) via ruby-core
  2025-05-13 11:49 ` [ruby-core:122049] " Eregon (Benoit Daloze) via ruby-core
                   ` (4 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: make_now_just (Hiroya Fujinami) via ruby-core @ 2025-05-13 11:20 UTC (permalink / raw)
  To: ruby-core; +Cc: make_now_just (Hiroya Fujinami)

Issue #21316 has been updated by make_now_just (Hiroya Fujinami).


I found an issue on `Marshal` and `Namespace` maybe related to this ticket.

When dumping an object defined in a namespace using Marshal, the result will vary depending on whether the namespace is held as a variable or constant, and whether `Marshal.dump` is performed inside or outside the namespace.

In other words, we have the following files:

`ns.rb`:
```ruby
class Foo
  def dump_in_ns = Marshal.dump(self)
end
```

`main.rb`:
```ruby
ns = Namespace.new
ns.require("./ns.rb")

NS = Namespace.new
NS.require("./ns.rb")

puts "var / in ns"
begin
  ns::Foo.new.dump
  p :ok
rescue => ex
  p :error
  p ex
end
puts

puts "const / in ns"
begin
  NS::Foo.new.dump
  p :ok
rescue => ex
  p :error
  p ex
end
puts

puts "var / out ns"
begin
  Marshal.dump(ns::Foo.new)
  p :ok
rescue => ex
  p :error
  p ex
end
puts

puts "const / out ns"
begin
  Marshal.dump(NS::Foo.new)
  p :ok
rescue => ex
  p :error
  p ex
end
```
Then, the result is as follows:
```console
$ RUBY_NAMESPACE=1 ruby main.rb
var / in ns
:error
#<NoMethodError: undefined method 'dump' for an instance of #<Namespace:0x000000012018ed88>::Foo>

const / in ns
:error
#<NoMethodError: undefined method 'dump' for an instance of NS::Foo>

var / out ns
:error
#<TypeError: can't dump anonymous class #<Namespace:0x000000012018ed88>::Foo>

const / out ns
:ok
```

I'm not sure this issue is completely related to this ticket,  but `Marshal.dump` should work in any case.

----------------------------------------
Bug #21316: Namespaces leak with permanent names
https://bugs.ruby-lang.org/issues/21316#change-113195

* Author: fxn (Xavier Noria)
* Status: Open
* Backport: 3.2: UNKNOWN, 3.3: UNKNOWN, 3.4: UNKNOWN
----------------------------------------
Namespaces are not transparent for this program

```ruby
C = Class.new
C.name == 'C'
```

because under a non-main user namespace, the name of `C` has the namespace as a prefix.



-- 
https://bugs.ruby-lang.org/
 ______________________________________________
 ruby-core mailing list -- ruby-core@ml.ruby-lang.org
 To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
 ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [ruby-core:122049] [Ruby Bug#21316] Namespaces leak with permanent names
  2025-05-09 22:05 [ruby-core:121950] [Ruby Bug#21316] Namespaces leak with permanent names fxn (Xavier Noria) via ruby-core
                   ` (3 preceding siblings ...)
  2025-05-13 11:20 ` [ruby-core:122045] " make_now_just (Hiroya Fujinami) via ruby-core
@ 2025-05-13 11:49 ` Eregon (Benoit Daloze) via ruby-core
  2025-05-13 11:52 ` [ruby-core:122050] " make_now_just (Hiroya Fujinami) via ruby-core
                   ` (3 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: Eregon (Benoit Daloze) via ruby-core @ 2025-05-13 11:49 UTC (permalink / raw)
  To: ruby-core; +Cc: Eregon (Benoit Daloze)

Issue #21316 has been updated by Eregon (Benoit Daloze).


@make_now_just In the first two cases, it should be `.dump_in_ns` not `.dump`

----------------------------------------
Bug #21316: Namespaces leak with permanent names
https://bugs.ruby-lang.org/issues/21316#change-113199

* Author: fxn (Xavier Noria)
* Status: Open
* Backport: 3.2: UNKNOWN, 3.3: UNKNOWN, 3.4: UNKNOWN
----------------------------------------
Namespaces are not transparent for this program

```ruby
C = Class.new
C.name == 'C'
```

because under a non-main user namespace, the name of `C` has the namespace as a prefix.



-- 
https://bugs.ruby-lang.org/
 ______________________________________________
 ruby-core mailing list -- ruby-core@ml.ruby-lang.org
 To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
 ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [ruby-core:122050] [Ruby Bug#21316] Namespaces leak with permanent names
  2025-05-09 22:05 [ruby-core:121950] [Ruby Bug#21316] Namespaces leak with permanent names fxn (Xavier Noria) via ruby-core
                   ` (4 preceding siblings ...)
  2025-05-13 11:49 ` [ruby-core:122049] " Eregon (Benoit Daloze) via ruby-core
@ 2025-05-13 11:52 ` make_now_just (Hiroya Fujinami) via ruby-core
  2025-05-23  7:15 ` [ruby-core:122246] " ko1 (Koichi Sasada) via ruby-core
                   ` (2 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: make_now_just (Hiroya Fujinami) via ruby-core @ 2025-05-13 11:52 UTC (permalink / raw)
  To: ruby-core; +Cc: make_now_just (Hiroya Fujinami)

Issue #21316 has been updated by make_now_just (Hiroya Fujinami).


@Eregon It has already been fixed. Thank you.

----------------------------------------
Bug #21316: Namespaces leak with permanent names
https://bugs.ruby-lang.org/issues/21316#change-113200

* Author: fxn (Xavier Noria)
* Status: Open
* Backport: 3.2: UNKNOWN, 3.3: UNKNOWN, 3.4: UNKNOWN
----------------------------------------
Namespaces are not transparent for this program

```ruby
C = Class.new
C.name == 'C'
```

because under a non-main user namespace, the name of `C` has the namespace as a prefix.



-- 
https://bugs.ruby-lang.org/
 ______________________________________________
 ruby-core mailing list -- ruby-core@ml.ruby-lang.org
 To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
 ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [ruby-core:122246] [Ruby Bug#21316] Namespaces leak with permanent names
  2025-05-09 22:05 [ruby-core:121950] [Ruby Bug#21316] Namespaces leak with permanent names fxn (Xavier Noria) via ruby-core
                   ` (5 preceding siblings ...)
  2025-05-13 11:52 ` [ruby-core:122050] " make_now_just (Hiroya Fujinami) via ruby-core
@ 2025-05-23  7:15 ` ko1 (Koichi Sasada) via ruby-core
  2025-05-23  7:31 ` [ruby-core:122247] " fxn (Xavier Noria) via ruby-core
  2025-06-05  4:32 ` [ruby-core:122420] " matz (Yukihiro Matsumoto) via ruby-core
  8 siblings, 0 replies; 10+ messages in thread
From: ko1 (Koichi Sasada) via ruby-core @ 2025-05-23  7:15 UTC (permalink / raw)
  To: ruby-core; +Cc: ko1 (Koichi Sasada)

Issue #21316 has been updated by ko1 (Koichi Sasada).


FYI: Java's case
"OBJ09-J. Compare classes and not class names"
https://wiki.sei.cmu.edu/confluence/display/java/OBJ09-J.+Compare+classes+and+not+class+names


----------------------------------------
Bug #21316: Namespaces leak with permanent names
https://bugs.ruby-lang.org/issues/21316#change-113392

* Author: fxn (Xavier Noria)
* Status: Open
* Backport: 3.2: UNKNOWN, 3.3: UNKNOWN, 3.4: UNKNOWN
----------------------------------------
Namespaces are not transparent for this program

```ruby
C = Class.new
C.name == 'C'
```

because under a non-main user namespace, the name of `C` has the namespace as a prefix.



-- 
https://bugs.ruby-lang.org/
 ______________________________________________
 ruby-core mailing list -- ruby-core@ml.ruby-lang.org
 To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
 ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [ruby-core:122247] [Ruby Bug#21316] Namespaces leak with permanent names
  2025-05-09 22:05 [ruby-core:121950] [Ruby Bug#21316] Namespaces leak with permanent names fxn (Xavier Noria) via ruby-core
                   ` (6 preceding siblings ...)
  2025-05-23  7:15 ` [ruby-core:122246] " ko1 (Koichi Sasada) via ruby-core
@ 2025-05-23  7:31 ` fxn (Xavier Noria) via ruby-core
  2025-06-05  4:32 ` [ruby-core:122420] " matz (Yukihiro Matsumoto) via ruby-core
  8 siblings, 0 replies; 10+ messages in thread
From: fxn (Xavier Noria) via ruby-core @ 2025-05-23  7:31 UTC (permalink / raw)
  To: ruby-core; +Cc: fxn (Xavier Noria)

Issue #21316 has been updated by fxn (Xavier Noria).


@ko1 yeah, in Ruby you can have two classes with the same permanent name today. You know that, but let me show an example for the archives:

```ruby
c = Class.new
C = c

Object.send(:remove_const, :C)

d = Class.new
C = d

p c.name == d.name # true
```

So, where possible, in arbitrary settings, you better work with class objects.

But there are common scenarios (configuration, etc., I described a few above) in which you do not have the object and the natural way to refer to the class is by its name.

Today, that is a common practice, and if you want to be able to load arbitrary code within a namespace, I think this has to be preserved. Otherwise, such code won't be loadable under a namespace.

----------------------------------------
Bug #21316: Namespaces leak with permanent names
https://bugs.ruby-lang.org/issues/21316#change-113393

* Author: fxn (Xavier Noria)
* Status: Open
* Backport: 3.2: UNKNOWN, 3.3: UNKNOWN, 3.4: UNKNOWN
----------------------------------------
Namespaces are not transparent for this program

```ruby
C = Class.new
C.name == 'C'
```

because under a non-main user namespace, the name of `C` has the namespace as a prefix.



-- 
https://bugs.ruby-lang.org/
 ______________________________________________
 ruby-core mailing list -- ruby-core@ml.ruby-lang.org
 To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
 ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [ruby-core:122420] [Ruby Bug#21316] Namespaces leak with permanent names
  2025-05-09 22:05 [ruby-core:121950] [Ruby Bug#21316] Namespaces leak with permanent names fxn (Xavier Noria) via ruby-core
                   ` (7 preceding siblings ...)
  2025-05-23  7:31 ` [ruby-core:122247] " fxn (Xavier Noria) via ruby-core
@ 2025-06-05  4:32 ` matz (Yukihiro Matsumoto) via ruby-core
  8 siblings, 0 replies; 10+ messages in thread
From: matz (Yukihiro Matsumoto) via ruby-core @ 2025-06-05  4:32 UTC (permalink / raw)
  To: ruby-core; +Cc: matz (Yukihiro Matsumoto)

Issue #21316 has been updated by matz (Yukihiro Matsumoto).


As #21335, we should not provide information that depends on namespace, until we fix high level API.

Matz.


----------------------------------------
Bug #21316: Namespaces leak with permanent names
https://bugs.ruby-lang.org/issues/21316#change-113600

* Author: fxn (Xavier Noria)
* Status: Open
* Backport: 3.2: UNKNOWN, 3.3: UNKNOWN, 3.4: UNKNOWN
----------------------------------------
Namespaces are not transparent for this program

```ruby
C = Class.new
C.name == 'C'
```

because under a non-main user namespace, the name of `C` has the namespace as a prefix.



-- 
https://bugs.ruby-lang.org/
______________________________________________
 ruby-core mailing list -- ruby-core@ml.ruby-lang.org
 To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
 ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/

^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2025-06-05  4:33 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-05-09 22:05 [ruby-core:121950] [Ruby Bug#21316] Namespaces leak with permanent names fxn (Xavier Noria) via ruby-core
2025-05-10  6:22 ` [ruby-core:121962] " mame (Yusuke Endoh) via ruby-core
2025-05-10  8:18 ` [ruby-core:121964] " fxn (Xavier Noria) via ruby-core
2025-05-10 12:13 ` [ruby-core:121983] " Eregon (Benoit Daloze) via ruby-core
2025-05-13 11:20 ` [ruby-core:122045] " make_now_just (Hiroya Fujinami) via ruby-core
2025-05-13 11:49 ` [ruby-core:122049] " Eregon (Benoit Daloze) via ruby-core
2025-05-13 11:52 ` [ruby-core:122050] " make_now_just (Hiroya Fujinami) via ruby-core
2025-05-23  7:15 ` [ruby-core:122246] " ko1 (Koichi Sasada) via ruby-core
2025-05-23  7:31 ` [ruby-core:122247] " fxn (Xavier Noria) via ruby-core
2025-06-05  4:32 ` [ruby-core:122420] " matz (Yukihiro Matsumoto) via ruby-core

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).