Skip to content

Commit a175e06

Browse files
perf(server): check Android SDK locations before PATH (pingdotgg#19960)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent d8798dd commit a175e06

2 files changed

Lines changed: 89 additions & 31 deletions

File tree

‎apps/server/src/device/LocalDeviceHost.test.ts‎

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,66 @@ const diagnose = (
255255
);
256256

257257
describe("Android SDK availability", () => {
258+
it.effect("uses the standard Windows SDK without probing PATH", () =>
259+
Effect.gen(function* () {
260+
const root = "C:\\Users\\test\\AppData\\Local\\Android\\Sdk";
261+
const probes: string[] = [];
262+
const sdk = yield* LocalDeviceHost.__testing.androidSdk.pipe(
263+
Effect.provideService(HostProcess.Environment, {
264+
USERPROFILE: "C:\\Users\\test",
265+
LOCALAPPDATA: "C:\\Users\\test\\AppData\\Local",
266+
PATH: "\\\\slow-share\\tools;C:\\other-tools",
267+
}),
268+
Effect.provideService(HostProcess.Platform, "win32"),
269+
Effect.provideService(
270+
FileSystem.FileSystem,
271+
FileSystem.makeNoop({
272+
exists: (file) =>
273+
Effect.sync(() => {
274+
probes.push(file);
275+
return file === `${root}\\platform-tools\\adb.exe`;
276+
}),
277+
realPath: () => Effect.die("Standard SDK must not resolve PATH executables"),
278+
}),
279+
),
280+
Effect.provide(NodePath.layerWin32),
281+
);
282+
expect(sdk).toMatchObject({ root, adb: true });
283+
expect(probes.filter((file) => !file.startsWith("C:\\Users\\test\\"))).toEqual([]);
284+
}),
285+
);
286+
287+
it.effect("falls back to adb on the Windows Path without resolving missing entries", () =>
288+
Effect.gen(function* () {
289+
const root = "D:\\Android\\Sdk";
290+
const resolved: string[] = [];
291+
const sdk = yield* LocalDeviceHost.__testing.androidSdk.pipe(
292+
Effect.provideService(HostProcess.Environment, {
293+
USERPROFILE: "C:\\Users\\test",
294+
Path: "C:\\missing;C:\\shims",
295+
}),
296+
Effect.provideService(HostProcess.Platform, "win32"),
297+
Effect.provideService(
298+
FileSystem.FileSystem,
299+
FileSystem.makeNoop({
300+
exists: (file) =>
301+
Effect.succeed(
302+
file === "C:\\shims\\adb.exe" || file === `${root}\\platform-tools\\adb.exe`,
303+
),
304+
realPath: (file) =>
305+
Effect.sync(() => {
306+
resolved.push(file);
307+
return `${root}\\platform-tools\\adb.exe`;
308+
}),
309+
}),
310+
),
311+
Effect.provide(NodePath.layerWin32),
312+
);
313+
expect(sdk).toMatchObject({ root, adb: true });
314+
expect(resolved).toEqual(["C:\\shims\\adb.exe"]);
315+
}),
316+
);
317+
258318
it.effect("explains that adb alone is insufficient to launch an emulator", () =>
259319
Effect.gen(function* () {
260320
const reason = yield* diagnose(["/sdk/platform-tools/adb"], { ANDROID_HOME: "/sdk" });

‎apps/server/src/device/LocalDeviceHost.ts‎

Lines changed: 29 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,25 @@ const androidSdk = Effect.gen(function* () {
143143
const path = yield* Path.Path;
144144
const environment = yield* HostProcess.Environment;
145145
const platform = yield* HostProcess.Platform;
146+
const windows = platform === "win32";
146147
const home = environment.HOME ?? environment.USERPROFILE ?? "";
147148
const explicit = environment.ANDROID_HOME?.trim() || environment.ANDROID_SDK_ROOT?.trim();
149+
const exists = (file: string) => fs.exists(file).pipe(Effect.orElseSucceed(() => false));
150+
const adbBinary = windows ? "adb.exe" : "adb";
151+
const avdmanagerBinary = windows ? "avdmanager.bat" : "avdmanager";
152+
const inspect = Effect.fnUntraced(function* (root: string) {
153+
const adb = yield* exists(path.join(root, "platform-tools", adbBinary));
154+
const emulator = yield* exists(
155+
path.join(root, "emulator", windows ? "emulator.exe" : "emulator"),
156+
);
157+
if (!explicit && !adb && !emulator) return null;
158+
const avdmanager = yield* exists(
159+
path.join(root, "cmdline-tools", "latest", "bin", avdmanagerBinary),
160+
);
161+
const legacyAvdmanager =
162+
!avdmanager && (yield* exists(path.join(root, "tools", "bin", avdmanagerBinary)));
163+
return { root, adb, emulator, avdmanager, legacyAvdmanager };
164+
});
148165
const candidates = explicit
149166
? [explicit]
150167
: [
@@ -156,39 +173,20 @@ const androidSdk = Effect.gen(function* () {
156173
"Sdk",
157174
),
158175
];
176+
for (const root of candidates) {
177+
const sdk = yield* inspect(root);
178+
if (sdk) return sdk;
179+
}
180+
// PATH entries can be slow to stat (network shares), so only consult them after the standard locations miss.
159181
if (!explicit) {
160-
for (const directory of (environment.PATH ?? "").split(platform === "win32" ? ";" : ":")) {
182+
const searchPath = environment.PATH ?? environment.Path ?? "";
183+
for (const directory of searchPath.split(windows ? ";" : ":")) {
161184
if (!directory) continue;
162-
const resolved = yield* fs
163-
.realPath(path.join(directory, platform === "win32" ? "adb.exe" : "adb"))
164-
.pipe(Effect.option);
165-
if (resolved._tag === "Some") candidates.push(path.dirname(path.dirname(resolved.value)));
166-
}
167-
}
168-
const exists = (file: string) => fs.exists(file).pipe(Effect.orElseSucceed(() => false));
169-
for (const root of candidates) {
170-
const adb = yield* exists(
171-
path.join(root, "platform-tools", platform === "win32" ? "adb.exe" : "adb"),
172-
);
173-
const emulator = yield* exists(
174-
path.join(root, "emulator", platform === "win32" ? "emulator.exe" : "emulator"),
175-
);
176-
if (explicit || adb || emulator) {
177-
const avdmanager = yield* exists(
178-
path.join(
179-
root,
180-
"cmdline-tools",
181-
"latest",
182-
"bin",
183-
platform === "win32" ? "avdmanager.bat" : "avdmanager",
184-
),
185-
);
186-
const legacyAvdmanager =
187-
!avdmanager &&
188-
(yield* exists(
189-
path.join(root, "tools", "bin", platform === "win32" ? "avdmanager.bat" : "avdmanager"),
190-
));
191-
return { root, adb, emulator, avdmanager, legacyAvdmanager };
185+
const adb = path.join(directory, adbBinary);
186+
if (!(yield* exists(adb))) continue;
187+
const resolved = yield* fs.realPath(adb).pipe(Effect.orElseSucceed(() => adb));
188+
const sdk = yield* inspect(path.dirname(path.dirname(resolved)));
189+
if (sdk) return sdk;
192190
}
193191
}
194192
return { root: null, adb: false, emulator: false, avdmanager: false, legacyAvdmanager: false };

0 commit comments

Comments
 (0)