Skip to content

Update to latest json gem version

Chad Woolley requested to merge caw-bump-json-gem into master

What does this MR do and why?

Upgrades json gem version from 2.5.1 to 2.6.3.

This avoids issues with tools/IDEs loading more recent versions prior to Rails initialization, then failing with version conflict.

For example, the following error occurs when attempting to run gdk pristine, but json 2.6.3 is also installed in the same Ruby asdf installation:

--------------------------------------------------------------------------------
Enabling Lefthook for gitlab-org/gitlab
--------------------------------------------------------------------------------
bundler: failed to load command: lefthook (/Users/pedropombeiro/.asdf/installs/ruby/2.7.7/bin/lefthook)
/Users/pedropombeiro/.asdf/installs/ruby/2.7.7/lib/ruby/site_ruby/2.7.0/bundler/runtime.rb:308:in `check_for_activated_spec!': You have already activated json 2.6.3, but your Gemfile requires json 2.5.1. Prepending `bundle exec` to your command may solve this. (Gem::LoadError)
	from /Users/pedropombeiro/.asdf/installs/ruby/2.7.7/lib/ruby/site_ruby/2.7.0/bundler/runtime.rb:25:in `block in setup'
	from /Users/pedropombeiro/.asdf/installs/ruby/2.7.7/lib/ruby/site_ruby/2.7.0/bundler/spec_set.rb:155:in `each'
	from /Users/pedropombeiro/.asdf/installs/ruby/2.7.7/lib/ruby/site_ruby/2.7.0/bundler/spec_set.rb:155:in `each'
	from /Users/pedropombeiro/.asdf/installs/ruby/2.7.7/lib/ruby/site_ruby/2.7.0/bundler/runtime.rb:24:in `map'
	from /Users/pedropombeiro/.asdf/installs/ruby/2.7.7/lib/ruby/site_ruby/2.7.0/bundler/runtime.rb:24:in `setup'
	from /Users/pedropombeiro/.asdf/installs/ruby/2.7.7/lib/ruby/site_ruby/2.7.0/bundler.rb:161:in `setup'
	from /Users/pedropombeiro/.asdf/installs/ruby/2.7.7/lib/ruby/site_ruby/2.7.0/bundler/setup.rb:20:in `block in <top (required)>'
	from /Users/pedropombeiro/.asdf/installs/ruby/2.7.7/lib/ruby/site_ruby/2.7.0/bundler/ui/shell.rb:136:in `with_level'
	from /Users/pedropombeiro/.asdf/installs/ruby/2.7.7/lib/ruby/site_ruby/2.7.0/bundler/ui/shell.rb:88:in `silence'
	from /Users/pedropombeiro/.asdf/installs/ruby/2.7.7/lib/ruby/site_ruby/2.7.0/bundler/setup.rb:20:in `<top (required)>'
	from /Users/pedropombeiro/.asdf/installs/ruby/2.7.7/lib/ruby/site_ruby/2.7.0/bundler/cli/exec.rb:56:in `require_relative'
	from /Users/pedropombeiro/.asdf/installs/ruby/2.7.7/lib/ruby/site_ruby/2.7.0/bundler/cli/exec.rb:56:in `kernel_load'
	from /Users/pedropombeiro/.asdf/installs/ruby/2.7.7/lib/ruby/site_ruby/2.7.0/bundler/cli/exec.rb:23:in `run'
	from /Users/pedropombeiro/.asdf/installs/ruby/2.7.7/lib/ruby/site_ruby/2.7.0/bundler/cli.rb:486:in `exec'
	from /Users/pedropombeiro/.asdf/installs/ruby/2.7.7/lib/ruby/site_ruby/2.7.0/bundler/vendor/thor/lib/thor/command.rb:27:in `run'
	from /Users/pedropombeiro/.asdf/installs/ruby/2.7.7/lib/ruby/site_ruby/2.7.0/bundler/vendor/thor/lib/thor/invocation.rb:127:in `invoke_command'
	from /Users/pedropombeiro/.asdf/installs/ruby/2.7.7/lib/ruby/site_ruby/2.7.0/bundler/vendor/thor/lib/thor.rb:392:in `dispatch'
	from /Users/pedropombeiro/.asdf/installs/ruby/2.7.7/lib/ruby/site_ruby/2.7.0/bundler/cli.rb:31:in `dispatch'
	from /Users/pedropombeiro/.asdf/installs/ruby/2.7.7/lib/ruby/site_ruby/2.7.0/bundler/vendor/thor/lib/thor/base.rb:485:in `start'
	from /Users/pedropombeiro/.asdf/installs/ruby/2.7.7/lib/ruby/site_ruby/2.7.0/bundler/cli.rb:25:in `start'
	from /Users/pedropombeiro/.asdf/installs/ruby/2.7.7/lib/ruby/gems/2.7.0/gems/bundler-2.3.26/exe/bundle:48:in `block in <top (required)>'
	from /Users/pedropombeiro/.asdf/installs/ruby/2.7.7/lib/ruby/site_ruby/2.7.0/bundler/friendly_errors.rb:120:in `with_friendly_errors'
	from /Users/pedropombeiro/.asdf/installs/ruby/2.7.7/lib/ruby/gems/2.7.0/gems/bundler-2.3.26/exe/bundle:36:in `<top (required)>'
	from /Users/pedropombeiro/.asdf/installs/ruby/2.7.7/bin/bundle:25:in `load'
	from /Users/pedropombeiro/.asdf/installs/ruby/2.7.7/bin/bundle:25:in `<main>'
make: *** [.gitlab-lefthook] Error 1
❌️ ERROR: Failed to run 'gdk pristine' - Had an issue with 'reset_configs'.

In this case, the only workaround is to manually uninstall the most recent version of the gem, but it will get reinstalled again if another project installs it in the same Ruby asdf version.

Required code changes

The following code changes had to be made to support the new gem version:

In the case of invalid encodings, a rescue for JSON::GeneratorError had to be added in addition to Encoding::UndefinedConversionError.

For example in in Gitlab::Diff::HighlightCache#write_to_redis_hash, which is a follow-on to the rescue which was originally added in !69069 (merged)

I spent a good deal of time trying to determine whether the rescue for Encoding::UndefinedConversionError is still necessary. Under the previous 2.5.1 json gem version, this seemed to come from 'C' native code somewhere, but I could not identify exactly what library or code was responsible for it.

So, it seemed safer to preserve the rescue for both Encoding::UndefinedConversionError, there's no risk as far as I can see to keeping it around, and it's minimal debt if it actually is unnecessary.

Also, there are a few places where Encoding::UndefinedConversionError was rescued, but did NOT appear to be related to JSON parsing (e.g. emails). In these cases, I did not add the extra rescue class for JSON::GeneratorError.

How to set up and validate locally

This is a core library used in many places, but if there are no CI or QA failures, then we should be able to assume it is a safe upgrade.

The pipeline should be green with:

MR acceptance checklist

This checklist encourages us to confirm any changes have been analyzed to reduce risks in quality, performance, reliability, security, and maintainability.

Edited by Pedro Pombeiro

Merge request reports