Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ci: Ability to open a browser when the debug session is created #315

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
35 changes: 35 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,12 @@
"url": "https://github.com/felixfbecker/vscode-php-debug/issues"
},
"dependencies": {
"append-query": "^2.0.1",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This dependency is now unused

"file-url": "^2.0.0",
"iconv-lite": "^0.4.15",
"minimatch": "^3.0.3",
"moment": "^2.17.1",
"opn": "^5.4.0",
"url-relative": "^1.0.0",
"urlencode": "^1.1.0",
"vscode-debugadapter": "^1.11.0",
Expand All @@ -51,6 +53,7 @@
"@types/minimatch": "^3.0.1",
"@types/mocha": "^2.2.44",
"@types/node": "7.0.32",
"@types/opn": "^5.1.0",
"@types/semver": "^5.4.0",
"@types/xmldom": "^0.1.29",
"chai": "^4.0.2",
Expand Down Expand Up @@ -180,6 +183,10 @@
"description": "Port on which to listen for XDebug",
"default": 9000
},
"openUrl": {
"type": "string",
"description": "Allows you to open the browser with xdebug start query string"
},
"serverSourceRoot": {
"type": "string",
"description": "Deprecated: The source root when debugging a remote host",
Expand Down
30 changes: 30 additions & 0 deletions src/phpDebug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import * as fs from 'fs'
import { Terminal } from './terminal'
import { isSameUri, convertClientPathToDebugger, convertDebuggerPathToClient } from './paths'
import minimatch = require('minimatch')
import opn = require('opn')

if (process.env['VSCODE_NLS_CONFIG']) {
try {
Expand Down Expand Up @@ -67,6 +68,9 @@ interface LaunchRequestArguments extends VSCodeDebugProtocol.LaunchRequestArgume
/** XDebug configuration */
xdebugSettings?: { [featureName: string]: string | number }

/** Ability to open browser */
openUrl?: string

// CLI options

/** If set, launches the specified PHP script in CLI mode */
Expand Down Expand Up @@ -311,6 +315,7 @@ class PhpDebugSession extends vscode.DebugSession {
this.sendErrorResponse(response, <Error>error)
})
server.listen(args.port || 9000, (error: NodeJS.ErrnoException) => (error ? reject(error) : resolve()))
this._openUrl(true)
})
try {
if (!args.noDebug) {
Expand All @@ -325,6 +330,30 @@ class PhpDebugSession extends vscode.DebugSession {
}
this.sendResponse(response)
}
/**
* Opens the urls specified in configuration, either with start or stop query string
* @param {boolean} start
*/
private _openUrl(start: boolean) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
private _openUrl(start: boolean) {
private async _openUrl(start: boolean): Promise<void> {

if (!this._args.openUrl) {
return
}
const myurl = new url.URL(this._args.openUrl)
if (start) {
myurl.searchParams.append('XDEBUG_SESSION_START', 'vscode')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be set, not append, since append would duplicate the parameter if it’s already set

Copy link
Author

@letynsoft letynsoft Dec 22, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would rather keep the append, as a the stop command would be confusing for the xdebug anyway and this may be visible straight away

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand what this has to do with the stop command - what I mean is that append would potentially add XDEBUG_SESSION_START twice or more times, like ?XDEBUG_SESSION_START=vscode&XDEBUG_SESSION_START=vscode, while set() would just override it so XDEBUG_SESSION_START appears exactly once

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but what would happen when the stop command should be sent? XDEBUG_SESSION_START=vscode&XDEBUG_SESSION_STOP=vscode (with set or append) as the param is diferrent, so it would be easier to spot such errors.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.set('XDEBUG_SESSION_START', ...) won't remove the XDEBUG_SESSION_STOP param.

} else {
myurl.searchParams.append('XDEBUG_SESSION_STOP', 'vscode')
}

const proc = opn(myurl.toString())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const proc = opn(myurl.toString())
const proc = await opn(myurl.toString())

proc.then(child => {
child.stderr.on('data', (chunk: string) => {
this.sendEvent(new vscode.OutputEvent('openUrl: ' + chunk + '\n'))
})
}).catch((reason: any) => {
this.sendEvent(new vscode.OutputEvent('openUrl-error: ' + util.inspect(reason) + '\n'))
})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of loggint the error here, it’s better to make this an async function and have the callers await it, so an error is reported as a response to the request

Copy link
Author

@letynsoft letynsoft Dec 22, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure what exactly you mean here... so i should remove the .catch and just return the result?
Also should the then be removed too?
The start call would have to be moved to 'connection' callback, but that should be fine

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See my two suggestions above. In addition you'd remove the .catch() and "unwrap" the .then().

}

/**
* Checks the status of a StatusResponse and notifies VS Code accordingly
Expand Down Expand Up @@ -992,6 +1021,7 @@ class PhpDebugSession extends vscode.DebugSession {
this._waitingConnections.delete(connection)
})
)
this._openUrl(false)
// If listening for connections, close server
if (this._server) {
await new Promise(resolve => this._server.close(resolve))
Expand Down
1 change: 1 addition & 0 deletions testproject/.vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"request": "launch",
"port": 9000,
"log": true
//"openUrl": "http://localhost/"
},
{
//"debugServer": 4711, // Uncomment for debugging the adapter
Expand Down