fix(farm): 重复生成复用已有 farm,不再 HTTP 400 #7

Merged
newbie merged 1 commits from feat/farm-mounts into main 2026-11-12 22:41:39 +08:00
5 changed files with 66 additions and 26 deletions

View File

@ -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
### 新增

View File

@ -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)}`);

View File

@ -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",

View File

@ -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<string>()
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<string>()
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)

View File

@ -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)
})
})