Skip to content

Commit db45827

Browse files
committed
Support debugging in non-workspace scenarios
1 parent 2a4aae9 commit db45827

File tree

2 files changed

+41
-4
lines changed

2 files changed

+41
-4
lines changed

src/extension/providers/JudgeViewProvider.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
} from "../utils/runtime";
2020
import type { Severity } from "../utils/runtime";
2121
import {
22+
getAttachDebugConfiguration,
2223
getFileRunSettings,
2324
openInNewEditor,
2425
openInTerminalTab,
@@ -1192,10 +1193,10 @@ export default class extends BaseViewProvider<typeof ProviderMessageSchema, Webv
11921193
const folder =
11931194
vscode.workspace.getWorkspaceFolder(vscode.Uri.file(this._currentFile)) ??
11941195
vscode.workspace.workspaceFolders?.at(0);
1195-
const attachConfig = vscode.workspace
1196-
.getConfiguration("launch", folder)
1197-
.get<vscode.DebugConfiguration[]>("configurations", [])
1198-
.find((config) => config.name === ctx.languageSettings.debugAttachConfig);
1196+
const attachConfig = await getAttachDebugConfiguration(
1197+
ctx.languageSettings.debugAttachConfig,
1198+
this._currentFile
1199+
);
11991200
if (!attachConfig) {
12001201
const logger = getLogger("judge");
12011202
logger.error(

src/extension/utils/vscode.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -655,6 +655,42 @@ export async function getFileWorkspace(file?: string): Promise<string | undefine
655655
return runSettingsFolder;
656656
}
657657

658+
export async function getAttachDebugConfiguration(
659+
configName: string,
660+
file: string
661+
): Promise<vscode.DebugConfiguration | undefined> {
662+
const folder =
663+
vscode.workspace.getWorkspaceFolder(vscode.Uri.file(file)) ??
664+
vscode.workspace.workspaceFolders?.at(0);
665+
666+
const workspaceConfig = vscode.workspace
667+
.getConfiguration("launch", folder)
668+
.get<vscode.DebugConfiguration[]>("configurations", [])
669+
.find((config) => config.name === configName);
670+
if (workspaceConfig) {
671+
return workspaceConfig;
672+
}
673+
674+
const fileWorkspacePath = await getFileWorkspace(file);
675+
if (!fileWorkspacePath) {
676+
return undefined;
677+
}
678+
679+
const launchJsonPath = path.join(fileWorkspacePath, ".vscode", "launch.json");
680+
try {
681+
const launchJsonRaw = await vscode.workspace.fs.readFile(vscode.Uri.file(launchJsonPath));
682+
const parsed = JSON.parse(Buffer.from(launchJsonRaw).toString("utf8")) as {
683+
configurations?: vscode.DebugConfiguration[];
684+
};
685+
if (!Array.isArray(parsed.configurations)) {
686+
return undefined;
687+
}
688+
return parsed.configurations.find((config) => config.name === configName);
689+
} catch {
690+
return undefined;
691+
}
692+
}
693+
658694
/**
659695
* Traverses from the file directory up to the workspace root,
660696
* loading and merging runSettings.json files along the way.

0 commit comments

Comments
 (0)