Skip to content

Commit e76d149

Browse files
RJWadleyclaude
andcommitted
fix(wsl): label WSL environments with their Windows host on every client
WSL inherits the Windows computer name as its hostname, so remote clients showed the Windows and WSL servers under the same name. The server now reports "HOST WSL (distro)" when running in WSL, and the desktop app uses the same format instead of "Windows" / "WSL (distro)", so the host and remote clients agree. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 07066c0 commit e76d149

9 files changed

Lines changed: 129 additions & 13 deletions

File tree

‎apps/desktop/src/backend/DesktopBackendConfiguration.ts‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import * as NodeOS from "node:os";
22

33
import { parsePersistedServerObservabilitySettings } from "@t3tools/shared/serverSettings";
44
import { currentDesktopBootstrapToken } from "@t3tools/shared/desktopBootstrapToken";
5+
import { wslEnvironmentLabel } from "@t3tools/shared/environmentLabel";
6+
import * as HostProcess from "@t3tools/shared/HostProcess";
57
import * as Clock from "effect/Clock";
68
import * as Context from "effect/Context";
79
import * as Crypto from "effect/Crypto";
@@ -824,10 +826,11 @@ export const make = Effect.gen(function* () {
824826
}).pipe(Effect.withSpan("desktop.backendConfiguration.resolvePrimary")),
825827
resolvePrimaryLabel: Effect.gen(function* () {
826828
const { useWsl, distro } = yield* describePrimary;
829+
const hostname = yield* HostProcess.Hostname;
827830
if (!useWsl) {
828-
return environment.platform === "win32" ? "Windows" : "Local environment";
831+
return environment.platform === "win32" ? hostname : "Local environment";
829832
}
830-
return distro ? `WSL (${distro})` : "WSL";
833+
return wslEnvironmentLabel(hostname, distro);
831834
}).pipe(Effect.withSpan("desktop.backendConfiguration.resolvePrimaryLabel")),
832835
// The span options depend on each request's port and distro.
833836
resolveWsl: Effect.fnUntraced(

‎apps/desktop/src/ipc/methods/window.test.ts‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ const readyWslConfig: DesktopBackendManager.DesktopBackendStartConfig = {
5151

5252
const defaultWslInstance: DesktopBackendManager.DesktopBackendInstance = {
5353
id: DesktopBackendManager.BackendInstanceId("wsl:default"),
54-
label: Effect.succeed("WSL (default distro)"),
54+
label: Effect.succeed("DESKTOP-PC WSL"),
5555
start: Effect.void,
5656
stop: () => Effect.void,
5757
currentConfig: Effect.succeedSome(readyWslConfig),
@@ -76,7 +76,11 @@ const backendConfigurationLayer = Layer.succeed(
7676
);
7777

7878
const bootstrapsLayer = (instances: ReadonlyArray<DesktopBackendManager.DesktopBackendInstance>) =>
79-
Layer.merge(DesktopBackendPool.layerTest([...instances]), backendConfigurationLayer);
79+
Layer.mergeAll(
80+
DesktopBackendPool.layerTest([...instances]),
81+
backendConfigurationLayer,
82+
Layer.succeed(HostProcess.Hostname, "DESKTOP-PC"),
83+
);
8084

8185
describe("getLocalEnvironmentBootstraps", () => {
8286
it.effect("hands out the current window's token to a backend launched with the secret", () =>
@@ -86,7 +90,7 @@ describe("getLocalEnvironmentBootstraps", () => {
8690
assert.deepEqual(result, [
8791
{
8892
id: "wsl:default",
89-
label: "WSL (Ubuntu)",
93+
label: "DESKTOP-PC WSL (Ubuntu)",
9094
runningDistro: "Ubuntu",
9195
httpBaseUrl: "http://127.0.0.1:3774/",
9296
wsBaseUrl: "ws://127.0.0.1:3774/",
@@ -137,7 +141,7 @@ describe("getLocalEnvironmentBootstraps", () => {
137141
assert.deepEqual(result, [
138142
{
139143
id: "wsl:default",
140-
label: "WSL (default distro)",
144+
label: "DESKTOP-PC WSL",
141145
runningDistro: null,
142146
httpBaseUrl: null,
143147
wsBaseUrl: null,

‎apps/desktop/src/ipc/methods/window.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
} from "@t3tools/contracts";
1616
import { PROJECT_FAVICON_EXTENSIONS } from "@t3tools/shared/projectFavicon";
1717
import { resolveEditorCommand } from "@t3tools/shared/editor";
18+
import { wslEnvironmentLabel } from "@t3tools/shared/environmentLabel";
1819
import * as HostProcess from "@t3tools/shared/HostProcess";
1920
import { isRelayTracingEnabled } from "@t3tools/shared/otelEnvironment";
2021
import * as FileSystem from "effect/FileSystem";
@@ -134,7 +135,10 @@ export const getLocalEnvironmentBootstraps = DesktopIpc.makeSyncIpcMethod({
134135
const runningDistro = config.value.runningDistro ?? null;
135136
bootstraps.push({
136137
id: instance.id,
137-
label: runningDistro === null ? yield* instance.label : `WSL (${runningDistro})`,
138+
label:
139+
runningDistro === null
140+
? yield* instance.label
141+
: wslEnvironmentLabel(yield* HostProcess.Hostname, runningDistro),
138142
runningDistro,
139143
httpBaseUrl: httpBaseUrl.href,
140144
wsBaseUrl: toWebSocketBaseUrl(httpBaseUrl),

‎apps/desktop/src/wsl/DesktopWslBackend.ts‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import * as Option from "effect/Option";
66
import * as Ref from "effect/Ref";
77
import * as Semaphore from "effect/Semaphore";
88

9+
import { wslEnvironmentLabel } from "@t3tools/shared/environmentLabel";
10+
import * as HostProcess from "@t3tools/shared/HostProcess";
911
import * as NetService from "@t3tools/shared/Net";
1012

1113
import * as DesktopObservability from "../app/DesktopObservability.ts";
@@ -38,9 +40,6 @@ const resolveTargetInstanceId = (distro: string | null): DesktopBackendPool.Back
3840
const isWslInstanceId = (id: DesktopBackendPool.BackendInstanceId): boolean =>
3941
id.startsWith(WSL_INSTANCE_ID_PREFIX);
4042

41-
const buildLabel = (distro: string | null): string =>
42-
distro === null ? "WSL (default distro)" : `WSL (${distro})`;
43-
4443
// WSL auto-forwards loopback ports, so only IPv4 loopback availability matters.
4544
const scanForWslPort = Effect.fn("desktop.wslBackend.scanForWslPort")(function* (
4645
startPort: number,
@@ -65,6 +64,7 @@ export const layer = Layer.effect(
6564
const wslEnvironment = yield* DesktopWslEnvironment.DesktopWslEnvironment;
6665
const appSettings = yield* DesktopAppSettings.DesktopAppSettings;
6766
const net = yield* NetService.NetService;
67+
const hostname = yield* HostProcess.Hostname;
6868
// Serialize bootstrap and IPC reconciles so concurrent reads cannot register competing WSL instances.
6969
const reconcileMutex = yield* Semaphore.make(1);
7070

@@ -118,7 +118,7 @@ export const layer = Layer.effect(
118118
const instance = yield* pool
119119
.register({
120120
id: targetId,
121-
label: Effect.succeed(buildLabel(input.distro)),
121+
label: Effect.succeed(wslEnvironmentLabel(hostname, input.distro)),
122122
configResolve: configuration.resolveWsl({ port: allocatedPort, distro: input.distro }),
123123
onPreflightFailed: (failure) =>
124124
Ref.set(preflightErrorRef, Option.some(failure.reason)).pipe(Effect.as(false)),
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import { expect, it } from "@effect/vitest";
2+
import * as HostProcess from "@t3tools/shared/HostProcess";
3+
import * as Effect from "effect/Effect";
4+
import * as FileSystem from "effect/FileSystem";
5+
import * as Layer from "effect/Layer";
6+
import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
7+
8+
import * as ProcessRunner from "../processRunner.ts";
9+
import { resolveServerEnvironmentLabel } from "./ServerEnvironmentLabel.ts";
10+
11+
const resolveLinuxLabel = (input: {
12+
readonly kernelRelease: string;
13+
readonly env: NodeJS.ProcessEnv;
14+
}) =>
15+
resolveServerEnvironmentLabel({ cwdBaseName: "project" }).pipe(
16+
Effect.provideService(
17+
ProcessRunner.ProcessRunner,
18+
ProcessRunner.ProcessRunner.of({
19+
run: () =>
20+
Effect.succeed({
21+
stdout: "",
22+
stderr: "",
23+
code: ChildProcessSpawner.ExitCode(1),
24+
timedOut: false,
25+
stdoutTruncated: false,
26+
stderrTruncated: false,
27+
stdoutInvalidUtf8: false,
28+
stderrInvalidUtf8: false,
29+
}),
30+
}),
31+
),
32+
Effect.provide(
33+
Layer.mergeAll(
34+
FileSystem.layerNoop({
35+
exists: () => Effect.succeed(false),
36+
readFileString: (path) =>
37+
path === "/proc/sys/kernel/osrelease"
38+
? Effect.succeed(input.kernelRelease)
39+
: Effect.die(`unexpected read: ${path}`),
40+
}),
41+
Layer.succeed(HostProcess.Platform, "linux"),
42+
Layer.succeed(HostProcess.Hostname, "DESKTOP-PC"),
43+
Layer.succeed(HostProcess.Environment, input.env),
44+
),
45+
),
46+
);
47+
48+
it.effect("labels a WSL server with its host and distro", () =>
49+
Effect.gen(function* () {
50+
const label = yield* resolveLinuxLabel({
51+
kernelRelease: "6.6.87.2-microsoft-standard-WSL2\n",
52+
env: { WSL_DISTRO_NAME: "Ubuntu" },
53+
});
54+
expect(label).toBe("DESKTOP-PC WSL (Ubuntu)");
55+
}),
56+
);
57+
58+
it.effect("labels a WSL server without a distro name by its host", () =>
59+
Effect.gen(function* () {
60+
const label = yield* resolveLinuxLabel({
61+
kernelRelease: "6.6.87.2-microsoft-standard-WSL2\n",
62+
env: {},
63+
});
64+
expect(label).toBe("DESKTOP-PC WSL");
65+
}),
66+
);
67+
68+
it.effect("keeps the hostname for non-WSL Linux", () =>
69+
Effect.gen(function* () {
70+
const label = yield* resolveLinuxLabel({
71+
kernelRelease: "6.8.0-45-generic\n",
72+
env: { WSL_DISTRO_NAME: "Ubuntu" },
73+
});
74+
expect(label).toBe("DESKTOP-PC");
75+
}),
76+
);

‎apps/server/src/environment/ServerEnvironmentLabel.ts‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { wslEnvironmentLabel } from "@t3tools/shared/environmentLabel";
12
import * as HostProcess from "@t3tools/shared/HostProcess";
23
import * as Effect from "effect/Effect";
34
import * as FileSystem from "effect/FileSystem";
@@ -179,15 +180,31 @@ const resolveFriendlyHostLabel = Effect.fn("resolveFriendlyHostLabel")(function*
179180
return null;
180181
});
181182

183+
const isWsl = Effect.fn("isWsl")(function* () {
184+
if ((yield* HostProcess.Platform) !== "linux") {
185+
return false;
186+
}
187+
const fileSystem = yield* FileSystem.FileSystem;
188+
const kernelRelease = yield* fileSystem
189+
.readFileString("/proc/sys/kernel/osrelease")
190+
.pipe(Effect.orElseSucceed(() => ""));
191+
return kernelRelease.toLowerCase().includes("microsoft");
192+
});
193+
182194
export const resolveServerEnvironmentLabel = Effect.fn("resolveServerEnvironmentLabel")(function* (
183195
input: ResolveServerEnvironmentLabelInput,
184196
) {
197+
const hostname = normalizeLabel(yield* HostProcess.Hostname);
198+
if (hostname && (yield* isWsl())) {
199+
const distro = normalizeLabel((yield* HostProcess.Environment).WSL_DISTRO_NAME);
200+
return wslEnvironmentLabel(hostname, distro);
201+
}
202+
185203
const friendlyHostLabel = yield* resolveFriendlyHostLabel();
186204
if (friendlyHostLabel) {
187205
return friendlyHostLabel;
188206
}
189207

190-
const hostname = normalizeLabel(yield* HostProcess.Hostname);
191208
if (hostname) {
192209
return hostname;
193210
}

‎apps/web/src/connection/platform.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,8 @@ const loadSecondaryConnectionRegistration = Effect.fn(
466466
}).pipe(Effect.mapError(mapRemoteEnvironmentError));
467467
// Desktop operations need the pool backend id to reach the owning instance; RPC uses the environment id.
468468
const connectionId = desktopLocalConnectionId(entry.id);
469-
const label = entry.label || descriptor.label;
469+
// The server's label is what every other client shows for this environment.
470+
const label = descriptor.label || entry.label;
470471
return {
471472
registration: new BearerConnectionRegistration({
472473
target: new BearerConnectionTarget({

‎packages/shared/package.json‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@
6767
"types": "./src/observability.ts",
6868
"import": "./src/observability.ts"
6969
},
70+
"./environmentLabel": {
71+
"types": "./src/environmentLabel.ts",
72+
"import": "./src/environmentLabel.ts"
73+
},
7074
"./otelEnvironment": {
7175
"types": "./src/otelEnvironment.ts",
7276
"import": "./src/otelEnvironment.ts"
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// WSL mirrors the Windows computer name into its hostname, so a WSL server
2+
// and its Windows host need the distro in the label to tell them apart. The
3+
// server and the desktop app both label WSL environments with this, so the
4+
// host and remote clients show the same name.
5+
export function wslEnvironmentLabel(hostname: string, distro: string | null): string {
6+
return distro ? `${hostname} WSL (${distro})` : `${hostname} WSL`;
7+
}

0 commit comments

Comments
 (0)