Skip to content

Commit 4d6ebbd

Browse files
RJWadleymaria-rcksjuliusmarminge
authored
fix(wsl): label WSL environments with their Windows host on every client (#22629)
Co-authored-by: maria-rcks <maria@kuuro.net> Co-authored-by: Julius Marminge <julius0216@outlook.com>
1 parent 1483e97 commit 4d6ebbd

2 files changed

Lines changed: 67 additions & 0 deletions

File tree

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

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,54 @@ it.effect("falls back to the hostname when hostnamectl never answers", () =>
5050
assert.equal(yield* Fiber.join(label), "devbox");
5151
}),
5252
);
53+
54+
const exitsWithFailure = ProcessRunner.ProcessRunner.of({
55+
run: () =>
56+
Effect.succeed({
57+
stdout: "",
58+
stderr: "",
59+
code: ChildProcessSpawner.ExitCode(1),
60+
timedOut: false,
61+
stdoutTruncated: false,
62+
stderrTruncated: false,
63+
stdoutInvalidUtf8: false,
64+
stderrInvalidUtf8: false,
65+
}),
66+
});
67+
68+
it.effect("qualifies the inherited Windows hostname only inside WSL", () =>
69+
Effect.gen(function* () {
70+
const cases = [
71+
{
72+
kernel: "6.6.87.2-microsoft-standard-WSL2",
73+
machineInfo: null,
74+
expected: "DESKTOP-PC WSL (Ubuntu)",
75+
},
76+
{ kernel: "6.8.0-45-generic", machineInfo: null, expected: "DESKTOP-PC" },
77+
{
78+
kernel: "6.6.87.2-microsoft-standard-WSL2",
79+
machineInfo: 'PRETTY_HOSTNAME="Work WSL"',
80+
expected: "Work WSL",
81+
},
82+
];
83+
for (const { kernel, machineInfo, expected } of cases) {
84+
const files: Record<string, string | null> = {
85+
"/proc/sys/kernel/osrelease": `${kernel}\n`,
86+
"/etc/machine-info": machineInfo,
87+
};
88+
const label = yield* resolveServerEnvironmentLabel({ cwdBaseName: "project" }).pipe(
89+
Effect.provideService(HostProcess.Platform, "linux"),
90+
Effect.provideService(HostProcess.Hostname, "DESKTOP-PC"),
91+
Effect.provideService(HostProcess.Environment, { WSL_DISTRO_NAME: "Ubuntu" }),
92+
Effect.provideService(ProcessRunner.ProcessRunner, exitsWithFailure),
93+
Effect.provide(
94+
FileSystem.layerNoop({
95+
exists: (path) => Effect.succeed(files[path] != null),
96+
readFileString: (path) => Effect.succeed(files[path] ?? ""),
97+
}),
98+
),
99+
);
100+
assert.equal(label, expected, kernel);
101+
}
102+
}),
103+
);

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,17 @@ const resolveFriendlyHostLabel = Effect.fn("resolveFriendlyHostLabel")(function*
181181
return null;
182182
});
183183

184+
const isWsl = Effect.fn("isWsl")(function* () {
185+
if ((yield* HostProcess.Platform) !== "linux") {
186+
return false;
187+
}
188+
const fileSystem = yield* FileSystem.FileSystem;
189+
const kernelRelease = yield* fileSystem
190+
.readFileString("/proc/sys/kernel/osrelease")
191+
.pipe(Effect.orElseSucceed(() => ""));
192+
return kernelRelease.toLowerCase().includes("microsoft");
193+
});
194+
184195
export const resolveServerEnvironmentLabel = Effect.fn("resolveServerEnvironmentLabel")(function* (
185196
input: ResolveServerEnvironmentLabelInput,
186197
) {
@@ -191,6 +202,11 @@ export const resolveServerEnvironmentLabel = Effect.fn("resolveServerEnvironment
191202

192203
const hostname = normalizeLabel(yield* HostProcess.Hostname);
193204
if (hostname) {
205+
// WSL inherits the Windows hostname, so qualify it to tell the two environments apart.
206+
if (yield* isWsl()) {
207+
const distro = normalizeLabel((yield* HostProcess.Environment).WSL_DISTRO_NAME);
208+
return distro ? `${hostname} WSL (${distro})` : `${hostname} WSL`;
209+
}
194210
return hostname;
195211
}
196212

0 commit comments

Comments
 (0)