Skip to content

Commit 3ae87da

Browse files
committed
(GH-274) Remove random port detection
This commit removes the code that detects and uses a random port for the LanguageServer to run on. It instead parses the STDOUT message from the LanguageServer for the port it is running on and uses that to connect. If the user sets a port in the config, that is used instead of reading the port from the LanguageServer.
1 parent d4bbd72 commit 3ae87da

File tree

2 files changed

+12
-22
lines changed

2 files changed

+12
-22
lines changed

src/configuration.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,9 @@ export class ConnectionConfiguration implements IConnectionConfiguration {
161161
} else {
162162
args.push('--ip=' + this.host);
163163
}
164-
args.push('--port=' + this.port);
164+
if (this.port !== 0) {
165+
args.push('--port=' + this.port);
166+
}
165167
break;
166168
default:
167169
break;

src/connection.ts

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,14 @@ export class ConnectionManager implements IConnectionManager {
127127
this.logger.debug('OUTPUT: ' + data.toString());
128128

129129
// If the language client isn't already running and it's sent the trigger text, start up a client
130-
if (this.languageServerClient === undefined && data.toString().match('LANGUAGE SERVER RUNNING') !== null) {
131-
this.languageServerClient = this.createLanguageClient();
130+
if (this.languageServerClient === undefined && /LANGUAGE SERVER RUNNING/.test(data.toString())) {
131+
if(this.connectionConfiguration.port){
132+
this.languageServerClient = this.createLanguageClient();
133+
}else{
134+
var p = data.toString().match(/LANGUAGE SERVER RUNNING.*:(\d+)/);
135+
this.connectionConfiguration.port = +p[1];
136+
this.languageServerClient = this.createLanguageClient();
137+
}
132138
this.extensionContext.subscriptions.push(this.languageServerClient.start());
133139
}
134140
});
@@ -184,26 +190,8 @@ export class ConnectionManager implements IConnectionManager {
184190
if(this.connectionConfiguration.protocol === ProtocolType.TCP){
185191
if(this.connectionConfiguration.port){
186192
this.logger.debug(logPrefix + 'Selected port for local language server: ' + this.connectionConfiguration.port);
187-
connMgr.startLanguageServerProcess(localServer.command, localServer.args, localServer.options, callback);
188-
}else{
189-
// Start a server to get a random port
190-
this.logger.debug(logPrefix + 'Creating server process to identify random port');
191-
const server = net
192-
.createServer()
193-
.on('close', () => {
194-
this.logger.debug(logPrefix + 'Server process to identify random port disconnected');
195-
connMgr.startLanguageServerProcess(localServer.command, localServer.args, localServer.options, callback);
196-
})
197-
.on('error', err => {
198-
throw err;
199-
});
200-
201-
// Listen on random port
202-
server.listen(0);
203-
this.logger.debug(logPrefix + 'Selected port for local language server: ' + server.address().port);
204-
connMgr.connectionConfiguration.port = server.address().port;
205-
server.close();
206193
}
194+
connMgr.startLanguageServerProcess(localServer.command, localServer.args, localServer.options, callback);
207195
}else{
208196
this.logger.debug(logPrefix + 'STDIO Server process starting');
209197
connMgr.startLanguageServerProcess(localServer.command, localServer.args, localServer.options, callback);

0 commit comments

Comments
 (0)