Open History to see the last commit
1// @effect-diagnostics nodeBuiltinImport:off - FileSystem cannot create a FIFO.
2import * as NodeChildProcess from "node:child_process";
3
4import * as NodeServices from "@effect/platform-node/NodeServices";
5import { it, describe, expect } from "@effect/vitest";
6import * as Effect from "effect/Effect";
7import * as FileSystem from "effect/FileSystem";
8import * as Layer from "effect/Layer";
9import * as Path from "effect/Path";
10
11import * as ServerConfig from "../config.ts";
12import * as VcsDriverRegistry from "../vcs/VcsDriverRegistry.ts";
13import * as VcsProcess from "../vcs/VcsProcess.ts";
14import * as WorkspaceEntries from "./WorkspaceEntries.ts";
15import * as WorkspaceFileSystem from "./WorkspaceFileSystem.ts";
16import * as WorkspacePaths from "./WorkspacePaths.ts";
17import * as HostProcess from "@t3tools/shared/HostProcess";
18import { symlinksSupported } from "@t3tools/shared/testing/symlinks";
19
20const layerProject = WorkspaceFileSystem.layer.pipe(
21 Layer.provide(WorkspacePaths.layer),
22 Layer.provide(WorkspaceEntries.layer.pipe(Layer.provide(WorkspacePaths.layer))),
23);
24
25const layerTest = Layer.empty.pipe(
26 Layer.provideMerge(layerProject),
27 Layer.provideMerge(WorkspaceEntries.layer.pipe(Layer.provide(WorkspacePaths.layer))),
28 Layer.provideMerge(WorkspacePaths.layer),
29 Layer.provideMerge(VcsDriverRegistry.layer.pipe(Layer.provide(VcsProcess.layer))),
30 Layer.provide(
31 ServerConfig.ServerConfig.layerTest(process.cwd(), {
32 prefix: "t3-workspace-files-test-",
33 }),
34 ),
35 Layer.provideMerge(NodeServices.layer),
36);
37
38const makeTempDir = Effect.gen(function* () {
39 const fileSystem = yield* FileSystem.FileSystem;
40 return yield* fileSystem.makeTempDirectoryScoped({
41 prefix: "t3code-workspace-files-",
42 });
43});
44
45const writeTextFile = Effect.fn("writeTextFile")(function* (
46 cwd: string,
47 relativePath: string,
48 contents = "",
49) {
50 const fileSystem = yield* FileSystem.FileSystem;
51 const path = yield* Path.Path;
52 const absolutePath = path.join(cwd, relativePath);
53 yield* fileSystem
54 .makeDirectory(path.dirname(absolutePath), { recursive: true })
55 .pipe(Effect.orDie);
56 yield* fileSystem.writeFileString(absolutePath, contents).pipe(Effect.orDie);
57});
58
59it.layer(layerTest, { excludeTestServices: true })("WorkspaceFileSystemLive", (it) => {
60 describe("getMetadata", () => {
61 it.effect("expands home-relative paths on the server", () =>
62 Effect.gen(function* () {
63 const service = yield* WorkspaceFileSystem.WorkspaceFileSystem;
64 const home = yield* makeTempDir;
65 yield* writeTextFile(home, "file", "hello");
66 expect(
67 (yield* service
68 .getMetadata({ paths: ["~/file"] })
69 .pipe(Effect.provideService(HostProcess.HomeDirectory, home))).entries,
70 ).toEqual([{ kind: "file", byteLength: 5 }]);
71 }),
72 );
73
74 it.effect(
75 "reports actual kinds for extensionless files, dotfiles, and dotted directories",
76 () =>
77 Effect.gen(function* () {
78 const service = yield* WorkspaceFileSystem.WorkspaceFileSystem;
79 const fs = yield* FileSystem.FileSystem;
80 const path = yield* Path.Path;
81 const cwd = yield* makeTempDir;
82 yield* writeTextFile(cwd, "custom-name", "hello");
83 yield* writeTextFile(cwd, ".npmrc", "registry=test");
84 yield* fs.makeDirectory(path.join(cwd, "folder.ts"));
85 const file = path.join(cwd, "custom-name");
86 const result = yield* service.getMetadata({
87 paths: [
88 file,
89 path.join(cwd, ".npmrc"),
90 path.join(cwd, "folder.ts"),
91 file,
92 path.join(cwd, "missing"),
93 "relative-path",
94 ],
95 });
96 expect(result.entries).toEqual([
97 { kind: "file", byteLength: 5 },
98 { kind: "file", byteLength: 13 },
99 { kind: "directory" },
100 { kind: "file", byteLength: 5 },
101 null,
102 null,
103 ]);
104 }),
105 );
106
107 it.effect("detects an extensionless script and image without returning their contents", () =>
108 Effect.gen(function* () {
109 const service = yield* WorkspaceFileSystem.WorkspaceFileSystem;
110 const fs = yield* FileSystem.FileSystem;
111 const path = yield* Path.Path;
112 const cwd = yield* makeTempDir;
113 const script = "#!/usr/bin/env python3\nprint('hello')\n";
114 yield* writeTextFile(cwd, "run", script);
115 const image = path.join(cwd, "image");
116 yield* fs.writeFile(
117 image,
118 new Uint8Array([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]),
119 );
120 expect(
121 (yield* service.getMetadata({ paths: [path.join(cwd, "run"), image] })).entries,
122 ).toEqual([
123 { kind: "file", byteLength: script.length, mimeType: "text/x-python" },
124 { kind: "file", byteLength: 8, mimeType: "image/png" },
125 ]);
126 }),
127 );
128
129 it.effect.skipIf(!symlinksSupported)("follows file and directory symlinks", () =>
130 Effect.gen(function* () {
131 const service = yield* WorkspaceFileSystem.WorkspaceFileSystem;
132 const fs = yield* FileSystem.FileSystem;
133 const path = yield* Path.Path;
134 const cwd = yield* makeTempDir;
135 yield* writeTextFile(cwd, "target", "text");
136 yield* fs.makeDirectory(path.join(cwd, "directory"));
137 const fileLink = path.join(cwd, "file-link");
138 const directoryLink = path.join(cwd, "directory-link");
139 yield* fs.symlink(path.join(cwd, "target"), fileLink);
140 yield* fs.symlink(path.join(cwd, "directory"), directoryLink);
141 expect((yield* service.getMetadata({ paths: [fileLink, directoryLink] })).entries).toEqual([
142 { kind: "file", byteLength: 4 },
143 { kind: "directory" },
144 ]);
145 }),
146 );
147 });
148
149 describe("readFile", () => {
150 it.effect("reads UTF-8 files relative to the workspace root", () =>
151 Effect.gen(function* () {
152 const workspaceFileSystem = yield* WorkspaceFileSystem.WorkspaceFileSystem;
153 const cwd = yield* makeTempDir;
154 yield* writeTextFile(cwd, "src/index.ts", "export const answer = 42;\n");
155
156 const result = yield* workspaceFileSystem.readFile({
157 cwd,
158 relativePath: "src/index.ts",
159 });
160
161 expect(result).toEqual({
162 relativePath: "src/index.ts",
163 contents: "export const answer = 42;\n",
164 byteLength: 26,
165 truncated: false,
166 });
167 }),
168 );
169
170 it.effect("reads host files outside the workspace root by absolute path", () =>
171 Effect.gen(function* () {
172 const workspaceFileSystem = yield* WorkspaceFileSystem.WorkspaceFileSystem;
173 const path = yield* Path.Path;
174 const cwd = yield* makeTempDir;
175 const outsideDir = yield* makeTempDir;
176 yield* writeTextFile(outsideDir, "cleanup-report.md", "# Report\n");
177 const absolutePath = path.join(outsideDir, "cleanup-report.md");
178
179 const result = yield* workspaceFileSystem.readFile({
180 cwd,
181 relativePath: absolutePath,
182 });
183
184 expect(result).toEqual({
185 relativePath: absolutePath,
186 contents: "# Report\n",
187 byteLength: 9,
188 truncated: false,
189 });
190 }),
191 );
192
193 // Needs mkfifo; Windows has no FIFOs to reject.
194 it.effect.skipIf(HostProcess.Platform.defaultValue() === "win32")(
195 "rejects a FIFO without blocking on open",
196 () =>
197 Effect.gen(function* () {
198 const workspaceFileSystem = yield* WorkspaceFileSystem.WorkspaceFileSystem;
199 const path = yield* Path.Path;
200 const cwd = yield* makeTempDir;
201 const outsideDir = yield* makeTempDir;
202 const fifoPath = path.join(outsideDir, "pipe");
203 yield* Effect.promise(
204 () =>
205 new Promise<void>((resolve, reject) =>
206 NodeChildProcess.execFile("mkfifo", [fifoPath], (error) =>
207 error ? reject(error) : resolve(),
208 ),
209 ),
210 );
211
212 const error = yield* workspaceFileSystem
213 .readFile({ cwd, relativePath: fifoPath })
214 .pipe(Effect.flip);
215
216 expect(error).toBeInstanceOf(WorkspaceFileSystem.WorkspacePathNotFileError);
217 expect((yield* workspaceFileSystem.getMetadata({ paths: [fifoPath] })).entries).toEqual([
218 { kind: "other" },
219 ]);
220 }),
221 );
222
223 it.effect("rejects reads outside the workspace root", () =>
224 Effect.gen(function* () {
225 const workspaceFileSystem = yield* WorkspaceFileSystem.WorkspaceFileSystem;
226 const cwd = yield* makeTempDir;
227
228 const error = yield* workspaceFileSystem
229 .readFile({ cwd, relativePath: "../escape.md" })
230 .pipe(Effect.flip);
231
232 expect(error.message).toContain(
233 "Workspace file path must be relative to the project root: ../escape.md",
234 );
235 }),
236 );
237
238 it.effect.skipIf(!symlinksSupported)(
239 "rejects symlinks that resolve outside the workspace root",
240 () =>
241 Effect.gen(function* () {
242 const workspaceFileSystem = yield* WorkspaceFileSystem.WorkspaceFileSystem;
243 const fileSystem = yield* FileSystem.FileSystem;
244 const path = yield* Path.Path;
245 const cwd = yield* makeTempDir;
246 const outsideDir = yield* makeTempDir;
247 yield* writeTextFile(outsideDir, "secret.txt", "outside\n");
248 yield* fileSystem.symlink(
249 path.join(outsideDir, "secret.txt"),
250 path.join(cwd, "linked-secret.txt"),
251 );
252
253 const error = yield* workspaceFileSystem
254 .readFile({ cwd, relativePath: "linked-secret.txt" })
255 .pipe(Effect.flip);
256 const resolvedWorkspaceRoot = yield* fileSystem.realPath(cwd);
257 const resolvedPath = yield* fileSystem.realPath(path.join(outsideDir, "secret.txt"));
258
259 expect(error).toBeInstanceOf(WorkspaceFileSystem.WorkspaceFilePathEscapeError);
260 expect(error).toMatchObject({
261 workspaceRoot: cwd,
262 relativePath: "linked-secret.txt",
263 resolvedWorkspaceRoot,
264 resolvedPath,
265 });
266 expect("cause" in error).toBe(false);
267 }),
268 );
269
270 it.effect("rejects directories without manufacturing an I/O cause", () =>
271 Effect.gen(function* () {
272 const workspaceFileSystem = yield* WorkspaceFileSystem.WorkspaceFileSystem;
273 const fileSystem = yield* FileSystem.FileSystem;
274 const path = yield* Path.Path;
275 const cwd = yield* makeTempDir;
276 yield* fileSystem.makeDirectory(path.join(cwd, "src"));
277
278 const error = yield* workspaceFileSystem
279 .readFile({ cwd, relativePath: "src" })
280 .pipe(Effect.flip);
281 const resolvedPath = yield* fileSystem.realPath(path.join(cwd, "src"));
282
283 expect(error).toBeInstanceOf(WorkspaceFileSystem.WorkspacePathNotFileError);
284 expect(error).toMatchObject({
285 workspaceRoot: cwd,
286 relativePath: "src",
287 resolvedPath,
288 });
289 expect("cause" in error).toBe(false);
290 }),
291 );
292
293 it.effect("rejects binary files without leaking their contents into the error", () =>
294 Effect.gen(function* () {
295 const workspaceFileSystem = yield* WorkspaceFileSystem.WorkspaceFileSystem;
296 const fileSystem = yield* FileSystem.FileSystem;
297 const path = yield* Path.Path;
298 const cwd = yield* makeTempDir;
299 const absolutePath = path.join(cwd, "asset.bin");
300 yield* fileSystem.writeFile(absolutePath, Uint8Array.from([0x61, 0, 0x62]));
301
302 const error = yield* workspaceFileSystem
303 .readFile({ cwd, relativePath: "asset.bin" })
304 .pipe(Effect.flip);
305 const resolvedPath = yield* fileSystem.realPath(absolutePath);
306
307 expect(error).toBeInstanceOf(WorkspaceFileSystem.WorkspaceBinaryFileError);
308 expect(error).toMatchObject({
309 workspaceRoot: cwd,
310 relativePath: "asset.bin",
311 resolvedPath,
312 });
313 expect("cause" in error).toBe(false);
314 expect("contents" in error).toBe(false);
315 }),
316 );
317
318 it.effect("preserves the real cause and path for I/O failures", () =>
319 Effect.gen(function* () {
320 const workspaceFileSystem = yield* WorkspaceFileSystem.WorkspaceFileSystem;
321 const path = yield* Path.Path;
322 const cwd = yield* makeTempDir;
323 const resolvedPath = path.join(cwd, "missing.txt");
324
325 const error = yield* workspaceFileSystem
326 .readFile({ cwd, relativePath: "missing.txt" })
327 .pipe(Effect.flip);
328
329 expect(error).toBeInstanceOf(WorkspaceFileSystem.WorkspaceFileSystemOperationError);
330 expect(error).toMatchObject({
331 workspaceRoot: cwd,
332 relativePath: "missing.txt",
333 resolvedPath,
334 operationPath: resolvedPath,
335 operation: "realpath-target",
336 });
337 expect(error.cause).toBeInstanceOf(Error);
338 expect((error.cause as NodeJS.ErrnoException).code).toBe("ENOENT");
339 }),
340 );
341 });
342
343 describe("writeFile", () => {
344 it.effect("writes files relative to the workspace root", () =>
345 Effect.gen(function* () {
346 const workspaceFileSystem = yield* WorkspaceFileSystem.WorkspaceFileSystem;
347 const cwd = yield* makeTempDir;
348 const fileSystem = yield* FileSystem.FileSystem;
349 const path = yield* Path.Path;
350 const result = yield* workspaceFileSystem.writeFile({
351 cwd,
352 relativePath: "plans/effect-rpc.md",
353 contents: "# Plan\n",
354 });
355 const saved = yield* fileSystem
356 .readFileString(path.join(cwd, "plans/effect-rpc.md"))
357 .pipe(Effect.orDie);
358
359 expect(result).toEqual({ relativePath: "plans/effect-rpc.md" });
360 expect(saved).toBe("# Plan\n");
361 }),
362 );
363
364 it.effect("rejects writes by absolute path", () =>
365 Effect.gen(function* () {
366 const workspaceFileSystem = yield* WorkspaceFileSystem.WorkspaceFileSystem;
367 const path = yield* Path.Path;
368 const cwd = yield* makeTempDir;
369 const outsideDir = yield* makeTempDir;
370 const absolutePath = path.join(outsideDir, "cleanup-report.md");
371
372 const error = yield* workspaceFileSystem
373 .writeFile({ cwd, relativePath: absolutePath, contents: "# Edited\n" })
374 .pipe(Effect.flip);
375
376 expect(error).toBeInstanceOf(WorkspacePaths.WorkspacePathOutsideRootError);
377 }),
378 );
379
380 it.effect("invalidates workspace entry search cache after writes", () =>
381 Effect.gen(function* () {
382 const workspaceEntries = yield* WorkspaceEntries.WorkspaceEntries;
383 const workspaceFileSystem = yield* WorkspaceFileSystem.WorkspaceFileSystem;
384 const cwd = yield* makeTempDir;
385 yield* writeTextFile(cwd, "src/existing.ts", "export {};\n");
386
387 const beforeWrite = yield* workspaceEntries.list({ cwd });
388 expect(beforeWrite.entries.some((entry) => entry.path === "plans/effect-rpc.md")).toBe(
389 false,
390 );
391
392 yield* workspaceFileSystem.writeFile({
393 cwd,
394 relativePath: "plans/effect-rpc.md",
395 contents: "# Plan\n",
396 });
397
398 const afterWrite = yield* workspaceEntries.list({ cwd });
399 expect(afterWrite.entries).toEqual(
400 expect.arrayContaining([expect.objectContaining({ path: "plans/effect-rpc.md" })]),
401 );
402 expect(afterWrite.truncated).toBe(false);
403 }),
404 );
405
406 it.effect("rejects writes outside the workspace root", () =>
407 Effect.gen(function* () {
408 const workspaceFileSystem = yield* WorkspaceFileSystem.WorkspaceFileSystem;
409 const cwd = yield* makeTempDir;
410 const path = yield* Path.Path;
411 const fileSystem = yield* FileSystem.FileSystem;
412
413 const error = yield* workspaceFileSystem
414 .writeFile({
415 cwd,
416 relativePath: "../escape.md",
417 contents: "# nope\n",
418 })
419 .pipe(Effect.flip);
420
421 expect(error.message).toContain(
422 "Workspace file path must be relative to the project root: ../escape.md",
423 );
424
425 const escapedPath = path.resolve(cwd, "..", "escape.md");
426 const escapedStat = yield* fileSystem
427 .stat(escapedPath)
428 .pipe(Effect.orElseSucceed(() => null));
429 expect(escapedStat).toBeNull();
430 }),
431 );
432 });
433});