Skip to content

Commit e22ef94

Browse files
committed
fix(kiro): resolve Windows kiro-cli executable without PATH
After the Windows SQLite store path fix, forced/add-account login still spawned bare `kiro-cli` and failed when the installer binary was not on PATH. Resolve PATH first, then fall back to the LocalAppData and Program Files Kiro-Cli install layouts, with pure unit coverage.
1 parent be177ea commit e22ef94

4 files changed

Lines changed: 151 additions & 2 deletions

File tree

‎docs-site/src/content/docs/guides/providers.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,10 @@ read-only. Two environment variables make the source and token row selection exp
165165
- `KIROCLI_TOKEN_KEY` selects the exact `auth_kv` token key when a database contains multiple
166166
otherwise ambiguous token rows. A missing selection fails login instead of guessing.
167167

168+
On Windows, import looks for `%LOCALAPPDATA%\Kiro-Cli\data.sqlite3`. Forced/add-account login
169+
also needs the local CLI binary: opencodex first uses `PATH`, then falls back to
170+
`%LOCALAPPDATA%\Kiro-Cli\kiro-cli.exe` and `C:\Program Files\Kiro-Cli\kiro-cli.exe`.
171+
168172
After a successful import, opencodex persists the imported credential to
169173
`~/.opencodex/auth.json`.
170174

‎src/oauth/kiro-credentials.ts‎

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,65 @@ export function resolveKiroCliNativeSessionEntries(
169169
return [{ location: "kiro-cli-linux-data", path: posix.join(home, ".local", "share", "kiro-cli", "data.sqlite3") }];
170170
}
171171

