Skip to content

Commit a56db1a

Browse files
DanTupbkonyi
authored andcommitted
[dartdev] "dart language-server": Only skip --protocol=lsp if the --protocol flag is passed
... and not just any flag starting with "protocol". I believe the original code used `startsWith()` to try and catch both `--protocol` as its own flag, and `--protocol=` when combined with the value. However, it also caught `--protocol-traffic-log`. `wasParsed()` should handle both of the first cases without the latter. I also unskipped the tests for Windows because the skip was added in 44cee12 (Jan 2021) because the `deleteDirectory()` call failed on Windows (file locking) but f40b06a (Jan 2022) already fixed that. Fixes #52501 Change-Id: I931a3f12dca2683167e999e7fe59bed18159eb73 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/441780 Auto-Submit: Danny Tuppeny <[email protected]> Reviewed-by: Brian Wilkerson <[email protected]> Reviewed-by: Ben Konyi <[email protected]>
1 parent 2bd7217 commit a56db1a

File tree

2 files changed

+55
-25
lines changed

2 files changed

+55
-25
lines changed

pkg/dartdev/lib/src/commands/language_server.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ For more information about the server's capabilities and configuration, see:
5050
const lsp = server.Driver.PROTOCOL_LSP;
5151

5252
var args = argResults!.arguments;
53-
if (!args.any((arg) => arg.startsWith('--$protocol'))) {
53+
if (!argResults!.wasParsed(protocol)) {
5454
args = [...args, '--$protocol=$lsp'];
5555
}
5656
try {

pkg/dartdev/test/commands/language_server_test.dart

Lines changed: 54 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import 'dart:async';
66
import 'dart:convert';
77
import 'dart:io';
88

9+
import 'package:path/path.dart' as path;
910
import 'package:test/test.dart';
1011

1112
import '../utils.dart' as utils;
@@ -17,19 +18,18 @@ void main() {
1718
'language-server',
1819
defineLanguageServerTests,
1920
timeout: utils.longTimeout,
20-
onPlatform: {
21-
'windows': Skip('https://github.com/dart-lang/sdk/issues/44679'),
22-
},
2321
);
2422
}
2523

2624
void defineLanguageServerTests() {
2725
late utils.TestProject project;
2826
Process? process;
2927

30-
Future<void> runWithLsp(List<String> args) async {
28+
setUp(() {
3129
project = utils.project();
30+
});
3231

32+
Future<void> runWithLsp(List<String> args) async {
3333
process = await project.start(args);
3434

3535
// Send an LSP init.
@@ -45,6 +45,13 @@ void defineLanguageServerTests() {
4545
},
4646
});
4747

48+
// If we get stderr output (for example because we passed invalid args)
49+
// then throw rather than waiting forever on stdout content that will never
50+
// arrive.
51+
process!.stderr.transform(utf8.decoder).listen((data) {
52+
throw data.toString();
53+
});
54+
4855
process!.stdin.write('Content-Length: ${message.length}\r\n');
4956
process!.stdin.write('\r\n');
5057
process!.stdin.write(message);
@@ -65,14 +72,44 @@ void defineLanguageServerTests() {
6572
process = null;
6673
}
6774

75+
Future<void> runWithLegacy(List<String> args) async {
76+
process = await project.start(args);
77+
78+
final Stream<String> inStream = process!.stdout
79+
.transform<String>(utf8.decoder)
80+
.transform<String>(const LineSplitter());
81+
82+
final line = await inStream.first;
83+
final json = jsonDecode(line);
84+
85+
expect(json['event'], 'server.connected');
86+
expect(json['params'], isNotNull);
87+
final params = json['params'];
88+
expect(params['version'], isNotEmpty);
89+
expect(params['pid'], isNot(0));
90+
91+
process!.kill();
92+
process = null;
93+
}
94+
6895
test('protocol default', () async {
6996
return runWithLsp(['language-server']);
7097
});
7198

72-
test('protocol lsp', () async {
99+
test('protocol default with --protocol-log-file', () async {
100+
// https://github.com/dart-lang/sdk/issues/52501
101+
final logFile = path.join(project.dir.path, 'log.txt');
102+
return runWithLsp(['language-server', '--protocol-traffic-log=$logFile']);
103+
});
104+
105+
test('--protocol=lsp', () async {
73106
return runWithLsp(['language-server', '--protocol=lsp']);
74107
});
75108

109+
test('--protocol lsp', () async {
110+
return runWithLsp(['language-server', '--protocol', 'lsp']);
111+
});
112+
76113
test('--use-aot-snapshot', () async {
77114
return runWithLsp(['language-server', '--use-aot-snapshot']);
78115
});
@@ -97,26 +134,12 @@ void defineLanguageServerTests() {
97134
]);
98135
});
99136

100-
test('protocol analyzer', () async {
101-
project = utils.project();
102-
103-
process = await project.start(['language-server', '--protocol=analyzer']);
104-
105-
final Stream<String> inStream = process!.stdout
106-
.transform<String>(utf8.decoder)
107-
.transform<String>(const LineSplitter());
108-
109-
final line = await inStream.first;
110-
final json = jsonDecode(line);
111-
112-
expect(json['event'], 'server.connected');
113-
expect(json['params'], isNotNull);
114-
final params = json['params'];
115-
expect(params['version'], isNotEmpty);
116-
expect(params['pid'], isNot(0));
137+
test('--protocol=analyzer', () async {
138+
await runWithLegacy(['language-server', '--protocol=analyzer']);
139+
});
117140

118-
process!.kill();
119-
process = null;
141+
test('--protocol analyzer', () async {
142+
await runWithLegacy(['language-server', '--protocol', 'analyzer']);
120143
});
121144
}
122145

@@ -138,6 +161,13 @@ Future<String> _readLspMessage(Stream<List<int>> stream) {
138161
// Check whether the buffer has a complete message.
139162
final bufferString = buffer.toString();
140163

164+
// If the buffer has what looks like the legacy banner, then just fail
165+
// because we will never get an LSP message.
166+
if (bufferString.contains('"event":"server.connected"')) {
167+
completer.completeError(
168+
'Expected LSP message but got legacy message: $bufferString');
169+
}
170+
141171
// To know if we have a complete message, we need to check we have the
142172
// headers, extract the content-length, then check we have that many
143173
// bytes in the body.

0 commit comments

Comments
 (0)