Skip to content

Commit 20c3979

Browse files
authored
Check if lockfile is valid before composing bundle (#3077)
### 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.
1 parent fab50c8 commit 20c3979

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

lib/ruby_lsp/server.rb

+7
Original file line numberDiff line numberDiff line change
@@ -1292,6 +1292,13 @@ def compose_bundle(message)
12921292
command = "#{Gem.ruby} #{File.expand_path("../../exe/ruby-lsp-launcher", __dir__)} #{@global_state.workspace_uri}"
12931293
id = message[:id]
12941294

1295+
begin
1296+
Bundler::LockfileParser.new(Bundler.default_lockfile.read)
1297+
rescue Bundler::LockfileError => e
1298+
send_message(Error.new(id: id, code: BUNDLE_COMPOSE_FAILED_CODE, message: e.message))
1299+
return
1300+
end
1301+
12951302
# We compose the bundle in a thread so that the LSP continues to work while we're checking for its validity. Once
12961303
# we return the response back to the editor, then the restart is triggered
12971304
Thread.new do

test/server_test.rb

+49
Original file line numberDiff line numberDiff line change
@@ -1056,6 +1056,55 @@ def test_compose_bundle_creates_file_to_skip_next_compose
10561056
end
10571057
end
10581058

1059+
def test_compose_bundle_detects_syntax_errors_in_lockfile
1060+
Dir.mktmpdir do |dir|
1061+
Dir.chdir(dir) do
1062+
@server.process_message({
1063+
id: 1,
1064+
method: "initialize",
1065+
params: {
1066+
initializationOptions: {},
1067+
capabilities: { general: { positionEncodings: ["utf-8"] } },
1068+
workspaceFolders: [{ uri: URI::Generic.from_path(path: dir).to_s }],
1069+
},
1070+
})
1071+
1072+
File.write(File.join(dir, "Gemfile"), <<~GEMFILE)
1073+
source "https://rubygems.org"
1074+
gem "stringio"
1075+
GEMFILE
1076+
1077+
# Write a lockfile that has a git conflict marker
1078+
lockfile_contents = <<~LOCKFILE
1079+
GEM
1080+
remote: https://rubygems.org/
1081+
specs:
1082+
<<<<<<< HEAD
1083+
stringio (3.1.0)
1084+
>>>>>> 12345
1085+
1086+
PLATFORMS
1087+
arm64-darwin-23
1088+
ruby
1089+
1090+
DEPENDENCIES
1091+
stringio
1092+
1093+
BUNDLED WITH
1094+
2.5.7
1095+
LOCKFILE
1096+
File.write(File.join(dir, "Gemfile.lock"), lockfile_contents)
1097+
1098+
Bundler.with_unbundled_env do
1099+
@server.process_message({ id: 2, method: "rubyLsp/composeBundle" })
1100+
end
1101+
1102+
error = find_message(RubyLsp::Error)
1103+
assert_match("Your Gemfile.lock contains merge conflicts.", error.message)
1104+
end
1105+
end
1106+
end
1107+
10591108
private
10601109

10611110
def with_uninstalled_rubocop(&block)

0 commit comments

Comments
 (0)