ruby / json

JSON implementation for Ruby
https://ruby.github.io/json
Other
705 stars 332 forks source link

Prevent loading both `json/ext` and `json/pure` #682

Closed casperisfine closed 3 weeks ago

casperisfine commented 3 weeks ago

Fix: https://github.com/ruby/json/issues/650 Ref: https://github.com/ruby/json/issues/646

json_pure currently doesn't work well at all because json/ext is a default gem, so if you have code requiring json/pure and some other code requiring json, you end up with both loaded.

If the json and json_pure versions match, it's not too bad, but if they don't match, you might run into issues with private APIs no longer matching.

eregon commented 3 weeks ago

Isn't it a problem if lib/json.rb is loaded from json_pure because earlier in $LOAD_PATH, but then that ends up loading the C extension with require 'json/ext'? Then you would get .rb files from both gems, potentially with different versions, and a confusing JSON::VERSION constant which represents only half of it, no?

eregon commented 3 weeks ago

Also in that scenario lib/json/common.rb would be loaded from json_pure, but maybe there is some code there which is not compatible with what the C extension expects.

casperisfine commented 3 weeks ago

Hum, right, I think I need to use more require_relative.

eregon commented 3 weeks ago

I think we should just have json_pure an empty gem depending on json, it seems intrinsically very brittle to have 2 gems which share some Ruby files (e.g. lib/json/common.rb and lib/json/version.rb) of potentially different versions.

If we wanted to just make sure the versions match we'd need to have json_pure depend on json same version, which is the same thing and then I think there is no point to ship the same files in two gems.