diff --git a/src/fs-operations.ts b/src/fs-operations.ts index 51b309c4..68d58c47 100644 --- a/src/fs-operations.ts +++ b/src/fs-operations.ts @@ -158,13 +158,19 @@ function sameSpelling(a: string, b: string): boolean { * drive. Each of them walked straight past this guard and turned "delete this * row" into a recursive delete of the whole project. `dev`+`ino` is the * identity the filesystem itself uses, and it does not care how the path is - * spelled. `stat` (not `lstat`) is deliberate: a symlink aimed AT the root is - * refused too — no link is worth the project behind it. A target that cannot - * be stat'ed (it may legitimately be gone) falls back to the spelling. + * spelled. + * + * `lstat` (not `stat`) is deliberate, and so is the removal below using the + * same call: the question is whether THIS ENTRY is the root, so a symlink is + * its own identity, never its target's. A link pointed at the root therefore + * stays deletable — only the link goes, no recursion — which is what the + * string compare did. The spellings above, the actual way a project was lost, + * are all still refused. A target that cannot be lstat'ed (it may legitimately + * be gone) falls back to the spelling. */ async function isWorkspaceRoot(target: string, root: string): Promise { try { - const [entry, base] = await Promise.all([stat(target), stat(root)]) + const [entry, base] = await Promise.all([lstat(target), lstat(root)]) // No inode = no identity: fall back to the spelling. if (entry.ino === 0 || base.ino === 0) return sameSpelling(target, root) return entry.dev === base.dev && entry.ino === base.ino diff --git a/tests/fs-operations.spec.ts b/tests/fs-operations.spec.ts index 42fe975f..60e80bec 100644 --- a/tests/fs-operations.spec.ts +++ b/tests/fs-operations.spec.ts @@ -275,16 +275,16 @@ describe('removeWorkspaceEntry', () => { .rejects.toMatchObject({ code: 'fs-error' }) }) - it.runIf(canSymlink)('refuses a symlink aimed AT the root', async () => { - // The guard resolves the target's identity on purpose: this link stands - // for the project itself, so it is refused instead of unlinked. + it.runIf(canSymlink)('unlinks a symlink aimed AT the root, leaving the project intact', async () => { + // The guard compares the ENTRY's own identity (the same `lstat` the + // removal below uses), never what the link resolves to: the link goes and + // the project behind it stays — the behaviour the string compare had. const { ws, inner } = makeProject() symlinkSync(inner, join(ws, 'link-to-proj')) try { - await expect(removeWorkspaceEntry({ cwd: inner, path: join(ws, 'link-to-proj') })) - .rejects.toMatchObject({ code: 'fs-error' }) + await removeWorkspaceEntry({ cwd: inner, path: join(ws, 'link-to-proj') }) + expect(existsSync(join(ws, 'link-to-proj'))).toBe(false) expect(existsSync(join(inner, 'src', 'keep.ts'))).toBe(true) - expect(existsSync(join(ws, 'link-to-proj'))).toBe(true) } finally { rmSync(ws, { recursive: true, force: true }) }