Skip to content

Commit d27abaa

Browse files
perf(server): find the Android SDK before probing PATH
Local device startup resolved adb on every PATH entry before checking the standard SDK locations, so a slow network share on PATH delayed device discovery even when the SDK sat in its default folder. Check ANDROID_HOME and the standard roots first and only walk PATH when they miss. On Windows, honor the `Path` spelling and skip realPath on entries without adb. Co-Authored-By: Claude Fable 5.1 <[email protected]>
1 parent 9d984ae commit d27abaa

1 file changed

Lines changed: 29 additions & 31 deletions

File tree

‎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)