Skip to content

Add command to generate launch configurations #1577

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
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
9 changes: 9 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@
}
],
"commands": [
{
"command": "swift.generateLaunchConfigurations",
"title": "Generate Launch Configurations",
"category": "Swift"
},
{
"command": "swift.previewDocumentation",
"title": "Preview Documentation",
Expand Down Expand Up @@ -886,6 +891,10 @@
}
],
"commandPalette": [
{
"command": "swift.generateLaunchConfigurations",
"when": "swift.hasExecutableProduct"
},
{
"command": "swift.previewDocumentation",
"when": "swift.supportsDocumentationLivePreview"
Expand Down
9 changes: 6 additions & 3 deletions src/WorkspaceContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ export class WorkspaceContext implements vscode.Disposable {
.then(async selected => {
if (selected === "Update") {
this.folders.forEach(ctx =>
makeDebugConfigurations(ctx, undefined, true)
makeDebugConfigurations(ctx, { yes: true })
);
}
});
Expand All @@ -120,7 +120,7 @@ export class WorkspaceContext implements vscode.Disposable {
.then(selected => {
if (selected === "Update") {
this.folders.forEach(ctx =>
makeDebugConfigurations(ctx, undefined, true)
makeDebugConfigurations(ctx, { yes: true })
);
}
});
Expand Down Expand Up @@ -228,15 +228,18 @@ export class WorkspaceContext implements vscode.Disposable {
updateContextKeys(folderContext: FolderContext | null) {
if (!folderContext) {
contextKeys.hasPackage = false;
contextKeys.hasExecutableProduct = false;
contextKeys.packageHasDependencies = false;
return;
}

Promise.all([
folderContext.swiftPackage.foundPackage,
folderContext.swiftPackage.executableProducts,
folderContext.swiftPackage.dependencies,
]).then(([foundPackage, dependencies]) => {
]).then(([foundPackage, executableProducts, dependencies]) => {
contextKeys.hasPackage = foundPackage;
contextKeys.hasExecutableProduct = executableProducts.length > 0;
contextKeys.packageHasDependencies = dependencies.length > 0;
});
}
Expand Down
4 changes: 4 additions & 0 deletions src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import { TestKind } from "./TestExplorer/TestKind";
import { pickProcess } from "./commands/pickProcess";
import { openDocumentation } from "./commands/openDocumentation";
import restartLSPServer from "./commands/restartLSPServer";
import { generateLaunchConfigurations } from "./commands/generateLaunchConfigurations";

/**
* References:
Expand Down Expand Up @@ -105,6 +106,9 @@ export enum Commands {
*/
export function register(ctx: WorkspaceContext): vscode.Disposable[] {
return [
vscode.commands.registerCommand("swift.generateLaunchConfigurations", () =>
generateLaunchConfigurations(ctx)
),
vscode.commands.registerCommand("swift.newFile", uri => newSwiftFile(uri)),
vscode.commands.registerCommand(Commands.RESOLVE_DEPENDENCIES, () =>
resolveDependencies(ctx)
Expand Down
70 changes: 70 additions & 0 deletions src/commands/generateLaunchConfigurations.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the VS Code Swift open source project
//
// Copyright (c) 2025 the VS Code Swift project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of VS Code Swift project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//

import { makeDebugConfigurations } from "../debugger/launch";
import { FolderContext } from "../FolderContext";
import { WorkspaceContext } from "../WorkspaceContext";
import * as vscode from "vscode";

export async function generateLaunchConfigurations(ctx: WorkspaceContext): Promise<boolean> {
if (ctx.folders.length === 0) {
return false;
}

if (ctx.folders.length === 1) {
return await makeDebugConfigurations(ctx.folders[0], { force: true, yes: true });
}

const quickPickItems: SelectFolderQuickPick[] = ctx.folders.map(folder => ({
type: "folder",
folder,
label: folder.name,
detail: folder.workspaceFolder.uri.fsPath,
}));
quickPickItems.push({ type: "all", label: "Generate For All Folders" });
const selection = await vscode.window.showQuickPick(quickPickItems, {
matchOnDetail: true,
placeHolder: "Select a folder to generate launch configurations for",
});

if (!selection) {
return false;
}

const foldersToUpdate: FolderContext[] = [];
if (selection.type === "all") {
foldersToUpdate.push(...ctx.folders);
} else {
foldersToUpdate.push(selection.folder);
}

return (
await Promise.all(
foldersToUpdate.map(folder =>
makeDebugConfigurations(folder, { force: true, yes: true })
)
)
).reduceRight((prev, curr) => prev || curr);
}

type SelectFolderQuickPick = AllQuickPickItem | FolderQuickPickItem;

interface AllQuickPickItem extends vscode.QuickPickItem {
type: "all";
}

interface FolderQuickPickItem extends vscode.QuickPickItem {
type: "folder";
folder: FolderContext;
}
15 changes: 15 additions & 0 deletions src/contextKeys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ interface ContextKeys {
*/
hasPackage: boolean;

/**
* Whether the workspace folder contains a Swift package with at least one executable product.
*/
hasExecutableProduct: boolean;

/**
* Whether the Swift package has any dependencies to display in the Package Dependencies view.
*/
Expand Down Expand Up @@ -94,6 +99,7 @@ interface ContextKeys {
function createContextKeys(): ContextKeys {
let isActivated: boolean = false;
let hasPackage: boolean = false;
let hasExecutableProduct: boolean = false;
let flatDependenciesList: boolean = false;
let packageHasDependencies: boolean = false;
let packageHasPlugins: boolean = false;
Expand Down Expand Up @@ -134,6 +140,15 @@ function createContextKeys(): ContextKeys {
vscode.commands.executeCommand("setContext", "swift.hasPackage", value);
},

get hasExecutableProduct() {
return hasExecutableProduct;
},

set hasExecutableProduct(value: boolean) {
hasExecutableProduct = value;
vscode.commands.executeCommand("setContext", "swift.hasExecutableTarget", value);
},

get packageHasDependencies() {
return packageHasDependencies;
},
Expand Down
16 changes: 13 additions & 3 deletions src/debugger/debugAdapterFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import { registerLoggingDebugAdapterTracker } from "./logTracker";
import { SwiftToolchain } from "../toolchain/toolchain";
import { SwiftOutputChannel } from "../ui/SwiftOutputChannel";
import { fileExists } from "../utilities/filesystem";
import { getLLDBLibPath } from "./lldb";
import { getErrorDescription } from "../utilities/utilities";
import { CI_DISABLE_ASLR, getLLDBLibPath } from "./lldb";
import { getErrorDescription, swiftRuntimeEnv } from "../utilities/utilities";
import configuration from "../configuration";

/**
Expand Down Expand Up @@ -137,6 +137,13 @@ export class LLDBDebugConfigurationProvider implements vscode.DebugConfiguration
launchConfig.pid = pid;
}

// Merge in the Swift runtime environment variables
const runtimeEnv = swiftRuntimeEnv(true);
if (runtimeEnv) {
const existingEnv = launchConfig.env ?? {};
launchConfig.env = { ...runtimeEnv, existingEnv };
}

// Delegate to the appropriate debug adapter extension
launchConfig.type = DebugAdapter.getLaunchConfigType(toolchain.swiftVersion);
if (launchConfig.type === LaunchConfigType.CODE_LLDB) {
Expand Down Expand Up @@ -164,7 +171,10 @@ export class LLDBDebugConfigurationProvider implements vscode.DebugConfiguration
launchConfig.debugAdapterExecutable = lldbDapPath;
}

return launchConfig;
return {
...launchConfig,
...CI_DISABLE_ASLR,
};
}

private async promptToInstallCodeLLDB(): Promise<boolean> {
Expand Down
Loading