Skip to content

Fix autoloading issue in test environment

username-removed-283999 requested to merge fix-build into master

Always prefer to use the full class namespace when specifying a superclass inside a module, because autoloading can occur in a different order between execution environments.

module Foo
  class Base
    def initialize
      raise "This is the wrong super class!"
    end
  end
end

module Foo
  module Bar
    class Sample < Base
      # Inherits from ::Foo::Base because Foo::Bar::Base isn't defined yet
    end
  end
end

module Foo
  module Bar
    class Base
      def initialize
        puts "Success!"
      end
    end
  end
end

> Foo::Bar::Sample.new
=> RuntimeError: This is the wrong super class!

The trouble occurs when Rails has already autoloaded Gitlab::SearchResults and not Gitlab::Elastic::SearchResults. In this case, Rails will never need to autoload Gitlab::Elastic::SearchResults because Gitlab::SearchResults already matches the superclass. This is why this build only fails when the spec run after spec/lib/gitlab/search_results_spec.rb.

/cc @DouweM @rspeicher @vsizov

Merge request reports