|
| 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 | +const PATH_VARIABLE = 'PATH'; |
| 7 | + |
| 8 | +export async function registerConfiglessDebug(context: IExtensionContext): Promise<void> { |
| 9 | + const collection = context.environmentVariableCollection; |
| 10 | + // constants |
| 11 | + const debuggerAdapterEndpointFolderPath = path.join(context.extensionPath, 'src/extension/configlessCommunication'); |
| 12 | + const debuggerAdapterEndpointPath = path.join(debuggerAdapterEndpointFolderPath, 'debuggerAdapterEndpoint.txt'); |
| 13 | + |
| 14 | + // Add env vars for DEBUGPY_ADAPTER_ENDPOINTS and PATH |
| 15 | + collection.replace('DEBUGPY_ADAPTER_ENDPOINTS', debuggerAdapterEndpointPath); |
| 16 | + // TODO: append for path |
| 17 | + |
| 18 | + // const pathVariableChange = path.delimiter + '/Users/eleanorboyd/vscode-python-debugger'; |
| 19 | + // if (context.environmentVariableCollection.get(PATH_VARIABLE)) { |
| 20 | + // context.environmentVariableCollection.delete(PATH_VARIABLE); |
| 21 | + // } else if (context.environmentVariableCollection.get(PATH_VARIABLE)?.value !== pathVariableChange) { |
| 22 | + // context.environmentVariableCollection.description = 'enable config-less debug'; |
| 23 | + // context.environmentVariableCollection.delete(PATH_VARIABLE); |
| 24 | + // context.environmentVariableCollection.append(PATH_VARIABLE, pathVariableChange); |
| 25 | + // } |
| 26 | + |
| 27 | + // create file system watcher for the debuggerAdapterEndpointFolder for when the communication port is written |
| 28 | + context.subscriptions.push( |
| 29 | + workspace |
| 30 | + .createFileSystemWatcher(new RelativePattern(debuggerAdapterEndpointFolderPath, '**/*')) |
| 31 | + .onDidCreate((uri) => { |
| 32 | + console.log(`File created: ${uri.fsPath}`); |
| 33 | + const filePath = uri.fsPath; |
| 34 | + fs.readFile(filePath, 'utf8', (err, data) => { |
| 35 | + if (err) { |
| 36 | + console.error(`Error reading file: ${err}`); |
| 37 | + return; |
| 38 | + } |
| 39 | + try { |
| 40 | + // parse the client port |
| 41 | + const jsonData = JSON.parse(data); |
| 42 | + const clientPort = jsonData.client?.port; |
| 43 | + console.log(`Client port: ${clientPort}`); |
| 44 | + |
| 45 | + const options: DebugSessionOptions = { |
| 46 | + noDebug: false, |
| 47 | + }; |
| 48 | + |
| 49 | + // start debug session with the client port |
| 50 | + debug |
| 51 | + .startDebugging( |
| 52 | + undefined, |
| 53 | + { |
| 54 | + type: 'python', |
| 55 | + request: 'attach', |
| 56 | + name: 'Attach to Python', |
| 57 | + port: clientPort, |
| 58 | + host: 'localhost', |
| 59 | + }, |
| 60 | + options, |
| 61 | + ) |
| 62 | + .then( |
| 63 | + (started) => { |
| 64 | + if (started) { |
| 65 | + console.log('Debug session started successfully'); |
| 66 | + } else { |
| 67 | + console.error('Failed to start debug session'); |
| 68 | + } |
| 69 | + }, |
| 70 | + (error) => { |
| 71 | + console.error(`Error starting debug session: ${error}`); |
| 72 | + }, |
| 73 | + ); |
| 74 | + } catch (parseErr) { |
| 75 | + console.error(`Error parsing JSON: ${parseErr}`); |
| 76 | + } |
| 77 | + }); |
| 78 | + JSON.parse; |
| 79 | + }), |
| 80 | + ); |
| 81 | +} |
0 commit comments