Slide 1

Slide 1 text

Satoshi Tagomori a.k.a., @tagomoris দߐRubyձٞ10 - Sep 16, 2023 Help Collisions, Isolate the Worlds Names, Modules, Versions, and NameSpaces

Slide 2

Slide 2 text

Satoshi Tagomori (@tagomoris) Independent developer Technical Consultant OSS Maintainer • Fluentd • MessagePack / msgpack-ruby • Norikra, Woothee, … tagomoris

Slide 3

Slide 3 text

https://www. fl ickr.com/photos/takkanm/3978417669 Asakusa.rb

Slide 4

Slide 4 text

“Namespace on read“ https://bugs.ruby-lang.org/issues/19744

Slide 5

Slide 5 text

Collisions Destroy Developers’ Experience

Slide 6

Slide 6 text

Collisions Names, Modules, Versions Name Collisions: No one can use a name in two ways. Module Collisions: Modules can be modified, globally, from anywhere. Version Collisions: Two different versions of a module can’t be loaded.

Slide 7

Slide 7 text

Name Collisions No one can use a name in two ways. • A structured namespace: • Foo::Bar::Baz • Major scenario: • Top-level “Configuration” in gems • Typical RoR classes vs Ruby, Gems, etc (like “Guild”) • Two apps in a process (in the context of “Modular monolith”)

Slide 8

Slide 8 text

Module Collisions Modules can be modi fi ed, globally, from anywhere. • A single Module/Class instance • Oj.default_options

Slide 9

Slide 9 text

Version Collisions Two di ff erent versions of a library can’t be loaded. • Dependency hell • Module A depends on C ver 1.5.x • Module B depends on C ver 1.6.x • The App depends on A and B

Slide 10

Slide 10 text

Avoid Collisions

Slide 11

Slide 11 text

Isolate Worlds

Slide 12

Slide 12 text

NameSpaces In other words: “Multiverse Ruby” https://rubykaigi.org/2023/presentations/shioyama.html#day2

Slide 13

Slide 13 text

NameSpaces in Practice N languages, N ways • Library space • Global NameSpace (+ Version) • Local names only • Names in code • Library name • Local alias • Class loader

Slide 14

Slide 14 text

NameSpaces in languages Many ways, Many hells… • JavaScript/TypeScript (npm) • Local names + Local Aliases • Java (classes w/ FQDN) • Global NameSpace + Library name • Class loaders (oops….) • Kotlin • Global NameSpace + Local Alias • Python (pip) • Global NameSpace (+ Local Alias) • Go • Local names + Local Aliases

Slide 15

Slide 15 text

Ruby Many Libraries in a Global NameSpace

Slide 16

Slide 16 text

Libraries and NameSpaces Libraries can’t be re-de fi ned • Once a library lives globally, it can’t be locally • Ruby has a huge library set - RubyGems

Slide 17

Slide 17 text

NameSpace Requirements What is it? • One native extension libraries of two versions at a time • Same name, Di ff erent (native) code, Di ff erent access path (local name) • Isolate libraries in speci fi ed namespaces at the required time (“on read”) Ruby Process LibraryX version: a.b.c “x.so” LibraryX version: x.y.z “x.so” ns1::X ns2::X

Slide 18

Slide 18 text

NameSpace Requirements What is it? • One native extension libraries of two versions at a time • Same name, Di ff erent (native) code, Di ff erent access path (local name) • Isolate libraries in speci fi ed namespaces at the required time (“on read”) Ruby Process LibraryX version: a.b.c “x.so” LibraryX version: x.y.z “x.so” ns1::X X

Slide 19

Slide 19 text

My Proposal: module NameSpace < Module Respects load( fi lename, wrap) • The wrap of load(filename, wrap): • works as a simple namespace • Introduce a sub-class of Module: NameSpace • works like the wrap of load • has instance methods #require, #load • Dependencies will be required/loaded in the namespace recursively

Slide 20

Slide 20 text

Let’s go! DEMO

Slide 21

Slide 21 text

🎉👏🍻 “Namespace on read PoC” https://github.com/tagomoris/ruby/pull/1

Slide 22

Slide 22 text

Hard Things, TODOs, Designs, … “How This Language Should Be?” • Hard Things: Native extension MAY need to be updated, in case • Native extensions using others’ C-functions
 https://github.com/tagomoris/sequel_pg/pull/1 • TODOs: Upgrade path SHOULD be de fi ned • How can we mark a gem is namespace-ready? • How can we determine the namespace of a library? (RubyGems/Bundler) • Designs • What’s the namespace in Ruby? • Problems around the top-level names in namespaces

Slide 23

Slide 23 text

Top-level Names in NameSpaces Should it be global? Or local? • “ns1::X” - A top-level name “X” de fi ned in a namespace “ns1” • It MUST be so, to isolate “ns1::X” from the “X” • “X” in the code isolated in “ns1” • It should be “ns1::X”, of course • But many gems add methods on “String” by “class String”… • “::X” in the code isolated in “ns1” • Should it be “ns1::X”? or the “X”? • “class ::String” should be possible

Slide 24

Slide 24 text

The TODOs I need discussions • To Go? Or No Go? • In this way? Or another way? Or NO WAY? • Which way to go? • How can we break codes IN NAMESPACES? (Not globally)

Slide 25

Slide 25 text

I NEED DISCUSSIONS For further developments. Thank you!