Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 6 additions & 22 deletions packages/next/src/lib/recursive-delete.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Dirent } from 'node:fs'
import * as fs from 'node:fs'
import { join, isAbsolute, dirname } from 'node:path'
import { join } from 'node:path'
import isError from './is-error'
import { wait } from './wait'

Expand Down Expand Up @@ -90,33 +90,17 @@ export async function recursiveDeleteSyncWithAsyncRetries(
await Promise.all(
result.map(async (part: Dirent) => {
const absolutePath = join(dir, part.name)

// readdir does not follow symbolic links
// if part is a symbolic link, follow it using stat
let isDirectory = part.isDirectory()
const isSymlink = part.isSymbolicLink()

if (isSymlink) {
const linkPath = fs.readlinkSync(absolutePath)

try {
const stats = fs.statSync(
isAbsolute(linkPath)
? linkPath
: join(dirname(absolutePath), linkPath)
)
isDirectory = stats.isDirectory()
} catch {}
}

const pp = join(previousPath, part.name)
const isNotExcluded = !exclude || !exclude.test(pp)

if (isNotExcluded) {
if (!isSymlink && isDirectory) {
// Note: readdir does not follow symbolic links, that's good: we want to
// delete the links and not the destination.
let isDirectory = part.isDirectory()
if (isDirectory) {
await recursiveDeleteSyncWithAsyncRetries(absolutePath, exclude, pp)
}
return unlinkPath(absolutePath, !isSymlink && isDirectory)
return unlinkPath(absolutePath, isDirectory)
}
})
)
Expand Down
Loading