@@ -57,15 +57,18 @@ def initialize(project_path, **options)
57
57
def setup!
58
58
raise BundleNotLocked if @gemfile &.exist? && !@lockfile &.exist?
59
59
60
- # Do not setup a custom bundle if both `ruby-lsp` and `debug` are already in the Gemfile
61
- if @dependencies [ "ruby-lsp" ] && @dependencies [ "debug" ]
60
+ # Do not set up a custom bundle if LSP dependencies are already in the Gemfile
61
+ if @dependencies [ "ruby-lsp" ] &&
62
+ @dependencies [ "debug" ] &&
63
+ ( @dependencies [ "rails" ] ? @dependencies [ "ruby-lsp-rails" ] : true )
62
64
$stderr. puts (
63
- "Ruby LSP> Skipping custom bundle setup since both `ruby-lsp` and `debug` are already in #{ @gemfile } " ,
65
+ "Ruby LSP> Skipping custom bundle setup since LSP dependencies are already in #{ @gemfile } " ,
64
66
)
65
67
66
- # If the user decided to add the `ruby-lsp` and `debug` to their Gemfile after having already run the Ruby LSP,
67
- # then we need to remove the `.ruby-lsp` folder, otherwise we will run `bundle install` for the top level and
68
- # try to execute the Ruby LSP using the custom bundle, which will fail since the gems are not installed there
68
+ # If the user decided to add `ruby-lsp` and `debug` (and potentially `ruby-lsp-rails`) to their Gemfile after
69
+ # having already run the Ruby LSP, then we need to remove the `.ruby-lsp` folder, otherwise we will run `bundle
70
+ # install` for the top level and try to execute the Ruby LSP using the custom bundle, which will fail since the
71
+ # gems are not installed there
69
72
@custom_dir . rmtree if @custom_dir . exist?
70
73
return run_bundle_install
71
74
end
@@ -143,6 +146,10 @@ def write_custom_gemfile
143
146
parts << 'gem "debug", require: false, group: :development, platforms: :mri'
144
147
end
145
148
149
+ if @dependencies [ "rails" ] && !@dependencies [ "ruby-lsp-rails" ]
150
+ parts << 'gem "ruby-lsp-rails", require: false, group: :development'
151
+ end
152
+
146
153
content = parts . join ( "\n " )
147
154
@custom_gemfile . write ( content ) unless @custom_gemfile . exist? && @custom_gemfile . read == content
148
155
end
@@ -183,22 +190,24 @@ def run_bundle_install(bundle_gemfile = @gemfile)
183
190
local_config_path = File . join ( Dir . pwd , ".bundle" )
184
191
env [ "BUNDLE_APP_CONFIG" ] = local_config_path if Dir . exist? ( local_config_path )
185
192
186
- # If both `ruby-lsp` and `debug` are already in the Gemfile, then we shouldn't try to upgrade them or else we'll
187
- # produce undesired source control changes. If the custom bundle was just created and either `ruby-lsp` or `debug`
188
- # weren't a part of the Gemfile, then we need to run `bundle install` for the first time to generate the
189
- # Gemfile.lock with them included or else Bundler will complain that they're missing. We can only update if the
190
- # custom `.ruby-lsp/Gemfile.lock` already exists and includes both gems
193
+ # If `ruby-lsp` and `debug` (and potentially `ruby-lsp-rails`) are already in the Gemfile, then we shouldn't try
194
+ # to upgrade them or else we'll produce undesired source control changes. If the custom bundle was just created
195
+ # and any of `ruby-lsp`, `ruby-lsp-rails` or `debug` weren't a part of the Gemfile, then we need to run `bundle
196
+ # install` for the first time to generate the Gemfile.lock with them included or else Bundler will complain that
197
+ # they're missing. We can only update if the custom `.ruby-lsp/Gemfile.lock` already exists and includes all gems
191
198
192
199
# When not updating, we run `(bundle check || bundle install)`
193
200
# When updating, we run `((bundle check && bundle update ruby-lsp debug) || bundle install)`
194
201
command = +"(bundle check"
195
202
196
203
if should_bundle_update?
197
- # If ruby-lsp or debug are not in the Gemfile, try to update them to the latest version
204
+ # If any of `ruby-lsp`, `ruby-lsp-rails` or `debug` are not in the Gemfile, try to update them to the latest
205
+ # version
198
206
command . prepend ( "(" )
199
207
command << " && bundle update "
200
208
command << "ruby-lsp " unless @dependencies [ "ruby-lsp" ]
201
209
command << "debug " unless @dependencies [ "debug" ]
210
+ command << "ruby-lsp-rails " if @dependencies [ "rails" ] && !@dependencies [ "ruby-lsp-rails" ]
202
211
command << "--pre" if @experimental
203
212
command . delete_suffix! ( " " )
204
213
command << ")"
@@ -221,13 +230,20 @@ def run_bundle_install(bundle_gemfile = @gemfile)
221
230
222
231
sig { returns ( T ::Boolean ) }
223
232
def should_bundle_update?
224
- # If both `ruby-lsp` and `debug` are in the Gemfile, then we shouldn't try to upgrade them or else it will produce
225
- # version control changes
226
- return false if @dependencies [ "ruby-lsp" ] && @dependencies [ "debug" ]
233
+ # If `ruby-lsp`, `ruby-lsp-rails` and `debug` are in the Gemfile, then we shouldn't try to upgrade them or else it
234
+ # will produce version control changes
235
+ if @dependencies [ "rails" ]
236
+ return false if @dependencies . values_at ( "ruby-lsp" , "ruby-lsp-rails" , "debug" ) . all?
237
+
238
+ # If the custom lockfile doesn't include `ruby-lsp`, `ruby-lsp-rails` or `debug`, we need to run bundle install
239
+ # before updating
240
+ return false if custom_bundle_dependencies . values_at ( "ruby-lsp" , "debug" , "ruby-lsp-rails" ) . any? ( &:nil? )
241
+ else
242
+ return false if @dependencies . values_at ( "ruby-lsp" , "debug" ) . all?
227
243
228
- # If the custom lockfile doesn't include either the `ruby-lsp` or `debug`, we need to run bundle install before
229
- # updating
230
- return false if custom_bundle_dependencies [ "ruby-lsp" ] . nil? || custom_bundle_dependencies [ "debug" ] . nil?
244
+ # If the custom lockfile doesn't include `ruby-lsp` or `debug`, we need to run bundle install before updating
245
+ return false if custom_bundle_dependencies . values_at ( "ruby-lsp" , "debug" ) . any? ( & :nil? )
246
+ end
231
247
232
248
# If the last updated file doesn't exist or was updated more than 4 hours ago, we should update
233
249
!@last_updated_path . exist? || Time . parse ( @last_updated_path . read ) < ( Time . now - FOUR_HOURS )
0 commit comments