@devries-cameron20 / t3code-1 / commits / d530d96155
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

fix(desktop): inherit login-shell Android SDK selection (#24623)

Co-authored-by: thelonewander3r <59575541+thelonewander3r@users.noreply.github.com>

Julius Marminge committed Nov 14, 2026 at 00:17 UTC d530d961550b23cf0d8b7f714f03a1cbfc11c7aa
5 files changed +147 -34
apps/desktop/src/shell/DesktopShellEnvironment.test.ts
+33
@@ -580,6 +580,39 @@ describe("DesktopShellEnvironment", () => {
580 }),
581 );
582
583 + it.effect.each([
584 + {
585 + name: "hydrates both Android SDK names when neither is inherited",
586 + inherited: {},
587 + expected: { ANDROID_HOME: "/shell/android-home", ANDROID_SDK_ROOT: "/shell/android-root" },
588 + },
589 + {
590 + // The device host reads ANDROID_HOME before ANDROID_SDK_ROOT, so a shell
591 + // ANDROID_HOME next to an inherited ANDROID_SDK_ROOT would change the SDK.
592 + name: "keeps an inherited ANDROID_SDK_ROOT selected over a login-shell ANDROID_HOME",
593 + inherited: { ANDROID_SDK_ROOT: "/inherited/android-sdk" },
594 + expected: { ANDROID_HOME: undefined, ANDROID_SDK_ROOT: "/inherited/android-sdk" },
595 + },
596 + ])("$name", ({ inherited, expected }) =>
597 + Effect.gen(function* () {
598 + const env: NodeJS.ProcessEnv = { SHELL: "/bin/zsh", PATH: "/usr/bin", ...inherited };
599 +
600 + yield* runShellEnvironment({
601 + env,
602 + platform: "darwin",
603 + handler: () =>
604 + envOutput({
605 + PATH: "/usr/bin",
606 + ANDROID_HOME: "/shell/android-home",
607 + ANDROID_SDK_ROOT: "/shell/android-root",
608 + }),
609 + });
610 +
611 + assert.equal(env.ANDROID_HOME, expected.ANDROID_HOME);
612 + assert.equal(env.ANDROID_SDK_ROOT, expected.ANDROID_SDK_ROOT);
613 + }),
614 + );
615 +
616 it.effect("keeps mise's PATH record with the login shell PATH", () =>
617 Effect.gen(function* () {
618 const env: NodeJS.ProcessEnv = {
apps/desktop/src/shell/DesktopShellEnvironment.ts
+14
@@ -88,6 +88,8 @@ export class DesktopShellEnvironment extends Context.Service<
88
89 const LOGIN_SHELL_ENV_NAMES = [
90 "PATH",
91 + "ANDROID_HOME",
92 + "ANDROID_SDK_ROOT",
93 "DBUS_SESSION_BUS_ADDRESS",
94 "DISPLAY",
95 "LANG",
@@ -114,6 +116,7 @@ const LOGIN_SHELL_ENV_NAMES = [
116 ] as const;
117 const WINDOWS_PROFILE_ENV_NAMES = ["PATH", "FNM_DIR", "FNM_MULTISHELL_PATH"] as const;
118 const LOCALE_ENV_NAMES = ["LANG", "LC_ALL", "LC_CTYPE"] as const;
119 +const ANDROID_SDK_ENV_NAMES = ["ANDROID_HOME", "ANDROID_SDK_ROOT"] as const;
120 const FALLBACK_LC_CTYPE = "en_US.UTF-8";
121 const WINDOWS_SHELL_CANDIDATES = ["pwsh.exe", "powershell.exe"] as const;
122 const LOGIN_SHELL_TIMEOUT = Duration.seconds(5);
@@ -528,6 +531,17 @@ const installPosixEnvironment = Effect.fn("desktop.shellEnvironment.installPosix
531 }
532 }
533
534 + // The device host picks the Android SDK from ANDROID_HOME before ANDROID_SDK_ROOT,
535 + // so a profile-only ANDROID_HOME must not shadow an inherited ANDROID_SDK_ROOT.
536 + if (ANDROID_SDK_ENV_NAMES.every((name) => Option.isNone(trimNonEmpty(config.env[name])))) {
537 + for (const name of ANDROID_SDK_ENV_NAMES) {
538 + const value = trimNonEmpty(shellEnvironment[name]);
539 + if (Option.isSome(value)) {
540 + config.env[name] = value.value;
541 + }
542 + }
543 + }
544 +
545 // Hydrate locale variables together; inherited LC_ALL can override LANG or LC_CTYPE.
546 if (
547 config.platform === "darwin" &&
apps/server/src/device/LocalDeviceHost.test.ts
+52 -1
@@ -43,6 +43,7 @@ const agentFixture = Effect.fn("agentFixture")(function* (
43 ) => Effect.Effect<ProcessRunner.ProcessRunOutput, ProcessRunner.ProcessRunError>,
44 avdOutput: ProcessRunner.ProcessRunOutput = processOutput(0),
45 previousDaemon?: typeof LocalDeviceHost.__testing.AgentDeviceDaemonFile.Type,
46 + environment?: NodeJS.ProcessEnv,
47 ) {
48 const fs = yield* FileSystem.FileSystem;
49 const path = yield* Path.Path;
@@ -68,7 +69,7 @@ const agentFixture = Effect.fn("agentFixture")(function* (
69 }
70 const host = yield* LocalDeviceHost.make().pipe(
71 Effect.provide(Layer.mergeAll(ServerConfig.layerTest(baseDir, baseDir), NetService.layer)),
71 - Effect.provideService(HostProcess.Environment, { HOME: baseDir, PATH: "" }),
72 + Effect.provideService(HostProcess.Environment, environment ?? { HOME: baseDir, PATH: "" }),
73 Effect.provideService(HostProcess.IsExecutable, false),
74 Effect.provideService(HostProcess.Platform, "darwin"),
75 Effect.provideService(
@@ -165,6 +166,56 @@ describe("AgentDevice startup", () => {
166 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
167 );
168
169 + it.effect("uses the SDK a later shell environment selects", () =>
170 + Effect.gen(function* () {
171 + const fs = yield* FileSystem.FileSystem;
172 + const home = yield* fs.makeTempDirectoryScoped({ prefix: "t3-device-sdk-" });
173 + const standard = `${home}/Library/Android/sdk`;
174 + const profile = `${home}/profile-sdk`;
175 + for (const root of [standard, profile]) {
176 + for (const tool of ["platform-tools/adb", "emulator/emulator"]) {
177 + yield* fs.makeDirectory(`${root}/${tool.split("/")[0]}`, { recursive: true });
178 + yield* fs.writeFileString(`${root}/${tool}`, "");
179 + }
180 + }
181 + const environment: NodeJS.ProcessEnv = { HOME: home, PATH: "" };
182 + const { host, hub, runs } = yield* agentFixture(
183 + (stateDir) =>
184 + fs
185 + .writeFileString(
186 + `${stateDir}/daemon.json`,
187 + JSON.stringify({ httpPort: 12345, token: "token" }),
188 + )
189 + .pipe(Effect.orDie, Effect.as(processOutput(0))),
190 + processOutput(0),
191 + undefined,
192 + environment,
193 + );
194 + // The desktop shell snapshot updates the server's environment after the host exists.
195 + environment.ANDROID_HOME = profile;
196 + hub.running = false;
197 + runs.length = 0;
198 + const ready = yield* host.ensureAgentReady(() => Effect.void);
199 + yield* ready.run("emulator", ["-list-avds"]);
200 +
201 + const emulator = `${profile}/emulator/emulator`;
202 + const sdkRuns = runs.filter(
203 + (run) => run.command === emulator || run.args.includes("devices"),
204 + );
205 + // Two hub camera probes, the agent-device launch, and the emulator command.
206 + expect(sdkRuns.map((run) => run.args[0])).toEqual([
207 + "-help-camera-front",
208 + "-help-camera-back",
209 + expect.stringContaining("agent-device"),
210 + "-list-avds",
211 + ]);
212 + for (const run of sdkRuns) {
213 + expect(run.env?.ANDROID_HOME).toBe(profile);
214 + expect(run.env?.PATH?.split(":")[0]).toBe(`${profile}/platform-tools`);
215 + }
216 + }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
217 + );
218 +
219 it.effect("reports an incompatible lock immediately and retains it for manual repair", () =>
220 Effect.gen(function* () {
221 const result = processOutput(
apps/server/src/device/LocalDeviceHost.ts
+46 -33
@@ -270,8 +270,16 @@ export const make = Effect.fn("LocalDeviceHost.make")(function* () {
270 const environment = yield* HostProcess.Environment;
271 const workingDirectory = yield* HostProcess.WorkingDirectory;
272 const hostPlatform = yield* HostProcess.Platform;
273 - const sdk = yield* androidSdk;
274 - const hostEnvironment = deviceHostEnvironment(environment, sdk.root, hostPlatform, path);
273 + // Resolved per use: the desktop shell snapshot can select another SDK after the host exists.
274 + const currentSdk = androidSdk.pipe(
275 + Effect.provideService(FileSystem.FileSystem, fs),
276 + Effect.provideService(Path.Path, path),
277 + Effect.provideService(HostProcess.Environment, environment),
278 + Effect.provideService(HostProcess.Platform, hostPlatform),
279 + );
280 + const currentHostEnvironment = currentSdk.pipe(
281 + Effect.map((sdk) => deviceHostEnvironment(environment, sdk.root, hostPlatform, path)),
282 + );
283 const startLock = yield* Semaphore.make(1);
284 const runningRef = yield* Ref.make<RunningHost | null>(null);
285 const restartDelayRef = yield* Ref.make(0);
@@ -433,7 +441,7 @@ export const make = Effect.fn("LocalDeviceHost.make")(function* () {
441 shell: false,
442 stdout: "pipe",
443 stderr: "pipe",
436 - env: yield* hubEnvironment(hostEnvironment).pipe(
444 + env: yield* hubEnvironment(yield* currentHostEnvironment).pipe(
445 Effect.provideService(FileSystem.FileSystem, fs),
446 Effect.provideService(Path.Path, path),
447 Effect.provideService(ProcessRunner.ProcessRunner, runner),
@@ -536,7 +544,7 @@ export const make = Effect.fn("LocalDeviceHost.make")(function* () {
544 yield* fs.makeDirectory(stateDir, { recursive: true }).pipe(Effect.ignore);
545 const existing = yield* readDaemonFile().pipe(Effect.option);
546 const daemonEnvironment: NodeJS.ProcessEnv = {
539 - ...hostEnvironment,
547 + ...(yield* currentHostEnvironment),
548 AGENT_DEVICE_STATE_DIR: stateDir,
549 AGENT_DEVICE_DAEMON_SERVER_MODE: "http",
550 // Disable the five-minute idle timeout; the server owns shutdown.
@@ -643,6 +651,7 @@ export const make = Effect.fn("LocalDeviceHost.make")(function* () {
651 AGENT_DEVICE_VERSION,
652 ]);
653 if (!entryPath) return;
654 + const hostEnvironment = yield* currentHostEnvironment;
655 const nodePath = yield* resolveNodeExecutable("Local device support", hostEnvironment).pipe(
656 Effect.provideService(HostProcess.Platform, hostPlatform),
657 );
@@ -675,7 +684,10 @@ export const make = Effect.fn("LocalDeviceHost.make")(function* () {
684 const alive = yield* running.hub.child.isRunning.pipe(Effect.orElseSucceed(() => false));
685 if (alive) return running;
686 }
678 - const nodePath = yield* resolveNodeExecutable("Local device support", hostEnvironment).pipe(
687 + const nodePath = yield* resolveNodeExecutable(
688 + "Local device support",
689 + yield* currentHostEnvironment,
690 + ).pipe(
691 Effect.provideService(FileSystem.FileSystem, fs),
692 Effect.provideService(Path.Path, path),
693 Effect.provideService(HostProcess.Platform, hostPlatform),
@@ -807,34 +819,35 @@ export const make = Effect.fn("LocalDeviceHost.make")(function* () {
819 };
820
821 const run: DeviceHost.DeviceHostReady["run"] = (command, args, options) =>
810 - runner
811 - .run({
812 - command:
813 - command === "emulator" && sdk.root
814 - ? path.join(
815 - sdk.root,
816 - "emulator",
817 - hostPlatform === "win32" ? "emulator.exe" : "emulator",
818 - )
819 - : command,
820 - args,
821 - cwd: workingDirectory,
822 - env: hostEnvironment,
823 - timeout: Duration.millis(options?.timeoutMs ?? 20_000),
824 - timeoutBehavior: "timedOutResult",
825 - ...(options?.stdin === undefined ? {} : { stdin: options.stdin }),
826 - ...(options?.maxOutputBytes === undefined
827 - ? {}
828 - : { maxOutputBytes: options.maxOutputBytes }),
829 - })
830 - .pipe(
831 - Effect.map((result) => ({
832 - stdout: result.stdout,
833 - stderr: result.stderr,
834 - code: result.timedOut ? 124 : Number(result.code ?? 127),
835 - })),
836 - Effect.catch((cause) => Effect.succeed({ stdout: "", stderr: String(cause), code: 127 })),
837 - );
822 + currentSdk.pipe(
823 + Effect.flatMap((sdk) =>
824 + runner.run({
825 + command:
826 + command === "emulator" && sdk.root
827 + ? path.join(
828 + sdk.root,
829 + "emulator",
830 + hostPlatform === "win32" ? "emulator.exe" : "emulator",
831 + )
832 + : command,
833 + args,
834 + cwd: workingDirectory,
835 + env: deviceHostEnvironment(environment, sdk.root, hostPlatform, path),
836 + timeout: Duration.millis(options?.timeoutMs ?? 20_000),
837 + timeoutBehavior: "timedOutResult",
838 + ...(options?.stdin === undefined ? {} : { stdin: options.stdin }),
839 + ...(options?.maxOutputBytes === undefined
840 + ? {}
841 + : { maxOutputBytes: options.maxOutputBytes }),
842 + }),
843 + ),
844 + Effect.map((result) => ({
845 + stdout: result.stdout,
846 + stderr: result.stderr,
847 + code: result.timedOut ? 124 : Number(result.code ?? 127),
848 + })),
849 + Effect.catch((cause) => Effect.succeed({ stdout: "", stderr: String(cause), code: 127 })),
850 + );
851
852 const toReady = (running: RunningHost): DeviceHost.DeviceHostReady => ({
853 hub: { origin: running.hub.origin } satisfies DeviceHost.DeviceHubEndpoint,
packages/contracts/src/resourceTelemetry.ts
+2
@@ -297,6 +297,8 @@ export type DesktopUpdateStatusReport = typeof DesktopUpdateStatusReport.Type;
297
298 export const ShellEnvironmentPatch = Schema.Struct({
299 PATH: Schema.optionalKey(Schema.NullOr(Schema.String)),
300 + ANDROID_HOME: Schema.optionalKey(Schema.NullOr(Schema.String)),
301 + ANDROID_SDK_ROOT: Schema.optionalKey(Schema.NullOr(Schema.String)),
302 DBUS_SESSION_BUS_ADDRESS: Schema.optionalKey(Schema.NullOr(Schema.String)),
303 DISPLAY: Schema.optionalKey(Schema.NullOr(Schema.String)),
304 LANG: Schema.optionalKey(Schema.NullOr(Schema.String)),