TypeScript
fix(server): recover Linux runtime directory for device hub (#12402)
mariaf729e0ebfe75
diff --git a/apps/server/src/device/LocalDeviceHost.test.ts b/apps/server/src/device/LocalDeviceHost.test.tsindex 5c9a6dcaf4d..3f299db6d0c 100644--- a/apps/server/src/device/LocalDeviceHost.test.ts+++ b/apps/server/src/device/LocalDeviceHost.test.ts@@ -4,9 +4,11 @@ import {HostProcessEnvironment,HostProcessPlatform,HostProcessIsExecutable,+ HostProcessUserId,} from "@t3tools/shared/hostProcess";import * as Effect from "effect/Effect";import * as Layer from "effect/Layer";+import * as Option from "effect/Option";import * as Path from "effect/Path";import * as FileSystem from "effect/FileSystem";@@ -134,6 +136,54 @@ it.effect("puts detected Android tools on the helper PATH without losing existin}).pipe(Effect.provide(NodePath.layer)),);+it.effect(+ "recovers an owned Linux runtime directory without overriding explicit environments",+ () =>+ Effect.gen(function* () {+ const fs = yield* FileSystem.FileSystem;+ const directory = yield* fs.makeTempDirectoryScoped();+ const stat = yield* fs.stat(directory);+ for (const [platform, uid, runtimeDir, type, owner, expected] of [+ ["linux", 1000, undefined, "Directory", 1000, "/run/user/1000"],+ ["linux", 1000, "/custom/runtime", "Directory", 1000, "/custom/runtime"],+ ["linux", 1000, "", "Directory", 1000, ""],+ ["linux", 1000, undefined, "Directory", 1001, undefined],+ ["linux", 1000, undefined, "File", 1000, undefined],+ ["linux", undefined, undefined, "Directory", 1000, undefined],+ ["darwin", 1000, undefined, "Directory", 1000, undefined],+ ["win32", 1000, undefined, "Directory", 1000, undefined],+ ] as const) {+ const environment = { PATH: "/usr/bin", XDG_RUNTIME_DIR: runtimeDir };+ const result = yield* LocalDeviceHost.__testing.hubEnvironment(environment).pipe(+ Effect.provideService(HostProcessPlatform, platform),+ Effect.provideService(HostProcessUserId, uid),+ Effect.provideService(+ FileSystem.FileSystem,+ FileSystem.makeNoop({+ stat: (path) => {+ expect(path).toBe("/run/user/1000");+ return Effect.succeed({ ...stat, type, uid: Option.some(owner) });+ },+ }),+ ),+ );+ expect(result.XDG_RUNTIME_DIR).toBe(expected);+ expect(result.PATH).toBe("/usr/bin");+ expect(result.FORCE_COLOR).toBe("0");+ expect(result.NO_COLOR).toBe("1");+ expect(environment.XDG_RUNTIME_DIR).toBe(runtimeDir);+ }+ const missing = yield* LocalDeviceHost.__testing+ .hubEnvironment({})+ .pipe(+ Effect.provideService(HostProcessPlatform, "linux"),+ Effect.provideService(HostProcessUserId, 1000),+ Effect.provideService(FileSystem.FileSystem, FileSystem.makeNoop({})),+ );+ expect(missing.XDG_RUNTIME_DIR).toBeUndefined();+ }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),+);+it.effect("constructs and inspects an unconfigured host without installing or starting helpers",() =>diff --git a/apps/server/src/device/LocalDeviceHost.ts b/apps/server/src/device/LocalDeviceHost.tsindex 5d0ba5176a1..feacf45aac8 100644--- a/apps/server/src/device/LocalDeviceHost.ts+++ b/apps/server/src/device/LocalDeviceHost.ts@@ -19,7 +19,11 @@ import {LOCAL_DEVICE_HOST_ID,} from "@t3tools/contracts";import { waitForHttpReady } from "@t3tools/shared/httpReadiness";-import { HostProcessEnvironment, HostProcessPlatform } from "@t3tools/shared/hostProcess";+import {+ HostProcessEnvironment,+ HostProcessPlatform,+ HostProcessUserId,+} from "@t3tools/shared/hostProcess";import {resolveNodeExecutable,type NodeRuntimeUnavailableError,@@ -32,6 +36,7 @@ import * as Effect from "effect/Effect";import * as Exit from "effect/Exit";import * as FileSystem from "effect/FileSystem";import * as Layer from "effect/Layer";+import * as Option from "effect/Option";import * as Path from "effect/Path";import * as Option from "effect/Option";import * as Ref from "effect/Ref";@@ -199,6 +204,28 @@ const deviceHostEnvironment = (: environment;};+const hubEnvironment = Effect.fn("LocalDeviceHost.hubEnvironment")(function* (+ environment: NodeJS.ProcessEnv,+) {+ const env: NodeJS.ProcessEnv = { ...environment, FORCE_COLOR: "0", NO_COLOR: "1" };+ const platform = yield* HostProcessPlatform;+ const uid = yield* HostProcessUserId;+ if (platform === "linux" && env.XDG_RUNTIME_DIR === undefined && uid !== undefined) {+ // SSH sessions may omit the directory where the emulator publishes its gRPC token.+ const runtimeDir = `/run/user/${uid}`;+ const fs = yield* FileSystem.FileSystem;+ const stat = yield* fs.stat(runtimeDir).pipe(Effect.option);+ if (+ stat._tag === "Some" &&+ stat.value.type === "Directory" &&+ Option.contains(stat.value.uid, uid)+ ) {+ env.XDG_RUNTIME_DIR = runtimeDir;+ }+ }+ return env;+});+export const make = Effect.fn("LocalDeviceHost.make")(function* () {const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;const config = yield* ServerConfig.ServerConfig;@@ -267,12 +294,6 @@ export const make = Effect.fn("LocalDeviceHost.make")(function* () {};});- const hubEnvironment = (): NodeJS.ProcessEnv => ({- ...hostEnvironment,- FORCE_COLOR: "0",- NO_COLOR: "1",- });-const stopHub = (hub: HubProcess | undefined) =>hub ? Scope.close(hub.scope, Exit.void).pipe(Effect.ignore) : Effect.void;@@ -376,7 +397,10 @@ export const make = Effect.fn("LocalDeviceHost.make")(function* () {shell: false,stdout: "pipe",stderr: "pipe",- env: hubEnvironment(),+ env: yield* hubEnvironment(hostEnvironment).pipe(+ Effect.provideService(FileSystem.FileSystem, fs),+ Effect.provideService(HostProcessPlatform, hostPlatform),+ ),},),)@@ -766,4 +790,5 @@ export const __testing = {androidSdk,platformReason,deviceHostEnvironment,+ hubEnvironment,};