ruby-lsp 0.21.3 → 0.22.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -12,7 +12,7 @@ require "digest"
12
12
  require "time"
13
13
  require "uri"
14
14
 
15
- # This file is a script that will configure a custom bundle for the Ruby LSP. The custom bundle allows developers to use
15
+ # This file is a script that will configure a composed bundle for the Ruby LSP. The composed bundle allows developers to use
16
16
  # the Ruby LSP without including the gem in their application's Gemfile while at the same time giving us access to the
17
17
  # exact locked versions of dependencies.
18
18
 
@@ -45,6 +45,9 @@ module RubyLsp
45
45
  )
46
46
  @lockfile = T.let(@gemfile ? Bundler.default_lockfile : nil, T.nilable(Pathname))
47
47
 
48
+ @gemfile_hash = T.let(@gemfile ? Digest::SHA256.hexdigest(@gemfile.read) : nil, T.nilable(String))
49
+ @lockfile_hash = T.let(@lockfile&.exist? ? Digest::SHA256.hexdigest(@lockfile.read) : nil, T.nilable(String))
50
+
48
51
  @gemfile_name = T.let(@gemfile&.basename&.to_s || "Gemfile", String)
49
52
 
50
53
  # Custom bundle paths
@@ -62,7 +65,7 @@ module RubyLsp
62
65
  @retry = T.let(false, T::Boolean)
63
66
  end
64
67
 
65
- # Sets up the custom bundle and returns the `BUNDLE_GEMFILE`, `BUNDLE_PATH` and `BUNDLE_APP_CONFIG` that should be
68
+ # Sets up the composed bundle and returns the `BUNDLE_GEMFILE`, `BUNDLE_PATH` and `BUNDLE_APP_CONFIG` that should be
66
69
  # used for running the server
67
70
  sig { returns(T::Hash[String, String]) }
68
71
  def setup!
@@ -73,12 +76,12 @@ module RubyLsp
73
76
  ignore_file = @custom_dir + ".gitignore"
74
77
  ignore_file.write("*") unless ignore_file.exist?
75
78
 
76
- # Do not set up a custom bundle if LSP dependencies are already in the Gemfile
79
+ # Do not set up a composed bundle if LSP dependencies are already in the Gemfile
77
80
  if @dependencies["ruby-lsp"] &&
78
81
  @dependencies["debug"] &&
79
82
  (@rails_app ? @dependencies["ruby-lsp-rails"] : true)
80
83
  $stderr.puts(
81
- "Ruby LSP> Skipping custom bundle setup since LSP dependencies are already in #{@gemfile}",
84
+ "Ruby LSP> Skipping composed bundle setup since LSP dependencies are already in #{@gemfile}",
82
85
  )
83
86
 
84
87
  return run_bundle_install
@@ -91,27 +94,25 @@ module RubyLsp
91
94
  return run_bundle_install(@custom_gemfile)
92
95
  end
93
96
 
94
- lockfile_contents = @lockfile.read
95
- current_lockfile_hash = Digest::SHA256.hexdigest(lockfile_contents)
96
-
97
- if @custom_lockfile.exist? && @lockfile_hash_path.exist? && @lockfile_hash_path.read == current_lockfile_hash
97
+ if @lockfile_hash && @custom_lockfile.exist? && @lockfile_hash_path.exist? &&
98
+ @lockfile_hash_path.read == @lockfile_hash
98
99
  $stderr.puts(
99
- "Ruby LSP> Skipping custom bundle setup since #{@custom_lockfile} already exists and is up to date",
100
+ "Ruby LSP> Skipping composed bundle setup since #{@custom_lockfile} already exists and is up to date",
100
101
  )
101
102
  return run_bundle_install(@custom_gemfile)
102
103
  end
103
104
 
104
105
  FileUtils.cp(@lockfile.to_s, @custom_lockfile.to_s)
105
106
  correct_relative_remote_paths
106
- @lockfile_hash_path.write(current_lockfile_hash)
107
+ @lockfile_hash_path.write(@lockfile_hash)
107
108
  run_bundle_install(@custom_gemfile)
108
109
  end
109
110
 
110
111
  private
111
112
 
112
113
  sig { returns(T::Hash[String, T.untyped]) }
