Repository navigation
Add one-click VS Code Xdebug setup #796
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
base: trunk
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| 'use strict'; | ||
|
|
||
| const path = require('node:path'); | ||
| const { isActionableSite } = require('./site-registry'); | ||
|
|
||
| const NOT_THERE = new Set(['ENOENT', 'ENOTDIR']); | ||
| const MAX_CONFIG_BYTES = 16 * 1024; | ||
|
|
||
| function inside(root, target) { | ||
| const relative = path.relative(root, target); | ||
| return relative !== '..' && !relative.startsWith(`..${path.sep}`) && !path.isAbsolute(relative); | ||
| } | ||
|
|
||
| function refusal(reason) { | ||
| return { ok: false, reason }; | ||
| } | ||
|
|
||
| async function existingLaunchFile(file, root, { realPath, stat }) { | ||
| let realFile; | ||
| try { | ||
| realFile = await realPath(file); | ||
| } catch (error) { | ||
| const code = typeof error?.code === 'string' ? error.code : 'UNKNOWN'; | ||
| return NOT_THERE.has(code) ? { ok: true, exists: false, created: false } : refusal('file-unavailable'); | ||
| } | ||
| if (!inside(root, realFile)) return refusal('outside-site'); | ||
| try { | ||
| const info = await stat(file); | ||
| return info.isFile() ? { ok: true, exists: true, created: false } : refusal('invalid-file'); | ||
| } catch (error) { | ||
| const code = typeof error?.code === 'string' ? error.code : 'UNKNOWN'; | ||
| return NOT_THERE.has(code) ? { ok: true, exists: false, created: false } : refusal('file-unavailable'); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Check for, or create without replacing, the Xdebug launch file for a site. | ||
| * | ||
| * @param {string} sitePath | ||
| * @param {Object} deps | ||
| * @param {Array} deps.sites | ||
| * @param {Array} deps.pending | ||
| * @param {Function} deps.realPath | ||
| * @param {Function} deps.stat | ||
| * @param {Function} deps.mkdir | ||
| * @param {Function} deps.writeFile | ||
| * @param {string} [deps.config] | ||
| * @param {boolean} [deps.create] | ||
| * @return {Promise<{ok: boolean, exists?: boolean, created?: boolean, reason?: string}>} | ||
| */ | ||
| async function xdebugLaunchConfigFile(sitePath, { sites, pending, realPath, stat, mkdir, writeFile, config, create = false } = {}) { | ||
| if (!isActionableSite(sitePath, { sites, pending })) return refusal('unregistered-site'); | ||
| if (create) { | ||
| if (typeof config !== 'string' || Buffer.byteLength(config, 'utf8') > MAX_CONFIG_BYTES) return refusal('invalid-config'); | ||
| try { | ||
| JSON.parse(config); | ||
| } catch { | ||
| return refusal('invalid-config'); | ||
| } | ||
| } | ||
|
|
||
| const rootPath = path.resolve(sitePath); | ||
| let root; | ||
| try { | ||
| root = await realPath(rootPath); | ||
| } catch { | ||
| return refusal('file-unavailable'); | ||
| } | ||
|
|
||
| const directory = path.join(root, '.vscode'); | ||
| let directoryInfo; | ||
| try { | ||
| directoryInfo = await stat(directory); | ||
| } catch (error) { | ||
| const code = typeof error?.code === 'string' ? error.code : 'UNKNOWN'; | ||
| if (!NOT_THERE.has(code)) return refusal('file-unavailable'); | ||
| if (!create) return { ok: true, exists: false, created: false }; | ||
| try { | ||
| await mkdir(directory, { recursive: true }); | ||
| directoryInfo = await stat(directory); | ||
| } catch { | ||
| return refusal('file-unavailable'); | ||
| } | ||
| } | ||
| if (!directoryInfo.isDirectory()) return refusal('invalid-directory'); | ||
|
|
||
| let realDirectory; | ||
| try { | ||
| realDirectory = await realPath(directory); | ||
| } catch { | ||
| return refusal('file-unavailable'); | ||
| } | ||
| if (!inside(root, realDirectory)) return refusal('outside-site'); | ||
|
|
||
| const file = path.join(directory, 'launch.json'); | ||
| const current = await existingLaunchFile(file, root, { realPath, stat }); | ||
| if (!current.ok || current.exists || !create) return current; | ||
|
|
||
| try { | ||
| await writeFile(file, config, { encoding: 'utf8', flag: 'wx' }); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Architecture — 🔵 low — [fix here]: Remove a launch file that a failed write leaves behind. When Remove the file when this call's exclusive create made it and the write then fails. Do not remove a file when the failure happens before this call creates it, for example with
|
||
| return { ok: true, exists: true, created: true }; | ||
| } catch (error) { | ||
| if (error?.code === 'EEXIST') return existingLaunchFile(file, root, { realPath, stat }); | ||
| return refusal('file-unavailable'); | ||
| } | ||
| } | ||
|
|
||
| module.exports = { xdebugLaunchConfigFile }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,6 +22,7 @@ | |
| */ | ||
|
|
||
| const path = require( 'node:path' ); | ||
| const fs = require( 'node:fs' ); | ||
| const { test, expect } = require( '../helpers/app.cjs' ); | ||
| const ui = require( '../helpers/ui.cjs' ); | ||
| const { makeSite } = require( '../helpers/git-site.cjs' ); | ||
|
|
@@ -78,6 +79,7 @@ async function setXdebug( page, on ) { | |
| } | ||
|
|
||
| test( 'the Debugging row says when Xdebug is on, and a server started with it offers the editor setup', async ( { session } ) => { | ||
| test.setTimeout( 90_000 ); | ||
| const site = await makeSite( session ); | ||
| const { app, page } = await session.start( site.settings ); | ||
| const followSetting = await stubServer( app, session ); | ||
|
|
@@ -127,6 +129,39 @@ test( 'the Debugging row says when Xdebug is on, and a server started with it of | |
| // INVARIANT — the launch.json listens on this machine only. | ||
| expect( launch.configurations[ 0 ].hostname ).toBe( '127.0.0.1' ); | ||
|
|
||
| // INVARIANT — the extension link opens the requested Marketplace page, and | ||
| // a missing launch.json can be created and opened in the detected editor. | ||
| await app.evaluate( ( { ipcMain, shell } ) => { | ||
| global.e2eOpened = []; | ||
| global.e2eEditorOpens = []; | ||
| shell.openExternal = async ( url ) => { global.e2eOpened.push( url ); }; | ||
| ipcMain.removeHandler( 'editor:open' ); | ||
| ipcMain.handle( 'editor:open', ( _event, sitePath, editorPath, relPath ) => { | ||
| global.e2eEditorOpens.push( { sitePath, editorPath, relPath } ); | ||
| return { ok: true }; | ||
| } ); | ||
| } ); | ||
| const extension = vscode.getByRole( 'link', { name: /PHP Debug/ } ); | ||
| await expect( extension ).toHaveAttribute( 'href', 'https://marketplace.visualstudio.com/items?itemName=xdebug.php-debug' ); | ||
| await extension.click(); | ||
| await expect.poll( () => app.evaluate( () => global.e2eOpened ) ).toEqual( [ 'https://marketplace.visualstudio.com/items?itemName=xdebug.php-debug' ] ); | ||
| const addLaunch = vscode.getByRole( 'button', { name: 'Add launch.json and open in VS Code', exact: true } ); | ||
| await expect( addLaunch ).toBeEnabled(); | ||
| await addLaunch.click(); | ||
| const launchFile = path.join( site.dir, '.vscode', 'launch.json' ); | ||
| expect( fs.readFileSync( launchFile, 'utf8' ) ).toBe( await vscode.locator( '.xdebug-launch-config' ).textContent() ); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Read The test reads the file at line 152 immediately after clicking "Add launch.json and open in VS Code". The click does not wait for the renderer's create call, so the file may not exist yet, and Suggested fix
|
||
| await expect.poll( () => app.evaluate( () => global.e2eEditorOpens ) ).toEqual( [ { sitePath: site.dir, editorPath: '/e2e/vscode', relPath: '.vscode/launch.json' } ] ); | ||
|
|
||
| // INVARIANT — reopening an existing file offers the edit action and does | ||
| // not replace the contributor's configuration. | ||
| const contributorConfig = '{"myConfiguration":true}\n'; | ||
| fs.writeFileSync( launchFile, contributorConfig ); | ||
| const editLaunch = vscode.getByRole( 'button', { name: 'Open launch.json in VS Code to edit', exact: true } ); | ||
| await expect( editLaunch ).toBeEnabled(); | ||
| await editLaunch.click(); | ||
| await expect.poll( () => app.evaluate( () => global.e2eEditorOpens ) ).toHaveLength( 2 ); | ||
| expect( fs.readFileSync( launchFile, 'utf8' ) ).toBe( contributorConfig ); | ||
|
|
||
| // INVARIANT — copying follows the design system's pattern: the button | ||
| // keeps its name, shows a check for two seconds after the copy, an error | ||
| // icon for a copy that failed, and the result is said. A second copy | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Architecture — 🔵 low — [fix here]: Apply the legacy-site gate before creation.
For a registered legacy site,
create=truereachesmkdirandwriteFilewithout callinglegacySiteBlock. The helper checks registration but does not check legacy status.Gate creation before filesystem writes. Keep read-only inspection available and preserve creation for supported sites. Add a refusal test.
As per coding guidelines, “Sites the old engine cloned are not written at all” and “a new write handler without that gate is a finding.”
Source: Coding guidelines