[ClassName] is not a class (TypeError)
Closed this issue · 3 comments
Running annotate I'm getting an error when it tries to annotate one of my ApplicationRecord
classes. The stack trace points to the file and is complaining about it.
Here's the end of the stack trace (The class in question is named Event
):
1: from /home/aaron/.rvm/gems/ruby-2.6.6@appt/gems/bootsnap-1.4.8/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:23:in `require'
/home/aaron/projects/clients/appt/app/models/event.rb:1:in `<main>': Event is not a class (TypeError)
I thought maybe I needed to run rake db:migrate
first and tried that but got a similar issue:
== 20210907203104 CreateEvents: migrating =====================================
-- create_table(:events)
-> 0.0265s
== 20210907203104 CreateEvents: migrated (0.0265s) ============================
Unable to annotate app/models/event.rb: Event is not a class
Model files unchanged.
This is the entirity of my Event
class in app/models/event.rb
:
class Event < ApplicationRecord
belongs_to :account
end
Commands
$ annotate
$ rake db:migrate
Version
- annotate version 3.1.1
- rails version 6.0.4.1
- ruby version ruby 2.6.6p146
The error means that Event is already defined as a module. It most likely does not have anything to do with this gem.
I was pretty sure I was doing something wrong. I'll have to figure out where that module would be but my guess is Event
is sort of a "reserved" name if its being used by Rails or something else.
Still it should be able to determine I want that particular class annotated and not some nebulous module somewhere in one of the gems I'm using in my project.
Ok, I solved this problem. As @maxcal previously stated, its not the gem causing this issue but a conflicting Module/Class.
For me, my Event
class was conflicting with Listen::Event
module. The listen
gem is being used by my developement
environment in conjunction with spring to monitor changes in files so the server doesn't need to be reloaded every time you change files (this is probably obvious already).
Solution:
If you get this error from annotate
, you can use Ruby 2.7+ to find where the conflicting file is located like so:
Object.const_source_location('Event')
=> ["/home/aaron/.rvm/gems/ruby-2.7.2@appt/gems/listen-3.1.5/lib/listen/event/processor.rb", 2]
Find out where that conflicting Module/Class is being included in your project. For me, it was including Listen
in my development environment like so:
require "#{Rails.root}/lib/listen"
# require is sufficient so including Listen here was not necessary
# commenting out the following line fixed my issue because Listen::Event was conflicting with my model Event.
include Listen
Rails.application.configure do
Removing the include Listen
fixed my issue.