fix(farm): 重复生成复用已有 farm,不再 HTTP 400 #7
|
|
@ -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
|
## 1.2.0 — 2026-11-12
|
||||||
|
|
||||||
### 新增
|
### 新增
|
||||||
|
|
|
||||||
|
|
@ -3454,24 +3454,32 @@ async function buildFarm(input) {
|
||||||
if (targets.length < 2) throw new Error("farm: needs at least two distinct folders");
|
if (targets.length < 2) throw new Error("farm: needs at least two distinct folders");
|
||||||
const root = defaultFarmRoot();
|
const root = defaultFarmRoot();
|
||||||
const path = join(root, farmDirectoryName(String(input.name)));
|
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 });
|
await mkdir(root, { recursive: true });
|
||||||
try {
|
try {
|
||||||
await mkdir(path);
|
await mkdir(path);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
if (error.code === "EEXIST") throw new Error(`farm: already exists, remove it first: ${path}`);
|
if (error.code !== "EEXIST") throw new Error(`farm: cannot create ${path}: ${error instanceof Error ? error.message : String(error)}`);
|
||||||
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
|
||||||
|
};
|
||||||
}
|
}
|
||||||
const taken = /* @__PURE__ */ new Set();
|
|
||||||
const links = [];
|
|
||||||
try {
|
try {
|
||||||
for (const target of targets) {
|
for (const link of links) await symlink(link.target, join(path, link.name), WINDOWS ? "junction" : "dir");
|
||||||
const name = linkNameFor(target, taken);
|
|
||||||
await symlink(target, join(path, name), WINDOWS ? "junction" : "dir");
|
|
||||||
links.push({
|
|
||||||
name,
|
|
||||||
target
|
|
||||||
});
|
|
||||||
}
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
await discard(path);
|
await discard(path);
|
||||||
throw new Error(`farm: cannot link into ${path}: ${error instanceof Error ? error.message : String(error)}`);
|
throw new Error(`farm: cannot link into ${path}: ${error instanceof Error ? error.message : String(error)}`);
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"name": "workspace-groups-remote",
|
"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.",
|
"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": [
|
"keywords": [
|
||||||
"dsh",
|
"dsh",
|
||||||
|
|
|
||||||
|
|
@ -151,22 +151,38 @@ export async function buildFarm(input: { name: string; folders: readonly unknown
|
||||||
|
|
||||||
const root = defaultFarmRoot()
|
const root = defaultFarmRoot()
|
||||||
const path = join(root, farmDirectoryName(String(input.name)))
|
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 })
|
await mkdir(root, { recursive: true })
|
||||||
try {
|
try {
|
||||||
await mkdir(path)
|
await mkdir(path)
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
const code = (error as { code?: string }).code
|
const code = (error as { code?: string }).code
|
||||||
if (code === 'EEXIST') throw new Error(`farm: already exists, remove it first: ${path}`)
|
if (code !== 'EEXIST') {
|
||||||
throw new Error(`farm: cannot create ${path}: ${error instanceof Error ? error.message : String(error)}`)
|
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 {
|
try {
|
||||||
for (const target of targets) {
|
for (const link of links) {
|
||||||
const name = linkNameFor(target, taken)
|
await symlink(link.target, join(path, link.name), WINDOWS ? 'junction' : 'dir')
|
||||||
await symlink(target, join(path, name), WINDOWS ? 'junction' : 'dir')
|
|
||||||
links.push({ name, target })
|
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
await discard(path)
|
await discard(path)
|
||||||
|
|
|
||||||
|
|
@ -98,13 +98,24 @@ describe('farm validation', () => {
|
||||||
.rejects.toThrow(/does not exist/u)
|
.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 alpha = await folder('alpha')
|
||||||
const beta = await folder('beta')
|
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] }))
|
const again = await buildFarm({ name: 'same', folders: [alpha, beta] })
|
||||||
.rejects.toThrow(/already exists/u)
|
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)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue