Rails 5.1: “unknown firstpos: NilClass” - Issue reloading application

萝らか妹 提交于 2019-12-02 18:46:22

I just faced exactly the same problem. I sovled it by setting:

config/environments/development.rb

from:

# Do not eager load code on boot.
config.eager_load = true

to:

**# Do not eager load code on boot.
config.eager_load = false

Hope this helps! Cheers, Nic.

Workaround found! https://github.com/rails/rails/pull/32296

The pull request is not merged, and will probably only be in 5.2+ anyway. Adding a monkey patch with the one-line change fixed the problem entirely for me.

config/initializers/routes.rb

# MONKEY PATCH!!!
# https://github.com/rails/rails/pull/32296
#
# Fixes:
# * Development mode deadlocks
# * ArgumentError: unknown firstpos: NilClass
#
# Allows use of "config.eager_load = true"


module ActionDispatch
  module Journey
    class Routes
      def simulator
        @simulator ||= begin
          gtg = GTG::Builder.new(ast).transition_table unless ast.blank?
          GTG::Simulator.new(gtg)
        end
      end
    end
  end
end

Seems it is Spring hanging or something. Just run spring stop and it should go away. Alternatively you can start the rails console without spring like this:

DISABLE_SPRING=true rails c.

I started having this problem after upgrading Rails from 5.1 to 5.2
It got solved by:

spring stop
spring binstub --all
spring start
rails s

You will not get this bug in production environment and in a test environment (if you don't use Spring). Because this bug "ArgumentError: unknown firstpos: NilClass" you got in "reload" when it tried to reload some your classes.

In production and test environments all things are in cache, so all your things will be cached and bug will not happen

Unfortunately (for now) for the development environment, I also found only this solution

config.eager_load = false
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!