Skip to content

Commit 7e61a4e

Browse files
committed
install launch.json for debugging
1 parent 661f146 commit 7e61a4e

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

src/extension.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,58 @@ async function fixPylanceConfig(
335335
}
336336
}
337337

338+
async function fixDebuggerConfig(
339+
context: vscode.ExtensionContext,
340+
folder?: vscode.WorkspaceFolder
341+
) {
342+
// Create a .vscode/launch.json for beginner debugging if one does not exist.
343+
// Use the provided workspace folder if available, otherwise fall back to the first workspace folder.
344+
const workspacePath = folder?.uri?.fsPath ?? (vscode.workspace.workspaceFolders && vscode.workspace.workspaceFolders[0]?.uri.fsPath);
345+
if (!workspacePath) {
346+
// error messages already shown in fixPylanceConfig
347+
return;
348+
}
349+
350+
const vscodeDir = path.join(workspacePath, '.vscode');
351+
const launchFile = path.join(vscodeDir, 'launch.json');
352+
353+
if (fs.existsSync(launchFile)) {
354+
// Do not overwrite existing launch.json
355+
return;
356+
}
357+
358+
try {
359+
if (!fs.existsSync(vscodeDir)) {
360+
fs.mkdirSync(vscodeDir, { recursive: true });
361+
}
362+
363+
const sep = path.delimiter; // ':' on POSIX, ';' on Windows
364+
const d1 = context.asAbsolutePath('python/code');
365+
const d2 = context.asAbsolutePath('python/code/wypp');
366+
const envValue = `${d1}${sep}${d2}${sep}\${env:PYTHONPATH}`;
367+
const envObj: any = {};
368+
envObj['PYTHONPATH'] = envValue;
369+
370+
const launchJson = {
371+
version: '0.2.0',
372+
configurations: [
373+
{
374+
name: 'WYPP Debugger',
375+
type: 'debugpy',
376+
request: 'launch',
377+
program: '${file}',
378+
console: 'integratedTerminal',
379+
env: envObj
380+
}
381+
]
382+
};
383+
384+
fs.writeFileSync(launchFile, JSON.stringify(launchJson, null, 2), { encoding: 'utf8' });
385+
} catch (e) {
386+
vscode.window.showWarningMessage('Write Your Python Program: failed to create launch.json: ' + String(e));
387+
}
388+
}
389+
338390
class Location implements vscode.TerminalLink {
339391
constructor(
340392
public startIndex: number,
@@ -476,6 +528,7 @@ export async function activate(context: vscode.ExtensionContext) {
476528
return;
477529
}
478530
await fixPylanceConfig(context);
531+
await fixDebuggerConfig(context);
479532
await vscode.window.activeTextEditor?.document.save();
480533
const pyCmd = getPythonCmd(pyExt);
481534
let verboseOpt = "";

0 commit comments

Comments
 (0)