-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinaryResolver.ts
More file actions
73 lines (68 loc) · 2.39 KB
/
binaryResolver.ts
File metadata and controls
73 lines (68 loc) · 2.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import { statSync } from "node:fs";
import type { ProjectLocation } from "@/shared/contracts";
import { getCachedExecutablePath, resolveExecutablePath, resolveWslExecutablePath } from "./base";
// Single process-wide cache, keyed by `${distro}\0${binary}`.
// Replaces per-adapter `detectedWslExecPaths` maps so detection probes and
// launch spec resolution share one cache instead of five.
const cache = new Map<string, string | undefined>();
function keyOf(scope: string, binary: string): string {
return `${scope}\0${binary}`;
}
/**
* Resolve the absolute path of a CLI binary for the given project location.
* WSL binaries are resolved inside the distro; native Windows binaries fall
* back to the registry-backed PATH lookup so packaged apps launched outside a
* shell can still find user-installed CLIs.
*/
export function resolveAgentBinaryPath(
location: ProjectLocation,
binary: string,
): string | undefined {
if (location.kind === "windows") {
const key = keyOf("windows", binary);
if (cache.has(key)) {
return cache.get(key);
}
const resolved = resolveExecutablePath(binary);
cache.set(key, resolved);
return resolved;
}
if (location.kind === "wsl") {
const key = keyOf(location.distro, binary);
if (cache.has(key)) {
return cache.get(key);
}
const resolved = resolveWslExecutablePath(location.distro, binary);
cache.set(key, resolved);
return resolved;
}
// posix: piggy-back on the shared exec-path cache populated by
// primeExecutablePathCache during agent detection. The cached path may come
// from a temporary login shell (e.g. fnm multishell) that has since been
// cleaned up — verify the file still exists so node-pty doesn't get a stale
// absolute path and fail with opaque "posix_spawnp failed".
const cached = getCachedExecutablePath(binary);
if (cached) {
try {
const s = statSync(cached);
if (!s.isFile() || (s.mode & 0o111) === 0) return undefined;
} catch {
return undefined;
}
}
return cached;
}
/**
* Populate the cache directly. Install detection already runs `command -v`
* as part of its probe — calling this avoids a second probe at launch time.
*/
export function primeAgentBinaryPath(
distro: string,
binary: string,
path: string | undefined,
): void {
cache.set(keyOf(distro, binary), path);
}
export function clearAgentBinaryPathCache(): void {
cache.clear();
}