@@ -35,9 +35,23 @@ function cleanupBrokenSymlinks(dirPath: string): void {
3535 for ( const entry of entries ) {
3636 const fullPath = path . join ( dirPath , entry . name )
3737 try {
38- if ( entry . isSymbolicLink ( ) && ! existsSync ( fullPath ) ) {
39- // Symlink exists but target does not, remove it.
40- rmSync ( fullPath , { force : true } )
38+ if ( entry . isSymbolicLink ( ) ) {
39+ // Check if symlink target exists.
40+ try {
41+ lstatSync ( fullPath )
42+ // Symlink itself exists, now check if target exists.
43+ if ( ! existsSync ( fullPath ) ) {
44+ // Symlink exists but target does not, remove it.
45+ rmSync ( fullPath , { force : true } )
46+ }
47+ } catch {
48+ // If lstat fails, the symlink is broken, try to remove it.
49+ try {
50+ rmSync ( fullPath , { force : true } )
51+ } catch {
52+ // Ignore removal errors.
53+ }
54+ }
4155 } else if ( entry . isDirectory ( ) ) {
4256 // Recursively check subdirectories.
4357 cleanupBrokenSymlinks ( fullPath )
@@ -54,7 +68,18 @@ function cleanupBrokenSymlinks(dirPath: string): void {
5468// Clean up broken symlinks before loading node_modules.
5569cleanupBrokenSymlinks ( rootNmPath )
5670
57- const mockedNmCallback = mockFs . load ( rootNmPath )
71+ // Load node_modules with error handling for any remaining issues.
72+ const mockedNmCallback = ( ( ) => {
73+ try {
74+ return mockFs . load ( rootNmPath )
75+ } catch ( e ) {
76+ // If loading fails due to broken symlinks or missing files, return empty mock.
77+ console . warn (
78+ `Warning: Failed to load node_modules for mock-fs: ${ e instanceof Error ? e . message : String ( e ) } ` ,
79+ )
80+ return { }
81+ }
82+ } ) ( )
5883
5984function mockTestFs ( config : FileSystem . DirectoryItems ) {
6085 return mockFs ( {
0 commit comments