113
- def custom_bundle_dependencies
114
- @custom_bundle_dependencies ||= T.let(
114
+ def composed_bundle_dependencies
115
+ @composed_bundle_dependencies ||= T.let(
115
116
  begin
116
117
  original_bundle_gemfile = ENV["BUNDLE_GEMFILE"]
117
118
 
@@ -136,8 +137,8 @@ module RubyLsp
136
137
  "",
137
138
  ]
138
139
 
139
- # If there's a top level Gemfile, we want to evaluate from the custom bundle. We get the source from the top level
140
- # Gemfile, so if there isn't one we need to add a default source
140
+ # If there's a top level Gemfile, we want to evaluate from the composed bundle. We get the source from the top
141
+ # level Gemfile, so if there isn't one we need to add a default source
141
142
  if @gemfile&.exist? && @lockfile&.exist?
142
143
  parts << "eval_gemfile(File.expand_path(\"../#{@gemfile_name}\", __dir__))"
143
144
  else
@@ -187,7 +188,7 @@ module RubyLsp
187
188
  env = bundler_settings_as_env
188
189
  env["BUNDLE_GEMFILE"] = bundle_gemfile.to_s
189
190
 
190
- # If the user has a custom bundle path configured, we need to ensure that we will use the absolute and not
191
+ # If the user has a composed bundle path configured, we need to ensure that we will use the absolute and not
191
192
  # relative version of it when running `bundle install`. This is necessary to avoid installing the gems under the
192
193
  # `.ruby-lsp` folder, which is not the user's intention. For example, if the path is configured as `vendor`, we
193
194
  # want to install it in the top level `vendor` and not `.ruby-lsp/vendor`
@@ -214,6 +215,23 @@ module RubyLsp
214
215
  @error_path.write(Marshal.dump(e))
215
216
  end
216
217
 
218
+ # If either the Gemfile or the lockfile have been modified during the process of setting up the bundle, retry
219
+ # composing the bundle from scratch
220
+
221
+ if @gemfile && @lockfile
222
+ current_gemfile_hash = Digest::SHA256.hexdigest(@gemfile.read)
223
+ current_lockfile_hash = Digest::SHA256.hexdigest(@lockfile.read)
224
+
225
+ if !@retry && (current_gemfile_hash != @gemfile_hash || current_lockfile_hash != @lockfile_hash)
226
+ @gemfile_hash = current_gemfile_hash
227
+ @lockfile_hash = current_lockfile_hash
228
+ @retry = true
229
+ @custom_dir.rmtree
230
+ $stderr.puts("Ruby LSP> Bundle was modified during setup. Retrying from scratch...")
231
+ return setup!
232
+ end
233
+ end
234
+
217
235
  env
218
236
  end
219
237
 
@@ -244,7 +262,7 @@ module RubyLsp
244
262
  base_bundle = base_bundle_command(env)
245
263
 
246
264
  # If `ruby-lsp` and `debug` (and potentially `ruby-lsp-rails`) are already in the Gemfile, then we shouldn't try
247
- # to upgrade them or else we'll produce undesired source control changes. If the custom bundle was just created
265
+ # to upgrade them or else we'll produce undesired source control changes. If the composed bundle was just created
248
266
  # and any of `ruby-lsp`, `ruby-lsp-rails` or `debug` weren't a part of the Gemfile, then we need to run `bundle
249
267
  # install` for the first time to generate the Gemfile.lock with them included or else Bundler will complain that
250
268
  # they're missing. We can only update if the custom `.ruby-lsp/Gemfile.lock` already exists and includes all gems
@@ -274,16 +292,16 @@ module RubyLsp
274
292
  command << "1>&2"
275
293
 
276
294
  # Add bundle update
277
- $stderr.puts("Ruby LSP> Running bundle install for the custom bundle. This may take a while...")
295
+ $stderr.puts("Ruby LSP> Running bundle install for the composed bundle. This may take a while...")
278
296
  $stderr.puts("Ruby LSP> Command: #{command}")
279
297
 
280
- # Try to run the bundle install or update command. If that fails, it normally means that the custom lockfile is in
281
- # a bad state that no longer reflects the top level one. In that case, we can remove the whole directory, try
298
+ # Try to run the bundle install or update command. If that fails, it normally means that the composed lockfile is
299
+ # in a bad state that no longer reflects the top level one. In that case, we can remove the whole directory, try
282
300
  # another time and give up if it fails again
283
301
  if !system(env, command) && !@retry && @custom_gemfile.exist?
284
302
  @retry = true
285
303
  @custom_dir.rmtree
286
- $stderr.puts("Ruby LSP> Running bundle install failed. Trying to re-generate the custom bundle from scratch")
304
+ $stderr.puts("Ruby LSP> Running bundle install failed. Trying to re-generate the composed bundle from scratch")
287
305
  return setup!
288
306
  end
289
307
 
@@ -330,14 +348,14 @@ module RubyLsp
330
348
  if @rails_app
331
349
  return false if @dependencies.values_at("ruby-lsp", "ruby-lsp-rails", "debug").all?
332
350
 
333
- # If the custom lockfile doesn't include `ruby-lsp`, `ruby-lsp-rails` or `debug`, we need to run bundle install
334
- # before updating
335
- return false if custom_bundle_dependencies.values_at("ruby-lsp", "debug", "ruby-lsp-rails").any?(&:nil?)
351
+ # If the composed lockfile doesn't include `ruby-lsp`, `ruby-lsp-rails` or `debug`, we need to run bundle
352
+ # install before updating
353
+ return false if composed_bundle_dependencies.values_at("ruby-lsp", "debug", "ruby-lsp-rails").any?(&:nil?)
336
354
  else
337
355
  return false if @dependencies.values_at("ruby-lsp", "debug").all?
338
356
 
339
- # If the custom lockfile doesn't include `ruby-lsp` or `debug`, we need to run bundle install before updating
340
- return false if custom_bundle_dependencies.values_at("ruby-lsp", "debug").any?(&:nil?)
357
+ # If the composed lockfile doesn't include `ruby-lsp` or `debug`, we need to run bundle install before updating
358
+ return false if composed_bundle_dependencies.values_at("ruby-lsp", "debug").any?(&:nil?)
341
359
  end
342
360
 
343
361
  # If the last updated file doesn't exist or was updated more than 4 hours ago, we should update
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-lsp
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.21.3
4
+ version: 0.22.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Shopify
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-11-05 00:00:00.000000000 Z
11
+ date: 2024-11-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: language_server-protocol
@@ -217,7 +217,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
217
217
  - !ruby/object:Gem::Version
218
218
  version: '0'
219
219
  requirements: []
220
- rubygems_version: 3.5.22
220
+ rubygems_version: 3.5.23
221
221
  signing_key:
222
222
  specification_version: 4
223
223
  summary: An opinionated language server for Ruby