Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions apps/server/src/environment/ServerEnvironmentLabel.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,54 @@ it.effect("falls back to the hostname when hostnamectl never answers", () =>
assert.equal(yield* Fiber.join(label), "devbox");
}),
);

const exitsWithFailure = ProcessRunner.ProcessRunner.of({
run: () =>
Effect.succeed({
stdout: "",
stderr: "",
code: ChildProcessSpawner.ExitCode(1),
timedOut: false,
stdoutTruncated: false,
stderrTruncated: false,
stdoutInvalidUtf8: false,
stderrInvalidUtf8: false,
}),
});

it.effect("qualifies the inherited Windows hostname only inside WSL", () =>
Effect.gen(function* () {
const cases = [
{
kernel: "6.6.87.2-microsoft-standard-WSL2",
machineInfo: null,
expected: "DESKTOP-PC WSL (Ubuntu)",
},
{ kernel: "6.8.0-45-generic", machineInfo: null, expected: "DESKTOP-PC" },
{
kernel: "6.6.87.2-microsoft-standard-WSL2",
machineInfo: 'PRETTY_HOSTNAME="Work WSL"',
expected: "Work WSL",
},
];
for (const { kernel, machineInfo, expected } of cases) {
const files: Record<string, string | null> = {
"/proc/sys/kernel/osrelease": `${kernel}\n`,
"/etc/machine-info": machineInfo,
};
const label = yield* resolveServerEnvironmentLabel({ cwdBaseName: "project" }).pipe(
Effect.provideService(HostProcess.Platform, "linux"),
Effect.provideService(HostProcess.Hostname, "DESKTOP-PC"),
Effect.provideService(HostProcess.Environment, { WSL_DISTRO_NAME: "Ubuntu" }),
Effect.provideService(ProcessRunner.ProcessRunner, exitsWithFailure),
Effect.provide(
FileSystem.layerNoop({
exists: (path) => Effect.succeed(files[path] != null),
readFileString: (path) => Effect.succeed(files[path] ?? ""),
}),
),
);
assert.equal(label, expected, kernel);
}
}),
);
16 changes: 16 additions & 0 deletions apps/server/src/environment/ServerEnvironmentLabel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,17 @@ const resolveFriendlyHostLabel = Effect.fn("resolveFriendlyHostLabel")(function*
return null;
});

const isWsl = Effect.fn("isWsl")(function* () {
if ((yield* HostProcess.Platform) !== "linux") {
return false;
}
const fileSystem = yield* FileSystem.FileSystem;
const kernelRelease = yield* fileSystem
.readFileString("/proc/sys/kernel/osrelease")
.pipe(Effect.orElseSucceed(() => ""));
return kernelRelease.toLowerCase().includes("microsoft");
});

export const resolveServerEnvironmentLabel = Effect.fn("resolveServerEnvironmentLabel")(function* (
input: ResolveServerEnvironmentLabelInput,
) {
Expand All @@ -191,6 +202,11 @@ export const resolveServerEnvironmentLabel = Effect.fn("resolveServerEnvironment

const hostname = normalizeLabel(yield* HostProcess.Hostname);
if (hostname) {
// WSL inherits the Windows hostname, so qualify it to tell the two environments apart.
if (yield* isWsl()) {
const distro = normalizeLabel((yield* HostProcess.Environment).WSL_DISTRO_NAME);
return distro ? `${hostname} WSL (${distro})` : `${hostname} WSL`;
}
return hostname;
}

Expand Down
Loading