Skip to content

Commit 50952be

Browse files
fix(server): No project behind a symlinked scratch folder stays No project (#34487)
1 parent 52ba328 commit 50952be

3 files changed

Lines changed: 52 additions & 4 deletions

File tree

‎apps/server/src/project/ManagedProjectFolders.test.ts‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,31 @@ it.effect("leaves threads in other projects alone", () =>
307307
),
308308
);
309309

310+
it.effect("still knows No project when its folder is the target of a symlinked Scratch", () =>
311+
withScratch(({ baseDir }) =>
312+
Effect.gen(function* () {
313+
const scratch = yield* ManagedProjectFolders.ManagedProjectFolders;
314+
const projects = yield* ProjectService.ProjectService;
315+
const fileSystem = yield* FileSystem.FileSystem;
316+
const path = yield* Path.Path;
317+
const root = yield* requireRoot;
318+
const target = path.join(baseDir, "visible-chats");
319+
yield* fileSystem.makeDirectory(target);
320+
yield* fileSystem.symlink(target, root);
321+
const { projectId } = yield* scratch.ensureScratchProject;
322+
yield* projects.update({
323+
commandId: CommandId.make("command:point-at-target"),
324+
projectId,
325+
workspaceRoot: target,
326+
});
327+
328+
assert.isTrue(yield* scratch.isScratchProject(projectId));
329+
assert.equal((yield* scratch.ensureScratchProject).projectId, projectId);
330+
assert.deepStrictEqual(yield* scratch.scratchRoot, Option.some(target));
331+
}),
332+
),
333+
);
334+
310335
const GIT_IDENTITY_KEYS = [
311336
"GIT_AUTHOR_NAME",
312337
"GIT_AUTHOR_EMAIL",

‎apps/server/src/project/ManagedProjectFolders.ts‎

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,25 @@ const make = Effect.gen(function* () {
273273
Duration.infinity,
274274
);
275275
const scratchProbe = probe.pipe(Effect.onInterrupt(() => invalidate));
276-
const scratchRoot = scratchProbe.pipe(Effect.map(Result.getSuccess));
276+
// A symlinked scratch folder lets the No project live at either the link or its target.
277+
const scratchProjectRoot = (root: string) =>
278+
fileSystem.realPath(root).pipe(
279+
Effect.flatMap((target) =>
280+
target === root
281+
? Effect.succeed(root)
282+
: projects
283+
.getByWorkspaceRoot(target)
284+
.pipe(Effect.map((project) => (Option.isSome(project) ? target : root))),
285+
),
286+
Effect.orElseSucceed(() => root),
287+
);
288+
const scratchRoot = scratchProbe.pipe(
289+
Effect.flatMap((probed) =>
290+
Result.isSuccess(probed)
291+
? scratchProjectRoot(probed.success).pipe(Effect.asSome)
292+
: Effect.succeedNone,
293+
),
294+
);
277295

278296
const makeScratchFolder = (folder: string) =>
279297
fileSystem
@@ -282,8 +300,9 @@ const make = Effect.gen(function* () {
282300

283301
const ensureScratchProject: ManagedProjectFolders["Service"]["ensureScratchProject"] = Effect.gen(
284302
function* () {
285-
const workspaceRoot = yield* scratchProbe.pipe(Effect.flatMap(Effect.fromResult));
286-
yield* makeScratchFolder(workspaceRoot);
303+
const probedRoot = yield* scratchProbe.pipe(Effect.flatMap(Effect.fromResult));
304+
yield* makeScratchFolder(probedRoot);
305+
const workspaceRoot = yield* scratchProjectRoot(probedRoot);
287306
const id = yield* random
288307
.uuidV4()
289308
.pipe(Effect.mapError((cause) => new ScratchProjectError({ workspaceRoot, cause })));

‎apps/server/src/project/ScratchThreadPromotion.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
import * as Context from "effect/Context";
88
import * as SecureRandom from "effect/crypto/SecureRandom";
99
import * as Effect from "effect/Effect";
10+
import * as FileSystem from "effect/FileSystem";
1011
import * as Layer from "effect/Layer";
1112
import * as Option from "effect/Option";
1213
import * as Path from "effect/Path";
@@ -50,6 +51,9 @@ const make = Effect.gen(function* () {
5051
const scheduledTasks = yield* ScheduledTaskService.ScheduledTaskService;
5152
const random = yield* SecureRandom.SecureRandom;
5253
const path = yield* Path.Path;
54+
const fileSystem = yield* FileSystem.FileSystem;
55+
const realPath = (folder: string) =>
56+
fileSystem.realPath(folder).pipe(Effect.orElseSucceed(() => folder));
5357

5458
const fail = (reason: string) => (cause: unknown) =>
5559
new ScratchThreadPromotionError({ reason, cause });
@@ -76,7 +80,7 @@ const make = Effect.gen(function* () {
7680
if (
7781
folder === undefined ||
7882
scratchRoot === undefined ||
79-
path.dirname(folder) !== scratchRoot ||
83+
(yield* realPath(path.dirname(folder))) !== (yield* realPath(scratchRoot)) ||
8084
!(isScratch || alreadyPromoted)
8185
) {
8286
return yield* new ScratchThreadPromotionError({

0 commit comments

Comments
 (0)