@@ -374,13 +374,13 @@ export function writeFileAndFlush(path: string, data: string | NodeBuffer, optio
374374/**
375375 * Copied from: https://github.com/Microsoft/vscode-node-debug/blob/master/src/node/pathUtilities.ts#L83
376376 *
377- * Given an absolute, normalized, and existing file path 'realpath ' returns the exact path that the file has on disk.
377+ * Given an absolute, normalized, and existing file path 'realcase ' returns the exact path that the file has on disk.
378378 * On a case insensitive file system, the returned path might differ from the original path by character casing.
379379 * On a case sensitive file system, the returned path will always be identical to the original path.
380380 * In case of errors, null is returned. But you cannot use this function to verify that a path exists.
381- * realpathSync does not handle '..' or '.' path segments and it does not take the locale into account.
381+ * realcaseSync does not handle '..' or '.' path segments and it does not take the locale into account.
382382 */
383- export function realpathSync ( path : string ) : string {
383+ export function realcaseSync ( path : string ) : string {
384384 const dir = paths . dirname ( path ) ;
385385 if ( path === dir ) { // end recursion
386386 return path ;
@@ -392,15 +392,15 @@ export function realpathSync(path: string): string {
392392 const found = entries . filter ( e => e . toLowerCase ( ) === name ) ; // use a case insensitive search
393393 if ( found . length === 1 ) {
394394 // on a case sensitive filesystem we cannot determine here, whether the file exists or not, hence we need the 'file exists' precondition
395- const prefix = realpathSync ( dir ) ; // recurse
395+ const prefix = realcaseSync ( dir ) ; // recurse
396396 if ( prefix ) {
397397 return paths . join ( prefix , found [ 0 ] ) ;
398398 }
399399 } else if ( found . length > 1 ) {
400400 // must be a case sensitive $filesystem
401401 const ix = found . indexOf ( name ) ;
402402 if ( ix >= 0 ) { // case sensitive
403- const prefix = realpathSync ( dir ) ; // recurse
403+ const prefix = realcaseSync ( dir ) ; // recurse
404404 if ( prefix ) {
405405 return paths . join ( prefix , found [ ix ] ) ;
406406 }
@@ -411,4 +411,44 @@ export function realpathSync(path: string): string {
411411 }
412412
413413 return null ;
414+ }
415+
416+ export function realpathSync ( path : string ) : string {
417+ try {
418+ return fs . realpathSync ( path ) ;
419+ } catch ( error ) {
420+
421+ // We hit an error calling fs.realpathSync(). Since fs.realpathSync() is doing some path normalization
422+ // we now do a similar normalization and then try again if we can access the path with read
423+ // permissions at least. If that succeeds, we return that path.
424+ // fs.realpath() is resolving symlinks and that can fail in certain cases. The workaround is
425+ // to not resolve links but to simply see if the path is read accessible or not.
426+ const normalizedPath = normalizePath ( path ) ;
427+ fs . accessSync ( normalizedPath , fs . constants . R_OK ) ; // throws in case of an error
428+
429+ return normalizedPath ;
430+ }
431+ }
432+
433+ export function realpath ( path : string , callback : ( error : Error , realpath : string ) => void ) : void {
434+ return fs . realpath ( path , ( error , realpath ) => {
435+ if ( ! error ) {
436+ return callback ( null , realpath ) ;
437+ }
438+
439+ // We hit an error calling fs.realpath(). Since fs.realpath() is doing some path normalization
440+ // we now do a similar normalization and then try again if we can access the path with read
441+ // permissions at least. If that succeeds, we return that path.
442+ // fs.realpath() is resolving symlinks and that can fail in certain cases. The workaround is
443+ // to not resolve links but to simply see if the path is read accessible or not.
444+ const normalizedPath = normalizePath ( path ) ;
445+
446+ return fs . access ( normalizedPath , fs . constants . R_OK , error => {
447+ return callback ( error , normalizedPath ) ;
448+ } ) ;
449+ } ) ;
450+ }
451+
452+ function normalizePath ( path : string ) : string {
453+ return strings . rtrim ( paths . normalize ( path ) , paths . sep ) ;
414454}
0 commit comments