In a Rails 8.1 app, why does renaming the class in lib/docket/pdf_exporter.rb to Docket::PDFExporter break production boot with Zeitwerk::NameError?
answer
- lib is autoloaded and eager loaded
- autoload_lib(ignore: %w[assets tasks])
- pdf_exporter camelizes to PdfExporter
- inflector.inflect overrides one basename
- development may never load it
basics
~10 sconfig.autoload_lib puts lib in the autoload and eager-load paths, and Zeitwerk expects pdf_exporter.rb to define Docket::PdfExporter. Production eager loads at boot, finds Docket::PDFExporter instead, and raises Zeitwerk::NameError.
solid answer
~40 sA new Rails 8.1 app's `config/application.rb` calls `config.autoload_lib(ignore: %w[assets tasks])`, which adds `lib` to both `autoload_paths` and `eager_load_paths`. Zeitwerk camelizes `pdf_exporter` to `PdfExporter`, so it expects the file to define `Docket::PdfExporter`. Production eager loads every file at boot, the file defines `Docket::PDFExporter` instead, and boot stops with `Zeitwerk::NameError: expected file .../lib/docket/pdf_exporter.rb to define constant Docket::PdfExporter, but didn't`. Development may never load the file, while callers using the new name hit a plain `NameError`. Fix it by keeping `PdfExporter`, or by teaching the autoloaders the name with `Rails.autoloaders.each { |l| l.inflector.inflect("pdf_exporter" => "PDFExporter") }`. An acronym inflection also works but changes inflection app-wide. Then run `bin/rails zeitwerk:check`.
code
ruby · 8 lines# lib/docket/pdf_exporter.rb
module Docket
class PDFExporter # Zeitwerk expects Docket::PdfExporter
def initialize(filing)
@filing = filing
end
end
endgo deeper
Know that lib is autoloaded in a new app and that file names decide constant names there too.
Explain what autoload_lib configures, why the error appears only when eager loading, and how to read the Zeitwerk::NameError message.
Choose between renaming, a per-basename inflector override and a global acronym, and keep the ignore list honest so eager loading never trips over lib.
Make naming errors a CI failure rather than a deploy failure, and plan the autoload_lib switch for apps that predate it.
## The setup: lib is autoloaded since Rails 7.1 The scenario is a legal-documents app. `lib/docket/pdf_exporter.rb` turns court filings into PDFs, and someone renamed its class from `Docket::PdfExporter` to `Docket::PDFExporter` because "PDF is an acronym". The file name stayed the same. Development worked, the tests that did not touch the exporter passed, and the production deploy failed at boot. The first thing to know is that `lib` is **managed by Zeitwerk** in a current app. `config.autoload_lib(ignore:)` arrived in Rails 7.1, and a Rails 8.1 `config/application.rb` contains: ```ruby config.autoload_lib(ignore: %w[assets tasks]) ``` The method does three things: 1. pushes `lib` onto `config.autoload_paths`; 2. pushes `lib` onto `config.eager_load_paths`; 3. tells `Rails.autoloaders.main` to **ignore** the listed subdirectories (`lib/assets`, `lib/tasks`), whose contents are not meant to be reloaded or eager loaded. So every `.rb` file under `lib`, apart from the ignored folders, follows the same file-to-constant rule as `app/models`. ## Why the rename breaks only production Zeitwerk derives the constant from the path: `lib` is a root, `docket` becomes `Docket`, and the basename `pdf_exporter` goes through Rails' inflector, which falls back to `String#camelize` and returns `PdfExporter`. Zeitwerk therefore expects `Docket::PdfExporter`. | Environment | What happens | |---|---| | development (`eager_load = false`) | Nothing loads the file until `Docket::PdfExporter` is referenced. Callers that already say `Docket::PDFExporter` get `NameError: uninitialized constant Docket::PDFExporter`. | | test without `CI` | Same as development: only the files the tests touch are loaded. | | CI and production (`eager_load = true`) | Every file loads at boot; the file defines the wrong constant and `Zeitwerk::NameError` stops the boot. | The message names both sides: "expected file .../lib/docket/pdf_exporter.rb to define constant Docket::PdfExporter, but didn't". `Zeitwerk::NameError` subclasses Ruby's `NameError`. ## Three fixes - **Follow the default inflection.** Name the class `Docket::PdfExporter`. This is the cheapest fix and needs no configuration. - **Override one basename for the autoloaders.** Rails' autoloader inflector has its own override table, used before `camelize`: ```ruby # config/initializers/zeitwerk.rb Rails.autoloaders.each do |autoloader| autoloader.inflector.inflect("pdf_exporter" => "PDFExporter") end ``` This touches only file-name inflection for the autoloaders, and only that basename. If the app uses the `once` autoloader, the configuration has to run from `config/application.rb`, because that loader is set up before initializers run. - **Declare an acronym.** `inflect.acronym "PDF"` in `config/initializers/inflections.rb` makes `camelize` produce `PDF` for every `pdf` word in the app, not just this file: a `pdf_files_controller.rb` would then have to define `PDFFilesController` instead of `PdfFilesController`. That global effect is why the per-basename override is usually preferred for a single file. Then run `bin/rails zeitwerk:check` to confirm the tree is consistent, and `Rails.autoloaders.main.cpath_expected_at("lib/docket/pdf_exporter.rb")` to see the expected name for one file. ## What belongs on the ignore list `ignore:` should name every `lib` subdirectory whose files are not meant to be autoloaded, reloaded or eager loaded: - `assets` and `tasks`, which a new app already lists; - folders such as `generators` or `templates`, whose Ruby files do not define constants that match their paths; - folders such as `middleware`, whose code you load with `require` because it is stored in a place that never reloads. An ignored path is invisible to Zeitwerk: it is neither autoloaded nor eager loaded, so code there must be required explicitly. ## Diagnosing it from the console When the error text is not at hand, the loaders can tell you what they expect: ```ruby Rails.autoloaders.main.cpath_expected_at("lib/docket/pdf_exporter.rb") # => "Docket::PdfExporter" ``` `cpath_expected_at` applies the current inflector, collapse and ignore rules, so it also confirms that an override took effect: after adding the `inflect` override above it returns `"Docket::PDFExporter"`. `Rails.autoloaders.log!` in `config/application.rb` goes further and prints every autoload Zeitwerk sets up and every file it loads, which helps when a whole directory behaves unexpectedly. ## Apps upgraded from before 7.1 An app generated before 7.1 may not call `autoload_lib`. There, `lib` is on `$LOAD_PATH` but not managed by Zeitwerk, so a renamed class only fails where something references the new name. Adding `autoload_lib` during an upgrade turns every non-conforming file under `lib` into a boot failure wherever the app eager loads, so run `zeitwerk:check` before deploying it.
- Why should lib/generators go on the autoload_lib ignore list?Generators follow the generator layout, not Zeitwerk's: `lib/generators/brief/brief_generator.rb` defines `BriefGenerator`, while Zeitwerk would expect `Generators::Brief::BriefGenerator`, and the templates beside it are not constants at all. `bin/rails generate` loads them itself when needed. Left unignored, eager loading would fail on them; ignored, Zeitwerk neither autoloads nor eager loads them.
- Where must the inflector override live if the app also uses config.autoload_lib_once?In `config/application.rb`, in the application class body. The `once` autoloader is set up early in boot, before `config/initializers` run, so an override placed in an initializer would arrive too late for it. When only the `main` loader matters, an initializer such as `config/initializers/zeitwerk.rb` is fine.
- Zeitwerk reads each file to find which class it defines, so the file name does not matter.
- lib is never autoloaded in Rails, so a class there always needs a require.
- The rename is fine because the app booted in development.
- Add inflect.acronym "PDF" and nothing else in the app changes.
- Put lib/docket on config.autoload_once_paths to make Zeitwerk accept any class name.
Assumes Rails 8.1, whose generated config/application.rb calls config.autoload_lib(ignore: %w[assets tasks]); the method was introduced in Rails 7.1.