Skip to content

Hide underlying debug adapter from the user #1024

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

Merged
merged 13 commits into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions assets/test/.vscode/launch.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"configurations": [
{
"type": "lldb",
"type": "swift-lldb",
"request": "launch",
"sourceLanguages": [
"swift"
Expand All @@ -13,7 +13,7 @@
"preLaunchTask": "swift: Build Debug PackageExe (defaultPackage)"
},
{
"type": "lldb",
"type": "swift-lldb",
"request": "launch",
"sourceLanguages": [
"swift"
Expand Down
10 changes: 2 additions & 8 deletions assets/test/defaultPackage/.vscode/launch.json
Original file line number Diff line number Diff line change
@@ -1,23 +1,17 @@
{
"configurations": [
{
"type": "lldb",
"type": "swift-lldb",
"request": "launch",
"sourceLanguages": [
"swift"
],
"name": "Debug package1",
"program": "${workspaceFolder:defaultPackage}/.build/debug/package1",
"args": [],
"cwd": "${workspaceFolder:defaultPackage}",
"preLaunchTask": "swift: Build Debug package1"
},
{
"type": "lldb",
"type": "swift-lldb",
"request": "launch",
"sourceLanguages": [
"swift"
],
"name": "Release package1",
"program": "${workspaceFolder:defaultPackage}/.build/release/package1",
"args": [],
Expand Down
20 changes: 4 additions & 16 deletions assets/test/diagnostics/.vscode/launch.json
Original file line number Diff line number Diff line change
@@ -1,47 +1,35 @@
{
"configurations": [
{
"type": "lldb",
"type": "swift-lldb",
"request": "launch",
"sourceLanguages": [
"swift"
],
"args": [],
"cwd": "${workspaceFolder:diagnostics}",
"name": "Debug diagnostics",
"program": "${workspaceFolder:diagnostics}/.build/debug/diagnostics",
"preLaunchTask": "swift: Build Debug diagnostics"
},
{
"type": "lldb",
"type": "swift-lldb",
"request": "launch",
"sourceLanguages": [
"swift"
],
"args": [],
"cwd": "${workspaceFolder:diagnostics}",
"name": "Release diagnostics",
"program": "${workspaceFolder:diagnostics}/.build/release/diagnostics",
"preLaunchTask": "swift: Build Release diagnostics"
},
{
"type": "lldb",
"type": "swift-lldb",
"request": "launch",
"sourceLanguages": [
"swift"
],
"args": [],
"cwd": "${workspaceFolder:diagnostics}",
"name": "Debug diagnostics",
"program": "${workspaceFolder:diagnostics}/.build/debug/diagnostics",
"preLaunchTask": "swift: Build Debug diagnostics"
},
{
"type": "lldb",
"type": "swift-lldb",
"request": "launch",
"sourceLanguages": [
"swift"
],
"args": [],
"cwd": "${workspaceFolder:diagnostics}",
"name": "Release diagnostics",
Expand Down
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,7 @@
"swift.debugger.useDebugAdapterFromToolchain": {
"type": "boolean",
"default": false,
"markdownDescription": "Use lldb debug adapter packaged with Swift toolchain as your debug adapter. This is currently only available on Windows or platforms using Swift 6",
"markdownDescription": "Use the LLDB debug adapter packaged with the Swift toolchain as your debug adapter. Note: this is only available starting with Swift 6. The CodeLLDB extension will be used if your Swift toolchain does not contain lldb-dap.",
"order": 1
},
"swift.debugger.path": {
Expand Down Expand Up @@ -1075,12 +1075,12 @@
"array",
"string"
],
"description": "Program arguments.",
"description": "Arguments to provide to the program.",
"default": []
},
"cwd": {
"type": "string",
"description": "Program working directory.",
"description": "The working directory that the program will be launched within.",
"default": "${workspaceRoot}"
},
"env": {
Expand Down Expand Up @@ -1277,9 +1277,9 @@
"format": "prettier --check *.json src test",
"pretest": "npm run compile && find ./assets/test -type d -name '.build' -exec rm -rf {} + && find . -type d -name 'Package.resolved' -exec rm -rf {} + && tsc -p ./",
"test": "vscode-test",
"integration-test": "vscode-test --label integrationTests",
"unit-test": "vscode-test --label unitTests",
"coverage": "npm run compile-tests && vscode-test --coverage",
"integration-test": "npm test -- --label integrationTests",
"unit-test": "npm test -- --label unitTests",
"coverage": "npm test -- --coverage",
"compile-tests": "find ./assets/test -type d -name '.build' -exec rm -rf {} + && npm run compile && npm run esbuild",
"package": "vsce package",
"dev-package": "vsce package --no-update-package-json 1.11.1-dev",
Expand Down
20 changes: 1 addition & 19 deletions src/WorkspaceContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,24 +104,6 @@ export class WorkspaceContext implements vscode.Disposable {
}
});
}
// on change of swift debugger type
if (
event.affectsConfiguration("swift.debugger.useDebugAdapterFromToolchain") ||
event.affectsConfiguration("swift.debugger.path")
) {
if (configuration.debugger.useDebugAdapterFromToolchain) {
if (!(await DebugAdapter.verifyDebugAdapterExists(this))) {
return;
}
}
this.folders.forEach(
async ctx =>
await makeDebugConfigurations(
ctx,
"Launch configurations need to be updated after changing the debug adapter."
)
);
}
});
const backgroundCompilationOnDidSave = BackgroundCompilation.start(this);
const contextKeysUpdate = this.observeFolders((folder, event) => {
Expand Down Expand Up @@ -443,7 +425,7 @@ export class WorkspaceContext implements vscode.Disposable {
/** find LLDB version and setup path in CodeLLDB */
async setLLDBVersion() {
// check we are using CodeLLDB
if (DebugAdapter.adapterName !== "lldb") {
if (DebugAdapter.getDebugAdapterType(this.swiftVersion) !== "lldb-vscode") {
return;
}
const libPathResult = await getLLDBLibPath(this.toolchain);
Expand Down
12 changes: 10 additions & 2 deletions src/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export interface LSPConfiguration {

/** debugger configuration */
export interface DebuggerConfiguration {
/** Are we using debug adapter provided with Toolchain */
/** Whether or not to use CodeLLDB for debugging instead of lldb-dap */
readonly useDebugAdapterFromToolchain: boolean;
/** Return path to debug adapter */
readonly customDebugAdapterPath: string;
Expand Down Expand Up @@ -151,8 +151,16 @@ const configuration = {
/** debugger configuration */
get debugger(): DebuggerConfiguration {
return {
/** Should we use the debug adapter included in the Toolchain or CodeLLDB */
get useDebugAdapterFromToolchain(): boolean {
// Enabled by default only when we're on Windows arm64 since CodeLLDB does not support
// this platform and gives an awful error message.
if (process.platform === "win32" && process.arch === "arm64") {
// We need to use inspect to find out if the value is explicitly set.
const inspect = vscode.workspace
.getConfiguration("swift.debugger")
.inspect<boolean>("useDebugAdapterFromToolchain");
return inspect?.workspaceValue ?? inspect?.globalValue ?? true;
}
return vscode.workspace
.getConfiguration("swift.debugger")
.get<boolean>("useDebugAdapterFromToolchain", false);
Expand Down
7 changes: 4 additions & 3 deletions src/debugger/buildConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,6 @@ export class TestingConfigurationFactory {
return {
type: DebugAdapter.adapterName,
request: "custom",
sourceLanguages: ["swift"],
name: `Test ${this.ctx.swiftPackage.name}`,
targetCreateCommands: [`file -a ${arch} ${xctestPath}/xctest`],
processCreateCommands: [
Expand Down Expand Up @@ -639,8 +638,10 @@ function getBaseConfig(ctx: FolderContext, expandEnvVariables: boolean) {

export function getFolderAndNameSuffix(
ctx: FolderContext,
expandEnvVariables = false
expandEnvVariables = false,
platform?: "posix" | "win32"
): { folder: string; nameSuffix: string } {
const nodePath = platform === "posix" ? path.posix : platform === "win32" ? path.win32 : path;
const workspaceFolder = expandEnvVariables
? ctx.workspaceFolder.uri.fsPath
: `\${workspaceFolder:${ctx.workspaceFolder.name}}`;
Expand All @@ -650,7 +651,7 @@ export function getFolderAndNameSuffix(
folder = workspaceFolder;
nameSuffix = "";
} else {
folder = path.join(workspaceFolder, ctx.relativePath);
folder = nodePath.join(workspaceFolder, ctx.relativePath);
nameSuffix = ` (${ctx.relativePath})`;
}
return { folder: folder, nameSuffix: nameSuffix };
Expand Down
19 changes: 8 additions & 11 deletions src/debugger/debugAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,17 @@ import { SwiftToolchain } from "../toolchain/toolchain";
* Class managing which debug adapter we are using. Will only setup lldb-vscode/lldb-dap if it is available.
*/
export class DebugAdapter {
private static debugAdapaterExists = false;

/** Debug adapter name */
public static get adapterName(): string {
return configuration.debugger.useDebugAdapterFromToolchain && this.debugAdapaterExists
? "swift-lldb"
: "lldb";
return "swift-lldb";
}

/** Return debug adapter for toolchain */
public static getDebugAdapter(swiftVersion: Version): "lldb-vscode" | "lldb-dap" {
return swiftVersion.isLessThan(new Version(6, 0, 0)) ? "lldb-vscode" : "lldb-dap";
public static getDebugAdapterType(swiftVersion: Version): "lldb-vscode" | "lldb-dap" {
return swiftVersion.isGreaterThanOrEqual(new Version(6, 0, 0)) &&
configuration.debugger.useDebugAdapterFromToolchain
? "lldb-dap"
: "lldb-vscode";
}

/** Return the path to the debug adapter */
Expand All @@ -45,7 +44,7 @@ export class DebugAdapter {
return customDebugAdapterPath;
}

const debugAdapter = this.getDebugAdapter(toolchain.swiftVersion);
const debugAdapter = this.getDebugAdapterType(toolchain.swiftVersion);
if (process.platform === "darwin" && debugAdapter === "lldb-dap") {
return await toolchain.getLLDBDebugAdapter();
} else {
Expand All @@ -67,20 +66,18 @@ export class DebugAdapter {

if (!(await fileExists(lldbDebugAdapterPath))) {
if (!quiet) {
const debugAdapterName = this.getDebugAdapter(workspace.toolchain.swiftVersion);
const debugAdapterName = this.getDebugAdapterType(workspace.toolchain.swiftVersion);
vscode.window.showErrorMessage(
configuration.debugger.customDebugAdapterPath.length > 0
? `Cannot find ${debugAdapterName} debug adapter specified in setting Swift.Debugger.Path.`
: `Cannot find ${debugAdapterName} debug adapter in your Swift toolchain.`
);
}
workspace.outputChannel.log(`Failed to find ${lldbDebugAdapterPath}`);
this.debugAdapaterExists = false;
contextKeys.lldbVSCodeAvailable = false;
return false;
}

this.debugAdapaterExists = true;
contextKeys.lldbVSCodeAvailable = true;
return true;
}
Expand Down
35 changes: 26 additions & 9 deletions src/debugger/debugAdapterFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,25 @@
//===----------------------------------------------------------------------===//

import * as vscode from "vscode";
import * as path from "path";
import { WorkspaceContext } from "../WorkspaceContext";
import { DebugAdapter } from "./debugAdapter";
import { Version } from "../utilities/version";

export function registerLLDBDebugAdapter(workspaceContext: WorkspaceContext): vscode.Disposable {
class LLDBDebugAdapterExecutableFactory implements vscode.DebugAdapterDescriptorFactory {
createDebugAdapterDescriptor(
async createDebugAdapterDescriptor(
_session: vscode.DebugSession,
executable: vscode.DebugAdapterExecutable | undefined
): vscode.ProviderResult<vscode.DebugAdapterDescriptor> {
): Promise<vscode.DebugAdapterDescriptor> {
if (executable) {
// make VS Code launch the debug adapter executable
return executable;
}

return DebugAdapter.debugAdapterPath(workspaceContext.toolchain)
.then(path =>
DebugAdapter.verifyDebugAdapterExists(workspaceContext).then(() => path)
)
.then(path => new vscode.DebugAdapterExecutable(path, [], {}));
const adapterPath = await DebugAdapter.debugAdapterPath(workspaceContext.toolchain);
await DebugAdapter.verifyDebugAdapterExists(workspaceContext);
return new vscode.DebugAdapterExecutable(adapterPath, [], {});
}
}

Expand All @@ -41,7 +41,10 @@ export function registerLLDBDebugAdapter(workspaceContext: WorkspaceContext): vs
);
const debugConfigProvider = vscode.debug.registerDebugConfigurationProvider(
"swift-lldb",
new LLDBDebugConfigurationProvider()
new LLDBDebugConfigurationProvider(
process.platform,
workspaceContext.toolchain.swiftVersion
)
);
return {
dispose: () => {
Expand All @@ -60,14 +63,28 @@ export function registerLLDBDebugAdapter(workspaceContext: WorkspaceContext): vs
* This could also be used to augment the configuration with values from the settings
* althought it isn't at the moment.
*/
class LLDBDebugConfigurationProvider implements vscode.DebugConfigurationProvider {
export class LLDBDebugConfigurationProvider implements vscode.DebugConfigurationProvider {
constructor(
private platform: NodeJS.Platform,
private swiftVersion: Version
) {}

async resolveDebugConfiguration(
folder: vscode.WorkspaceFolder | undefined,
launchConfig: vscode.DebugConfiguration,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
cancellation?: vscode.CancellationToken
): Promise<vscode.DebugConfiguration> {
launchConfig.env = this.convertEnvironmentVariables(launchConfig.env);
// Fix the program path on Windows to include the ".exe" extension
if (this.platform === "win32" && path.extname(launchConfig.program) !== ".exe") {
launchConfig.program += ".exe";
}
// Delegate to CodeLLDB if that's the debug adapter we have selected
if (DebugAdapter.getDebugAdapterType(this.swiftVersion) === "lldb-vscode") {
launchConfig.type = "lldb";
launchConfig.sourceLanguages = ["swift"];
}
return launchConfig;
}

Expand Down
Loading