Open History to see the last commit
1import * as NodeServices from "@effect/platform-node/NodeServices";
2import { it, describe, expect } from "@effect/vitest";
3import * as Effect from "effect/Effect";
4import * as FileSystem from "effect/FileSystem";
5import * as Layer from "effect/Layer";
6import * as Path from "effect/Path";
7
8import * as ServerConfig from "../config.ts";
9import * as VcsDriverRegistry from "../vcs/VcsDriverRegistry.ts";
10import * as VcsProcess from "../vcs/VcsProcess.ts";
11import * as WorkspaceEntries from "./WorkspaceEntries.ts";
12import * as WorkspaceFileSystem from "./WorkspaceFileSystem.ts";
13import * as WorkspacePaths from "./WorkspacePaths.ts";
14
15const ProjectLayer = WorkspaceFileSystem.layer.pipe(
16 Layer.provide(WorkspacePaths.layer),
17 Layer.provide(WorkspaceEntries.layer.pipe(Layer.provide(WorkspacePaths.layer))),
18);
19
20const TestLayer = Layer.empty.pipe(
21 Layer.provideMerge(ProjectLayer),
22 Layer.provideMerge(WorkspaceEntries.layer.pipe(Layer.provide(WorkspacePaths.layer))),
23 Layer.provideMerge(WorkspacePaths.layer),
24 Layer.provideMerge(VcsDriverRegistry.layer.pipe(Layer.provide(VcsProcess.layer))),
25 Layer.provide(
26 ServerConfig.ServerConfig.layerTest(process.cwd(), {
27 prefix: "t3-workspace-files-test-",
28 }),
29 ),
30 Layer.provideMerge(NodeServices.layer),
31);
32
33const makeTempDir = Effect.gen(function* () {
34 const fileSystem = yield* FileSystem.FileSystem;
35 return yield* fileSystem.makeTempDirectoryScoped({
36 prefix: "t3code-workspace-files-",
37 });
38});
39
40const writeTextFile = Effect.fn("writeTextFile")(function* (
41 cwd: string,
42 relativePath: string,
43 contents = "",
44) {
45 const fileSystem = yield* FileSystem.FileSystem;
46 const path = yield* Path.Path;
47 const absolutePath = path.join(cwd, relativePath);
48 yield* fileSystem
49 .makeDirectory(path.dirname(absolutePath), { recursive: true })
50 .pipe(Effect.orDie);
51 yield* fileSystem.writeFileString(absolutePath, contents).pipe(Effect.orDie);
52});
53
54it.layer(TestLayer, { excludeTestServices: true })("WorkspaceFileSystemLive", (it) => {
55 describe("readFile", () => {
56 it.effect("reads UTF-8 files relative to the workspace root", () =>
57 Effect.gen(function* () {
58 const workspaceFileSystem = yield* WorkspaceFileSystem.WorkspaceFileSystem;
59 const cwd = yield* makeTempDir;
60 yield* writeTextFile(cwd, "src/index.ts", "export const answer = 42;\n");
61
62 const result = yield* workspaceFileSystem.readFile({
63 cwd,
64 relativePath: "src/index.ts",
65 });
66
67 expect(result).toEqual({
68 relativePath: "src/index.ts",
69 contents: "export const answer = 42;\n",
70 byteLength: 26,
71 truncated: false,
72 });
73 }),
74 );
75
76 it.effect("rejects reads outside the workspace root", () =>
77 Effect.gen(function* () {
78 const workspaceFileSystem = yield* WorkspaceFileSystem.WorkspaceFileSystem;
79 const cwd = yield* makeTempDir;
80
81 const error = yield* workspaceFileSystem
82 .readFile({ cwd, relativePath: "../escape.md" })
83 .pipe(Effect.flip);
84
85 expect(error.message).toContain(
86 "Workspace file path must be relative to the project root: ../escape.md",
87 );
88 }),
89 );
90
91 it.effect("rejects symlinks that resolve outside the workspace root", () =>
92 Effect.gen(function* () {
93 const workspaceFileSystem = yield* WorkspaceFileSystem.WorkspaceFileSystem;
94 const fileSystem = yield* FileSystem.FileSystem;
95 const path = yield* Path.Path;
96 const cwd = yield* makeTempDir;
97 const outsideDir = yield* makeTempDir;
98 yield* writeTextFile(outsideDir, "secret.txt", "outside\n");
99 yield* fileSystem.symlink(
100 path.join(outsideDir, "secret.txt"),
101 path.join(cwd, "linked-secret.txt"),
102 );
103
104 const error = yield* workspaceFileSystem
105 .readFile({ cwd, relativePath: "linked-secret.txt" })
106 .pipe(Effect.flip);
107 const resolvedWorkspaceRoot = yield* fileSystem.realPath(cwd);
108 const resolvedPath = yield* fileSystem.realPath(path.join(outsideDir, "secret.txt"));
109
110 expect(error).toBeInstanceOf(WorkspaceFileSystem.WorkspaceFilePathEscapeError);
111 expect(error).toMatchObject({
112 workspaceRoot: cwd,
113 relativePath: "linked-secret.txt",
114 resolvedWorkspaceRoot,
115 resolvedPath,
116 });
117 expect("cause" in error).toBe(false);
118 }),
119 );
120
121 it.effect("rejects directories without manufacturing an I/O cause", () =>
122 Effect.gen(function* () {
123 const workspaceFileSystem = yield* WorkspaceFileSystem.WorkspaceFileSystem;
124 const fileSystem = yield* FileSystem.FileSystem;
125 const path = yield* Path.Path;
126 const cwd = yield* makeTempDir;
127 yield* fileSystem.makeDirectory(path.join(cwd, "src"));
128
129 const error = yield* workspaceFileSystem
130 .readFile({ cwd, relativePath: "src" })
131 .pipe(Effect.flip);
132 const resolvedPath = yield* fileSystem.realPath(path.join(cwd, "src"));
133
134 expect(error).toBeInstanceOf(WorkspaceFileSystem.WorkspacePathNotFileError);
135 expect(error).toMatchObject({
136 workspaceRoot: cwd,
137 relativePath: "src",
138 resolvedPath,
139 });
140 expect("cause" in error).toBe(false);
141 }),
142 );
143
144 it.effect("rejects binary files without leaking their contents into the error", () =>
145 Effect.gen(function* () {
146 const workspaceFileSystem = yield* WorkspaceFileSystem.WorkspaceFileSystem;
147 const fileSystem = yield* FileSystem.FileSystem;
148 const path = yield* Path.Path;
149 const cwd = yield* makeTempDir;
150 const absolutePath = path.join(cwd, "asset.bin");
151 yield* fileSystem.writeFile(absolutePath, Uint8Array.from([0x61, 0, 0x62]));
152
153 const error = yield* workspaceFileSystem
154 .readFile({ cwd, relativePath: "asset.bin" })
155 .pipe(Effect.flip);
156 const resolvedPath = yield* fileSystem.realPath(absolutePath);
157
158 expect(error).toBeInstanceOf(WorkspaceFileSystem.WorkspaceBinaryFileError);
159 expect(error).toMatchObject({
160 workspaceRoot: cwd,
161 relativePath: "asset.bin",
162 resolvedPath,
163 });
164 expect("cause" in error).toBe(false);
165 expect("contents" in error).toBe(false);
166 }),
167 );
168
169 it.effect("preserves the real cause and path for I/O failures", () =>
170 Effect.gen(function* () {
171 const workspaceFileSystem = yield* WorkspaceFileSystem.WorkspaceFileSystem;
172 const path = yield* Path.Path;
173 const cwd = yield* makeTempDir;
174 const resolvedPath = path.join(cwd, "missing.txt");
175
176 const error = yield* workspaceFileSystem
177 .readFile({ cwd, relativePath: "missing.txt" })
178 .pipe(Effect.flip);
179
180 expect(error).toBeInstanceOf(WorkspaceFileSystem.WorkspaceFileSystemOperationError);
181 expect(error).toMatchObject({
182 workspaceRoot: cwd,
183 relativePath: "missing.txt",
184 resolvedPath,
185 operationPath: resolvedPath,
186 operation: "realpath-target",
187 });
188 expect(error.cause).toBeInstanceOf(Error);
189 expect((error.cause as NodeJS.ErrnoException).code).toBe("ENOENT");
190 }),
191 );
192 });
193
194 describe("writeFile", () => {
195 it.effect("writes files relative to the workspace root", () =>
196 Effect.gen(function* () {
197 const workspaceFileSystem = yield* WorkspaceFileSystem.WorkspaceFileSystem;
198 const cwd = yield* makeTempDir;
199 const fileSystem = yield* FileSystem.FileSystem;
200 const path = yield* Path.Path;
201 const result = yield* workspaceFileSystem.writeFile({
202 cwd,
203 relativePath: "plans/effect-rpc.md",
204 contents: "# Plan\n",
205 });
206 const saved = yield* fileSystem
207 .readFileString(path.join(cwd, "plans/effect-rpc.md"))
208 .pipe(Effect.orDie);
209
210 expect(result).toEqual({ relativePath: "plans/effect-rpc.md" });
211 expect(saved).toBe("# Plan\n");
212 }),
213 );
214
215 it.effect("invalidates workspace entry search cache after writes", () =>
216 Effect.gen(function* () {
217 const workspaceEntries = yield* WorkspaceEntries.WorkspaceEntries;
218 const workspaceFileSystem = yield* WorkspaceFileSystem.WorkspaceFileSystem;
219 const cwd = yield* makeTempDir;
220 yield* writeTextFile(cwd, "src/existing.ts", "export {};\n");
221
222 const beforeWrite = yield* workspaceEntries.list({ cwd });
223 expect(beforeWrite.entries.some((entry) => entry.path === "plans/effect-rpc.md")).toBe(
224 false,
225 );
226
227 yield* workspaceFileSystem.writeFile({
228 cwd,
229 relativePath: "plans/effect-rpc.md",
230 contents: "# Plan\n",
231 });
232
233 const afterWrite = yield* workspaceEntries.list({ cwd });
234 expect(afterWrite.entries).toEqual(
235 expect.arrayContaining([expect.objectContaining({ path: "plans/effect-rpc.md" })]),
236 );
237 expect(afterWrite.truncated).toBe(false);
238 }),
239 );
240
241 it.effect("rejects writes outside the workspace root", () =>
242 Effect.gen(function* () {
243 const workspaceFileSystem = yield* WorkspaceFileSystem.WorkspaceFileSystem;
244 const cwd = yield* makeTempDir;
245 const path = yield* Path.Path;
246 const fileSystem = yield* FileSystem.FileSystem;
247
248 const error = yield* workspaceFileSystem
249 .writeFile({
250 cwd,
251 relativePath: "../escape.md",
252 contents: "# nope\n",
253 })
254 .pipe(Effect.flip);
255
256 expect(error.message).toContain(
257 "Workspace file path must be relative to the project root: ../escape.md",
258 );
259
260 const escapedPath = path.resolve(cwd, "..", "escape.md");
261 const escapedStat = yield* fileSystem
262 .stat(escapedPath)
263 .pipe(Effect.orElseSucceed(() => null));
264 expect(escapedStat).toBeNull();
265 }),
266 );
267 });
268});