1515 * limitations under the License.
1616 */
1717
18- import type { PathLike , Stats , BigIntStats } from 'fs'
18+ import type { PathLike , Stats , StatSyncOptions , BigIntStats } from 'fs'
1919import type * as FsType from 'fs'
2020import type * as UrlType from 'url'
2121import * as path from 'path'
@@ -677,6 +677,10 @@ export function patcher(roots: string[]): () => void {
677677 } )
678678 }
679679
680+ const symlinkNoThrow : StatSyncOptions = Object . freeze ( {
681+ throwIfNoEntry : false ,
682+ } )
683+
680684 const hopLinkCache = Object . create ( null ) as { [ f : string ] : HopResults }
681685 function readHopLinkSync ( p : string ) : HopResults {
682686 if ( hopLinkCache [ p ] ) {
@@ -685,26 +689,20 @@ export function patcher(roots: string[]): () => void {
685689
686690 let link : HopResults
687691
688- try {
689- if ( origLstatSync ( p ) . isSymbolicLink ( ) ) {
690- link = origReadlinkSync ( p ) as string
691- if ( link ) {
692- if ( ! path . isAbsolute ( link ) ) {
693- link = path . resolve ( path . dirname ( p ) , link )
694- }
695- } else {
696- link = HOP_NON_LINK
692+ const pStats = origLstatSync ( p , symlinkNoThrow )
693+ if ( ! pStats ) {
694+ link = HOP_NOT_FOUND
695+ } else if ( pStats . isSymbolicLink ( ) ) {
696+ link = origReadlinkSync ( p ) as string
697+ if ( link ) {
698+ if ( ! path . isAbsolute ( link ) ) {
699+ link = path . resolve ( path . dirname ( p ) , link )
697700 }
698701 } else {
699702 link = HOP_NON_LINK
700703 }
701- } catch ( err : any ) {
702- if ( err . code === 'ENOENT' ) {
703- // file does not exist
704- link = HOP_NOT_FOUND
705- } else {
706- link = HOP_NON_LINK
707- }
704+ } else {
705+ link = HOP_NON_LINK
708706 }
709707
710708 hopLinkCache [ p ] = link
0 commit comments