Skip to content

Commit e2eee89

Browse files
authored
chore: Remove useless symlink resolution logic in recursive-delete.js (#84582)
If `isSymlink` is `true`, then none of the branches that check for `isDirectory` can be taken, so following symlinks to set `isDirectory` to `true` is useless. This more closely mirrors most recursive directory deletion algorithms out there, which skip symlink resolution entirely. ## History This logic was added in: #27779 And the symlink logic was nerfed in: #82191
1 parent 6ec8065 commit e2eee89

File tree

1 file changed

+6
-22
lines changed

1 file changed

+6
-22
lines changed

packages/next/src/lib/recursive-delete.ts

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { Dirent } from 'node:fs'
22
import * as fs from 'node:fs'
3-
import { join, isAbsolute, dirname } from 'node:path'
3+
import { join } from 'node:path'
44
import isError from './is-error'
55
import { wait } from './wait'
66

@@ -90,33 +90,17 @@ export async function recursiveDeleteSyncWithAsyncRetries(
9090
await Promise.all(
9191
result.map(async (part: Dirent) => {
9292
const absolutePath = join(dir, part.name)
93-
94-
// readdir does not follow symbolic links
95-
// if part is a symbolic link, follow it using stat
96-
let isDirectory = part.isDirectory()
97-
const isSymlink = part.isSymbolicLink()
98-
99-
if (isSymlink) {
100-
const linkPath = fs.readlinkSync(absolutePath)
101-
102-
try {
103-
const stats = fs.statSync(
104-
isAbsolute(linkPath)
105-
? linkPath
106-
: join(dirname(absolutePath), linkPath)
107-
)
108-
isDirectory = stats.isDirectory()
109-
} catch {}
110-
}
111-
11293
const pp = join(previousPath, part.name)
11394
const isNotExcluded = !exclude || !exclude.test(pp)
11495

11596
if (isNotExcluded) {
116-
if (!isSymlink && isDirectory) {
97+
// Note: readdir does not follow symbolic links, that's good: we want to
98+
// delete the links and not the destination.
99+
let isDirectory = part.isDirectory()
100+
if (isDirectory) {
117101
await recursiveDeleteSyncWithAsyncRetries(absolutePath, exclude, pp)
118102
}
119-
return unlinkPath(absolutePath, !isSymlink && isDirectory)
103+
return unlinkPath(absolutePath, isDirectory)
120104
}
121105
})
122106
)

0 commit comments

Comments
 (0)