Skip to content

Commit d3f4d0f

Browse files
trevorturkclaude
andcommitted
Fix Zeitwerk eager loading crash in non-Rails environments
Applications were crashing during Zeitwerk eager loading when railtie.rb references Rails::Railtie in non-Rails contexts. This happened because Zeitwerk's eager loading occurs before conditional require checks can run. This fix implements two complementary changes: 1. Wrap the Railtie class definition with a conditional check in railtie.rb 2. Tell Zeitwerk to ignore railtie.rb using loader.ignore() This ensures the file is only loaded in Rails environments and prevents NameError crashes during eager loading. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent 3b08a4a commit d3f4d0f

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed

lib/ruby_llm/mcp.rb

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,10 @@ def logger
100100
end
101101
end
102102

103-
require_relative "mcp/railtie" if defined?(Rails::Railtie)
104-
105103
loader = Zeitwerk::Loader.for_gem_extension(RubyLLM)
104+
105+
loader.ignore("#{__dir__}/mcp/railtie.rb")
106+
106107
loader.inflector.inflect("mcp" => "MCP")
107108
loader.inflector.inflect("sse" => "SSE")
108109
loader.inflector.inflect("openai" => "OpenAI")
@@ -118,3 +119,7 @@ def logger
118119
loader.inflector.inflect("browser_oauth_provider" => "BrowserOAuthProvider")
119120

120121
loader.setup
122+
123+
if defined?(Rails::Railtie)
124+
require_relative "mcp/railtie"
125+
end

lib/ruby_llm/mcp/railtie.rb

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
# frozen_string_literal: true
22

3-
module RubyLLM
4-
module MCP
5-
class Railtie < Rails::Railtie
6-
generators do
7-
require_relative "../../generators/ruby_llm/mcp/install/install_generator"
8-
require_relative "../../generators/ruby_llm/mcp/oauth/install_generator"
3+
if defined?(Rails::Railtie)
4+
module RubyLLM
5+
module MCP
6+
class Railtie < Rails::Railtie
7+
generators do
8+
require_relative "../../generators/ruby_llm/mcp/install/install_generator"
9+
require_relative "../../generators/ruby_llm/mcp/oauth/install_generator"
10+
end
911
end
1012
end
1113
end

0 commit comments

Comments
 (0)