@@ -121,12 +121,12 @@ async function resolveSpawnExecutableWithNode(
121121 const emptyComponent = pathEntry === "" && pathValue !== "" && platform !== "win32" ;
122122 const normalizedPathEntry = emptyComponent ? "." : normalizePathEntry ( pathEntry , platform ) ;
123123 if ( normalizedPathEntry . length === 0 ) continue ;
124- const names =
124+ const listing =
125125 platform === "win32"
126126 ? await listResolverPathDirectory ( normalizedPathEntry , path . isAbsolute )
127127 : undefined ;
128128 for ( const candidate of candidates ) {
129- if ( isAbsentFromListing ( names , candidate ) ) continue ;
129+ if ( isAbsentFromListing ( listing ?. names , candidate ) ) continue ;
130130 const candidatePath = path . join ( normalizedPathEntry , candidate ) ;
131131 // Anchored before the probe: callers may launch it from another cwd or through a wrapper,
132132 // and the cwd can change while the probe waits.
@@ -138,7 +138,10 @@ async function resolveSpawnExecutableWithNode(
138138 continue ;
139139 }
140140 }
141- if ( await isExecutable ( probePath ) ) return probePath ;
141+ if ( await isExecutable ( probePath ) ) {
142+ const cased = listedCandidateName ( listing ?. casedNames , candidate ) ;
143+ return cased === candidate ? probePath : path . join ( normalizedPathEntry , cased ) ;
144+ }
142145 }
143146 }
144147 return undefined ;
@@ -643,6 +646,8 @@ interface PathDirectoryListing {
643646 readonly modified : number | null | undefined ;
644647 /** Lowercased entry names; undefined when the directory cannot be listed. */
645648 readonly names : ReadonlySet < string > | undefined ;
649+ /** Entry names as the directory spells them; undefined with `names`. */
650+ readonly casedNames : ReadonlySet < string > | undefined ;
646651 readonly reusable : boolean ;
647652}
648653
@@ -664,6 +669,20 @@ function isAbsentFromListing(names: ReadonlySet<string> | undefined, candidate:
664669 ) ;
665670}
666671
672+ // A case-folded probe can succeed under a spelling the directory does not hold.
673+ function listedCandidateName (
674+ casedNames : ReadonlySet < string > | undefined ,
675+ candidate : string ,
676+ ) : string {
677+ if ( ! casedNames || casedNames . has ( candidate ) || ! LISTED_NAME_PATTERN . test ( candidate ) ) {
678+ return candidate ;
679+ }
680+ for ( const entry of casedNames ) {
681+ if ( entry . toLowerCase ( ) === candidate . toLowerCase ( ) ) return entry ;
682+ }
683+ return candidate ;
684+ }
685+
667686// The listing must start after the mtime's timestamp step has passed, and a relative entry
668687// names another directory once the working directory changes.
669688function isReusableListing (
@@ -690,16 +709,24 @@ const listPathDirectory = Effect.fnUntraced(function* (
690709 const path = yield * Path . Path ;
691710 const modified = yield * directoryMtime ( directory ) ;
692711 if ( modified === null ) {
693- return { modified, names : NO_DIRECTORY_NAMES , reusable : path . isAbsolute ( directory ) } ;
712+ return {
713+ modified,
714+ names : NO_DIRECTORY_NAMES ,
715+ casedNames : NO_DIRECTORY_NAMES ,
716+ reusable : path . isAbsolute ( directory ) ,
717+ } ;
718+ }
719+ if ( modified === undefined ) {
720+ return { modified, names : undefined , casedNames : undefined , reusable : false } ;
694721 }
695- if ( modified === undefined ) return { modified, names : undefined , reusable : false } ;
696722 const listedAtMs = yield * Clock . currentTimeMillis ;
697723 const entries = yield * FileSystem . FileSystem . use ( ( fileSystem ) =>
698724 fileSystem . readDirectory ( directory ) ,
699725 ) . pipe ( Effect . orElseSucceed ( ( ) => undefined ) ) ;
700726 return {
701727 modified,
702728 names : entries && new Set ( entries . map ( ( entry ) => entry . toLowerCase ( ) ) ) ,
729+ casedNames : entries && new Set ( entries ) ,
703730 reusable :
704731 entries !== undefined && isReusableListing ( directory , modified , listedAtMs , path . isAbsolute ) ,
705732 } ;
@@ -718,33 +745,44 @@ export const PathDirectoryListings = Context.Reference(
718745 { defaultValue : ( ) => Effect . runSync ( makePathDirectoryListings ) } ,
719746) ;
720747
748+ interface ResolverPathDirectoryListing {
749+ readonly names : ReadonlySet < string > ;
750+ readonly casedNames : ReadonlySet < string > ;
751+ }
752+
721753const resolverPathDirectoryListings = new Map <
722754 string ,
723- { readonly modified : number ; readonly names : ReadonlySet < string > }
755+ { readonly modified : number ; readonly listing : ResolverPathDirectoryListing }
724756> ( ) ;
725757
726758// The spawn resolver's counterpart of `PathDirectoryListings`, under the same rules.
727759async function listResolverPathDirectory (
728760 directory : string ,
729761 isAbsolute : ( path : string ) => boolean ,
730- ) : Promise < ReadonlySet < string > | undefined > {
762+ ) : Promise < ResolverPathDirectoryListing | undefined > {
731763 let modified : number ;
732764 try {
733765 const stats = await NodeFS . promises . stat ( directory ) ;
734766 if ( ! stats . isDirectory ( ) ) return undefined ;
735767 modified = stats . mtimeMs ;
736768 } catch ( error ) {
737- return ( error as NodeJS . ErrnoException ) . code === "ENOENT" ? NO_DIRECTORY_NAMES : undefined ;
769+ return ( error as NodeJS . ErrnoException ) . code === "ENOENT"
770+ ? { names : NO_DIRECTORY_NAMES , casedNames : NO_DIRECTORY_NAMES }
771+ : undefined ;
738772 }
739773 const cached = resolverPathDirectoryListings . get ( directory ) ;
740- if ( cached ?. modified === modified ) return cached . names ;
774+ if ( cached ?. modified === modified ) return cached . listing ;
741775 resolverPathDirectoryListings . delete ( directory ) ;
742776 // This resolver runs outside Effect, so it has no Clock to read.
743777 // @effect -diagnostics-next-line globalDate:off
744778 const listedAtMs = Date . now ( ) ;
745- let names : ReadonlySet < string > ;
779+ let listing : ResolverPathDirectoryListing ;
746780 try {
747- names = new Set ( ( await NodeFS . promises . readdir ( directory ) ) . map ( ( entry ) => entry . toLowerCase ( ) ) ) ;
781+ const entries = await NodeFS . promises . readdir ( directory ) ;
782+ listing = {
783+ names : new Set ( entries . map ( ( entry ) => entry . toLowerCase ( ) ) ) ,
784+ casedNames : new Set ( entries ) ,
785+ } ;
748786 } catch {
749787 return undefined ;
750788 }
@@ -753,9 +791,9 @@ async function listResolverPathDirectory(
753791 const oldest = resolverPathDirectoryListings . keys ( ) . next ( ) . value ;
754792 if ( oldest !== undefined ) resolverPathDirectoryListings . delete ( oldest ) ;
755793 }
756- resolverPathDirectoryListings . set ( directory , { modified, names } ) ;
794+ resolverPathDirectoryListings . set ( directory , { modified, listing } ) ;
757795 }
758- return names ;
796+ return listing ;
759797}
760798
761799// An injected resolver or locator may answer differently for the same search, so its
@@ -881,8 +919,15 @@ const findOnPath = Effect.fnUntraced(function* (
881919 ? path . join ( cwd , candidate )
882920 : path . join ( pathEntry , candidate ) ;
883921 if ( yield * isExecutableFile ( match , platform , windowsPathExtensions ) ) {
884- matches . push ( match ) ;
885- if ( isLast ( match ) ) return matches ;
922+ const cased = listedCandidateName ( listing ?. casedNames , candidate ) ;
923+ const returned =
924+ cased === candidate
925+ ? match
926+ : anchorsCwd && pathEntry === "."
927+ ? path . join ( cwd , cased )
928+ : path . join ( pathEntry , cased ) ;
929+ matches . push ( returned ) ;
930+ if ( isLast ( returned ) ) return matches ;
886931 break ;
887932 }
888933 }
0 commit comments