TypeScript
perf(server): check Android SDK locations before PATH (#19960)
Julius Marmingea175e06cdc8c
diff --git a/apps/server/src/device/LocalDeviceHost.test.ts b/apps/server/src/device/LocalDeviceHost.test.tsindex eef93e4bdae..3a0fa3094fc 100644--- a/apps/server/src/device/LocalDeviceHost.test.ts+++ b/apps/server/src/device/LocalDeviceHost.test.ts@@ -255,6 +255,66 @@ const diagnose = ();describe("Android SDK availability", () => {+ it.effect("uses the standard Windows SDK without probing PATH", () =>+ Effect.gen(function* () {+ const root = "C:\\Users\\test\\AppData\\Local\\Android\\Sdk";+ const probes: string[] = [];+ const sdk = yield* LocalDeviceHost.__testing.androidSdk.pipe(+ Effect.provideService(HostProcess.Environment, {+ USERPROFILE: "C:\\Users\\test",+ LOCALAPPDATA: "C:\\Users\\test\\AppData\\Local",+ PATH: "\\\\slow-share\\tools;C:\\other-tools",+ }),+ Effect.provideService(HostProcess.Platform, "win32"),+ Effect.provideService(+ FileSystem.FileSystem,+ FileSystem.makeNoop({+ exists: (file) =>+ Effect.sync(() => {+ probes.push(file);+ return file === `${root}\\platform-tools\\adb.exe`;+ }),+ realPath: () => Effect.die("Standard SDK must not resolve PATH executables"),+ }),+ ),+ Effect.provide(NodePath.layerWin32),+ );+ expect(sdk).toMatchObject({ root, adb: true });+ expect(probes.filter((file) => !file.startsWith("C:\\Users\\test\\"))).toEqual([]);+ }),+ );++ it.effect("falls back to adb on the Windows Path without resolving missing entries", () =>+ Effect.gen(function* () {+ const root = "D:\\Android\\Sdk";+ const resolved: string[] = [];+ const sdk = yield* LocalDeviceHost.__testing.androidSdk.pipe(+ Effect.provideService(HostProcess.Environment, {+ USERPROFILE: "C:\\Users\\test",+ Path: "C:\\missing;C:\\shims",+ }),+ Effect.provideService(HostProcess.Platform, "win32"),+ Effect.provideService(+ FileSystem.FileSystem,+ FileSystem.makeNoop({+ exists: (file) =>+ Effect.succeed(+ file === "C:\\shims\\adb.exe" || file === `${root}\\platform-tools\\adb.exe`,+ ),+ realPath: (file) =>+ Effect.sync(() => {+ resolved.push(file);+ return `${root}\\platform-tools\\adb.exe`;+ }),+ }),+ ),+ Effect.provide(NodePath.layerWin32),+ );+ expect(sdk).toMatchObject({ root, adb: true });+ expect(resolved).toEqual(["C:\\shims\\adb.exe"]);+ }),+ );+it.effect("explains that adb alone is insufficient to launch an emulator", () =>Effect.gen(function* () {const reason = yield* diagnose(["/sdk/platform-tools/adb"], { ANDROID_HOME: "/sdk" });diff --git a/apps/server/src/device/LocalDeviceHost.ts b/apps/server/src/device/LocalDeviceHost.tsindex 63245da93d2..de58c913572 100644--- a/apps/server/src/device/LocalDeviceHost.ts+++ b/apps/server/src/device/LocalDeviceHost.ts@@ -143,8 +143,25 @@ const androidSdk = Effect.gen(function* () {const path = yield* Path.Path;const environment = yield* HostProcess.Environment;const platform = yield* HostProcess.Platform;+ const windows = platform === "win32";const home = environment.HOME ?? environment.USERPROFILE ?? "";const explicit = environment.ANDROID_HOME?.trim() || environment.ANDROID_SDK_ROOT?.trim();+ const exists = (file: string) => fs.exists(file).pipe(Effect.orElseSucceed(() => false));+ const adbBinary = windows ? "adb.exe" : "adb";+ const avdmanagerBinary = windows ? "avdmanager.bat" : "avdmanager";+ const inspect = Effect.fnUntraced(function* (root: string) {+ const adb = yield* exists(path.join(root, "platform-tools", adbBinary));+ const emulator = yield* exists(+ path.join(root, "emulator", windows ? "emulator.exe" : "emulator"),+ );+ if (!explicit && !adb && !emulator) return null;+ const avdmanager = yield* exists(+ path.join(root, "cmdline-tools", "latest", "bin", avdmanagerBinary),+ );+ const legacyAvdmanager =+ !avdmanager && (yield* exists(path.join(root, "tools", "bin", avdmanagerBinary)));+ return { root, adb, emulator, avdmanager, legacyAvdmanager };+ });const candidates = explicit? [explicit]: [@@ -156,39 +173,20 @@ const androidSdk = Effect.gen(function* () {"Sdk",),];+ for (const root of candidates) {+ const sdk = yield* inspect(root);+ if (sdk) return sdk;+ }+ // PATH entries can be slow to stat (network shares), so only consult them after the standard locations miss.if (!explicit) {- for (const directory of (environment.PATH ?? "").split(platform === "win32" ? ";" : ":")) {+ const searchPath = environment.PATH ?? environment.Path ?? "";+ for (const directory of searchPath.split(windows ? ";" : ":")) {if (!directory) continue;- const resolved = yield* fs- .realPath(path.join(directory, platform === "win32" ? "adb.exe" : "adb"))- .pipe(Effect.option);- if (resolved._tag === "Some") candidates.push(path.dirname(path.dirname(resolved.value)));- }- }- const exists = (file: string) => fs.exists(file).pipe(Effect.orElseSucceed(() => false));- for (const root of candidates) {- const adb = yield* exists(- path.join(root, "platform-tools", platform === "win32" ? "adb.exe" : "adb"),- );- const emulator = yield* exists(- path.join(root, "emulator", platform === "win32" ? "emulator.exe" : "emulator"),- );- if (explicit || adb || emulator) {- const avdmanager = yield* exists(- path.join(- root,- "cmdline-tools",- "latest",- "bin",- platform === "win32" ? "avdmanager.bat" : "avdmanager",- ),- );- const legacyAvdmanager =- !avdmanager &&- (yield* exists(- path.join(root, "tools", "bin", platform === "win32" ? "avdmanager.bat" : "avdmanager"),- ));- return { root, adb, emulator, avdmanager, legacyAvdmanager };+ const adb = path.join(directory, adbBinary);+ if (!(yield* exists(adb))) continue;+ const resolved = yield* fs.realPath(adb).pipe(Effect.orElseSucceed(() => adb));+ const sdk = yield* inspect(path.dirname(path.dirname(resolved)));+ if (sdk) return sdk;}}return { root: null, adb: false, emulator: false, avdmanager: false, legacyAvdmanager: false };