Skip to content

Commit ec385ca

Browse files
committed
Setting port=0 will use a random port and pass that to either env or runtimeArgs.
1 parent c18654d commit ec385ca

File tree

4 files changed

+88
-17
lines changed

4 files changed

+88
-17
lines changed

package.json

Lines changed: 58 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@
202202
"port": {
203203
"type": "number",
204204
"description": "Port on which to listen for Xdebug",
205-
"default": 9000
205+
"default": 9003
206206
},
207207
"serverSourceRoot": {
208208
"type": "string",
@@ -261,15 +261,70 @@
261261
"name": "Listen for Xdebug",
262262
"type": "php",
263263
"request": "launch",
264-
"port": 9000
264+
"port": 9003
265265
},
266266
{
267267
"name": "Launch currently open script",
268268
"type": "php",
269269
"request": "launch",
270270
"program": "${file}",
271271
"cwd": "${fileDirname}",
272-
"port": 9000
272+
"port": 0,
273+
"runtimeArgs": [
274+
"-dxdebug.start_with_request=yes"
275+
],
276+
"env": {
277+
"XDEBUG_MODE": "debug,develop",
278+
"XDEBUG_CONFIG": "client_port=${port}"
279+
}
280+
}
281+
],
282+
"configurationSnippets": [
283+
{
284+
"label": "PHP: Listen for Xdebug",
285+
"description": "Listening for Xdebug",
286+
"body": {
287+
"name": "Listen for Xdebug",
288+
"type": "php",
289+
"request": "launch",
290+
"port": 9003
291+
}
292+
},
293+
{
294+
"label": "PHP: Launch currently open script",
295+
"description": "Launch currently open script",
296+
"body": {
297+
"name": "Launch currently open script",
298+
"type": "php",
299+
"request": "launch",
300+
"program": "^\"${1:\\${file\\}}\"",
301+
"cwd": "^\"${2:\\${fileDirname\\}}\"",
302+
"port": 0,
303+
"runtimeArgs": [
304+
"-dxdebug.start_with_request=yes"
305+
],
306+
"env": {
307+
"XDEBUG_MODE": "debug,develop",
308+
"XDEBUG_CONFIG": "^\"client_port=\\${port\\}\""
309+
}
310+
}
311+
},
312+
{
313+
"label": "PHP: Launch currently open script with Xdebug 2",
314+
"description": "Launch currently open script",
315+
"body": {
316+
"name": "Launch currently open script",
317+
"type": "php",
318+
"request": "launch",
319+
"program": "^\"${1:\\${file\\}}\"",
320+
"cwd": "^\"${2:\\${fileDirname\\}}\"",
321+
"port": 0,
322+
"runtimeArgs": [
323+
"-dxdebug.remote_enable=1",
324+
"-dxdebug.remote_autostart=1",
325+
"^\"-dxdebug.remote_port=\\${port\\}\""
326+
]
327+
}
273328
}
274329
]
275330
}

src/phpDebug.ts

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -205,16 +205,18 @@ class PhpDebugSession extends vscode.DebugSession {
205205
}
206206
this._args = args
207207
/** launches the script as CLI */
208-
const launchScript = async () => {
208+
const launchScript = async (port: number) => {
209209
// check if program exists
210210
await new Promise<void>((resolve, reject) =>
211211
fs.access(args.program!, fs.constants.F_OK, err => (err ? reject(err) : resolve()))
212212
)
213-
const runtimeArgs = args.runtimeArgs || []
213+
const runtimeArgs = (args.runtimeArgs || []).map(v => v.replace('${port}', port.toString()))
214214
const runtimeExecutable = args.runtimeExecutable || 'php'
215215
const programArgs = args.args || []
216216
const cwd = args.cwd || process.cwd()
217-
const env = args.env || process.env
217+
const env = Object.fromEntries(
218+
Object.entries(args.env || process.env).map(v => [v[0], v[1]?.replace('${port}', port.toString())])
219+
)
218220
// launch in CLI mode
219221
if (args.externalConsole) {
220222
const script = await Terminal.launchInTerminal(
@@ -252,7 +254,7 @@ class PhpDebugSession extends vscode.DebugSession {
252254
}
253255
/** sets up a TCP server to listen for Xdebug connections */
254256
const createServer = () =>
255-
new Promise<void>((resolve, reject) => {
257+
new Promise<number>((resolve, reject) => {
256258
const server = (this._server = net.createServer())
257259
server.on('connection', async (socket: net.Socket) => {
258260
try {
@@ -316,16 +318,22 @@ class PhpDebugSession extends vscode.DebugSession {
316318
})
317319
server.on('error', (error: Error) => {
318320
this.sendEvent(new vscode.OutputEvent(util.inspect(error) + '\n'))
319-
this.sendErrorResponse(response, <Error>error)
321+
reject(error)
322+
})
323+
server.on('listening', () => {
324+
const port = (server.address() as net.AddressInfo).port
325+
resolve(port)
320326
})
321-
server.listen(args.port || 9000, args.hostname, () => resolve())
327+
const listenPort = args.port === undefined ? 9000 : args.port
328+
server.listen(listenPort, args.hostname)
322329
})
323330
try {
331+
let port = 0
324332
if (!args.noDebug) {
325-
await createServer()
333+
port = await createServer()
326334
}
327335
if (args.program) {
328-
await launchScript()
336+
await launchScript(port)
329337
}
330338
} catch (error) {
331339
this.sendErrorResponse(response, <Error>error)

testproject/.vscode/launch.json

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,30 @@
11
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
25
"version": "0.2.0",
36
"configurations": [
47
{
58
//"debugServer": 4711, // Uncomment for debugging the adapter
69
"name": "Listen for Xdebug",
710
"type": "php",
811
"request": "launch",
9-
"port": 9000,
12+
"port": 9003,
1013
"log": true
1114
},
1215
{
1316
//"debugServer": 4711, // Uncomment for debugging the adapter
14-
"name": "Launch",
15-
"request": "launch",
17+
"name": "Launch currently open script",
1618
"type": "php",
19+
"request": "launch",
1720
"program": "${file}",
18-
"cwd": "${workspaceRoot}",
19-
"externalConsole": false
21+
"cwd": "${fileDirname}",
22+
"port": 0,
23+
"runtimeArgs": ["-dxdebug.start_with_request=yes"],
24+
"env": {
25+
"XDEBUG_MODE": "debug,develop",
26+
"XDEBUG_CONFIG": "client_port=${port}"
27+
}
2028
}
2129
]
2230
}

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"include": ["src/**/*"],
33
"compilerOptions": {
4-
"target": "es6",
4+
"target": "es2019",
55
"module": "commonjs",
66
"rootDir": "src",
77
"outDir": "out",

0 commit comments

Comments
 (0)