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