From 984fca5a8a7de726dc8d3ad232f45e1ae395829c Mon Sep 17 00:00:00 2001 From: Da-Viper <57949090+Da-Viper@users.noreply.github.com> Date: Sun, 1 Sep 2024 16:41:18 +0100 Subject: [PATCH] [lldb-dap] show dialog when executable is not found (#104711) --- .../lldb-dap/src-ts/debug-adapter-factory.ts | 43 +++++++++++++++++ lldb/tools/lldb-dap/src-ts/extension.ts | 48 ++++++++++++++----- 2 files changed, 80 insertions(+), 11 deletions(-) diff --git a/lldb/tools/lldb-dap/src-ts/debug-adapter-factory.ts b/lldb/tools/lldb-dap/src-ts/debug-adapter-factory.ts index 01c671f41ff782..2be21bfdf0dd69 100644 --- a/lldb/tools/lldb-dap/src-ts/debug-adapter-factory.ts +++ b/lldb/tools/lldb-dap/src-ts/debug-adapter-factory.ts @@ -14,10 +14,53 @@ export class LLDBDapDescriptorFactory this.lldbDapOptions = lldbDapOptions; } + static async isValidDebugAdapterPath( + pathUri: vscode.Uri, + ): Promise { + try { + const fileStats = await vscode.workspace.fs.stat(pathUri); + if (!(fileStats.type & vscode.FileType.File)) { + return false; + } + } catch (err) { + return false; + } + return true; + } + async createDebugAdapterDescriptor( session: vscode.DebugSession, executable: vscode.DebugAdapterExecutable | undefined, ): Promise { + const config = vscode.workspace.getConfiguration( + "lldb-dap", + session.workspaceFolder, + ); + const customPath = config.get("executable-path"); + const path: string = customPath || executable!!.command; + + const fileUri = vscode.Uri.file(path); + if (!(await LLDBDapDescriptorFactory.isValidDebugAdapterPath(fileUri))) { + LLDBDapDescriptorFactory.showLLDBDapNotFoundMessage(fileUri.path); + } return this.lldbDapOptions.createDapExecutableCommand(session, executable); } + + /** + * Shows a message box when the debug adapter's path is not found + */ + static async showLLDBDapNotFoundMessage(path: string) { + const openSettingsAction = "Open Settings"; + const callbackValue = await vscode.window.showErrorMessage( + `Debug adapter path: ${path} is not a valid file`, + openSettingsAction, + ); + + if (openSettingsAction === callbackValue) { + vscode.commands.executeCommand( + "workbench.action.openSettings", + "lldb-dap.executable-path", + ); + } + } } diff --git a/lldb/tools/lldb-dap/src-ts/extension.ts b/lldb/tools/lldb-dap/src-ts/extension.ts index 7df09f7a29dad7..fdc4f47b238b5a 100644 --- a/lldb/tools/lldb-dap/src-ts/extension.ts +++ b/lldb/tools/lldb-dap/src-ts/extension.ts @@ -14,26 +14,32 @@ function createDefaultLLDBDapOptions(): LLDBDapOptions { session: vscode.DebugSession, packageJSONExecutable: vscode.DebugAdapterExecutable | undefined, ): Promise { - const config = vscode.workspace - .getConfiguration("lldb-dap", session.workspaceFolder); + const config = vscode.workspace.getConfiguration( + "lldb-dap", + session.workspaceFolder, + ); const path = config.get("executable-path"); const log_path = config.get("log-path"); - let env : { [key: string]: string } = {}; + let env: { [key: string]: string } = {}; if (log_path) { env["LLDBDAP_LOG"] = log_path; } if (path) { - return new vscode.DebugAdapterExecutable(path, [], {env}); + return new vscode.DebugAdapterExecutable(path, [], { env }); } else if (packageJSONExecutable) { - return new vscode.DebugAdapterExecutable(packageJSONExecutable.command, packageJSONExecutable.args, { - ...packageJSONExecutable.options, - env: { - ...packageJSONExecutable.options?.env, - ...env - } - }); + return new vscode.DebugAdapterExecutable( + packageJSONExecutable.command, + packageJSONExecutable.args, + { + ...packageJSONExecutable.options, + env: { + ...packageJSONExecutable.options?.env, + ...env, + }, + }, + ); } else { return undefined; } @@ -58,6 +64,26 @@ export class LLDBDapExtension extends DisposableContext { new LLDBDapDescriptorFactory(this.lldbDapOptions), ), ); + + this.pushSubscription( + vscode.workspace.onDidChangeConfiguration(async (event) => { + if (event.affectsConfiguration("lldb-dap.executable-path")) { + const dapPath = vscode.workspace + .getConfiguration("lldb-dap") + .get("executable-path"); + + if (dapPath) { + const fileUri = vscode.Uri.file(dapPath); + if ( + await LLDBDapDescriptorFactory.isValidDebugAdapterPath(fileUri) + ) { + return; + } + } + LLDBDapDescriptorFactory.showLLDBDapNotFoundMessage(dapPath || ""); + } + }), + ); } }