Open History to see the last commit
1import * as HostProcess from "@t3tools/shared/HostProcess";
2import { isWindowsDrivePath } from "@t3tools/shared/path";
3import * as Effect from "effect/Effect";
4import * as ChildProcess from "effect/process/ChildProcess";
5import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
6
7// WSL mounts Windows drives under a configurable automount root, so only wslpath knows where.
8// No WSL env check: servers started by systemd lack WSL_DISTRO_NAME, and off WSL the spawn just fails.
9// Untraced: it runs for every chip path, and only drive paths on Linux do any work.
10export const toWslDrivePath = Effect.fnUntraced(function* (requestedPath: string) {
11 // spawn throws on a NUL byte instead of failing.
12 if (!isWindowsDrivePath(requestedPath) || requestedPath.includes("\0")) return requestedPath;
13 if ((yield* HostProcess.Platform) !== "linux") return requestedPath;
14 const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
15 const output = yield* spawner
16 .string(
17 ChildProcess.make("wslpath", ["-u", requestedPath], {
18 env: yield* HostProcess.Environment,
19 stdin: "ignore",
20 stderr: "ignore",
21 }),
22 )
23 .pipe(
24 Effect.timeout("1 second"),
25 Effect.orElseSucceed(() => ""),
26 );
27 const linuxPath = output.endsWith("\n") ? output.slice(0, -1) : output;
28 return linuxPath.startsWith("/") ? linuxPath : requestedPath;
29});