Skip to content

Commit 4ee0783

Browse files
EreMaijalafelixfbecker
authored andcommitted
fix(phpDebug): make connection init more robust (#243)
Move 'started' event after Xdebug settings have been set to make sure that 'run' is not executed before settings are processed and make sure that invalid settings don't prevent init from completing. Add some line feeds to output messages. Send error responses instead of just shutting down so that e.g. EADDRINUSE caused by debugger being already active in another window doesn't result in an invalid state.
1 parent ed1e32e commit 4ee0783

1 file changed

Lines changed: 26 additions & 12 deletions

File tree

src/phpDebug.ts

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ class PhpDebugSession extends vscode.DebugSession {
238238
this.sendEvent(new vscode.TerminatedEvent())
239239
})
240240
script.on('error', (error: Error) => {
241-
this.sendEvent(new vscode.OutputEvent(error.message))
241+
this.sendEvent(new vscode.OutputEvent(error.message + '\n'))
242242
})
243243
this._phpProcess = script
244244
}
@@ -262,7 +262,11 @@ class PhpDebugSession extends vscode.DebugSession {
262262
this.sendEvent(new vscode.OutputEvent('connection ' + connection.id + ' closed\n'))
263263
}
264264
if (error) {
265-
this.sendEvent(new vscode.OutputEvent(error.message))
265+
this.sendEvent(
266+
new vscode.OutputEvent(
267+
'connection ' + connection.id + ': ' + error.message + '\n'
268+
)
269+
)
266270
}
267271
this.sendEvent(new vscode.ThreadEvent('exited', connection.id))
268272
connection.close()
@@ -271,30 +275,40 @@ class PhpDebugSession extends vscode.DebugSession {
271275
}
272276
}
273277
connection.on('warning', (warning: string) => {
274-
this.sendEvent(new vscode.OutputEvent(warning))
278+
this.sendEvent(new vscode.OutputEvent(warning + '\n'))
275279
})
276280
connection.on('error', disposeConnection)
277281
connection.on('close', disposeConnection)
278282
await connection.waitForInitPacket()
279-
this.sendEvent(new vscode.ThreadEvent('started', connection.id))
280283

281284
// override features from launch.json
282-
const xdebugSettings = args.xdebugSettings || {}
283-
await Promise.all(
284-
Object.keys(xdebugSettings).map(setting =>
285-
connection.sendFeatureSetCommand(setting, xdebugSettings[setting])
285+
try {
286+
const xdebugSettings = args.xdebugSettings || {}
287+
await Promise.all(
288+
Object.keys(xdebugSettings).map(setting =>
289+
connection.sendFeatureSetCommand(setting, xdebugSettings[setting])
290+
)
286291
)
287-
)
292+
} catch (error) {
293+
throw new Error(
294+
'Error applying xdebugSettings: ' + (error instanceof Error ? error.message : error)
295+
)
296+
}
297+
298+
this.sendEvent(new vscode.ThreadEvent('started', connection.id))
288299

289300
// request breakpoints from VS Code
290301
await this.sendEvent(new vscode.InitializedEvent())
291302
} catch (error) {
292-
this.sendEvent(new vscode.OutputEvent(error instanceof Error ? error.message : error))
303+
this.sendEvent(
304+
new vscode.OutputEvent((error instanceof Error ? error.message : error) + '\n', 'stderr')
305+
)
306+
this.shutdown()
293307
}
294308
})
295309
server.on('error', (error: Error) => {
296-
this.sendEvent(new vscode.OutputEvent(error.message))
297-
this.shutdown()
310+
this.sendEvent(new vscode.OutputEvent('ERROR: ' + error.message + '\n', 'stderr'))
311+
this.sendErrorResponse(response, <Error>error)
298312
})
299313
server.listen(args.port || 9000, (error: NodeJS.ErrnoException) => (error ? reject(error) : resolve()))
300314
})

0 commit comments

Comments
 (0)