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

Check if lockfile is valid before composing bundle #3077

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
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
Loading