@@ -8,6 +8,7 @@ import * as Clock from "effect/Clock";
88import * as Data from "effect/Data" ;
99import * as Effect from "effect/Effect" ;
1010import * as FileSystem from "effect/FileSystem" ;
11+ import * as Option from "effect/Option" ;
1112import * as Path from "effect/Path" ;
1213
1314import { HostProcessEnvironment , HostProcessPlatform } from "./hostProcess.ts" ;
@@ -512,32 +513,58 @@ export const CommandResolutionCache = Context.Reference<Map<string, CommandResol
512513 } ,
513514) ;
514515
515- // A missing directory holds nothing. Any other listing failure (permissions, a
516- // busy share) yields undefined so lookups probe that directory's candidates
517- // directly, since a known file can still be reachable there.
518- const listPathDirectory = ( directory : string ) =>
519- FileSystem . FileSystem . use ( ( fileSystem ) => fileSystem . readDirectory ( directory ) ) . pipe (
520- Effect . map (
521- ( entries ) : ReadonlySet < string > | undefined =>
522- new Set ( entries . map ( ( entry ) => entry . toLowerCase ( ) ) ) ,
523- ) ,
524- Effect . catch ( ( error ) =>
525- Effect . succeed ( error . reason . _tag === "NotFound" ? new Set < string > ( ) : undefined ) ,
526- ) ,
516+ interface PathDirectoryListing {
517+ readonly modified : number | null | undefined ;
518+ /** Lowercased entry names, or undefined to probe candidates directly. */
519+ readonly names : ReadonlySet < string > | undefined ;
520+ }
521+
522+ // The directory's mtime, null when it does not exist, or undefined when it
523+ // cannot be read or dated (permissions, a busy share).
524+ const pathDirectoryModified = ( directory : string ) =>
525+ FileSystem . FileSystem . use ( ( fileSystem ) => fileSystem . stat ( directory ) ) . pipe (
526+ Effect . map ( ( info ) => Option . getOrUndefined ( info . mtime ) ?. getTime ( ) ) ,
527+ Effect . catch ( ( error ) => Effect . succeed ( error . reason . _tag === "NotFound" ? null : undefined ) ) ,
527528 ) ;
528529
530+ // Dated before it is read, so an entry added in between shows up as a changed
531+ // mtime on the next check instead of hiding behind a matching one. A missing
532+ // directory holds nothing; one that cannot be dated or listed yields undefined
533+ // names so lookups probe its candidates directly.
534+ const listPathDirectory = ( directory : string ) =>
535+ Effect . gen ( function * ( ) : Effect . fn . Return < PathDirectoryListing , never , FileSystem . FileSystem > {
536+ const modified = yield * pathDirectoryModified ( directory ) ;
537+ if ( modified === null ) return { modified, names : new Set ( ) } ;
538+ if ( modified === undefined ) return { modified, names : undefined } ;
539+ const entries = yield * FileSystem . FileSystem . use ( ( fileSystem ) =>
540+ fileSystem . readDirectory ( directory ) ,
541+ ) . pipe ( Effect . orElseSucceed ( ( ) => undefined ) ) ;
542+ return { modified, names : entries && new Set ( entries . map ( ( entry ) => entry . toLowerCase ( ) ) ) } ;
543+ } ) ;
544+
529545const PathDirectoryListings = Context . Reference <
530- Cache . Cache < string , ReadonlySet < string > | undefined , never , FileSystem . FileSystem > | undefined
546+ Cache . Cache < string , PathDirectoryListing , never , FileSystem . FileSystem > | undefined
531547> ( "@t3tools/shared/shell/PathDirectoryListings" , { defaultValue : ( ) => undefined } ) ;
532548
549+ // Every use re-checks the directory's mtime and relists it if it changed, so a
550+ // command installed mid-batch is found exactly as a direct probe would find it.
551+ // One directory stat per lookup still replaces a stat per PATHEXT candidate.
552+ const readPathDirectoryNames = Effect . fn ( "shell.readPathDirectoryNames" ) ( function * (
553+ listings : Cache . Cache < string , PathDirectoryListing , never , FileSystem . FileSystem > ,
554+ directory : string ,
555+ ) {
556+ const listing = yield * Cache . get ( listings , directory ) ;
557+ if ( listing . names === undefined ) return undefined ;
558+ if ( ( yield * pathDirectoryModified ( directory ) ) === listing . modified ) return listing . names ;
559+ yield * Cache . invalidate ( listings , directory ) ;
560+ return ( yield * Cache . get ( listings , directory ) ) . names ;
561+ } ) ;
562+
533563/**
534564 * Run a batch of command lookups (such as editor discovery) that lists each
535565 * PATH directory once and only probes names the listing contains, instead of
536566 * stat-ing every PATH x PATHEXT candidate per command. A single lookup is
537- * cheaper without it. Listings are a snapshot for the run: a command installed
538- * into a directory after the run listed it is missed until the next run, and
539- * that miss is cached like any other. Accepted for the tens of thousands of
540- * stats this saves on long Windows PATHs.
567+ * cheaper without it.
541568 */
542569export const withPathDirectoryListings = < A , E , R > ( effect : Effect . Effect < A , E , R > ) =>
543570 Effect . gen ( function * ( ) {
@@ -642,7 +669,8 @@ const resolveCommandPathForPlatform = Effect.fn("shell.resolveCommandPathForPlat
642669
643670 const listings = yield * PathDirectoryListings ;
644671 for ( const pathEntry of pathEntries ) {
645- const names = listings === undefined ? undefined : yield * Cache . get ( listings , pathEntry ) ;
672+ const names =
673+ listings === undefined ? undefined : yield * readPathDirectoryNames ( listings , pathEntry ) ;
646674 for ( const candidate of commandCandidates ) {
647675 // Listings are lowercased; the stat below still checks the exact
648676 // spelling for case-sensitive directories and rejects non-files.
0 commit comments