Registers a command that waits for PSES Activation and returns the PID, used to auto-attach the PSES debugger
(
context: vscode.ExtensionContext,
)
| 225 | |
| 226 | /** Registers a command that waits for PSES Activation and returns the PID, used to auto-attach the PSES debugger */ |
| 227 | function registerWaitForPsesActivationCommand( |
| 228 | context: vscode.ExtensionContext, |
| 229 | ): vscode.Disposable { |
| 230 | return vscode.commands.registerCommand( |
| 231 | "PowerShell.WaitForPsesActivationAndReturnProcessId", |
| 232 | async () => { |
| 233 | const pidFileName = `PSES-${vscode.env.sessionId}.pid`; |
| 234 | const pidFile = vscode.Uri.joinPath( |
| 235 | context.globalStorageUri, |
| 236 | "sessions", |
| 237 | pidFileName, |
| 238 | ); |
| 239 | const fs = vscode.workspace.fs; |
| 240 | // Wait for the file to be created |
| 241 | // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition |
| 242 | while (true) { |
| 243 | try { |
| 244 | const pidContent = await fs.readFile(pidFile); |
| 245 | const pid = parseInt(pidContent.toString(), 10); |
| 246 | try { |
| 247 | // Check if the process is still alive, delete the PID file if not and continue waiting. |
| 248 | // https://nodejs.org/api/process.html#process_process_kill_pid_signal |
| 249 | // "As a special case, a signal of 0 can be used to test for the existence of a process. " |
| 250 | const NODE_TEST_PROCESS_EXISTENCE = 0; |
| 251 | process.kill(pid, NODE_TEST_PROCESS_EXISTENCE); |
| 252 | } catch { |
| 253 | await fs.delete(pidFile); |
| 254 | continue; |
| 255 | } |
| 256 | // VSCode command returns for launch configurations *must* be string type explicitly, will error on number or otherwise. |
| 257 | return pidContent.toString(); |
| 258 | } catch { |
| 259 | // File doesn't exist yet, wait and try again |
| 260 | await sleep(200); |
| 261 | } |
| 262 | } |
| 263 | }, |
| 264 | ); |
| 265 | } |
| 266 | |
| 267 | /** Restarts the extension host when extension file changes are detected. Useful for development. */ |
| 268 | function restartOnExtensionFileChanges(context: vscode.ExtensionContext): void { |