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

Make Rails app detection based on Rails::Application superclass #2218

Merged
merged 6 commits into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
19 changes: 15 additions & 4 deletions lib/ruby_lsp/setup_bundler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ def initialize(project_path, **options)
@last_updated_path = T.let(@custom_dir + "last_updated", Pathname)

@dependencies = T.let(load_dependencies, T::Hash[String, T.untyped])
@rails_app = T.let(rails_app?, T::Boolean)
@retry = T.let(false, T::Boolean)
end

Expand All @@ -62,7 +63,7 @@ def setup!
# Do not set up a custom bundle if LSP dependencies are already in the Gemfile
if @dependencies["ruby-lsp"] &&
@dependencies["debug"] &&
(@dependencies["rails"] ? @dependencies["ruby-lsp-rails"] : true)
(@rails_app ? @dependencies["ruby-lsp-rails"] : true)
$stderr.puts(
"Ruby LSP> Skipping custom bundle setup since LSP dependencies are already in #{@gemfile}",
)
Expand Down Expand Up @@ -148,7 +149,7 @@ def write_custom_gemfile
parts << 'gem "debug", require: false, group: :development, platforms: :mri'
end

if @dependencies["rails"] && !@dependencies["ruby-lsp-rails"]
if @rails_app && !@dependencies["ruby-lsp-rails"]
parts << 'gem "ruby-lsp-rails", require: false, group: :development'
end

Expand Down Expand Up @@ -209,7 +210,7 @@ def run_bundle_install(bundle_gemfile = @gemfile)
command << " && bundle update "
command << "ruby-lsp " unless @dependencies["ruby-lsp"]
command << "debug " unless @dependencies["debug"]
command << "ruby-lsp-rails " if @dependencies["rails"] && !@dependencies["ruby-lsp-rails"]
command << "ruby-lsp-rails " if @rails_app && !@dependencies["ruby-lsp-rails"]
command << "--pre" if @experimental
command.delete_suffix!(" ")
command << ")"
Expand Down Expand Up @@ -244,7 +245,7 @@ def run_bundle_install(bundle_gemfile = @gemfile)
def should_bundle_update?
# If `ruby-lsp`, `ruby-lsp-rails` and `debug` are in the Gemfile, then we shouldn't try to upgrade them or else it
# will produce version control changes
if @dependencies["rails"]
if @rails_app
return false if @dependencies.values_at("ruby-lsp", "ruby-lsp-rails", "debug").all?

# If the custom lockfile doesn't include `ruby-lsp`, `ruby-lsp-rails` or `debug`, we need to run bundle install
Expand Down Expand Up @@ -280,5 +281,15 @@ def correct_relative_remote_paths

@custom_lockfile.write(content)
end

# Detects if the project is a Rails app by looking if the superclass of the main class is `Rails::Application`
sig { returns(T::Boolean) }
def rails_app?
config = Pathname.new("config/application.rb").expand_path
application_contents = config.read if config.exist?
return false unless application_contents

/class .* < (::)?Rails::Application/.match?(application_contents)
end
end
end
4 changes: 4 additions & 0 deletions test/fixtures/rails_application.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module MyApp
class Application < Rails::Application
end
end
6 changes: 6 additions & 0 deletions test/setup_bundler_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ def test_creates_custom_bundle_for_a_rails_app
bundle_env(".ruby-lsp/Gemfile"),
"(bundle check || bundle install) 1>&2",
).returns(true)

FileUtils.mkdir("config")
FileUtils.cp("test/fixtures/rails_application.rb", "config/application.rb")
Bundler::LockfileParser.any_instance.expects(:dependencies).returns({ "rails" => true }).at_least_once
run_script

Expand All @@ -91,6 +94,7 @@ def test_creates_custom_bundle_for_a_rails_app
assert_match("ruby-lsp-rails", File.read(".ruby-lsp/Gemfile"))
ensure
FileUtils.rm_r(".ruby-lsp") if Dir.exist?(".ruby-lsp")
FileUtils.rm_rf("config") if Dir.exist?("config")
andyw8 marked this conversation as resolved.
Show resolved Hide resolved
end

def test_changing_lockfile_causes_custom_bundle_to_be_rebuilt
Expand Down Expand Up @@ -480,6 +484,8 @@ def test_ensures_lockfile_remotes_are_relative_to_default_gemfile

def test_ruby_lsp_rails_is_automatically_included_in_rails_apps
Dir.mktmpdir do |dir|
FileUtils.mkdir("#{dir}/config")
FileUtils.cp("test/fixtures/rails_application.rb", "#{dir}/config/application.rb")
Dir.chdir(dir) do
File.write(File.join(dir, "Gemfile"), <<~GEMFILE)
source "https://rubygems.org"
Expand Down
Loading