Skip to content

Commit 6fe782c

Browse files
fix(server): Windows drive paths open under WSL (#26030)
1 parent 2a21f41 commit 6fe782c

6 files changed

Lines changed: 167 additions & 4 deletions

File tree

‎apps/server/src/assets/AssetAccess.test.ts‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import { vi } from "vite-plus/test";
2828
import * as AssetSigner from "./AssetSigner.ts";
2929
import * as ServerSecretStore from "../auth/ServerSecretStore.ts";
3030
import * as ServerConfig from "../config.ts";
31+
import { makeFakeWslHost } from "../testUtils/fakeWsl.ts";
3132
import * as Orchestrator from "../orchestration-v2/Orchestrator.ts";
3233
import * as ProjectFaviconResolver from "../project/ProjectFaviconResolver.ts";
3334
import * as T3ProjectFileLoader from "../project/T3ProjectFileLoader.ts";
@@ -570,6 +571,35 @@ describe("AssetAccess", () => {
570571
}).pipe(Effect.provide(layerTest)),
571572
);
572573

574+
it.effect.skipIf(HostProcess.Platform.defaultValue() === "win32")(
575+
"resolves a Windows drive media path under WSL to where wslpath mounts it",
576+
() =>
577+
Effect.gen(function* () {
578+
const fs = yield* FileSystem.FileSystem;
579+
const path = yield* Path.Path;
580+
const { mountRoot, underWsl } = yield* makeFakeWslHost;
581+
const workspaceRoot = yield* fs.makeTempDirectoryScoped({ prefix: "t3-media-wsl-root-" });
582+
const filePath = path.join(mountRoot, "c", "Users", "me", "Pictures", "shot.png");
583+
yield* fs.makeDirectory(path.dirname(filePath), { recursive: true });
584+
yield* fs.writeFile(filePath, screenshotPng);
585+
const canonicalFile = yield* fs.realPath(filePath);
586+
const drivePath = "C:\\Users\\me\\Pictures\\shot.png";
587+
588+
for (const resource of [
589+
{ _tag: "media-file", threadId: ThreadId.make("thread-1"), path: drivePath },
590+
{ _tag: "draft-workspace-file", cwd: workspaceRoot, path: drivePath },
591+
] as const) {
592+
const result = yield* underWsl(issueAssetUrl({ resource, workspaceRoot }));
593+
expect(result.imageDimensions).toEqual({ width: 390, height: 844 });
594+
const suffix = result.relativeUrl.slice(`${ASSET_ROUTE_PREFIX}/`.length);
595+
const separator = suffix.indexOf("/");
596+
expect(
597+
yield* resolveAsset(suffix.slice(0, separator), suffix.slice(separator + 1)),
598+
).toMatchObject({ kind: "file", path: canonicalFile, mimeType: "image/png" });
599+
}
600+
}).pipe(Effect.provide(layerTest)),
601+
);
602+
573603
// Resolving `file://attacker.test/share/cat.jpg` from an agent's markdown sent the
574604
// Windows user's NTLM credentials to attacker.test.
575605
it.effect("never resolves a network media path outside the thread's project", () =>

‎apps/server/src/assets/AssetAccess.ts‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ import { expandHomePath } from "@t3tools/provider-core/server/pathExpansion";
5757
import * as ProjectFaviconResolver from "../project/ProjectFaviconResolver.ts";
5858
import { isWithinRoot } from "../workspace/KnownWorkspaces.ts";
5959
import * as WorkspacePaths from "../workspace/WorkspacePaths.ts";
60+
import { toWslDrivePath } from "../workspace/wslDrivePath.ts";
6061
import * as NativeAppIconResolver from "./NativeAppIconResolver.ts";
6162
import { sourceControlDefinitions } from "../sourceControl/sourceControlDefinitions.ts";
6263
import { extractIcnsPng } from "./icns.ts";
@@ -548,7 +549,10 @@ export const issueAssetUrl = Effect.fn("AssetAccess.issueAssetUrl")(function* (i
548549

549550
switch (input.resource._tag) {
550551
case "media-file": {
551-
let requestedPath = expandHomePath(input.resource.path, yield* HostProcess.HomeDirectory);
552+
let requestedPath = expandHomePath(
553+
yield* toWslDrivePath(input.resource.path),
554+
yield* HostProcess.HomeDirectory,
555+
);
552556
if (!path.isAbsolute(requestedPath)) {
553557
if (!input.workspaceRoot) {
554558
return yield* new AssetWorkspaceContextNotFoundError({ resource: input.resource });
@@ -605,7 +609,7 @@ export const issueAssetUrl = Effect.fn("AssetAccess.issueAssetUrl")(function* (i
605609
const draftWorkspaceRoot = input.workspaceRoot ?? input.resource.cwd;
606610
const hostPath = input.resource.path.startsWith("~/")
607611
? expandHomePath(input.resource.path, yield* HostProcess.HomeDirectory)
608-
: input.resource.path;
612+
: yield* toWslDrivePath(input.resource.path);
609613
if (path.isAbsolute(hostPath)) {
610614
const finalized = yield* finalizeAbsoluteMediaFileAsset({
611615
requestedPath: hostPath,
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import * as HostProcess from "@t3tools/shared/HostProcess";
2+
import * as Effect from "effect/Effect";
3+
import * as FileSystem from "effect/FileSystem";
4+
import * as Path from "effect/Path";
5+
6+
// `wslpath` honours the distro's configurable automount root, so the fake maps drives under a temp dir.
7+
export const makeFakeWslHost = Effect.gen(function* () {
8+
const fileSystem = yield* FileSystem.FileSystem;
9+
const path = yield* Path.Path;
10+
const mountRoot = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3code-wsl-mnt-" });
11+
const binDir = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3code-wsl-bin-" });
12+
const wslpath = path.join(binDir, "wslpath");
13+
yield* fileSystem
14+
.writeFileString(
15+
wslpath,
16+
[
17+
"#!/bin/sh",
18+
'[ "$1" = "-u" ] || exit 1',
19+
"p=$(printf '%s' \"$2\" | tr '\\\\' '/')",
20+
"drive=$(printf '%s' \"$p\" | cut -c1 | tr 'A-Z' 'a-z')",
21+
`printf '%s/%s%s\\n' '${mountRoot}' "$drive" "\${p#?:}"`,
22+
"",
23+
].join("\n"),
24+
)
25+
.pipe(Effect.orDie);
26+
yield* fileSystem.chmod(wslpath, 0o755).pipe(Effect.orDie);
27+
const environment = {
28+
...HostProcess.Environment.defaultValue(),
29+
PATH: `${binDir}:${HostProcess.Environment.defaultValue().PATH ?? ""}`,
30+
};
31+
const underWsl = <A, E, R>(effect: Effect.Effect<A, E, R>) =>
32+
effect.pipe(
33+
Effect.provideService(HostProcess.Platform, "linux"),
34+
Effect.provideService(HostProcess.Environment, environment),
35+
);
36+
return { mountRoot, underWsl };
37+
});

‎apps/server/src/workspace/WorkspaceFileSystem.test.ts‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import * as Layer from "effect/Layer";
1212
import * as Path from "effect/Path";
1313

1414
import * as ServerConfig from "../config.ts";
15+
import { makeFakeWslHost } from "../testUtils/fakeWsl.ts";
1516
import * as VcsDriverRegistry from "../vcs/VcsDriverRegistry.ts";
1617
import * as VcsProcess from "../vcs/VcsProcess.ts";
1718
import * as KnownWorkspaces from "./KnownWorkspaces.ts";
@@ -96,6 +97,14 @@ const writeTextFile = Effect.fn("writeTextFile")(function* (
9697
yield* fileSystem.writeFileString(absolutePath, contents).pipe(Effect.orDie);
9798
});
9899

100+
const makeWslHost = Effect.gen(function* () {
101+
const { mountRoot, underWsl } = yield* makeFakeWslHost;
102+
const service = yield* underWsl(
103+
WorkspaceFileSystem.make.pipe(Effect.provide(layerKnownWorkspacesTest)),
104+
);
105+
return { mountRoot, service, underWsl };
106+
});
107+
99108
it.layer(layerTest, { excludeTestServices: true })("WorkspaceFileSystemLive", (it) => {
100109
describe("getMetadata", () => {
101110
it.effect(
@@ -181,6 +190,31 @@ it.layer(layerTest, { excludeTestServices: true })("WorkspaceFileSystemLive", (i
181190
expect(statted).toEqual([]);
182191
}),
183192
);
193+
194+
it.effect.skipIf(HostProcess.Platform.defaultValue() === "win32")(
195+
"reports a Windows drive path under WSL as the file it maps to",
196+
() =>
197+
Effect.gen(function* () {
198+
const { mountRoot, service, underWsl } = yield* makeWslHost;
199+
yield* writeTextFile(mountRoot, "c/Users/me/Documents/notes.md", "# Notes\n");
200+
201+
const { entries } = yield* underWsl(
202+
service.getMetadata({
203+
paths: [
204+
"C:\\Users\\me\\Documents\\notes.md",
205+
"C:/Users/me/Documents/notes.md",
206+
"C:/Users/me/Documents/notes\u0000.md",
207+
],
208+
}),
209+
);
210+
211+
expect(entries).toEqual([
212+
{ kind: "file", byteLength: 8 },
213+
{ kind: "file", byteLength: 8 },
214+
null,
215+
]);
216+
}),
217+
);
184218
});
185219

186220
describe("readFile", () => {
@@ -351,6 +385,24 @@ it.layer(layerTest, { excludeTestServices: true })("WorkspaceFileSystemLive", (i
351385
}),
352386
);
353387

388+
it.effect.skipIf(HostProcess.Platform.defaultValue() === "win32")(
389+
"reads a Windows drive path under WSL from where wslpath mounts it",
390+
() =>
391+
Effect.gen(function* () {
392+
const { mountRoot, service, underWsl } = yield* makeWslHost;
393+
const cwd = yield* makeTempDir;
394+
yield* writeTextFile(mountRoot, "c/Users/me/Documents/notes.md", "# Notes\n");
395+
396+
for (const relativePath of [
397+
"C:\\Users\\me\\Documents\\notes.md",
398+
"C:/Users/me/Documents/notes.md",
399+
]) {
400+
const result = yield* underWsl(service.readFile({ cwd, relativePath }));
401+
expect(result).toMatchObject({ contents: "# Notes\n", truncated: false });
402+
}
403+
}),
404+
);
405+
354406
it.effect("refuses a network path outside the workspace", () =>
355407
Effect.gen(function* () {
356408
const workspaceFileSystem = yield* WorkspaceFileSystem.WorkspaceFileSystem;

‎apps/server/src/workspace/WorkspaceFileSystem.ts‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,15 @@ import * as Effect from "effect/Effect";
1919
import * as FileSystem from "effect/FileSystem";
2020
import * as Layer from "effect/Layer";
2121
import * as Path from "effect/Path";
22+
import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
2223
import * as Schema from "effect/Schema";
2324

2425
import { makeIsServerInternalPath } from "../auth/serverInternalPaths.ts";
2526
import * as KnownWorkspaces from "./KnownWorkspaces.ts";
2627
import * as WorkspaceEntries from "./WorkspaceEntries.ts";
2728
import * as WorkspacePaths from "./WorkspacePaths.ts";
2829
import { fileHeaderMimeType } from "./fileHeaderMimeType.ts";
30+
import { toWslDrivePath } from "./wslDrivePath.ts";
2931

3032
const PROJECT_READ_FILE_MAX_BYTES = 1024 * 1024;
3133

@@ -141,9 +143,18 @@ export const make = Effect.gen(function* () {
141143
const knownWorkspaces = yield* KnownWorkspaces.KnownWorkspaces;
142144
const workspaceEntries = yield* WorkspaceEntries.WorkspaceEntries;
143145
const isServerInternalPath = yield* makeIsServerInternalPath;
146+
const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
147+
148+
const toHostPath = (requestedPath: string) =>
149+
toWslDrivePath(requestedPath).pipe(
150+
Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
151+
);
144152

145153
const metadataForPath = Effect.fnUntraced(function* (requestedPath: string) {
146-
const expandedPath = expandHomePath(requestedPath, yield* HostProcess.HomeDirectory);
154+
const expandedPath = expandHomePath(
155+
yield* toHostPath(requestedPath),
156+
yield* HostProcess.HomeDirectory,
157+
);
147158
// Chips ask as soon as an agent's message renders, and touching a network
148159
// path makes Windows sign in to its host with the user's NTLM credentials.
149160
if (!path.isAbsolute(expandedPath) || isNetworkPath(expandedPath)) return null;
@@ -230,7 +241,7 @@ export const make = Effect.gen(function* () {
230241
const requestedPath = input.relativePath;
231242
const hostPath = requestedPath.startsWith("~/")
232243
? expandHomePath(requestedPath, yield* HostProcess.HomeDirectory)
233-
: requestedPath;
244+
: yield* toHostPath(requestedPath);
234245
if (
235246
isNetworkPath(hostPath) &&
236247
!KnownWorkspaces.isWithinRoot(path, path.resolve(hostPath), path.resolve(input.cwd))
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import * as HostProcess from "@t3tools/shared/HostProcess";
2+
import { isWindowsDrivePath } from "@t3tools/shared/path";
3+
import * as Effect from "effect/Effect";
4+
import * as ChildProcess from "effect/process/ChildProcess";
5+
import * 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.
10+
export 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+
});

0 commit comments

Comments
 (0)