Skip to content

Code reloading issue with multiple classes inside a single file #51

@Edouard-chin

Description

@Edouard-chin

Hello @fxn 👋 ,

Thanks a lot for Zeitwerk, it's a really cool gem.

I'm opening this issue to check if this is something Zeitwerk should support or our application is just not compliant with the file structure described by the Gem.

We have multiple files that have multiple classes inside, it's especially common for grouping error class inside a single file instead of having a bunch of files.

# application_error.rb

class ApplicationError < StandardError
end

class CustomError < ApplicationError
end

Zeitwerk has issues with reloading this and ruby will throw a TypeError: superclass mismatch for class CustomError. This make sense since CustomError was never marked for autoloading.

Here is a failing test

index dd69b8b..372ab87 100644
--- a/test/lib/zeitwerk/test_reloading.rb
+++ b/test/lib/zeitwerk/test_reloading.rb
@@ -18,6 +18,21 @@ class TestReloading < LoaderTest
     assert loader.reloading_enabled?
   end

+  test "superclass mismatch error" do
+    files = [
+      ["x.rb", "class X; end; class AnotherX < X; end"]
+    ]
+
+    with_setup(files) do
+      assert X
+      assert AnotherX
+
+      loader.reload
+
+      assert X
+    end
+  end
+
   test "reloading works if the flag is set" do
     files = [
       ["x.rb", "X = 1"],         # top-level

Happy to work on finding a fix if you consider that Zeitwerk should handle this case. Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions