Skip to content

Commit

Permalink
Check if lockfile is valid before composing bundle (#3077)
Browse files Browse the repository at this point in the history
### Motivation

After #3066 moves forward, we can start getting smarter about our checks of when it's valid to restart. For example, we don't need to go all the way to composing the bundle, if we already know the lockfile's syntax is invalid.

### Implementation

Started parsing the lockfile before composing the bundle, so that we can more quickly reject a restart request if there's an issue.

### Automated Tests

Added a test.
  • Loading branch information
vinistock authored Jan 23, 2025
1 parent fab50c8 commit 20c3979
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/ruby_lsp/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1292,6 +1292,13 @@ def compose_bundle(message)
command = "#{Gem.ruby} #{File.expand_path("../../exe/ruby-lsp-launcher", __dir__)} #{@global_state.workspace_uri}"
id = message[:id]

begin
Bundler::LockfileParser.new(Bundler.default_lockfile.read)
rescue Bundler::LockfileError => e
send_message(Error.new(id: id, code: BUNDLE_COMPOSE_FAILED_CODE, message: e.message))
return
end

# We compose the bundle in a thread so that the LSP continues to work while we're checking for its validity. Once
# we return the response back to the editor, then the restart is triggered
Thread.new do
Expand Down
49 changes: 49 additions & 0 deletions test/server_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1056,6 +1056,55 @@ def test_compose_bundle_creates_file_to_skip_next_compose
end
end

def test_compose_bundle_detects_syntax_errors_in_lockfile
Dir.mktmpdir do |dir|
Dir.chdir(dir) do
@server.process_message({
id: 1,
method: "initialize",
params: {
initializationOptions: {},
capabilities: { general: { positionEncodings: ["utf-8"] } },
workspaceFolders: [{ uri: URI::Generic.from_path(path: dir).to_s }],
},
})

File.write(File.join(dir, "Gemfile"), <<~GEMFILE)
source "https://rubygems.org"
gem "stringio"
GEMFILE

# Write a lockfile that has a git conflict marker
lockfile_contents = <<~LOCKFILE
GEM
remote: https://rubygems.org/
specs:
<<<<<<< HEAD
stringio (3.1.0)
>>>>>> 12345
PLATFORMS
arm64-darwin-23
ruby
DEPENDENCIES
stringio
BUNDLED WITH
2.5.7
LOCKFILE
File.write(File.join(dir, "Gemfile.lock"), lockfile_contents)

Bundler.with_unbundled_env do
@server.process_message({ id: 2, method: "rubyLsp/composeBundle" })
end

error = find_message(RubyLsp::Error)
assert_match("Your Gemfile.lock contains merge conflicts.", error.message)
end
end
end

private

def with_uninstalled_rubocop(&block)
Expand Down

0 comments on commit 20c3979

Please sign in to comment.