@@ -32,21 +32,31 @@ try {
3232 const dependencyPath = join ( nodeModulesPath , dependency ) ;
3333
3434 try {
35- const stat = await fs . stat ( dependencyPath ) ;
36- if ( stat . isSymbolicLink ( ) ) {
37- const linkPath = await fs . readlink ( dependencyPath ) ;
38- if ( linkPath === path ) {
39- // already done
40- continue ;
35+ try {
36+ const stat = await fs . lstat ( dependencyPath ) ;
37+ console . log ( `Existing is ${ stat . isSymbolicLink ( ) ? "symlink" : "directory" } ` ) ;
38+ if ( stat . isSymbolicLink ( ) ) {
39+ const linkPath = await fs . readlink ( dependencyPath ) ;
40+ if ( linkPath === path ) {
41+ // already done
42+ continue ;
43+ } else {
44+ await fs . unlink ( dependencyPath ) ;
45+ }
4146 } else {
42- await fs . unlink ( dependencyPath ) ;
47+ await fs . rm ( dependencyPath , { recursive : true } ) ;
48+ }
49+ } catch ( e : any ) {
50+ // fs.lstat throws ENOENT if the path doesn't exist (on Windows)
51+ if ( e . code === "ENOENT" ) {
52+ console . log ( "Received ENOENT error on dependency path - assuming it doesn't exist" ) ;
53+ } else {
54+ throw e ;
4355 }
44- } else {
45- await fs . rm ( dependencyPath , { recursive : true } ) ;
4656 }
4757
4858 console . log ( `Linking ${ dependency } to ${ path } ` ) ;
49- await fs . symlink ( path , dependencyPath ) ;
59+ await fs . symlink ( path , dependencyPath , "junction" ) ; // use a junction type to avoid EPERM errors on Windows
5060
5161 const pkgJson = await fs . readFile ( join ( path , "package.json" ) , "utf-8" ) ;
5262 const pkgManager = JSON . parse ( pkgJson ) [ "packageManager" ] ?. split ( "@" ) . at ( 0 ) ?? "yarn" ;
0 commit comments