172+
/**
173+
* Resolve the absolute kiro-cli executable for spawn/login helpers.
174+
*
175+
* Pure + parameterized like `resolveKiroCliNativeSessionEntries` so Windows install layouts can be
176+
* covered from any host. PATH remains the first choice; only when bare `kiro-cli` is missing do we
177+
* fall back to the platform-native install directories next to the session database.
178+
*
179+
* Windows: official MSI installs to `C:\Program Files\Kiro-Cli\kiro-cli.exe`, while some local
180+
* installs keep the binary next to `%LOCALAPPDATA%\Kiro-Cli\data.sqlite3`.
181+
* macOS/Linux: prefer PATH, then the usual user-local bin directories.
182+
*/
183+
export function resolveKiroCliExecutable(
184+
inputs: KiroCliNativeInputs & {
185+
pathEntries?: string[];
186+
exists?: (path: string) => boolean;
187+
},
188+
): string {
189+
const exists = inputs.exists ?? existsSync;
190+
const pathEntries = inputs.pathEntries
191+
?? (inputs.env.PATH ?? inputs.env.Path ?? "").split(inputs.platform === "win32" ? ";" : ":")
192+
.map(entry => entry.trim())
193+
.filter(Boolean);
194+
195+
const pathCandidates = inputs.platform === "win32"
196+
? pathEntries.flatMap(entry => [
197+
win32.join(entry, "kiro-cli.exe"),
198+
win32.join(entry, "kiro-cli"),
199+
])
200+
: pathEntries.map(entry => posix.join(entry, "kiro-cli"));
201+
202+
const installCandidates: string[] = [];
203+
if (inputs.platform === "win32") {
204+
const localBase = inputs.env.LOCALAPPDATA?.trim()
205+
|| (inputs.env.USERPROFILE?.trim() ? win32.join(inputs.env.USERPROFILE.trim(), "AppData", "Local") : "")
206+
|| win32.join(inputs.home, "AppData", "Local");
207+
const programFiles = inputs.env["ProgramFiles"]?.trim() || "C:\\Program Files";
208+
installCandidates.push(
209+
win32.join(localBase, "Kiro-Cli", "kiro-cli.exe"),
210+
win32.join(programFiles, "Kiro-Cli", "kiro-cli.exe"),
211+
);
212+
} else if (inputs.platform === "darwin") {
213+
installCandidates.push(
214+
posix.join(inputs.home, ".local", "bin", "kiro-cli"),
215+
"/usr/local/bin/kiro-cli",
216+
"/opt/homebrew/bin/kiro-cli",
217+
);
218+
} else {
219+
installCandidates.push(
220+
posix.join(inputs.home, ".local", "bin", "kiro-cli"),
221+
"/usr/local/bin/kiro-cli",
222+
);
223+
}
224+
225+
for (const candidate of [...pathCandidates, ...installCandidates]) {
226+
if (exists(candidate)) return candidate;
227+
}
228+
return inputs.platform === "win32" ? "kiro-cli.exe" : "kiro-cli";
229+
}
230+
172231
function nativeKiroCliSessionEntries(): Array<{ location: KiroCliNativeLocation; path: string }> {
173232
// Only the stores that `kiro-cli logout` / `kiro-cli login` themselves mutate. Import fallbacks
174233
// (Amazon Q / SSO cache) and KIROCLI_DB_PATH selectors must not be snapshotted for rollback.

‎src/oauth/kiro.ts‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,15 @@ import {
1818
persistKiroCliSessionRecovery,
1919
readImportedKiroCredential,
2020
readKiroCliSqliteCredential,
21+
resolveKiroCliExecutable,
2122
restoreKiroCliSession,
2223
restoreStaleKiroCliSessionRecovery,
2324
requireKiroRegion,
2425
type ImportedKiroCredential,
2526
type KiroCliSessionSnapshot,
2627
type KiroImportDiagnostic,
2728
} from "./kiro-credentials";
29+
import { homedir } from "node:os";
2830
import { getAccountSet, saveAccountCredential } from "./store";
2931

3032
const DEFAULT_REGION = "us-east-1";
@@ -72,9 +74,18 @@ const pendingKiroLoginTransactions = new WeakMap<OAuthCredentials, KiroCliSessio
7274
/** Forced logins that started with no native CLI DB must logout on persistence failure. */
7375
const pendingKiroEmptyPriorSessions = new WeakSet<OAuthCredentials>();
7476

77+
78+
function resolveRuntimeKiroCliExecutable(): string {
79+
return resolveKiroCliExecutable({
80+
env: process.env,
81+
platform: process.platform,
82+
home: process.platform === "win32" ? homedir() : (process.env.HOME || homedir()),
83+
});
84+
}
85+
7586
function logoutKiroCliBestEffort(): void {
7687
try {
77-
Bun.spawnSync(["kiro-cli", "logout"], {
88+
Bun.spawnSync([resolveRuntimeKiroCliExecutable(), "logout"], {
7889
stdin: "ignore",
7990
stdout: "ignore",
8091
stderr: "ignore",
@@ -128,7 +139,7 @@ async function defaultKiroCliRunner(args: string[], signal?: AbortSignal): Promi
128139
throwIfKiroLoginCancelled(signal);
129140
let child: ReturnType<typeof Bun.spawn>;
130141
try {
131-
child = Bun.spawn(["kiro-cli", ...args], {
142+
child = Bun.spawn([resolveRuntimeKiroCliExecutable(), ...args], {
132143
stdin: "ignore",
133144
stdout: "pipe",
134145
stderr: "ignore",
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import { describe, expect, test } from "bun:test";
2+
import { resolveKiroCliExecutable } from "../src/oauth/kiro-credentials";
3+
4+
/**
5+
* Forced/add-account Kiro login still shells out to the local CLI. After #710 fixed Windows
6+
* SQLite discovery, Windows installs can import tokens while PATH still lacks `kiro-cli`.
7+
* The pure executable resolver covers that layout without launching the real binary.
8+
*/
9+
describe("kiro-cli executable resolution", () => {
10+
const WIN_HOME = "C:\\Users\\u";
11+
12+
test("prefers the first PATH hit before install-directory fallbacks", () => {
13+
const exists = (path: string) => path === "C:\\Tools\\kiro-cli.exe";
14+
expect(resolveKiroCliExecutable({
15+
env: {
16+
PATH: "C:\\Tools;C:\\Windows\\System32",
17+
LOCALAPPDATA: "C:\\Users\\u\\AppData\\Local",
18+
},
19+
platform: "win32",
20+
home: WIN_HOME,
21+
pathEntries: ["C:\\Tools", "C:\\Windows\\System32"],
22+
exists,
23+
})).toBe("C:\\Tools\\kiro-cli.exe");
24+
});
25+
26+
test("win32 falls back to %LOCALAPPDATA%\\Kiro-Cli\\kiro-cli.exe", () => {
27+
const exists = (path: string) => path === "C:\\Users\\u\\AppData\\Local\\Kiro-Cli\\kiro-cli.exe";
28+
expect(resolveKiroCliExecutable({
29+
env: {
30+
PATH: "C:\\Windows\\System32",
31+
LOCALAPPDATA: "C:\\Users\\u\\AppData\\Local",
32+
},
33+
platform: "win32",
34+
home: WIN_HOME,
35+
pathEntries: ["C:\\Windows\\System32"],
36+
exists,
37+
})).toBe("C:\\Users\\u\\AppData\\Local\\Kiro-Cli\\kiro-cli.exe");
38+
});
39+
40+
test("win32 falls back to Program Files\\Kiro-Cli when LOCALAPPDATA binary is absent", () => {
41+
const exists = (path: string) => path === "C:\\Program Files\\Kiro-Cli\\kiro-cli.exe";
42+
expect(resolveKiroCliExecutable({
43+
env: {
44+
PATH: "C:\\Windows\\System32",
45+
LOCALAPPDATA: "C:\\Users\\u\\AppData\\Local",
46+
ProgramFiles: "C:\\Program Files",
47+
},
48+
platform: "win32",
49+
home: WIN_HOME,
50+
pathEntries: ["C:\\Windows\\System32"],
51+
exists,
52+
})).toBe("C:\\Program Files\\Kiro-Cli\\kiro-cli.exe");
53+
});
54+
55+
test("linux keeps PATH-first resolution and falls back to ~/.local/bin", () => {
56+
const exists = (path: string) => path === "/home/u/.local/bin/kiro-cli";
57+
expect(resolveKiroCliExecutable({
58+
env: { PATH: "/usr/bin" },
59+
platform: "linux",
60+
home: "/home/u",
61+
pathEntries: ["/usr/bin"],
62+
exists,
63+
})).toBe("/home/u/.local/bin/kiro-cli");
64+
});
65+
66+
test("returns the bare command when no candidate exists so spawn can report the original error", () => {
67+
expect(resolveKiroCliExecutable({
68+
env: { PATH: "C:\\Windows\\System32" },
69+
platform: "win32",
70+
home: WIN_HOME,
71+
pathEntries: ["C:\\Windows\\System32"],
72+
exists: () => false,
73+
})).toBe("kiro-cli.exe");
74+
});
75+
});

0 commit comments

Comments
 (0)