Open History to see the last commit
1import * as NodeServices from "@effect/platform-node/NodeServices";
2import { assert, it } from "@effect/vitest";
3import { ProjectId, type ProjectTrackedFolder } from "@t3tools/contracts";
4import * as Effect from "effect/Effect";
5import * as FileSystem from "effect/FileSystem";
6import * as Layer from "effect/Layer";
7import * as Path from "effect/Path";
8
9import * as ProjectStore from "../orchestration-v2/ProjectStore.ts";
10import * as SqlitePersistence from "../persistence/Sqlite.ts";
11import * as TaskFolderStore from "./TaskFolderStore.ts";
12import * as WorkspaceFolders from "./WorkspaceFolders.ts";
13
14const projectId = ProjectId.make("project:workspace-folders");
15
16const layerFor = (project?: {
17 readonly workspaceRoot: string;
18 readonly trackedFolders: ReadonlyArray<ProjectTrackedFolder>;
19}) =>
20 WorkspaceFolders.layer.pipe(
21 Layer.provide(
22 Layer.mock(ProjectStore.ProjectStoreV2)({
23 get: () =>
24 project === undefined
25 ? Effect.succeedNone
26 : Effect.succeedSome({
27 projectId,
28 title: "Project",
29 hasCustomTitle: false,
30 defaultModelSelection: null,
31 defaultThreadEnvMode: null,
32 autoPull: false,
33 faviconPath: null,
34 projectIcon: null,
35 scripts: [],
36 pinnedAt: null,
37 archivedAt: null,
38 createdAt: "2026-06-21T00:00:00.000Z",
39 updatedAt: "2026-06-21T00:00:00.000Z",
40 deletedAt: null,
41 ...project,
42 }),
43 }),
44 ),
45 Layer.provideMerge(TaskFolderStore.layer.pipe(Layer.provide(SqlitePersistence.layerMemory))),
46 );
47
48// Resolved, so a symlinked temp directory does not show up as a second spelling of each path.
49const makeTempDir = Effect.gen(function* () {
50 const fileSystem = yield* FileSystem.FileSystem;
51 return yield* fileSystem.realPath(
52 yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3code-workspace-folders-" }),
53 );
54});
55
56it.layer(NodeServices.layer)("WorkspaceFolders", (it) => {
57 it.effect("scans child repositories and the folders a code-workspace file lists", () =>
58 Effect.gen(function* () {
59 const fileSystem = yield* FileSystem.FileSystem;
60 const path = yield* Path.Path;
61 const tmp = yield* makeTempDir;
62 const root = path.join(tmp, "wrapper");
63 const outside = path.join(tmp, "outside");
64 for (const folder of [
65 ".git",
66 "api/.git",
67 "web/.git",
68 "sub/.git",
69 "notes",
70 "src/svc/.git",
71 "tree",
72 ".hidden/.git",
73 "node_modules/.git",
74 "../outside/.git",
75 ]) {
76 yield* fileSystem.makeDirectory(path.join(root, folder), { recursive: true });
77 }
78 yield* fileSystem.symlink(outside, path.join(root, "linked"));
79 yield* fileSystem.symlink(root, path.join(root, "self"));
80 // A worktree of `api`: its history is that repository's.
81 yield* fileSystem.writeFileString(
82 path.join(root, "tree", ".git"),
83 `gitdir: ${path.join(root, "api", ".git", "worktrees", "tree")}\n`,
84 );
85 yield* fileSystem.writeFileString(
86 path.join(root, ".gitmodules"),
87 '[submodule "sub"]\n\tpath = sub\n\turl = https://example.test/sub.git\n',
88 );
89 yield* fileSystem.writeFileString(
90 path.join(root, "team.code-workspace"),
91 `{
92 // Editors write comments and trailing commas here.
93 "folders": [
94 // Entries that lead nowhere take none of the room the real ones need.
95 ${Array.from({ length: 130 }, (_, index) => `{ "path": "gone-${index}" },`).join("")}
96 { "path": "api", "name": "API" },
97 { "path": "notes" },
98 { "path": "../outside" },
99 { "path": "gone" },
100 { "path": "." },
101 ],
102 }`,
103 );
104
105 const workspaceFolders = yield* WorkspaceFolders.WorkspaceFolders;
106 assert.deepEqual(yield* workspaceFolders.scan({ workspaceRoot: `${root}${path.sep}` }), {
107 workspaceRoot: root,
108 isGitRepository: true,
109 codeWorkspaceFile: path.join(root, "team.code-workspace"),
110 folders: [
111 {
112 path: path.join(root, "api"),
113 name: "API",
114 isGitRepository: true,
115 source: "code-workspace",
116 },
117 {
118 path: path.join(root, "notes"),
119 name: "notes",
120 isGitRepository: false,
121 source: "code-workspace",
122 },
123 { path: outside, name: "outside", isGitRepository: true, source: "code-workspace" },
124 // One level below a plain folder is looked into.
125 {
126 path: path.join(root, "src", "svc"),
127 name: "svc",
128 isGitRepository: true,
129 source: "child",
130 },
131 { path: path.join(root, "web"), name: "web", isGitRepository: true, source: "child" },
132 ],
133 });
134 assert.equal(
135 (yield* workspaceFolders.scan({ workspaceRoot: path.join(root, "gone") }).pipe(Effect.flip))
136 ._tag,
137 "WorkspaceFoldersError",
138 );
139 }).pipe(Effect.provide(layerFor())),
140 );
141
142 it.effect("finds the repositories inside a project folder from disk each time", () =>
143 Effect.gen(function* () {
144 const fileSystem = yield* FileSystem.FileSystem;
145 const path = yield* Path.Path;
146 const tmp = yield* makeTempDir;
147 const workspaceRoot = path.join(tmp, "wrapper");
148 const outside = path.join(tmp, "outside");
149 const repository = (name: string) => path.join(workspaceRoot, "src", name);
150 for (const folder of ["src/api/.git", "src/notes", "docs/.git", "vendor/big/.git", "tree"]) {
151 yield* fileSystem.makeDirectory(path.join(workspaceRoot, folder), { recursive: true });
152 }
153 // A worktree of `docs` follows that repository, even when a workspace file lists it.
154 yield* fileSystem.writeFileString(
155 path.join(workspaceRoot, "tree", ".git"),
156 `gitdir: ${path.join(workspaceRoot, "docs", ".git", "worktrees", "tree")}\n`,
157 );
158 yield* fileSystem.writeFileString(
159 path.join(workspaceRoot, "team.code-workspace"),
160 JSON.stringify({ folders: [{ path: "tree" }] }),
161 );
162 yield* fileSystem.makeDirectory(path.join(outside, ".git"), { recursive: true });
163 // A link to a repository elsewhere is not inside the project.
164 yield* fileSystem.symlink(outside, path.join(workspaceRoot, "linked"));
165 yield* fileSystem.writeFileString(
166 path.join(workspaceRoot, "t3.json"),
167 JSON.stringify({ ignoreRepositories: ["vendor/big"] }),
168 );
169 // The stored list is only trusted for what someone added.
170 const trackedFolders = [
171 { path: path.join(workspaceRoot, "gone"), name: "gone", discovered: true },
172 { path: outside, name: "Outside" },
173 ];
174
175 yield* Effect.gen(function* () {
176 const workspaceFolders = yield* WorkspaceFolders.WorkspaceFolders;
177 const listed = Effect.map(workspaceFolders.list({ projectId }), ({ folders }) =>
178 folders.map((folder) => [folder.name, folder.path]),
179 );
180 assert.deepEqual(yield* listed, [
181 ["docs", path.join(workspaceRoot, "docs")],
182 ["api", repository("api")],
183 ["Outside", outside],
184 ]);
185 yield* fileSystem.makeDirectory(path.join(repository("web"), ".git"), { recursive: true });
186 yield* fileSystem.remove(repository("api"), { recursive: true });
187 assert.deepEqual(yield* listed, [
188 ["docs", path.join(workspaceRoot, "docs")],
189 ["web", repository("web")],
190 ["Outside", outside],
191 ]);
192 }).pipe(Effect.provide(layerFor({ workspaceRoot, trackedFolders })));
193 }),
194 );
195
196 it.effect("does not walk a project's folders when t3.json ignores the whole project", () =>
197 Effect.gen(function* () {
198 const fileSystem = yield* FileSystem.FileSystem;
199 const path = yield* Path.Path;
200 const workspaceRoot = yield* makeTempDir;
201 for (const folder of ["api/.git", "src/web/.git", "notes"]) {
202 yield* fileSystem.makeDirectory(path.join(workspaceRoot, folder), { recursive: true });
203 }
204 yield* fileSystem.writeFileString(
205 path.join(workspaceRoot, "t3.json"),
206 JSON.stringify({ ignoreRepositories: ["."] }),
207 );
208 const probed: Array<string> = [];
209 const counting = FileSystem.FileSystem.of({
210 ...fileSystem,
211 readDirectory: (target, options) => {
212 probed.push(target);
213 return fileSystem.readDirectory(target, options);
214 },
215 stat: (target) => {
216 probed.push(target);
217 return fileSystem.stat(target);
218 },
219 });
220
221 const found = yield* WorkspaceFolders.discoverFolders(workspaceRoot).pipe(
222 Effect.provideService(FileSystem.FileSystem, counting),
223 );
224
225 assert.deepEqual(found, []);
226 assert.deepEqual(
227 probed.filter((target) => target !== workspaceRoot),
228 [],
229 );
230 }),
231 );
232
233 it.effect("keeps the folders someone added when a project holds more than fit", () =>
234 Effect.gen(function* () {
235 const fileSystem = yield* FileSystem.FileSystem;
236 const path = yield* Path.Path;
237 const tmp = yield* makeTempDir;
238 const workspaceRoot = path.join(tmp, "wrapper");
239 const outside = path.join(tmp, "outside");
240 const longName = "r".repeat(200);
241 yield* Effect.forEach(
242 [longName, ...Array.from({ length: 130 }, (_, index) => `s${index}`)],
243 (name) =>
244 fileSystem.makeDirectory(path.join(workspaceRoot, "src", name, ".git"), {
245 recursive: true,
246 }),
247 { discard: true },
248 );
249 yield* fileSystem.makeDirectory(outside, { recursive: true });
250
251 yield* Effect.gen(function* () {
252 const workspaceFolders = yield* WorkspaceFolders.WorkspaceFolders;
253 const { folders } = yield* workspaceFolders.list({ projectId });
254 assert.equal(folders.length, 128);
255 assert.deepEqual(
256 [folders[0]!.name, folders[0]!.path],
257 ["r".repeat(128), path.join(workspaceRoot, "src", longName)],
258 );
259 assert.deepEqual([folders.at(-1)!.name, folders.at(-1)!.path], ["Outside", outside]);
260 }).pipe(
261 Effect.provide(
262 layerFor({ workspaceRoot, trackedFolders: [{ path: outside, name: "Outside" }] }),
263 ),
264 );
265 }),
266 );
267
268 it.effect("reports each tracked folder's state and grants a thread the ones outside", () =>
269 Effect.gen(function* () {
270 const fileSystem = yield* FileSystem.FileSystem;
271 const path = yield* Path.Path;
272 const tmp = yield* makeTempDir;
273 const workspaceRoot = path.join(tmp, "app");
274 const folder = (name: string, ...segments: ReadonlyArray<string>) => ({
275 name,
276 path: path.join(tmp, ...segments),
277 });
278 const inside = folder("web", "app", "packages", "web");
279 const repository = folder("api", "api");
280 const missing = folder("gone", "gone");
281 const linked = folder("shared", "app", "linked");
282 const shared = path.join(tmp, "shared");
283 const worktree = path.join(tmp, "tree");
284 // `lib` is checked out with a submodule of its own; `docs` was never initialized. A path
285 // listed twice is one submodule.
286 // `ext` is a symlink out of the repository, so what it leads to is no submodule of it.
287 const gitmodules =
288 '[submodule "lib"]\n\tpath = vendor/lib\n[submodule "docs"]\n\tpath = docs\n[submodule "again"]\n\tpath = vendor/lib\n[submodule "out"]\n\tpath = ext/x\n';
289 for (const root of [workspaceRoot, worktree]) {
290 yield* fileSystem.makeDirectory(path.join(root, "vendor", "lib", "deep", ".git"), {
291 recursive: true,
292 });
293 yield* fileSystem.makeDirectory(path.join(root, "docs"), { recursive: true });
294 yield* fileSystem.writeFileString(path.join(root, "vendor", "lib", ".git"), "gitdir: x\n");
295 yield* fileSystem.writeFileString(path.join(root, ".gitmodules"), gitmodules);
296 yield* fileSystem.writeFileString(
297 path.join(root, "vendor", "lib", ".gitmodules"),
298 '[submodule "deep"]\n\tpath = deep\n',
299 );
300 }
301 for (const directory of [inside.path, path.join(repository.path, ".git"), shared]) {
302 yield* fileSystem.makeDirectory(directory, { recursive: true });
303 }
304 yield* fileSystem.symlink(shared, linked.path);
305 yield* fileSystem.makeDirectory(path.join(tmp, "elsewhere", "x", ".git"), {
306 recursive: true,
307 });
308 yield* fileSystem.symlink(path.join(tmp, "elsewhere"), path.join(workspaceRoot, "ext"));
309
310 yield* Effect.gen(function* () {
311 const workspaceFolders = yield* WorkspaceFolders.WorkspaceFolders;
312 assert.deepEqual(yield* workspaceFolders.list({ projectId }), {
313 folders: [
314 { ...inside, state: "plain" },
315 { ...repository, state: "git" },
316 { ...missing, state: "missing" },
317 { ...linked, state: "plain" },
318 ],
319 });
320 assert.deepEqual(yield* workspaceFolders.forThread({ projectId, worktreePath: null }), {
321 folders: [
322 { ...inside, isGitRepository: false },
323 { ...repository, isGitRepository: true },
324 { ...linked, isGitRepository: false },
325 ],
326 // A symlink inside the project folder still leads outside it.
327 extraRoots: [repository.path, linked.path, shared],
328 });
329 assert.deepEqual(yield* workspaceFolders.forThread({ projectId, worktreePath: worktree }), {
330 folders: [],
331 extraRoots: [],
332 });
333 const { folders } = yield* workspaceFolders.forThread({ projectId, worktreePath: null });
334 assert.deepEqual(
335 yield* workspaceFolders.submodules({ projectId, worktreePath: null, folders }),
336 [
337 { name: "lib", path: path.join(workspaceRoot, "vendor", "lib"), isGitRepository: true },
338 {
339 name: "deep",
340 path: path.join(workspaceRoot, "vendor", "lib", "deep"),
341 isGitRepository: true,
342 },
343 ],
344 );
345 // A worktree reaches its own submodules, named by where the project folder has them.
346 assert.deepEqual(
347 yield* workspaceFolders.submodules({ projectId, worktreePath: worktree, folders: [] }),
348 [
349 {
350 name: "lib",
351 path: path.join(worktree, "vendor", "lib"),
352 isGitRepository: true,
353 repositoryPath: path.join(workspaceRoot, "vendor", "lib"),
354 },
355 {
356 name: "deep",
357 path: path.join(worktree, "vendor", "lib", "deep"),
358 isGitRepository: true,
359 repositoryPath: path.join(workspaceRoot, "vendor", "lib", "deep"),
360 },
361 ],
362 );
363 }).pipe(
364 Effect.provide(
365 layerFor({ workspaceRoot, trackedFolders: [inside, repository, missing, linked] }),
366 ),
367 );
368 }),
369 );
370 it.effect("gives a task folder thread its worktrees, never the checkouts they came from", () =>
371 Effect.gen(function* () {
372 const fileSystem = yield* FileSystem.FileSystem;
373 const path = yield* Path.Path;
374 const tmp = yield* makeTempDir;
375 const ctx = path.join(tmp, "x", "ctx");
376 const app = path.join(tmp, "x", "app");
377 const lib = path.join(ctx, "vendor", "lib");
378 // The worktrees directory is reached through a symlink, as on a machine that moved it.
379 const realWorktrees = path.join(tmp, "disk", "worktrees");
380 const worktrees = path.join(tmp, "worktrees");
381 const taskRoot = path.join(worktrees, "x", "feature-0123");
382 for (const directory of [
383 lib,
384 app,
385 path.join(realWorktrees, "x", "feature-0123", "ctx", "vendor", "lib"),
386 path.join(realWorktrees, "x", "feature-0123", "app"),
387 path.join(realWorktrees, "x", "single"),
388 ]) {
389 yield* fileSystem.makeDirectory(directory, { recursive: true });
390 }
391 yield* fileSystem.symlink(realWorktrees, worktrees);
392 const trackedFolders = [
393 { name: "app", path: app },
394 { name: "lib", path: lib },
395 ];
396
397 yield* Effect.gen(function* () {
398 const taskFolders = yield* TaskFolderStore.TaskFolderStore;
399 const workspaceFolders = yield* WorkspaceFolders.WorkspaceFolders;
400 const taskFolder = {
401 root: taskRoot,
402 projectId,
403 projectRoot: ctx,
404 anchor: path.join(tmp, "x"),
405 projectPath: path.join(taskRoot, "ctx"),
406 projectIsWorktree: true,
407 repositories: [
408 { repositoryPath: app, worktreePath: path.join(taskRoot, "app"), name: "app" },
409 {
410 repositoryPath: lib,
411 worktreePath: path.join(taskRoot, "ctx", "vendor", "lib"),
412 name: "lib",
413 },
414 ],
415 };
416 yield* taskFolders.put(taskFolder);
417 const expected = {
418 folders: [
419 {
420 name: "app",
421 path: path.join(taskRoot, "app"),
422 isGitRepository: true,
423 repositoryPath: app,
424 },
425 {
426 name: "lib",
427 path: path.join(taskRoot, "ctx", "vendor", "lib"),
428 isGitRepository: true,
429 repositoryPath: lib,
430 },
431 ],
432 // Only the sibling lies outside the thread's folder, under both of its spellings.
433 extraRoots: [
434 path.join(taskRoot, "app"),
435 path.join(realWorktrees, "x", "feature-0123", "app"),
436 ],
437 };
438 // The thread stores the path as named; a sandbox or Git may hand back the resolved one.
439 for (const worktreePath of [
440 path.join(taskRoot, "ctx"),
441 path.join(realWorktrees, "x", "feature-0123", "ctx"),
442 ]) {
443 assert.deepEqual(
444 yield* workspaceFolders.forThread({ projectId, worktreePath }),
445 expected,
446 );
447 }
448 assert.deepEqual(
449 yield* taskFolders.findContaining(
450 path.join(realWorktrees, "x", "feature-0123", "app", "src"),
451 ),
452 taskFolder,
453 );
454 // A single worktree beside it is no task folder.
455 assert.deepEqual(
456 yield* workspaceFolders.forThread({
457 projectId,
458 worktreePath: path.join(worktrees, "x", "single"),
459 }),
460 { folders: [], extraRoots: [] },
461 );
462 }).pipe(Effect.provide(layerFor({ workspaceRoot: ctx, trackedFolders })));
463 }),
464 );
465});