diff --git a/package.json b/package.json index 6a09a0c..865a752 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "devstar", "displayName": "%displayName%", "description": "%description%", - "version": "0.4.1", + "version": "0.4.3", "keywords": [], "publisher": "mengning", "engines": { diff --git a/src/remote-container.ts b/src/remote-container.ts index f6377a9..11de8cf 100644 --- a/src/remote-container.ts +++ b/src/remote-container.ts @@ -268,34 +268,46 @@ export default class RemoteContainer { `root@${hostname}` ]; - const sshProcess = spawn('ssh', sshArgs); + // 使用 detached 选项让 SSH 进程独立运行,不随父进程退出 + const sshProcess = spawn('ssh', sshArgs, { + detached: true, // 让进程在后台独立运行 + stdio: 'ignore' // 忽略输入输出,避免进程挂起 + }); + + // 解除父进程对子进程的引用,使其真正独立 + sshProcess.unref(); sshProcess.on('error', (error: Error) => { console.error(`[ERROR] SSH 进程启动失败:`, error); reject(error); }); - sshProcess.stdout.on('data', (data: Buffer) => { - console.log(`[SSH stdout] ${data.toString()}`); - }); + // 由于使用了 stdio: 'ignore',这些事件监听器不再需要 + // sshProcess.stdout.on('data', (data: Buffer) => { + // console.log(`[SSH stdout] ${data.toString()}`); + // }); - sshProcess.stderr.on('data', (data: Buffer) => { - console.error(`[SSH stderr] ${data.toString()}`); - }); + // sshProcess.stderr.on('data', (data: Buffer) => { + // console.error(`[SSH stderr] ${data.toString()}`); + // }); - // 监听进程退出 - sshProcess.on('exit', (code: number) => { + // 监听进程退出(detached 进程可能不会触发此事件) + sshProcess.on('exit', (code: number | null) => { console.log(`[DEBUG] SSH 进程退出,代码: ${code}`); }); - if (!this.sshProcesses) { - this.sshProcesses = new Map(); - } - const key = `${hostname}:${sshPort}:${containerPort}`; - this.sshProcesses.set(key, sshProcess); + // 注意:由于进程已 detached 和 unref,不再需要保存到 sshProcesses Map + // 因为我们无法也不需要控制这些独立进程的生命周期 + // if (!this.sshProcesses) { + // this.sshProcesses = new Map(); + // } + // const key = `${hostname}:${sshPort}:${containerPort}`; + // this.sshProcesses.set(key, sshProcess); + // 等待 SSH 连接建立 setTimeout(() => { - console.log(`[DEBUG] 端口映射完成: ${containerPort} -> ${localPort}`); + console.log(`[DEBUG] 端口映射完成: ${containerPort} -> ${localPort} (进程已独立运行)`); + console.log(`[DEBUG] SSH 进程 PID: ${sshProcess.pid}`); resolve(); }, 2000); }); @@ -367,33 +379,42 @@ export default class RemoteContainer { * local env */ async openRemoteFolder(host: string, port: number, _username: string, path: string, context: vscode.ExtensionContext): Promise { + console.log(`[openRemoteFolder] 开始执行`); + console.log(`[openRemoteFolder] 当前环境: ${vscode.env.remoteName || '本地'}`); + console.log(`[openRemoteFolder] 参数: host=${host}, port=${port}, path=${path}`); + try { const sshConfig = await this.getSSHConfig(host); + console.log(`[openRemoteFolder] SSH配置:`, sshConfig); + if (sshConfig) { try { + console.log(`[openRemoteFolder] 准备建立端口映射...`); // 调用 setupPortForwardingFromGlobalState 方法 await this.setupPortForwardingFromGlobalState(sshConfig.hostname, port, context); + console.log(`[openRemoteFolder] 端口映射设置完成`); } catch (portError) { + console.error(`[openRemoteFolder] 端口映射失败:`, portError); vscode.window.showWarningMessage('端口映射设置失败,但容器连接已建立'); } + } else { + console.log(`[openRemoteFolder] 未找到SSH配置,跳过端口映射`); } - // 添加延迟确保 SSH 隧道完全建立 - await new Promise(resolve => setTimeout(resolve, 3000)); - - // 使用 --new-window 而不是 --reuse-window,避免当前窗口被切换 + console.log(`[openRemoteFolder] 准备打开远程文件夹...`); let terminal = vscode.window.activeTerminal || vscode.window.createTerminal(`Ext Terminal`); terminal.show(true); - const command = `code --remote ssh-remote+root@${host}:${port} ${path} --new-window`; + const command = `code --remote ssh-remote+root@${host}:${port} ${path} --reuse-window`; + console.log(`[openRemoteFolder] 执行命令: ${command}`); terminal.sendText(command); - - vscode.window.showInformationMessage('端口映射已在本地窗口建立,新窗口将打开远程环境'); + console.log(`[openRemoteFolder] 命令已发送到终端`); } catch (error) { const errorMessage = error instanceof Error ? error.message : '未知错误'; + console.error(`[openRemoteFolder] 发生错误:`, error); vscode.window.showErrorMessage(`打开远程文件夹失败: ${errorMessage}`); } }