|
| 1 | +import * as fs from 'fs'; |
| 2 | +import * as path from 'path'; |
| 3 | +import { IExtensionContext } from './common/types'; |
| 4 | +import { DebugSessionOptions, debug, RelativePattern, workspace } from 'vscode'; |
| 5 | + |
| 6 | +/** |
| 7 | + * Registers the configuration-less debugging setup for the extension. |
| 8 | + * |
| 9 | + * This function sets up environment variables and a file system watcher to |
| 10 | + * facilitate debugging without requiring a pre-configured launch.json file. |
| 11 | + * |
| 12 | + * @param context - The extension context which provides access to the environment variable collection and subscriptions. |
| 13 | + * |
| 14 | + * Environment Variables: |
| 15 | + * - `DEBUGPY_ADAPTER_ENDPOINTS`: Path to the file containing the debugger adapter endpoint. |
| 16 | + * - `BUNDLED_DEBUGPY_PATH`: Path to the bundled debugpy library. |
| 17 | + * - `PATH`: Appends the path to the noConfigScripts directory. |
| 18 | + */ |
| 19 | +export async function registerConfiglessDebug(context: IExtensionContext): Promise<void> { |
| 20 | + const collection = context.environmentVariableCollection; |
| 21 | + |
| 22 | + // Add env vars for DEBUGPY_ADAPTER_ENDPOINTS, BUNDLED_DEBUGPY_PATH, and PATH |
| 23 | + const debugAdapterEndpointDir = path.join(context.extensionPath, 'noConfigDebugAdapterEndpoints'); |
| 24 | + const debuggerAdapterEndpointPath = path.join(debugAdapterEndpointDir, 'debuggerAdapterEndpoint.txt'); |
| 25 | + collection.replace('DEBUGPY_ADAPTER_ENDPOINTS', debuggerAdapterEndpointPath); |
| 26 | + |
| 27 | + const noConfigScriptsDir = path.join(context.extensionPath, 'bundled/scripts/noConfigScripts'); |
| 28 | + collection.append('PATH', `:${noConfigScriptsDir}`); |
| 29 | + |
| 30 | + const bundledDebugPath = path.join(context.extensionPath, 'bundled/libs/debugpy'); |
| 31 | + collection.replace('BUNDLED_DEBUGPY_PATH', bundledDebugPath); |
| 32 | + |
| 33 | + // create file system watcher for the debuggerAdapterEndpointFolder for when the communication port is written |
| 34 | + context.subscriptions.push( |
| 35 | + workspace.createFileSystemWatcher(new RelativePattern(debugAdapterEndpointDir, '**/*')).onDidCreate((uri) => { |
| 36 | + console.log(`File created: ${uri.fsPath}`); |
| 37 | + const filePath = uri.fsPath; |
| 38 | + fs.readFile(filePath, 'utf8', (err, data) => { |
| 39 | + if (err) { |
| 40 | + console.error(`Error reading file: ${err}`); |
| 41 | + return; |
| 42 | + } |
| 43 | + try { |
| 44 | + // parse the client port |
| 45 | + const jsonData = JSON.parse(data); |
| 46 | + const clientPort = jsonData.client?.port; |
| 47 | + console.log(`Client port: ${clientPort}`); |
| 48 | + |
| 49 | + const options: DebugSessionOptions = { |
| 50 | + noDebug: false, |
| 51 | + }; |
| 52 | + |
| 53 | + // start debug session with the client port |
| 54 | + debug |
| 55 | + .startDebugging( |
| 56 | + undefined, |
| 57 | + { |
| 58 | + type: 'python', |
| 59 | + request: 'attach', |
| 60 | + name: 'Attach to Python', |
| 61 | + port: clientPort, |
| 62 | + host: 'localhost', |
| 63 | + }, |
| 64 | + options, |
| 65 | + ) |
| 66 | + .then( |
| 67 | + (started) => { |
| 68 | + if (started) { |
| 69 | + console.log('Debug session started successfully'); |
| 70 | + } else { |
| 71 | + console.error('Failed to start debug session'); |
| 72 | + } |
| 73 | + }, |
| 74 | + (error) => { |
| 75 | + console.error(`Error starting debug session: ${error}`); |
| 76 | + }, |
| 77 | + ); |
| 78 | + } catch (parseErr) { |
| 79 | + console.error(`Error parsing JSON: ${parseErr}`); |
| 80 | + } |
| 81 | + }); |
| 82 | + JSON.parse; |
| 83 | + }), |
| 84 | + ); |
| 85 | +} |
0 commit comments