🐛 fix(vscode): 删 worktree 前扫 /proc 检测进程占用,命中则中止清理
dev server 常驻在 worktree 里持续写构建产物,递归删除与写入竞态导致 rmdir 撞 ENOTEMPTY。新增 findProcessesUsingWorktree 扫 /proc 找 cwd 或 fd 落在 worktree 下的进程,三处删除调用点(拖到完成列 / 手动删 / 删整工单) 命中即弹错误 toast 列出 pid+命令并中止(不删、不杀进程),用户停掉后重试。 非 Linux 无 /proc 返回空,降级为原直接删除。
This commit is contained in:
1 parent
b585016cd7
commit
aff4ee2758
4 files changed
+155
-3
No files matched your search
@@ -0,0 +1,37 @@
|
||||
import { spawn } from 'node:child_process'
|
||||
import { existsSync, mkdtempSync, rmSync } from 'node:fs'
|
||||
import * as os from 'node:os'
|
||||
import * as path from 'node:path'
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import { findProcessesUsingWorktree } from './worktree'
|
||||
|
||||
// 检测靠扫 /proc,只有 Linux 有;别的平台整组跳过(函数本身也会返回空)。
|
||||
describe.runIf(existsSync('/proc'))('findProcessesUsingWorktree', () => {
|
||||
it('抓到 cwd 落在 worktree 目录下的常驻进程', async () => {
|
||||
const dir = mkdtempSync(path.join(os.tmpdir(), 'wt-proc-'))
|
||||
// cwd 设进 dir 的 sleep,模拟仍在 worktree 里跑的 dev server
|
||||
const child = spawn('sleep', ['30'], { cwd: dir, stdio: 'ignore' })
|
||||
try {
|
||||
await new Promise<void>((resolve, reject) => {
|
||||
child.on('spawn', () => resolve())
|
||||
child.on('error', reject)
|
||||
})
|
||||
const hits = findProcessesUsingWorktree(dir)
|
||||
expect(hits.some(h => h.pid === String(child.pid))).toBe(true)
|
||||
}
|
||||
finally {
|
||||
child.kill('SIGKILL')
|
||||
rmSync(dir, { recursive: true, force: true })
|
||||
}
|
||||
})
|
||||
|
||||
it('没进程占用的目录返回空数组', () => {
|
||||
const dir = mkdtempSync(path.join(os.tmpdir(), 'wt-empty-'))
|
||||
try {
|
||||
expect(findProcessesUsingWorktree(dir)).toEqual([])
|
||||
}
|
||||
finally {
|
||||
rmSync(dir, { recursive: true, force: true })
|
||||
}
|
||||
})
|
||||
})
|
||||
Reference in new issue
Block a user