Skip to content

Commit 48011f6

Browse files
committed
fix(server): deduplicate symlinked project checkouts
1 parent b90e554 commit 48011f6

4 files changed

Lines changed: 136 additions & 20 deletions

File tree

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

Lines changed: 88 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ import { CommandId, type Project, ProjectId, ProviderInstanceId } from "@t3tools
99
import * as Deferred from "effect/Deferred";
1010
import * as Effect from "effect/Effect";
1111
import * as Fiber from "effect/Fiber";
12+
import * as FileSystem from "effect/FileSystem";
13+
import * as Path from "effect/Path";
14+
import { symlinksSupported } from "@t3tools/shared/testing/symlinks";
1215
import * as Layer from "effect/Layer";
1316
import * as Metric from "effect/Metric";
1417
import * as Option from "effect/Option";
@@ -96,10 +99,15 @@ const layerTestFor = (
9699
| ProjectFaviconResolver.ProjectFaviconResolver
97100
| RepositoryIdentityResolver.RepositoryIdentityResolver
98101
>,
102+
workspaceLayer: Layer.Layer<
103+
WorkspacePaths.WorkspacePaths,
104+
never,
105+
FileSystem.FileSystem | Path.Path
106+
> = layerWorkspacePaths,
99107
) =>
100108
RuntimeLayer.layerProjectService.pipe(
101109
Layer.provideMerge(ProjectEnrichmentService.layer),
102-
Layer.provideMerge(layerWorkspacePaths),
110+
Layer.provideMerge(workspaceLayer),
103111
Layer.provideMerge(projectMetadataLayer),
104112
Layer.provideMerge(SqlitePersistence.layerMemory),
105113
Layer.provideMerge(ServerSettings.layerTest()),
@@ -110,6 +118,85 @@ const layerTestFor = (
110118

111119
const layerTest = layerTestFor(layerMetadata);
112120

121+
it.effect.skipIf(!symlinksSupported)(
122+
"reuses symlinked checkouts, including legacy roots, without merging distinct folders",
123+
() =>
124+
Effect.gen(function* () {
125+
const service = yield* ProjectService.ProjectService;
126+
const fs = yield* FileSystem.FileSystem;
127+
const path = yield* Path.Path;
128+
const sql = yield* SqlClient.SqlClient;
129+
const cwd = yield* fs.makeTempDirectoryScoped({ prefix: "project-alias-" });
130+
const physicalParent = path.join(cwd, "Development", "Code");
131+
const aliasParent = path.join(cwd, "Code");
132+
yield* fs.makeDirectory(physicalParent, { recursive: true });
133+
yield* fs.symlink(physicalParent, aliasParent);
134+
const root = path.join(physicalParent, "project");
135+
const alias = path.join(aliasParent, "project");
136+
const input = {
137+
commandId: CommandId.make("command:alias:first"),
138+
projectId: ProjectId.make("project:alias:first"),
139+
title: "Alias",
140+
workspaceRoot: alias,
141+
createWorkspaceRootIfMissing: true,
142+
};
143+
const first = { project: yield* service.create(input) };
144+
const canonicalRoot = yield* fs.realPath(root);
145+
assert.equal(first.project.workspaceRoot, canonicalRoot);
146+
// Older versions persisted the unresolved path. Keep that project's identity and settings.
147+
yield* sql`UPDATE projection_projects SET workspace_root = ${alias}
148+
WHERE project_id = ${input.projectId}`;
149+
const second = yield* service.bootstrap({
150+
...input,
151+
commandId: CommandId.make("command:alias:second"),
152+
projectId: ProjectId.make("project:alias:second"),
153+
workspaceRoot: root,
154+
});
155+
assert.isFalse(second.created);
156+
assert.equal(second.project.id, first.project.id);
157+
const conflict = yield* service
158+
.create({
159+
...input,
160+
commandId: CommandId.make("command:alias:duplicate"),
161+
projectId: ProjectId.make("project:alias:duplicate"),
162+
workspaceRoot: root,
163+
})
164+
.pipe(Effect.flip);
165+
assert.equal(conflict._tag, "ProjectConflictError");
166+
const separate = yield* service.create({
167+
...input,
168+
commandId: CommandId.make("command:alias:separate"),
169+
projectId: ProjectId.make("project:alias:separate"),
170+
workspaceRoot: path.join(cwd, "other", "project"),
171+
createWorkspaceRootIfMissing: true,
172+
});
173+
assert.notEqual(separate.id, first.project.id);
174+
const moveConflict = yield* service
175+
.update({
176+
commandId: CommandId.make("command:alias:move"),
177+
projectId: separate.id,
178+
workspaceRoot: alias,
179+
})
180+
.pipe(Effect.flip);
181+
assert.equal(moveConflict._tag, "ProjectConflictError");
182+
assert.equal(
183+
Option.getOrThrow(yield* service.getByWorkspaceRoot(alias)).id,
184+
first.project.id,
185+
);
186+
assert.equal(
187+
Option.getOrThrow(yield* service.getByWorkspaceRoot(root, { includeDeleted: true })).id,
188+
first.project.id,
189+
);
190+
assert.lengthOf((yield* service.snapshot).projects, 2);
191+
}).pipe(
192+
Effect.provide(
193+
layerTestFor(layerMetadata, WorkspacePaths.layer).pipe(
194+
Layer.provideMerge(NodeServices.layer),
195+
),
196+
),
197+
),
198+
);
199+
113200
it.effect("resolves only the requested active project page in creation order", () =>
114201
Effect.gen(function* () {
115202
const inspected = yield* Ref.make<ReadonlySet<string>>(new Set());

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

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,39 @@ export const make = Effect.gen(function* () {
235235
),
236236
);
237237

238+
const findByWorkspaceRoot = Effect.fn("ProjectService.findByWorkspaceRoot")(function* (
239+
workspaceRoot: string,
240+
options?: { readonly includeDeleted?: boolean },
241+
) {
242+
if (options?.includeDeleted !== true) {
243+
const exact = yield* projects
244+
.findActiveByWorkspaceRoot(workspaceRoot)
245+
.pipe(
246+
Effect.mapError(
247+
(cause) => new ProjectOperationError({ operation: "read-project", cause }),
248+
),
249+
);
250+
if (Option.isSome(exact)) return exact;
251+
}
252+
const rows = yield* projects
253+
.list(options)
254+
.pipe(
255+
Effect.mapError(
256+
(cause) => new ProjectOperationError({ operation: "list-projects", cause }),
257+
),
258+
);
259+
const exact = rows.find((row) => row.workspaceRoot === workspaceRoot);
260+
if (exact !== undefined) return Option.some(exact);
261+
// Older registrations can still store a symlink spelling. Resolve without rewriting their history.
262+
return yield* Effect.findFirst(rows, (row) =>
263+
normalizeWorkspaceRoot({ workspaceRoot: row.workspaceRoot }).pipe(
264+
Effect.map((root) => root === workspaceRoot),
265+
// An unavailable checkout cannot establish a matching filesystem identity.
266+
Effect.orElseSucceed(() => false),
267+
),
268+
);
269+
});
270+
238271
const commit = Effect.fn("ProjectService.commit")(function* (command: ProjectCommand) {
239272
const { projectId } = command;
240273
const dispatchError = (cause: unknown) =>
@@ -246,9 +279,7 @@ export const make = Effect.gen(function* () {
246279
workspaceRoot === undefined
247280
? undefined
248281
: Option.getOrUndefined(
249-
yield* projects
250-
.findActiveByWorkspaceRoot(workspaceRoot)
251-
.pipe(Effect.mapError(dispatchError)),
282+
yield* findByWorkspaceRoot(workspaceRoot).pipe(Effect.mapError(dispatchError)),
252283
);
253284
const now = yield* DateTime.now;
254285
const eventId = yield* idAllocator.allocate
@@ -335,19 +366,7 @@ export const make = Effect.gen(function* () {
335366
"ProjectService.getByWorkspaceRoot",
336367
)(function* (workspaceRoot, options) {
337368
const normalized = yield* normalizeWorkspaceRoot({ workspaceRoot });
338-
const row = yield* (
339-
options?.includeDeleted === true
340-
? projects
341-
.list({ includeDeleted: true })
342-
.pipe(
343-
Effect.map((rows) =>
344-
Option.fromUndefinedOr(rows.find((row) => row.workspaceRoot === normalized)),
345-
),
346-
)
347-
: projects.findActiveByWorkspaceRoot(normalized)
348-
).pipe(
349-
Effect.mapError((cause) => new ProjectOperationError({ operation: "list-projects", cause })),
350-
);
369+
const row = yield* findByWorkspaceRoot(normalized, options);
351370
return Option.isNone(row) ? Option.none() : Option.some(yield* hydrate(row.value));
352371
});
353372

‎apps/server/src/workspace/WorkspacePaths.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ it.layer(layerTest)("WorkspacePathsLive", (it) => {
3636
});
3737
const stat = yield* fileSystem.stat(resolved);
3838

39-
expect(resolved).toBe(missingPath);
39+
expect(resolved).toBe(yield* fileSystem.realPath(missingPath));
4040
expect(stat.type).toBe("Directory");
4141
}),
4242
);

‎apps/server/src/workspace/WorkspacePaths.ts‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export class WorkspaceRootStatFailedError extends Schema.TaggedError<WorkspaceRo
3838
{
3939
workspaceRoot: Schema.String,
4040
normalizedWorkspaceRoot: Schema.String,
41-
phase: Schema.Literals(["validate-existing", "verify-created"]),
41+
phase: Schema.Literals(["validate-existing", "verify-created", "resolve-real-path"]),
4242
cause: Schema.Defect(),
4343
},
4444
) {
@@ -170,7 +170,17 @@ const make = Effect.gen(function* () {
170170
normalizedWorkspaceRoot,
171171
});
172172
}
173-
return normalizedWorkspaceRoot;
173+
return yield* fileSystem.realPath(normalizedWorkspaceRoot).pipe(
174+
Effect.mapError(
175+
(cause) =>
176+
new WorkspaceRootStatFailedError({
177+
workspaceRoot,
178+
normalizedWorkspaceRoot,
179+
phase: "resolve-real-path",
180+
cause,
181+
}),
182+
),
183+
);
174184
});
175185

176186
const resolveRelativePathWithinRoot: WorkspacePaths["Service"]["resolveRelativePathWithinRoot"] =

0 commit comments

Comments
 (0)