Repository navigation
Expand file tree
/
Copy pathfind-bin.js
More file actions
219 lines (211 loc) · 10.4 KB
/
Copy pathfind-bin.js
File metadata and controls
219 lines (211 loc) · 10.4 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
// Universal .app bundles (packaging/build-desktop.sh UNIVERSAL=1) ship one
// complete backend tree per CPU architecture under backend-dist/. Maps a Node
// `process.arch` value to the directory suffix; arches without an entry
// (e.g. "ia32") simply skip the arch-suffixed candidates.
const ARCH_DIR_SUFFIX = { arm64: "arm64", x64: "x64" };
/**
* Locate the kirocrew backend binary by checking well-known paths in order.
*
* Returns the first executable candidate, or bare `"kirocrew"` as a PATH
* fallback. Dependencies are injected so the function is pure and testable
* without mocking globals.
*
* @param {typeof import("fs")} fs - Node fs module (needs `accessSync`, `constants.X_OK`)
* @param {typeof import("os")} os - Node os module (needs `homedir()`)
* @param {typeof import("path")} path - Node path module
* @param {string|undefined} resourcesPath - `process.resourcesPath` (Electron only)
* @param {string} dirname - `__dirname` of the calling module
* @param {string} [arch] - CPU arch selecting the backend tree in universal
* bundles (defaults to `process.arch`)
* @param {boolean} [isWindows] - whether the host is Windows (defaults to
* `process.platform === "win32"`). On Windows the backend ships as a real
* `kirocrew.exe` console script under `Scripts\` (venv) — Node's `spawn()`
* does no PATHEXT resolution for a bare name, so an absolute `.exe` path is
* required.
* @returns {string} Absolute path to the binary, or `"kirocrew"` /
* `"kirocrew.exe"` (Windows) as a PATH fallback
*/
function findKirocrewBin(
fs,
os,
path,
resourcesPath,
dirname,
arch = process.arch,
isWindows = process.platform === "win32"
) {
const home = os.homedir();
const candidates = [];
// 0. Universal-bundle layout: arch-suffixed backend trees, selected by the
// running shell's arch. Ranked above the unsuffixed layout so a universal
// bundle never falls back to a wrong-arch tree; plain per-arch bundles
// don't ship these dirs so the probes miss (ENOENT) and fall through.
const suffix = ARCH_DIR_SUFFIX[arch];
if (suffix) {
const archBackend = `kirocrew-backend-${suffix}`;
candidates.push(
path.join(resourcesPath || "", "backend-dist", archBackend, "bin", "kirocrew"),
path.resolve(dirname, "backend-dist", archBackend, "bin", "kirocrew")
);
}
// 1. Windows SOURCE CHECKOUT: a pip/venv install exposes `kirocrew.exe`
// under `Scripts\` (not the POSIX `bin/kirocrew` launcher). Probed before
// the bundled candidates so a developer running from a checkout gets
// their own venv, and as an absolute `.exe` that `spawn()` can launch
// without a shell. On POSIX these are skipped entirely so mac/Linux
// behavior is unchanged.
//
// Only the checkout venvs are ranked here. The BUNDLE's own
// Scripts\kirocrew.exe is ranked further down, below the .cmd shim --
// see the note there.
if (isWindows) {
candidates.push(
// Source checkout: repo-root `.venv` — electron/ is <repo>/website/electron,
// so the venv is two levels up; one level up covers a <repo>/website venv.
path.resolve(dirname, "..", "..", ".venv", "Scripts", "kirocrew.exe"),
path.resolve(dirname, "..", ".venv", "Scripts", "kirocrew.exe")
);
}
candidates.push(
// 2. Windows bundled layout (packaging/build-desktop.sh
// build_backend_windows): the PBS interpreter ships python.exe at
// the tree root with a bin\kirocrew.cmd launcher shim. Probed on
// every platform (costs one ENOENT elsewhere) so this function
// stays platform-agnostic and testable; only a Windows bundle
// actually contains the .cmd. Keep in sync with
// build-desktop.sh's bin/kirocrew.cmd.
//
// This MUST outrank backend-dist/.../Scripts/kirocrew.exe below.
// `pip install` also drops a console-script .exe in the bundle's
// Scripts\ dir, but distlib embeds the ABSOLUTE interpreter path of
// the machine that built it, so inside a shipped bundle that .exe
// points at a build-agent path (D:\a\KiroCrew\...) that does not
// exist on the user's machine. The .cmd shim resolves the
// interpreter via %~dp0 and is the only relocatable launcher of the
// two. (Both bundle writers delete that .exe and put a
// Scripts\kirocrew.cmd copy of this shim in its place, but a bundle
// built without that step still carries it.) Ranking them the other
// way round both broke the build-time
// resolver gate and, had the gate not caught it, would have shipped
// an app whose backend could never start.
path.join(resourcesPath || "", "backend-dist", "kirocrew-backend", "bin", "kirocrew.cmd"),
path.resolve(dirname, "backend-dist", "kirocrew-backend", "bin", "kirocrew.cmd"),
// 3. Bundled POSIX layout (packaging/build-desktop.sh): a
// python-build-standalone interpreter copied into backend-dist with a
// `bin/kirocrew` launcher wrapper (exec python3.12 -s -P -m kiro_crew).
// This is what a freshly-built .app actually ships. Keep this in sync
// with build-desktop.sh's BACKEND_OUT/bin/kirocrew path.
path.join(resourcesPath || "", "backend-dist", "kirocrew-backend", "bin", "kirocrew"),
path.resolve(dirname, "backend-dist", "kirocrew-backend", "bin", "kirocrew"),
path.resolve(dirname, "..", "bin", "kirocrew")
);
if (isWindows) {
// 4. The bundle's pip console-script .exe. Ranked BELOW the .cmd shim
// (distlib bakes the building machine's absolute interpreter path into
// it, so in a shipped bundle it points at a path that does not exist)
// but still ABOVE the user-level install paths below: a bundled app
// must prefer its own backend over whatever happens to be installed on
// the machine. The bundle writers replace it with Scripts\kirocrew.cmd,
// so it is probed only for a bundle built without that step.
candidates.push(
path.join(resourcesPath || "", "backend-dist", "kirocrew-backend", "Scripts", "kirocrew.exe"),
path.resolve(dirname, "backend-dist", "kirocrew-backend", "Scripts", "kirocrew.exe")
);
}
// 5. Well-known install paths (toolbox, installer symlink, and venv). Last,
// so a packaged app never prefers a stray user-level install over the
// backend it shipped with.
candidates.push(
path.join(home, ".toolbox", "bin", "kirocrew"),
path.join(home, ".local", "bin", "kirocrew"),
path.join(home, ".kirocrew-app", ".venv", "bin", "kirocrew")
);
if (isWindows) {
// Windows equivalents of the user-level paths above (one-liner installer
// venv, toolbox, and local pip Scripts dirs).
candidates.push(
path.join(home, ".kirocrew-app", ".venv", "Scripts", "kirocrew.exe"),
path.join(home, ".toolbox", "bin", "kirocrew.exe"),
path.join(home, ".local", "bin", "kirocrew.exe")
);
}
for (const bin of candidates) {
try {
fs.accessSync(bin, fs.constants.X_OK);
return bin;
} catch (e) {
if (e.code !== "ENOENT") console.warn(`kirocrew candidate ${bin}: ${e.code}`);
}
}
return isWindows ? PATH_FALLBACK_WINDOWS : PATH_FALLBACK; // fall back to PATH
}
const PATH_FALLBACK = "kirocrew";
const PATH_FALLBACK_WINDOWS = "kirocrew.exe";
/**
* True when `bin` is findKirocrewBin's bare PATH fallback rather than a
* candidate it found on disk: nothing at any probed path was executable, and
* spawning `bin` runs whatever that name resolves to on PATH.
*
* @param {string} bin a findKirocrewBin result
* @returns {boolean}
*/
function isPathFallback(bin) {
return bin === PATH_FALLBACK || bin === PATH_FALLBACK_WINDOWS;
}
// Root-owned directories an ssh client may be taken from. Mirrors
// `platform_compat._TRUSTED_SYSTEM_BIN_DIRS`: PATH can lead with agent-writable
// directories (`~/.local/bin`, a worktree venv), and this binary runs in the
// un-sandboxed main process with the remote command and returns the token.
const TRUSTED_POSIX_SSH_DIRS = ["/usr/bin", "/bin", "/usr/sbin", "/sbin", "/run/current-system/sw/bin"];
// The kernel's own `\SystemRoot` object link, reached through the global object
// namespace. Only the kernel sets it, so it names the running Windows directory
// whatever the environment says. `%SystemRoot%` is not usable:
// `HKCU\Environment` is writable without elevation, so a restarted app would
// inherit a root naming a planted `ssh.exe` (`platform_compat._windows_system_dirs`
// records this as measured). A fixed `C:\Windows` is not usable either: on a
// Windows installed off `C:` it is an ordinary directory any user can create.
const KERNEL_SYSTEM_ROOT = "\\\\?\\GLOBALROOT\\SystemRoot";
// The in-box Windows OpenSSH client under the kernel's Windows directory, or
// null when that directory does not resolve to a plain `X:\Windows`. Null
// refuses the fetch: no guessed path is safer than the one the kernel names.
function windowsSshBin(fs, path) {
let root;
try {
root = fs.realpathSync.native(KERNEL_SYSTEM_ROOT);
} catch {
return null;
}
root = String(root).replace(/^\\\\\?\\/, "");
if (!/^[A-Za-z]:\\Windows$/i.test(root)) return null;
return path.win32.join(root, "System32", "OpenSSH", "ssh.exe");
}
/**
* Resolve the local OpenSSH client for an `execFile` call from trusted,
* non-user-writable locations only: never PATH, never the environment.
*
* POSIX takes the first executable `ssh` in the trusted system directories
* (NixOS included), falling back to `/usr/bin/ssh` so a miss surfaces as a
* spawn ENOENT naming that path. Windows takes the in-box client under the
* Windows directory the kernel names, or null when that cannot be proven.
*
* @param {typeof import("fs")} fs - Node fs module (needs `accessSync`,
* `constants.X_OK`, and on Windows `realpathSync.native`)
* @param {typeof import("path")} path - Node path module
* @param {boolean} [isWindows] - whether the host is Windows
* @returns {string|null} Absolute path to the ssh client; null on Windows when
* the system directory does not resolve
*/
function findSshBin(fs, path, isWindows = process.platform === "win32") {
if (isWindows) return windowsSshBin(fs, path);
for (const dir of TRUSTED_POSIX_SSH_DIRS) {
const bin = path.join(dir, "ssh");
try {
fs.accessSync(bin, fs.constants.X_OK);
return bin;
} catch {
// not here; try the next trusted directory
}
}
return "/usr/bin/ssh";
}
module.exports = { findKirocrewBin, isPathFallback, findSshBin };