@@ -263,36 +263,67 @@ export class Remote {
263
263
}
264
264
const binPath = agent . operating_system === "windows" ? "code-server" : "$HOME/.vscode-remote/bin/code-server"
265
265
266
- const remotePort = await new Promise < number > ( ( resolve , reject ) => {
267
- const script =
268
- binPath +
269
- " serve-local --start-server --port 0 --without-connection-token --commit-id " +
270
- this . vscodeCommit +
271
- " --accept-server-license-terms"
272
- this . ipc
273
- ?. execute ( shell , script , ( data ) => {
274
- const lines = data . split ( "\n" )
275
- lines . forEach ( ( line ) => {
276
- this . output . appendLine ( line )
266
+ let running : {
267
+ commit : string
268
+ process_id : number
269
+ } [ ] = [ ]
270
+ await this . ipc . execute ( shell , `${ binPath } ps` , ( data ) => {
271
+ try {
272
+ running = JSON . parse ( data )
273
+ } catch {
274
+ // We can ignore this, it's probably blank!
275
+ }
276
+ } )
277
+ // Store the running port for the current commit in a file for reconnection!
278
+ const portFilePath = `/tmp/.vscode-remote-${ this . vscodeCommit } -port`
279
+ let remotePort = 0
280
+ if ( running . filter ( ( instance ) => instance . commit === this . vscodeCommit ) ) {
281
+ await this . ipc . execute ( shell , `cat ${ portFilePath } ` , ( data ) => {
282
+ if ( data . trim ( ) ) {
283
+ remotePort = Number . parseInt ( data . trim ( ) )
284
+ }
285
+ } )
277
286
278
- if ( ! line . startsWith ( "Server bound to" ) ) {
279
- return
280
- }
281
- const parts = line . split ( " " ) . filter ( ( part ) => part . startsWith ( "127.0.0.1:" ) )
282
- if ( parts . length === 0 ) {
283
- return reject ( "No port found in output: " + line )
284
- }
285
- const port = parts [ 0 ] . split ( ":" ) . pop ( )
286
- if ( ! port ) {
287
- return reject ( "No port found in parts: " + parts . join ( "," ) )
288
- }
289
- resolve ( Number . parseInt ( port ) )
287
+ this . output . appendLine ( "Found existing server running on port: " + remotePort )
288
+ }
289
+
290
+ if ( ! remotePort ) {
291
+ remotePort = await new Promise < number > ( ( resolve , reject ) => {
292
+ const script =
293
+ binPath +
294
+ " serve-local --start-server --port 0 --without-connection-token --commit-id " +
295
+ this . vscodeCommit +
296
+ " --accept-server-license-terms"
297
+ this . ipc
298
+ ?. execute ( shell , script , ( data ) => {
299
+ const lines = data . split ( "\n" )
300
+ lines . forEach ( ( line ) => {
301
+ this . output . appendLine ( line )
302
+ if ( ! line . startsWith ( "Server bound to" ) ) {
303
+ return
304
+ }
305
+ const parts = line . split ( " " ) . filter ( ( part ) => part . startsWith ( "127.0.0.1:" ) )
306
+ if ( parts . length === 0 ) {
307
+ return reject ( "No port found in output: " + line )
308
+ }
309
+ const port = parts [ 0 ] . split ( ":" ) . pop ( )
310
+ if ( ! port ) {
311
+ return reject ( "No port found in parts: " + parts . join ( "," ) )
312
+ }
313
+ resolve ( Number . parseInt ( port ) )
314
+ } )
290
315
} )
291
- } )
292
- . then ( ( exitCode ) => {
293
- reject ( "Exited with: " + exitCode )
294
- } )
295
- } )
316
+ . then ( ( exitCode ) => {
317
+ reject ( "Exited with: " + exitCode )
318
+ } )
319
+ } )
320
+
321
+ await this . ipc . execute (
322
+ shell ,
323
+ `echo ${ remotePort } > /tmp/.vscode-remote-${ this . vscodeCommit } -port` ,
324
+ ( ) => undefined ,
325
+ )
326
+ }
296
327
297
328
const forwarded = await this . ipc . portForward ( remotePort )
298
329
vscode . commands . executeCommand ( "setContext" , "forwardedPortsViewEnabled" , true )
0 commit comments