0

A Rails 6 application was in development mode running with passenger.

The change in the nginx file from passenger_app_env development; to passenger_app_env production; passes the sudo nginx -t

However, upon hitting the domain, the following error appears in the nginx.log

Error: The application encountered the following error: expected file /home/deploy/fedel/releases/20221105055205/app/controllers/masquerades_controller.rb to define constant MasqueradesController, but didn't (Zeitwerk::NameError)

when reverting back into development, the applicatino runs.

The controller's contents are, consistent with the gem devise_masquerade:

class Admin::MasqueradesController < Devise::MasqueradesController
  protected

  def masquerade_authorize!
    authorize(User, :masquerade?) unless params[:action] == 'back'
  end
end

So, yes the controller does not reference the literal constant MasqueradesController but Devise::MasqueradesController.

Why is this functioning differently between development and production? How can this be overcome?

Jerome
  • 5,583
  • 3
  • 33
  • 76
  • 1
    It's extremely strange that this controller works in the `development` environment or the test suite is green (assuming it uses it), because that file should define `MasqueradesController` but it defines the unrelated `Admin::MasqueradesController` under the `Admin` namespace. You sure it works in `development`? If the answer is affirmative, we need to dig into it. Otherwise, you see where's the error. – Xavier Noria Nov 13 '22 at 19:53
  • Positive it works in development. Been using it that way for months. With a caveat: link to admin works, all masquerade functions run, but after logging in masquerading, the status div to reutrn to the admin control panel disappeared. Sorta got used to it and ignored that behaviour as it was not so bothersome. – Jerome Nov 13 '22 at 20:06
  • I "see where's the error." but this controller if I recall correctly, is the stock version from installation. – Jerome Nov 13 '22 at 20:09
  • You might check to see if you have `config.autoloader = :classic` in your dev environment config for some reason, but yes Zeitwerk will error because it expects to find `Admin::MasqueradesController` in `controllers/admin/masquerades_controller.rb`. If the gem generates the file in controllers, it's a bug with the gem. – rmlockerd Nov 13 '22 at 22:01
  • One simple way in which this situation could be happening is that you have `Admin::MasqueradesController` correctly defined in the file `app/controllers/admin/masquerades_controller.rb`, but that by mistake you also have a file called `app/controllers/masquerades_controller.rb` with those contents. In development mode, referencing `Admin::MasqueradesController` would work because the former exists, but when eager loading, the latter cannot be loaded. Could you check that please? If yes, just delete the file without the `admin` directory. – Xavier Noria Nov 14 '22 at 07:22
  • ah! There is only one file, under `app/controllers`. Thus it should be moved to `app/controllers/admin` , yes? – Jerome Nov 14 '22 at 07:28
  • Point is, if there is only one file under `app/controllers`, there's no way that can work in `development` today. Maybe it was moved? In any case, yes, it should be moved. – Xavier Noria Nov 14 '22 at 12:03

1 Answers1

1

The file for Admin::MasqueradesController should be defined in the subdirectory app/controllers/admin to match the Admin namespace.

It is a good practice to eager load in CI to anticipate anything like this. The Testing Rails Applications guide has a section with some recommendations to put this in place.

Xavier Noria
  • 1,640
  • 1
  • 8
  • 10