@@ -47,39 +47,8 @@ export async function activate(context: vscode.ExtensionContext): Promise<IPower
4747 restartOnExtensionFileChanges ( context ) ;
4848 }
4949
50- // Register a command that waits for the Externsion Terminal to be active. Can be used by .NET Attach Tasks
51- vscode . commands . registerCommand (
52- "PowerShell.WaitForPsesActivationAndReturnProcessId" ,
53- async ( ) => {
54- const pidFileName = `PSES-${ vscode . env . sessionId } .pid` ;
55- const pidFile = vscode . Uri . joinPath ( context . globalStorageUri , "sessions" , pidFileName ) ;
56- const fs = vscode . workspace . fs ;
57- // Wait for the file to be created
58- // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition, no-constant-condition
59- while ( true ) {
60- try {
61- const pidContent = await fs . readFile ( pidFile ) ;
62- const pid = parseInt ( pidContent . toString ( ) , 10 ) ;
63- try {
64- // Check if the process is still alive, delete the PID file if not and continue waiting.
65- process . kill ( pid , 0 ) ;
66- } catch {
67- await fs . delete ( pidFile ) ;
68- continue ;
69- }
70- return pidContent . toString ( ) ;
71- } catch {
72- // File doesn't exist yet, wait and try again
73- await new Promise ( resolve => setTimeout ( resolve , 1000 ) ) ;
74- }
75- }
76- }
77- ) ;
7850
79- vscode . commands . registerCommand (
80- "GetVsCodeSessionId" ,
81- ( ) => vscode . env . sessionId
82- ) ;
51+
8352
8453 telemetryReporter = new TelemetryReporter ( TELEMETRY_KEY ) ;
8554
@@ -187,7 +156,13 @@ export async function activate(context: vscode.ExtensionContext): Promise<IPower
187156 vscode . commands . registerCommand (
188157 "PowerShell.ShowLogs" ,
189158 ( ) => { logger . showLogPanel ( ) ; }
190- )
159+ ) ,
160+ vscode . commands . registerCommand (
161+ "GetVsCodeSessionId" ,
162+ ( ) => vscode . env . sessionId
163+ ) ,
164+ // Register a command that waits for the Externsion Terminal to be active. Can be used by .NET Attach Tasks
165+ registerWaitForPsesActivationCommand ( context )
191166 ] ;
192167
193168 const externalApi = new ExternalApiFeature ( context , sessionManager , logger ) ;
@@ -220,6 +195,37 @@ export async function activate(context: vscode.ExtensionContext): Promise<IPower
220195 } ;
221196}
222197
198+ /** Registers a command that waits for PSES Activation and returns the PID, used to auto-attach the PSES debugger */
199+ function registerWaitForPsesActivationCommand ( context : vscode . ExtensionContext ) {
200+ return vscode . commands . registerCommand (
201+ "PowerShell.WaitForPsesActivationAndReturnProcessId" ,
202+ async ( ) => {
203+ const pidFileName = `PSES-${ vscode . env . sessionId } .pid` ;
204+ const pidFile = vscode . Uri . joinPath ( context . globalStorageUri , "sessions" , pidFileName ) ;
205+ const fs = vscode . workspace . fs ;
206+ // Wait for the file to be created
207+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition, no-constant-condition
208+ while ( true ) {
209+ try {
210+ const pidContent = await fs . readFile ( pidFile ) ;
211+ const pid = parseInt ( pidContent . toString ( ) , 10 ) ;
212+ try {
213+ // Check if the process is still alive, delete the PID file if not and continue waiting.
214+ process . kill ( pid , 0 ) ;
215+ } catch {
216+ await fs . delete ( pidFile ) ;
217+ continue ;
218+ }
219+ return pidContent . toString ( ) ;
220+ } catch {
221+ // File doesn't exist yet, wait and try again
222+ await new Promise ( resolve => setTimeout ( resolve , 1000 ) ) ;
223+ }
224+ }
225+ }
226+ ) ;
227+ }
228+
223229/** Restarts the extension host when extension file changes are detected. Useful for development.. */
224230function restartOnExtensionFileChanges ( context : vscode . ExtensionContext ) : void {
225231 const watcher = vscode . workspace . createFileSystemWatcher (
0 commit comments