Skip to content

Commit 40406b5

Browse files
committed
(GH-2410) Honor specified tcp port
This commit fixes the logic that determines the port used to connect to the LanguageServer if using the `tcp` protocol. Previously it would always pick a random unused port to start and connect to the language server, now it will use the port specified in the configuration if it is there. Note, the default value for the `puppet.languageserver.port` configuration value was removed in this commit. If it was left, there would always be a value for the port, and the logic would never choose a random port. This way, the default is actually random as the original behavior implies but can be set to determinisitic if the user requires.
1 parent 75552d2 commit 40406b5

File tree

2 files changed

+22
-18
lines changed

2 files changed

+22
-18
lines changed

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,6 @@
300300
},
301301
"puppet.languageserver.port": {
302302
"type": "integer",
303-
"default": 8081,
304303
"description": "The TCP Port of the Puppet Language Server to connect to"
305304
},
306305
"puppet.languageserver.timeout": {

src/connection.ts

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -182,23 +182,28 @@ export class ConnectionManager implements IConnectionManager {
182182

183183
let connMgr: ConnectionManager = this;
184184
if(this.connectionConfiguration.protocol === ProtocolType.TCP){
185-
// Start a server to get a random port
186-
this.logger.debug(logPrefix + 'Creating server process to identify random port');
187-
const server = net
188-
.createServer()
189-
.on('close', () => {
190-
this.logger.debug(logPrefix + 'Server process to identify random port disconnected');
191-
connMgr.startLanguageServerProcess(localServer.command, localServer.args, localServer.options, callback);
192-
})
193-
.on('error', err => {
194-
throw err;
195-
});
196-
197-
// Listen on random port
198-
server.listen(0);
199-
this.logger.debug(logPrefix + 'Selected port for local language server: ' + server.address().port);
200-
connMgr.connectionConfiguration.port = server.address().port;
201-
server.close();
185+
if(this.connectionConfiguration.port){
186+
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();
206+
}
202207
}else{
203208
this.logger.debug(logPrefix + 'STDIO Server process starting');
204209
connMgr.startLanguageServerProcess(localServer.command, localServer.args, localServer.options, callback);

0 commit comments

Comments
 (0)