Skip to content

Commit f729e0e

Browse files
authored
fix(server): recover Linux runtime directory for device hub (pingdotgg#12402)
1 parent f4153a7 commit f729e0e

2 files changed

Lines changed: 83 additions & 8 deletions

File tree

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

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@ import {
44
HostProcessEnvironment,
55
HostProcessPlatform,
66
HostProcessIsExecutable,
7+
HostProcessUserId,
78
} from "@t3tools/shared/hostProcess";
89
import * as Effect from "effect/Effect";
910
import * as Layer from "effect/Layer";
11+
import * as Option from "effect/Option";
1012
import * as Path from "effect/Path";
1113
import * as FileSystem from "effect/FileSystem";
1214

@@ -134,6 +136,54 @@ it.effect("puts detected Android tools on the helper PATH without losing existin
134136
}).pipe(Effect.provide(NodePath.layer)),
135137
);
136138

139+
it.effect(
140+
"recovers an owned Linux runtime directory without overriding explicit environments",
141+
() =>
142+
Effect.gen(function* () {
143+
const fs = yield* FileSystem.FileSystem;
144+
const directory = yield* fs.makeTempDirectoryScoped();
145+
const stat = yield* fs.stat(directory);
146+
for (const [platform, uid, runtimeDir, type, owner, expected] of [
147+
["linux", 1000, undefined, "Directory", 1000, "/run/user/1000"],
148+
["linux", 1000, "/custom/runtime", "Directory", 1000, "/custom/runtime"],
149+
["linux", 1000, "", "Directory", 1000, ""],
150+
["linux", 1000, undefined, "Directory", 1001, undefined],
151+
["linux", 1000, undefined, "File", 1000, undefined],
152+
["linux", undefined, undefined, "Directory", 1000, undefined],
153+
["darwin", 1000, undefined, "Directory", 1000, undefined],
154+
["win32", 1000, undefined, "Directory", 1000, undefined],
155+
] as const) {
156+
const environment = { PATH: "/usr/bin", XDG_RUNTIME_DIR: runtimeDir };
157+
const result = yield* LocalDeviceHost.__testing.hubEnvironment(environment).pipe(
158+
Effect.provideService(HostProcessPlatform, platform),
159+
Effect.provideService(HostProcessUserId, uid),
160+
Effect.provideService(
161+
FileSystem.FileSystem,
162+
FileSystem.makeNoop({
163+
stat: (path) => {
164+
expect(path).toBe("/run/user/1000");
165+
return Effect.succeed({ ...stat, type, uid: Option.some(owner) });
166+
},
167+
}),
168+
),
169+
);
170+
expect(result.XDG_RUNTIME_DIR).toBe(expected);
171+
expect(result.PATH).toBe("/usr/bin");
172+
expect(result.FORCE_COLOR).toBe("0");
173+
expect(result.NO_COLOR).toBe("1");
174+
expect(environment.XDG_RUNTIME_DIR).toBe(runtimeDir);
175+
}
176+
const missing = yield* LocalDeviceHost.__testing
177+
.hubEnvironment({})
178+
.pipe(
179+
Effect.provideService(HostProcessPlatform, "linux"),
180+
Effect.provideService(HostProcessUserId, 1000),
181+
Effect.provideService(FileSystem.FileSystem, FileSystem.makeNoop({})),
182+
);
183+
expect(missing.XDG_RUNTIME_DIR).toBeUndefined();
184+
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
185+
);
186+
137187
it.effect(
138188
"constructs and inspects an unconfigured host without installing or starting helpers",
139189
() =>

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

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@ import {
1919
LOCAL_DEVICE_HOST_ID,
2020
} from "@t3tools/contracts";
2121
import { waitForHttpReady } from "@t3tools/shared/httpReadiness";
22-
import { HostProcessEnvironment, HostProcessPlatform } from "@t3tools/shared/hostProcess";
22+
import {
23+
HostProcessEnvironment,
24+
HostProcessPlatform,
25+
HostProcessUserId,
26+
} from "@t3tools/shared/hostProcess";
2327
import {
2428
resolveNodeExecutable,
2529
type NodeRuntimeUnavailableError,
@@ -32,6 +36,7 @@ import * as Effect from "effect/Effect";
3236
import * as Exit from "effect/Exit";
3337
import * as FileSystem from "effect/FileSystem";
3438
import * as Layer from "effect/Layer";
39+
import * as Option from "effect/Option";
3540
import * as Path from "effect/Path";
3641
import * as Option from "effect/Option";
3742
import * as Ref from "effect/Ref";
@@ -199,6 +204,28 @@ const deviceHostEnvironment = (
199204
: environment;
200205
};
201206

207+
const hubEnvironment = Effect.fn("LocalDeviceHost.hubEnvironment")(function* (
208+
environment: NodeJS.ProcessEnv,
209+
) {
210+
const env: NodeJS.ProcessEnv = { ...environment, FORCE_COLOR: "0", NO_COLOR: "1" };
211+
const platform = yield* HostProcessPlatform;
212+
const uid = yield* HostProcessUserId;
213+
if (platform === "linux" && env.XDG_RUNTIME_DIR === undefined && uid !== undefined) {
214+
// SSH sessions may omit the directory where the emulator publishes its gRPC token.
215+
const runtimeDir = `/run/user/${uid}`;
216+
const fs = yield* FileSystem.FileSystem;
217+
const stat = yield* fs.stat(runtimeDir).pipe(Effect.option);
218+
if (
219+
stat._tag === "Some" &&
220+
stat.value.type === "Directory" &&
221+
Option.contains(stat.value.uid, uid)
222+
) {
223+
env.XDG_RUNTIME_DIR = runtimeDir;
224+
}
225+
}
226+
return env;
227+
});
228+
202229
export const make = Effect.fn("LocalDeviceHost.make")(function* () {
203230
const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
204231
const config = yield* ServerConfig.ServerConfig;
@@ -267,12 +294,6 @@ export const make = Effect.fn("LocalDeviceHost.make")(function* () {
267294
};
268295
});
269296

270-
const hubEnvironment = (): NodeJS.ProcessEnv => ({
271-
...hostEnvironment,
272-
FORCE_COLOR: "0",
273-
NO_COLOR: "1",
274-
});
275-
276297
const stopHub = (hub: HubProcess | undefined) =>
277298
hub ? Scope.close(hub.scope, Exit.void).pipe(Effect.ignore) : Effect.void;
278299

@@ -376,7 +397,10 @@ export const make = Effect.fn("LocalDeviceHost.make")(function* () {
376397
shell: false,
377398
stdout: "pipe",
378399
stderr: "pipe",
379-
env: hubEnvironment(),
400+
env: yield* hubEnvironment(hostEnvironment).pipe(
401+
Effect.provideService(FileSystem.FileSystem, fs),
402+
Effect.provideService(HostProcessPlatform, hostPlatform),
403+
),
380404
},
381405
),
382406
)
@@ -766,4 +790,5 @@ export const __testing = {
766790
androidSdk,
767791
platformReason,
768792
deviceHostEnvironment,
793+
hubEnvironment,
769794
};

0 commit comments

Comments
 (0)