Skip to content

Commit 72d6758

Browse files
committed
Fix mock-fs test failure caused by broken symlinks
Add cleanup function to remove broken symlinks in node_modules before loading with mock-fs. This prevents ENOENT errors when mock-fs tries to stat broken symlinks during directory traversal. The broken symlinks were in @socketbin packages pointing to non-existent directories, which caused mockFs.load() to fail.
1 parent d8968df commit 72d6758

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

src/utils/path-resolve.test.mts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { existsSync, lstatSync, readdirSync, rmSync } from 'node:fs'
12
import path from 'node:path'
23
import { fileURLToPath } from 'node:url'
34

@@ -23,6 +24,36 @@ const __dirname = path.dirname(__filename)
2324
const rootNmPath = path.join(__dirname, '../..', NODE_MODULES)
2425
const mockFixturePath = normalizePath(path.join(__dirname, 'mock'))
2526
const mockNmPath = normalizePath(rootNmPath)
27+
28+
// Remove broken symlinks in node_modules before loading to prevent mock-fs errors.
29+
function cleanupBrokenSymlinks(dirPath: string): void {
30+
try {
31+
if (!existsSync(dirPath)) {
32+
return
33+
}
34+
const entries = readdirSync(dirPath, { withFileTypes: true })
35+
for (const entry of entries) {
36+
const fullPath = path.join(dirPath, entry.name)
37+
try {
38+
if (entry.isSymbolicLink() && !existsSync(fullPath)) {
39+
// Symlink exists but target does not, remove it.
40+
rmSync(fullPath, { force: true })
41+
} else if (entry.isDirectory()) {
42+
// Recursively check subdirectories.
43+
cleanupBrokenSymlinks(fullPath)
44+
}
45+
} catch {
46+
// Ignore errors for individual entries.
47+
}
48+
}
49+
} catch {
50+
// If we cannot read the directory, skip cleanup.
51+
}
52+
}
53+
54+
// Clean up broken symlinks before loading node_modules.
55+
cleanupBrokenSymlinks(rootNmPath)
56+
2657
const mockedNmCallback = mockFs.load(rootNmPath)
2758

2859
function mockTestFs(config: FileSystem.DirectoryItems) {

0 commit comments

Comments
 (0)