Skip to content

Commit 392d1aa

Browse files
louimEarlopainandyw8Morriarvinistock
authored
Make Rails app detection based on Rails::Application superclass (#2218)
* Make Rails app detection based on Rails::Application superclass This change makes the Rails app detection based on the superclass of the main class being `Rails::Application`. This is a more reliable way to detect Rails apps than looking for the presence of the `rails` gem in the Gemfile. Application can require some of the Rails components without requiring the `rails` gem itself. co-authored-by: Earlopain <[email protected]> * Apply earl's suggestion * Create files instead of stubbing * Update lib/ruby_lsp/setup_bundler.rb Co-authored-by: Alexandre Terrasa <[email protected]> * Use fixture for test * Update test/setup_bundler_test.rb Co-authored-by: Vinicius Stock <[email protected]> --------- Co-authored-by: Earlopain <[email protected]> Co-authored-by: Andy Waite <[email protected]> Co-authored-by: Andy Waite <[email protected]> Co-authored-by: Alexandre Terrasa <[email protected]> Co-authored-by: Vinicius Stock <[email protected]>
1 parent e650df9 commit 392d1aa

File tree

3 files changed

+25
-4
lines changed

3 files changed

+25
-4
lines changed

lib/ruby_lsp/setup_bundler.rb

+15-4
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ def initialize(project_path, **options)
5050
@last_updated_path = T.let(@custom_dir + "last_updated", Pathname)
5151

5252
@dependencies = T.let(load_dependencies, T::Hash[String, T.untyped])
53+
@rails_app = T.let(rails_app?, T::Boolean)
5354
@retry = T.let(false, T::Boolean)
5455
end
5556

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

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

@@ -209,7 +210,7 @@ def run_bundle_install(bundle_gemfile = @gemfile)
209210
command << " && bundle update "
210211
command << "ruby-lsp " unless @dependencies["ruby-lsp"]
211212
command << "debug " unless @dependencies["debug"]
212-
command << "ruby-lsp-rails " if @dependencies["rails"] && !@dependencies["ruby-lsp-rails"]
213+
command << "ruby-lsp-rails " if @rails_app && !@dependencies["ruby-lsp-rails"]
213214
command << "--pre" if @experimental
214215
command.delete_suffix!(" ")
215216
command << ")"
@@ -244,7 +245,7 @@ def run_bundle_install(bundle_gemfile = @gemfile)
244245
def should_bundle_update?
245246
# If `ruby-lsp`, `ruby-lsp-rails` and `debug` are in the Gemfile, then we shouldn't try to upgrade them or else it
246247
# will produce version control changes
247-
if @dependencies["rails"]
248+
if @rails_app
248249
return false if @dependencies.values_at("ruby-lsp", "ruby-lsp-rails", "debug").all?
249250

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

281282
@custom_lockfile.write(content)
282283
end
284+
285+
# Detects if the project is a Rails app by looking if the superclass of the main class is `Rails::Application`
286+
sig { returns(T::Boolean) }
287+
def rails_app?
288+
config = Pathname.new("config/application.rb").expand_path
289+
application_contents = config.read if config.exist?
290+
return false unless application_contents
291+
292+
/class .* < (::)?Rails::Application/.match?(application_contents)
293+
end
283294
end
284295
end

test/fixtures/rails_application.rb

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module MyApp
2+
class Application < Rails::Application
3+
end
4+
end

test/setup_bundler_test.rb

+6
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ def test_creates_custom_bundle_for_a_rails_app
7979
bundle_env(".ruby-lsp/Gemfile"),
8080
"(bundle check || bundle install) 1>&2",
8181
).returns(true)
82+
83+
FileUtils.mkdir("config")
84+
FileUtils.cp("test/fixtures/rails_application.rb", "config/application.rb")
8285
Bundler::LockfileParser.any_instance.expects(:dependencies).returns({ "rails" => true }).at_least_once
8386
run_script
8487

@@ -91,6 +94,7 @@ def test_creates_custom_bundle_for_a_rails_app
9194
assert_match("ruby-lsp-rails", File.read(".ruby-lsp/Gemfile"))
9295
ensure
9396
FileUtils.rm_r(".ruby-lsp") if Dir.exist?(".ruby-lsp")
97+
FileUtils.rm_r("config") if Dir.exist?("config")
9498
end
9599

96100
def test_changing_lockfile_causes_custom_bundle_to_be_rebuilt
@@ -480,6 +484,8 @@ def test_ensures_lockfile_remotes_are_relative_to_default_gemfile
480484

481485
def test_ruby_lsp_rails_is_automatically_included_in_rails_apps
482486
Dir.mktmpdir do |dir|
487+
FileUtils.mkdir("#{dir}/config")
488+
FileUtils.cp("test/fixtures/rails_application.rb", "#{dir}/config/application.rb")
483489
Dir.chdir(dir) do
484490
File.write(File.join(dir, "Gemfile"), <<~GEMFILE)
485491
source "https://rubygems.org"

0 commit comments

Comments
 (0)