@@ -549,11 +549,11 @@ export const withPathDirectoryListings =
549549 return yield * effect . pipe ( Effect . provideService ( PathDirectoryListings , listings ) ) ;
550550 } ) ;
551551
552- // An injected resolver may answer differently for the same search, so its
552+ // An injected resolver or locator may answer differently for the same search, so its
553553// entries are kept apart from every other resolver's.
554554let spawnResolverCacheIdCount = 0 ;
555- const spawnResolverCacheIds = new WeakMap < SpawnExecutableResolver , number > ( ) ;
556- function spawnResolverCacheId ( resolver : SpawnExecutableResolver ) : number {
555+ const spawnResolverCacheIds = new WeakMap < SpawnExecutableResolver | XcodeGitLocator , number > ( ) ;
556+ function spawnResolverCacheId ( resolver : SpawnExecutableResolver | XcodeGitLocator ) : number {
557557 const known = spawnResolverCacheIds . get ( resolver ) ;
558558 if ( known !== undefined ) return known ;
559559 const id = spawnResolverCacheIdCount ++ ;
@@ -765,6 +765,71 @@ export function npmShimExecutable(
765765 return isFile ( executable ) ? executable : undefined ;
766766}
767767
768+ export type XcodeGitLocator = ( env : NodeJS . ProcessEnv ) => string | undefined ;
769+
770+ // xcrun honors DEVELOPER_DIR and the `xcode-select` choice, as the shim does.
771+ function locateXcodeGitWithXcrun ( env : NodeJS . ProcessEnv ) : string | undefined {
772+ try {
773+ const located = NodeChildProcess . execFileSync ( "/usr/bin/xcrun" , [ "--find" , "git" ] , {
774+ encoding : "utf8" ,
775+ env,
776+ timeout : 5_000 ,
777+ stdio : [ "ignore" , "pipe" , "ignore" ] ,
778+ } ) . trim ( ) ;
779+ return NodePath . isAbsolute ( located ) ? located : undefined ;
780+ } catch {
781+ return undefined ;
782+ }
783+ }
784+
785+ export const XcodeGitLocation = Context . Reference < XcodeGitLocator > (
786+ "@t3tools/shared/shell/XcodeGitLocation" ,
787+ { defaultValue : ( ) => locateXcodeGitWithXcrun } ,
788+ ) ;
789+
790+ const MACOS_GIT_SHIM = "/usr/bin/git" ;
791+
792+ /**
793+ * /usr/bin/git is an xcrun shim that finds the active developer directory on
794+ * every run: about 8 ms per spawn, against under 3 ms for the git behind it.
795+ * Only the shim is replaced, so a git the user put first on PATH (Homebrew,
796+ * Nix) still wins, and git's own children keep the same PATH. Any lookup
797+ * failure keeps plain `git`.
798+ */
799+ const resolveMacosGit = Effect . fnUntraced ( function * (
800+ env : NodeJS . ProcessEnv ,
801+ resolveExecutable : SpawnExecutableResolver ,
802+ ) {
803+ const locateXcodeGit = yield * XcodeGitLocation ;
804+ const cache = yield * CommandResolutionCache ;
805+ const cacheKey = [
806+ "xcode-git" ,
807+ String ( spawnResolverCacheId ( resolveExecutable ) ) ,
808+ String ( spawnResolverCacheId ( locateXcodeGit ) ) ,
809+ resolvePathEnvironmentVariable ( env ) ,
810+ env . DEVELOPER_DIR ?? "" ,
811+ ] . join ( COMMAND_RESOLUTION_CACHE_KEY_SEPARATOR ) ;
812+ const nowNanos = yield * Clock . currentTimeNanos ;
813+ const cached = cache . get ( cacheKey ) ;
814+ // An Xcode update or removal deletes the binary; look again instead of failing the spawn.
815+ if (
816+ cached !== undefined &&
817+ cached . expiresAtNanos > nowNanos &&
818+ ( cached . resolvedPath === null ||
819+ resolveExecutable ( cached . resolvedPath , "darwin" , env ) !== undefined )
820+ ) {
821+ return cached . resolvedPath ?? "git" ;
822+ }
823+ const located =
824+ resolveExecutable ( "git" , "darwin" , env ) === MACOS_GIT_SHIM ? locateXcodeGit ( env ) : undefined ;
825+ const realGit =
826+ located !== undefined && resolveExecutable ( located , "darwin" , env ) !== undefined
827+ ? located
828+ : null ;
829+ cacheCommandResolution ( cache , cacheKey , realGit , nowNanos ) ;
830+ return realGit ?? "git" ;
831+ } ) ;
832+
768833export interface SpawnCommandOptions extends CommandAvailabilityOptions {
769834 /** Run the executable behind an npm cmd-shim directly (see `npmShimExecutable`). */
770835 readonly bypassNpmShim ?: boolean ;
@@ -776,7 +841,8 @@ export const resolveSpawnCommand = Effect.fnUntraced(function* (
776841 options : SpawnCommandOptions = { } ,
777842) : Effect . fn . Return < ResolvedSpawnCommand > {
778843 const platform = yield * HostProcess . Platform ;
779- if ( platform !== "win32" ) {
844+ const isMacosGit = platform === "darwin" && command === "git" ;
845+ if ( platform !== "win32" && ! isMacosGit ) {
780846 return { command, args : [ ...args ] , shell : false } ;
781847 }
782848
@@ -788,6 +854,13 @@ export const resolveSpawnCommand = Effect.fnUntraced(function* (
788854 ? { ...hostEnvironment , ...options . env }
789855 : options . env ;
790856 const resolveExecutable = yield * SpawnExecutableResolution ;
857+ if ( isMacosGit ) {
858+ return {
859+ command : yield * resolveMacosGit ( env , resolveExecutable ) ,
860+ args : [ ...args ] ,
861+ shell : false ,
862+ } ;
863+ }
791864 // The scan is synchronous and runs before every child process, so it shares
792865 // the PATH scan cache above. Explicit paths stay uncached for the same reason,
793866 // and so do misses: a failed spawn is how providers report "not installed",
0 commit comments