Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Report Rails load errors and continue generating #1087

Merged
merged 1 commit into from
Aug 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions lib/tapioca/loaders/loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,17 @@ def load_rails_application(environment_load: false, eager_load: false)
silence_deprecations

if environment_load
safe_require("./config/environment")
require "./config/environment"
else
safe_require("./config/application")
require "./config/application"
end

eager_load_rails_app if eager_load
rescue LoadError, StandardError => e
say("Tapioca attempted to load the Rails application after encountering a `config/application.rb` file, " \
"but it failed. If your application uses Rails please ensure it can be loaded correctly before generating " \
"RBIs.\n#{e}", :yellow)
say("Continuing RBI generation without loading the Rails application.")
end

sig { void }
Expand Down
35 changes: 35 additions & 0 deletions spec/tapioca/cli/dsl_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1758,5 +1758,40 @@ def title=(title); end
end
end
end

describe "cli::dsl::custom application.rb" do
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The application.rb change was leaking to other tests so I moved it to a new top level describe block.

Other solutions could be changing this to use before_each or to write the original application.rb back at the end.

it "output errors when rails application cannot be loaded" do
@project.write("config/environment.rb", <<~RB)
require_relative "application.rb"
RB

@project.write("config/application.rb", <<~RB)
require "rails"

module Test
class Application < Rails::Application
raise "Error during application loading"
end
end
RB

@project.require_real_gem("rails")
@project.bundle_install
res = @project.tapioca("dsl")

out = "Tapioca attempted to load the Rails application after encountering a `config/application.rb` file, " \
"but it failed. If your application uses Rails please ensure it can be loaded correctly before " \
"generating RBIs."
assert_includes(res.out, out)
assert_includes(res.out, <<~OUT)
Error during application loading
Continuing RBI generation without loading the Rails application.
Done
Loading DSL compiler classes... Done
Compiling DSL RBI files...
OUT
assert_success_status(res)
end
end
end
end