Bug #18177


An error occurs when running RSpec with Ruby 3.1.0-dev

Added by koic (Koichi ITO) 9 months ago. Updated 9 months ago.

Target version:
ruby -v:
ruby 3.1.0dev (2021-09-17T11:46:18Z master b45fe48fbb) [x86_64-darwin19]


Reproduction context

The following code causes an ArgumentError error when using Ruby 3.1.0-dev.

# foo_spec.rb
RSpec.shared_context 'config', :config do

RSpec.describe 'foo', :config do

RSpec version is as follows.

% gem i rspec
% rspec -v
RSpec 3.10
  - rspec-core 3.10.1
  - rspec-expectations 3.10.1
  - rspec-mocks 3.10.2
  - rspec-rails 5.0.1
  - rspec-support 3.10.2

Expected Behavior (Ruby 3.0.2)

No errors.

% ruby -v
ruby 3.0.2p107 (2021-07-07 revision 0db68f0233) [x86_64-darwin19]
% rspec foo_spec.rb
No examples found.

Finished in 0.00023 seconds (files took 0.11432 seconds to load)
0 examples, 0 failures

Actual Behavior (Ruby 3.1.0-dev)

ArgumentError: uninitialized module exception is raised.

% ruby -v
ruby 3.1.0dev (2021-09-17T11:46:18Z master b45fe48fbb) [x86_64-darwin19]
% rspec foo_spec.rb

An error occurred while loading ./foo_spec.rb.
Failure/Error: host.__send__(:include, mod) unless host < mod

  uninitialized module
# ./foo_spec.rb:5:in `<top (required)>'
No examples found.

Finished in 0.00003 seconds (files took 0.18197 seconds to load)
0 examples, 0 failures, 1 error occurred outside of examples

Perhaps the following commits have changed the behavior.

I encountered this error in the following build error of RuboCop Rails.

I opened this issue because I'm not sure what an expected behavior is. Thank you.

Updated by nobu (Nobuyoshi Nakada) 9 months ago

As I'm not sure where the uninitialized module came from, can't make a simple test code.

Does this fix it?

diff --git a/class.c b/class.c
index 0b075a9abb8..c56a149afc7 100644
--- a/class.c
+++ b/class.c
@@ -917,7 +917,8 @@ ensure_includable(VALUE klass, VALUE module)
     Check_Type(module, T_MODULE);
     if (RMODULE_UNINITIALIZED(module)) {
-        rb_raise(rb_eArgError, "uninitialized module");
+        RB_OBJ_WRITE(module, &RCLASS(module)->super, 0);
+        /* no more re-initialization */
     if (!NIL_P(rb_refinement_module_get_refined_class(module))) {
 	rb_raise(rb_eArgError, "refinement module is not allowed");

Updated by nobu (Nobuyoshi Nakada) 9 months ago

Maybe, like this?

m = {def initialize_copy(other);end}.new.dup
Actions #4

Updated by nobu (Nobuyoshi Nakada) 9 months ago

  • Status changed from Open to Closed

Applied in changeset git|2e3d43e5775799d1b4d6672a3a18b3fc5777c52b.

Allow to include uninitialized modules [Bug #18177]

The module that is about to be included is considered initialized.

Updated by koic (Koichi ITO) 9 months ago

I have confirmed that the issue has been resolved that was raising ArgumentError when running RSpec. Thanks a lot for the quick fix!


Also available in: Atom PDF