From 60cbf403b0d9e1c49cc39c4c43df0ab61fb78b4a Mon Sep 17 00:00:00 2001 From: newbie Date: Thu, 12 Nov 2026 22:41:01 +0800 Subject: [PATCH] fix(farm): make repeated gather idempotent --- CHANGELOG.md | 5 +++++ lib/groups.js | 34 +++++++++++++++++++++------------- package.json | 2 +- src/host-farm.ts | 32 ++++++++++++++++++++++++-------- tests/farm.test.ts | 19 +++++++++++++++---- 5 files changed, 66 insertions(+), 26 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index cf5824b..1dcda55 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # 更新日志 +## 1.2.1 — 2026-11-12 + +- Farm 创建改为幂等:同一组文件夹再次执行时复用已有 farm,不再返回 HTTP 400;文件夹不同仍明确冲突,防止误合并。 +- 冲突提示改为 `already exists with different folders`,便于区分“已成功”和“配置变了”。 + ## 1.2.0 — 2026-11-12 ### 新增 diff --git a/lib/groups.js b/lib/groups.js index bb2d3ca..66f7c37 100644 --- a/lib/groups.js +++ b/lib/groups.js @@ -3454,24 +3454,32 @@ async function buildFarm(input) { if (targets.length < 2) throw new Error("farm: needs at least two distinct folders"); const root = defaultFarmRoot(); const path = join(root, farmDirectoryName(String(input.name))); + const taken = /* @__PURE__ */ new Set(); + const links = targets.map((target) => ({ + name: linkNameFor(target, taken), + target + })); await mkdir(root, { recursive: true }); try { await mkdir(path); } catch (error) { - if (error.code === "EEXIST") throw new Error(`farm: already exists, remove it first: ${path}`); - throw new Error(`farm: cannot create ${path}: ${error instanceof Error ? error.message : String(error)}`); - } - const taken = /* @__PURE__ */ new Set(); - const links = []; - try { - for (const target of targets) { - const name = linkNameFor(target, taken); - await symlink(target, join(path, name), WINDOWS ? "junction" : "dir"); - links.push({ - name, - target - }); + if (error.code !== "EEXIST") throw new Error(`farm: cannot create ${path}: ${error instanceof Error ? error.message : String(error)}`); + const existing = (await readdir(path)).sort(); + const expected = links.map((link) => link.name).sort(); + if (existing.length !== expected.length || existing.some((name, index) => name !== expected[index])) throw new Error(`farm: already exists with different folders: ${path}`); + for (const link of links) { + const entryPath = join(path, link.name); + if (!(await lstat(entryPath)).isSymbolicLink()) throw new Error(`farm: already exists, entry is not a link: ${entryPath}`); + const actual = await realpath(entryPath); + if (!(WINDOWS ? actual.toLowerCase() === link.target.toLowerCase() : actual === link.target)) throw new Error(`farm: already exists, link points elsewhere: ${entryPath}`); } + return { + path, + links + }; + } + try { + for (const link of links) await symlink(link.target, join(path, link.name), WINDOWS ? "junction" : "dir"); } catch (error) { await discard(path); throw new Error(`farm: cannot link into ${path}: ${error instanceof Error ? error.message : String(error)}`); diff --git a/package.json b/package.json index 0580996..2628763 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "workspace-groups-remote", - "version": "1.2.0", + "version": "1.2.1", "description": "Merged DSH bundle: the workspace-groups sidebar manager (category -> project -> session grouped tree, session tab strip) plus dsh-remote-ssh (VSCode Remote-SSH-like remote development: remote workspaces, file access, integrated terminal) in one installable package.", "keywords": [ "dsh", diff --git a/src/host-farm.ts b/src/host-farm.ts index c004bac..8e9ed11 100644 --- a/src/host-farm.ts +++ b/src/host-farm.ts @@ -151,22 +151,38 @@ export async function buildFarm(input: { name: string; folders: readonly unknown const root = defaultFarmRoot() const path = join(root, farmDirectoryName(String(input.name))) + const taken = new Set() + const links: FarmLink[] = targets.map(target => ({ name: linkNameFor(target, taken), target })) await mkdir(root, { recursive: true }) try { await mkdir(path) } catch (error) { const code = (error as { code?: string }).code - if (code === 'EEXIST') throw new Error(`farm: already exists, remove it first: ${path}`) - throw new Error(`farm: cannot create ${path}: ${error instanceof Error ? error.message : String(error)}`) + if (code !== 'EEXIST') { + throw new Error(`farm: cannot create ${path}: ${error instanceof Error ? error.message : String(error)}`) + } + // Repeating the same gather is harmless: the workspace may already own this + // farm. Reuse it only when every entry is the same link; anything else is a + // real conflict and must not be silently merged into. + const existing = (await readdir(path)).sort() + const expected = links.map(link => link.name).sort() + if (existing.length !== expected.length || existing.some((name, index) => name !== expected[index])) { + throw new Error(`farm: already exists with different folders: ${path}`) + } + for (const link of links) { + const entryPath = join(path, link.name) + const info = await lstat(entryPath) + if (!info.isSymbolicLink()) throw new Error(`farm: already exists, entry is not a link: ${entryPath}`) + const actual = await realpath(entryPath) + const same = WINDOWS ? actual.toLowerCase() === link.target.toLowerCase() : actual === link.target + if (!same) throw new Error(`farm: already exists, link points elsewhere: ${entryPath}`) + } + return { path, links } } - const taken = new Set() - const links: FarmLink[] = [] try { - for (const target of targets) { - const name = linkNameFor(target, taken) - await symlink(target, join(path, name), WINDOWS ? 'junction' : 'dir') - links.push({ name, target }) + for (const link of links) { + await symlink(link.target, join(path, link.name), WINDOWS ? 'junction' : 'dir') } } catch (error) { await discard(path) diff --git a/tests/farm.test.ts b/tests/farm.test.ts index fd5b148..5ad82ff 100644 --- a/tests/farm.test.ts +++ b/tests/farm.test.ts @@ -98,13 +98,24 @@ describe('farm validation', () => { .rejects.toThrow(/does not exist/u) }) - it('refuses to reuse an existing farm', async () => { + it('reuses an existing farm with the same folders', async () => { const alpha = await folder('alpha') const beta = await folder('beta') - await buildFarm({ name: 'same', folders: [alpha, beta] }) + const first = await buildFarm({ name: 'same', folders: [alpha, beta] }) - await expect(buildFarm({ name: 'same', folders: [alpha, beta] })) - .rejects.toThrow(/already exists/u) + const again = await buildFarm({ name: 'same', folders: [alpha, beta] }) + expect(again.path).toBe(first.path) + expect(again.links).toEqual(first.links) + }) + + it('refuses to reuse a farm whose folders changed', async () => { + const alpha = await folder('alpha') + const beta = await folder('beta') + const gamma = await folder('gamma') + await buildFarm({ name: 'same-different', folders: [alpha, beta] }) + + await expect(buildFarm({ name: 'same-different', folders: [alpha, gamma] })) + .rejects.toThrow(/different folders/u) }) }) -- 2.43.0