@@ -640,6 +640,47 @@ const isExecutableFile = Effect.fnUntraced(function* ( |
640 | 640 | return canExecuteFile(filePath); |
641 | 641 | }); |
642 | 642 |
|
| 643 | +// Called within the traced command resolvers. |
| 644 | +const findOnPath = Effect.fnUntraced(function* ( |
| 645 | + pathValue: string, |
| 646 | + commandCandidates: ReadonlyArray<string>, |
| 647 | + platform: NodeJS.Platform, |
| 648 | + windowsPathExtensions: ReadonlyArray<string>, |
| 649 | + isLast: (match: string) => boolean, |
| 650 | +): Effect.fn.Return<ReadonlyArray<string>, never, FileSystem.FileSystem | Path.Path> { |
| 651 | + const path = yield* Path.Path; |
| 652 | + // Keep case variants: Windows can make PATH directories case-sensitive. |
| 653 | + const pathEntries: string[] = []; |
| 654 | + const seenPathEntries = new Set<string>(); |
| 655 | + for (const entry of pathValue.split(pathDelimiterForPlatform(platform))) { |
| 656 | + const pathEntry = stripWrappingQuotes(entry.trim()); |
| 657 | + if (pathEntry.length === 0 || seenPathEntries.has(pathEntry)) continue; |
| 658 | + |
| 659 | + seenPathEntries.add(pathEntry); |
| 660 | + pathEntries.push(pathEntry); |
| 661 | + } |
| 662 | + |
| 663 | + const listings = yield* PathDirectoryListings; |
| 664 | + const matches: string[] = []; |
| 665 | + for (const pathEntry of pathEntries) { |
| 666 | + let listing = listings && (yield* Cache.get(listings, pathEntry)); |
| 667 | + if (listings && listing?.names && listing.modified !== (yield* directoryMtime(pathEntry))) { |
| 668 | + yield* Cache.invalidate(listings, pathEntry); |
| 669 | + listing = yield* Cache.get(listings, pathEntry); |
| 670 | + } |
| 671 | + for (const candidate of commandCandidates) { |
| 672 | + if (listing?.names && !listing.names.has(candidate.toLowerCase())) continue; |
| 673 | + const candidatePath = path.join(pathEntry, candidate); |
| 674 | + if (yield* isExecutableFile(candidatePath, platform, windowsPathExtensions)) { |
| 675 | + matches.push(candidatePath); |
| 676 | + if (isLast(candidatePath)) return matches; |
| 677 | + break; |
| 678 | + } |
| 679 | + } |
| 680 | + } |
| 681 | + return matches; |
| 682 | +}); |
| 683 | + |
643 | 684 | const resolveCommandPathForPlatform = Effect.fn("shell.resolveCommandPathForPlatform")(function* ( |
644 | 685 | command: string, |
645 | 686 | options: CommandAvailabilityOptions & { readonly platform: NodeJS.Platform }, |
@@ -679,35 +720,15 @@ const resolveCommandPathForPlatform = Effect.fn("shell.resolveCommandPathForPlat |
679 | 720 | return cached.resolvedPath; |
680 | 721 | } |
681 | 722 |
|
682 | | - // Keep case variants: Windows can make PATH directories case-sensitive. |
683 | | - const pathEntries: string[] = []; |
684 | | - const seenPathEntries = new Set<string>(); |
685 | | - for (const entry of pathValue.split(pathDelimiterForPlatform(platform))) { |
686 | | - const pathEntry = stripWrappingQuotes(entry.trim()); |
687 | | - if (pathEntry.length === 0 || seenPathEntries.has(pathEntry)) continue; |
688 | | - |
689 | | - seenPathEntries.add(pathEntry); |
690 | | - pathEntries.push(pathEntry); |
691 | | - } |
692 | | - |
693 | | - const listings = yield* PathDirectoryListings; |
694 | | - for (const pathEntry of pathEntries) { |
695 | | - let listing = listings && (yield* Cache.get(listings, pathEntry)); |
696 | | - if (listings && listing?.names && listing.modified !== (yield* directoryMtime(pathEntry))) { |
697 | | - yield* Cache.invalidate(listings, pathEntry); |
698 | | - listing = yield* Cache.get(listings, pathEntry); |
699 | | - } |
700 | | - for (const candidate of commandCandidates) { |
701 | | - if (listing?.names && !listing.names.has(candidate.toLowerCase())) continue; |
702 | | - const candidatePath = path.join(pathEntry, candidate); |
703 | | - if (yield* isExecutableFile(candidatePath, platform, windowsPathExtensions)) { |
704 | | - cacheCommandResolution(cache, cacheKey, candidatePath, nowNanos); |
705 | | - return candidatePath; |
706 | | - } |
707 | | - } |
708 | | - } |
709 | | - cacheCommandResolution(cache, cacheKey, null, nowNanos); |
710 | | - return null; |
| 723 | + const [resolvedPath = null] = yield* findOnPath( |
| 724 | + pathValue, |
| 725 | + commandCandidates, |
| 726 | + platform, |
| 727 | + windowsPathExtensions, |
| 728 | + () => true, |
| 729 | + ); |
| 730 | + cacheCommandResolution(cache, cacheKey, resolvedPath, nowNanos); |
| 731 | + return resolvedPath; |
711 | 732 | }); |
712 | 733 |
|
713 | 734 | export const resolveCommandPath = Effect.fn("shell.resolveCommandPath")(function* ( |
@@ -720,6 +741,24 @@ export const resolveCommandPath = Effect.fn("shell.resolveCommandPath")(function |
720 | 741 | }); |
721 | 742 | }); |
722 | 743 |
|
| 744 | +// Matches for a bare command in PATH order, up to the first one `until` accepts; not cached. |
| 745 | +export const resolveCommandPaths = Effect.fn("shell.resolveCommandPaths")(function* ( |
| 746 | + command: string, |
| 747 | + options: CommandAvailabilityOptions & { readonly until: (match: string) => boolean }, |
| 748 | +) { |
| 749 | + const path = yield* Path.Path; |
| 750 | + const platform = yield* HostProcess.Platform; |
| 751 | + const env = options.env ?? (yield* HostProcess.Environment); |
| 752 | + const windowsPathExtensions = platform === "win32" ? resolveWindowsPathExtensions(env) : []; |
| 753 | + return yield* findOnPath( |
| 754 | + resolvePathEnvironmentVariable(env), |
| 755 | + resolveCommandCandidates(command, platform, windowsPathExtensions, path.extname), |
| 756 | + platform, |
| 757 | + windowsPathExtensions, |
| 758 | + options.until, |
| 759 | + ); |
| 760 | +}); |
| 761 | + |
723 | 762 | // Git 2.56 x64 uses ucrt64; ARM64 uses clangarm64; older x64/32-bit builds use mingw64/mingw32. |
724 | 763 | const GIT_FOR_WINDOWS_BUILDS = ["ucrt64", "clangarm64", "mingw64", "mingw32"] as const; |
725 | 764 |
|
|