From 48011f6b8cc1782ea9a0414e358af814fd5122a2 Mon Sep 17 00:00:00 2001 From: jrust-DFS Date: Fri, 6 Nov 2026 17:17:32 -0500 Subject: [PATCH 1/8] fix(server): deduplicate symlinked project checkouts --- .../server/src/project/ProjectService.test.ts | 89 ++++++++++++++++++- apps/server/src/project/ProjectService.ts | 51 +++++++---- .../src/workspace/WorkspacePaths.test.ts | 2 +- apps/server/src/workspace/WorkspacePaths.ts | 14 ++- 4 files changed, 136 insertions(+), 20 deletions(-) diff --git a/apps/server/src/project/ProjectService.test.ts b/apps/server/src/project/ProjectService.test.ts index 988578f49d101..a4468e82b58f7 100644 --- a/apps/server/src/project/ProjectService.test.ts +++ b/apps/server/src/project/ProjectService.test.ts @@ -9,6 +9,9 @@ import { CommandId, type Project, ProjectId, ProviderInstanceId } from "@t3tools import * as Deferred from "effect/Deferred"; import * as Effect from "effect/Effect"; import * as Fiber from "effect/Fiber"; +import * as FileSystem from "effect/FileSystem"; +import * as Path from "effect/Path"; +import { symlinksSupported } from "@t3tools/shared/testing/symlinks"; import * as Layer from "effect/Layer"; import * as Metric from "effect/Metric"; import * as Option from "effect/Option"; @@ -96,10 +99,15 @@ const layerTestFor = ( | ProjectFaviconResolver.ProjectFaviconResolver | RepositoryIdentityResolver.RepositoryIdentityResolver >, + workspaceLayer: Layer.Layer< + WorkspacePaths.WorkspacePaths, + never, + FileSystem.FileSystem | Path.Path + > = layerWorkspacePaths, ) => RuntimeLayer.layerProjectService.pipe( Layer.provideMerge(ProjectEnrichmentService.layer), - Layer.provideMerge(layerWorkspacePaths), + Layer.provideMerge(workspaceLayer), Layer.provideMerge(projectMetadataLayer), Layer.provideMerge(SqlitePersistence.layerMemory), Layer.provideMerge(ServerSettings.layerTest()), @@ -110,6 +118,85 @@ const layerTestFor = ( const layerTest = layerTestFor(layerMetadata); +it.effect.skipIf(!symlinksSupported)( + "reuses symlinked checkouts, including legacy roots, without merging distinct folders", + () => + Effect.gen(function* () { + const service = yield* ProjectService.ProjectService; + const fs = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + const sql = yield* SqlClient.SqlClient; + const cwd = yield* fs.makeTempDirectoryScoped({ prefix: "project-alias-" }); + const physicalParent = path.join(cwd, "Development", "Code"); + const aliasParent = path.join(cwd, "Code"); + yield* fs.makeDirectory(physicalParent, { recursive: true }); + yield* fs.symlink(physicalParent, aliasParent); + const root = path.join(physicalParent, "project"); + const alias = path.join(aliasParent, "project"); + const input = { + commandId: CommandId.make("command:alias:first"), + projectId: ProjectId.make("project:alias:first"), + title: "Alias", + workspaceRoot: alias, + createWorkspaceRootIfMissing: true, + }; + const first = { project: yield* service.create(input) }; + const canonicalRoot = yield* fs.realPath(root); + assert.equal(first.project.workspaceRoot, canonicalRoot); + // Older versions persisted the unresolved path. Keep that project's identity and settings. + yield* sql`UPDATE projection_projects SET workspace_root = ${alias} + WHERE project_id = ${input.projectId}`; + const second = yield* service.bootstrap({ + ...input, + commandId: CommandId.make("command:alias:second"), + projectId: ProjectId.make("project:alias:second"), + workspaceRoot: root, + }); + assert.isFalse(second.created); + assert.equal(second.project.id, first.project.id); + const conflict = yield* service + .create({ + ...input, + commandId: CommandId.make("command:alias:duplicate"), + projectId: ProjectId.make("project:alias:duplicate"), + workspaceRoot: root, + }) + .pipe(Effect.flip); + assert.equal(conflict._tag, "ProjectConflictError"); + const separate = yield* service.create({ + ...input, + commandId: CommandId.make("command:alias:separate"), + projectId: ProjectId.make("project:alias:separate"), + workspaceRoot: path.join(cwd, "other", "project"), + createWorkspaceRootIfMissing: true, + }); + assert.notEqual(separate.id, first.project.id); + const moveConflict = yield* service + .update({ + commandId: CommandId.make("command:alias:move"), + projectId: separate.id, + workspaceRoot: alias, + }) + .pipe(Effect.flip); + assert.equal(moveConflict._tag, "ProjectConflictError"); + assert.equal( + Option.getOrThrow(yield* service.getByWorkspaceRoot(alias)).id, + first.project.id, + ); + assert.equal( + Option.getOrThrow(yield* service.getByWorkspaceRoot(root, { includeDeleted: true })).id, + first.project.id, + ); + assert.lengthOf((yield* service.snapshot).projects, 2); + }).pipe( + Effect.provide( + layerTestFor(layerMetadata, WorkspacePaths.layer).pipe( + Layer.provideMerge(NodeServices.layer), + ), + ), + ), +); + it.effect("resolves only the requested active project page in creation order", () => Effect.gen(function* () { const inspected = yield* Ref.make>(new Set()); diff --git a/apps/server/src/project/ProjectService.ts b/apps/server/src/project/ProjectService.ts index 9afaf22087a33..b17112f74e161 100644 --- a/apps/server/src/project/ProjectService.ts +++ b/apps/server/src/project/ProjectService.ts @@ -235,6 +235,39 @@ export const make = Effect.gen(function* () { ), ); + const findByWorkspaceRoot = Effect.fn("ProjectService.findByWorkspaceRoot")(function* ( + workspaceRoot: string, + options?: { readonly includeDeleted?: boolean }, + ) { + if (options?.includeDeleted !== true) { + const exact = yield* projects + .findActiveByWorkspaceRoot(workspaceRoot) + .pipe( + Effect.mapError( + (cause) => new ProjectOperationError({ operation: "read-project", cause }), + ), + ); + if (Option.isSome(exact)) return exact; + } + const rows = yield* projects + .list(options) + .pipe( + Effect.mapError( + (cause) => new ProjectOperationError({ operation: "list-projects", cause }), + ), + ); + const exact = rows.find((row) => row.workspaceRoot === workspaceRoot); + if (exact !== undefined) return Option.some(exact); + // Older registrations can still store a symlink spelling. Resolve without rewriting their history. + return yield* Effect.findFirst(rows, (row) => + normalizeWorkspaceRoot({ workspaceRoot: row.workspaceRoot }).pipe( + Effect.map((root) => root === workspaceRoot), + // An unavailable checkout cannot establish a matching filesystem identity. + Effect.orElseSucceed(() => false), + ), + ); + }); + const commit = Effect.fn("ProjectService.commit")(function* (command: ProjectCommand) { const { projectId } = command; const dispatchError = (cause: unknown) => @@ -246,9 +279,7 @@ export const make = Effect.gen(function* () { workspaceRoot === undefined ? undefined : Option.getOrUndefined( - yield* projects - .findActiveByWorkspaceRoot(workspaceRoot) - .pipe(Effect.mapError(dispatchError)), + yield* findByWorkspaceRoot(workspaceRoot).pipe(Effect.mapError(dispatchError)), ); const now = yield* DateTime.now; const eventId = yield* idAllocator.allocate @@ -335,19 +366,7 @@ export const make = Effect.gen(function* () { "ProjectService.getByWorkspaceRoot", )(function* (workspaceRoot, options) { const normalized = yield* normalizeWorkspaceRoot({ workspaceRoot }); - const row = yield* ( - options?.includeDeleted === true - ? projects - .list({ includeDeleted: true }) - .pipe( - Effect.map((rows) => - Option.fromUndefinedOr(rows.find((row) => row.workspaceRoot === normalized)), - ), - ) - : projects.findActiveByWorkspaceRoot(normalized) - ).pipe( - Effect.mapError((cause) => new ProjectOperationError({ operation: "list-projects", cause })), - ); + const row = yield* findByWorkspaceRoot(normalized, options); return Option.isNone(row) ? Option.none() : Option.some(yield* hydrate(row.value)); }); diff --git a/apps/server/src/workspace/WorkspacePaths.test.ts b/apps/server/src/workspace/WorkspacePaths.test.ts index b178c621453b8..890e8994e6b5b 100644 --- a/apps/server/src/workspace/WorkspacePaths.test.ts +++ b/apps/server/src/workspace/WorkspacePaths.test.ts @@ -36,7 +36,7 @@ it.layer(layerTest)("WorkspacePathsLive", (it) => { }); const stat = yield* fileSystem.stat(resolved); - expect(resolved).toBe(missingPath); + expect(resolved).toBe(yield* fileSystem.realPath(missingPath)); expect(stat.type).toBe("Directory"); }), ); diff --git a/apps/server/src/workspace/WorkspacePaths.ts b/apps/server/src/workspace/WorkspacePaths.ts index ea183ed43e1ab..0219e834718d8 100644 --- a/apps/server/src/workspace/WorkspacePaths.ts +++ b/apps/server/src/workspace/WorkspacePaths.ts @@ -38,7 +38,7 @@ export class WorkspaceRootStatFailedError extends Schema.TaggedError + new WorkspaceRootStatFailedError({ + workspaceRoot, + normalizedWorkspaceRoot, + phase: "resolve-real-path", + cause, + }), + ), + ); }); const resolveRelativePathWithinRoot: WorkspacePaths["Service"]["resolveRelativePathWithinRoot"] = From 7cc3c4dca93c750593d4adfe28a25ae6f8979e91 Mon Sep 17 00:00:00 2001 From: maria-rcks Date: Fri, 6 Nov 2026 23:01:33 +0000 Subject: [PATCH 2/8] test(server): cli lookup expects symlink aliases to resolve to one project --- apps/server/src/cli/project.test.ts | 23 +++++++---------------- 1 file changed, 7 insertions(+), 16 deletions(-) diff --git a/apps/server/src/cli/project.test.ts b/apps/server/src/cli/project.test.ts index 035b7d7e6f710..9524c100b67ef 100644 --- a/apps/server/src/cli/project.test.ts +++ b/apps/server/src/cli/project.test.ts @@ -436,7 +436,7 @@ it.layer(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))( }), ); - it.effect("normalizes existing paths without conflating separately registered symlinks", () => + it.effect("normalizes existing paths and resolves symlink aliases to the same project", () => Effect.gen(function* () { const { baseDir, workspaceRoot, project } = yield* makeProjectLookupFixture(); yield* runCli([ @@ -450,25 +450,16 @@ it.layer(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))( assert.equal((yield* readProjects(baseDir)).projects[0]?.title, "Normalized"); const aliasPath = `${workspaceRoot}-alias`; NodeFS.symlinkSync(workspaceRoot, aliasPath, "junction"); - const unknownAlias = yield* runCli([ - "project", - "remove", - aliasPath, - "--base-dir", - baseDir, - ]).pipe(Effect.flip); - assert.include(unknownAlias.message, "No active project found"); - yield* runCli(["project", "add", aliasPath, "--base-dir", baseDir]); - const added = (yield* readProjects(baseDir)).projects; - assert.equal(added.length, 2); - const aliasProject = added.find((entry) => entry.workspaceRoot === aliasPath); - assert.isDefined(aliasProject); - assert.notEqual(aliasProject?.id, project.id); - yield* runCli(["project", "remove", `${aliasPath}${NodePath.sep}.`, "--base-dir", baseDir]); + const duplicate = yield* runCli(["project", "add", aliasPath, "--base-dir", baseDir]).pipe( + Effect.flip, + ); + assert.include(duplicate.message, "already exists"); assert.deepEqual( (yield* readProjects(baseDir)).projects.map((entry) => entry.id), [project.id], ); + yield* runCli(["project", "remove", `${aliasPath}${NodePath.sep}.`, "--base-dir", baseDir]); + assert.lengthOf((yield* readProjects(baseDir)).projects, 0); assert.isTrue(NodeFS.existsSync(workspaceRoot)); }), ); From 6dfb3e62000bb7ff492e46a9f41e7cb7af483711 Mon Sep 17 00:00:00 2001 From: maria-rcks Date: Sun, 15 Nov 2026 02:16:23 +0000 Subject: [PATCH 3/8] fix(server): resolve legacy project aliases in cli --- apps/server/src/cli/project.test.ts | 4 ++++ apps/server/src/cli/project.ts | 11 ++++++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/apps/server/src/cli/project.test.ts b/apps/server/src/cli/project.test.ts index 9524c100b67ef..9372396cf9e04 100644 --- a/apps/server/src/cli/project.test.ts +++ b/apps/server/src/cli/project.test.ts @@ -458,6 +458,10 @@ it.layer(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))( (yield* readProjects(baseDir)).projects.map((entry) => entry.id), [project.id], ); + NodeFS.renameSync(workspaceRoot, `${workspaceRoot}-physical`); + NodeFS.symlinkSync(`${workspaceRoot}-physical`, workspaceRoot, "junction"); + yield* runCli(["project", "rename", aliasPath, "Legacy", "--base-dir", baseDir]); + assert.equal((yield* readProjects(baseDir)).projects[0]?.title, "Legacy"); yield* runCli(["project", "remove", `${aliasPath}${NodePath.sep}.`, "--base-dir", baseDir]); assert.lengthOf((yield* readProjects(baseDir)).projects, 0); assert.isTrue(NodeFS.existsSync(workspaceRoot)); diff --git a/apps/server/src/cli/project.ts b/apps/server/src/cli/project.ts index f604f8eb43696..e89df59c9fe4f 100644 --- a/apps/server/src/cli/project.ts +++ b/apps/server/src/cli/project.ts @@ -303,7 +303,16 @@ const findActiveProjectTarget = Effect.fn("findActiveProjectTarget")(function* ( (project) => project.workspaceRoot === normalizedWorkspaceRoot, ); - const resolved = exactWorkspaceMatch; + const resolved = + exactWorkspaceMatch ?? + Option.getOrUndefined( + yield* Effect.findFirst(activeProjects, (project) => + normalizeWorkspaceRootForProjectCommand(project.workspaceRoot).pipe( + Effect.map((root) => root === normalizedWorkspaceRoot), + Effect.orElseSucceed(() => false), + ), + ), + ); if (!resolved) { return yield* new ProjectNotFoundError({ operation: "resolveProjectTarget", From bee7ea2de9e1c752cc7a8b18e76656804098c575 Mon Sep 17 00:00:00 2001 From: maria-rcks Date: Sun, 15 Nov 2026 02:27:52 +0000 Subject: [PATCH 4/8] fix(server): preserve saved project path targets --- apps/server/src/cli/project.test.ts | 30 ++++++++++++++++++++++++++--- apps/server/src/cli/project.ts | 10 ++++++++-- 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/apps/server/src/cli/project.test.ts b/apps/server/src/cli/project.test.ts index 9372396cf9e04..d2b55edd6fc65 100644 --- a/apps/server/src/cli/project.test.ts +++ b/apps/server/src/cli/project.test.ts @@ -124,7 +124,7 @@ it.effect("adds, renames, and removes projects through the V2 project CLI domain yield* runCli(["project", "add", workspaceRoot, "--title", "Alpha", "--base-dir", baseDir]); const added = (yield* readProjects(baseDir)).projects[0]; assert.equal(added?.title, "Alpha"); - assert.equal(added?.workspaceRoot, workspaceRoot); + assert.equal(added?.workspaceRoot, NodeFS.realpathSync(workspaceRoot)); yield* runCli(["project", "rename", workspaceRoot, "Beta", "--base-dir", baseDir]); assert.equal((yield* readProjects(baseDir)).projects[0]?.title, "Beta"); @@ -196,7 +196,8 @@ it.effect.each(["snapshot", "mutation"] as const)( const makeProjectLookupFixture = Effect.fn("ProjectCliTest.makeProjectLookupFixture")(function* () { const fs = yield* FileSystem.FileSystem; - const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-v2-project-lookup-" }); + const temporaryRoot = yield* fs.makeTempDirectoryScoped({ prefix: "t3-v2-project-lookup-" }); + const root = yield* fs.realPath(temporaryRoot); const baseDir = NodePath.join(root, "state"); const workspaceRoot = NodePath.join(root, "workspace"); yield* fs.makeDirectory(workspaceRoot); @@ -458,8 +459,31 @@ it.layer(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))( (yield* readProjects(baseDir)).projects.map((entry) => entry.id), [project.id], ); - NodeFS.renameSync(workspaceRoot, `${workspaceRoot}-physical`); + const physicalRoot = `${workspaceRoot}-physical`; + NodeFS.mkdirSync(physicalRoot); + yield* runCli(["project", "add", physicalRoot, "--base-dir", baseDir]); + const physicalProject = (yield* readProjects(baseDir)).projects.find( + (entry) => entry.workspaceRoot === physicalRoot, + ); + assert.isDefined(physicalProject); + NodeFS.rmdirSync(physicalRoot); + NodeFS.renameSync(workspaceRoot, physicalRoot); NodeFS.symlinkSync(`${workspaceRoot}-physical`, workspaceRoot, "junction"); + yield* runCli([ + "project", + "rename", + `${workspaceRoot}${NodePath.sep}.`, + "Saved alias", + "--base-dir", + baseDir, + ]); + const renamed = (yield* readProjects(baseDir)).projects; + assert.equal(renamed.find((entry) => entry.id === project.id)?.title, "Saved alias"); + assert.equal( + renamed.find((entry) => entry.id === physicalProject.id)?.title, + physicalProject.title, + ); + yield* runCli(["project", "remove", physicalProject.id, "--base-dir", baseDir]); yield* runCli(["project", "rename", aliasPath, "Legacy", "--base-dir", baseDir]); assert.equal((yield* readProjects(baseDir)).projects[0]?.title, "Legacy"); yield* runCli(["project", "remove", `${aliasPath}${NodePath.sep}.`, "--base-dir", baseDir]); diff --git a/apps/server/src/cli/project.ts b/apps/server/src/cli/project.ts index e89df59c9fe4f..a56c82ffca5b1 100644 --- a/apps/server/src/cli/project.ts +++ b/apps/server/src/cli/project.ts @@ -7,6 +7,8 @@ import { type ProjectSnapshot, ProjectId, } from "@t3tools/contracts"; +import { expandHomePath } from "@t3tools/provider-core/server/pathExpansion"; +import * as HostProcess from "@t3tools/shared/HostProcess"; import * as Console from "effect/Console"; import * as SecureRandom from "effect/crypto/SecureRandom"; import * as Duration from "effect/Duration"; @@ -299,9 +301,13 @@ const findActiveProjectTarget = Effect.fn("findActiveProjectTarget")(function* ( ? normalizedWorkspaceRootResult.success : normalizedWorkspaceRootResult.failure.normalizedWorkspaceRoot; - const exactWorkspaceMatch = activeProjects.find( - (project) => project.workspaceRoot === normalizedWorkspaceRoot, + const path = yield* Path.Path; + const savedWorkspaceRoot = path.resolve( + expandHomePath(trimmedIdentifier, yield* HostProcess.HomeDirectory), ); + const exactWorkspaceMatch = + activeProjects.find((project) => project.workspaceRoot === savedWorkspaceRoot) ?? + activeProjects.find((project) => project.workspaceRoot === normalizedWorkspaceRoot); const resolved = exactWorkspaceMatch ?? From 3bf768c16fd72ea8252abbb4f4582b8d47ef3025 Mon Sep 17 00:00:00 2001 From: maria-rcks Date: Wed, 18 Nov 2026 08:32:07 +0000 Subject: [PATCH 5/8] fix(server): restore archived legacy checkout aliases --- apps/server/src/cli/project.test.ts | 12 ++++++++++++ apps/server/src/cli/project.ts | 8 +++++--- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/apps/server/src/cli/project.test.ts b/apps/server/src/cli/project.test.ts index 696dc0c21c7fe..666353ebbdde2 100644 --- a/apps/server/src/cli/project.test.ts +++ b/apps/server/src/cli/project.test.ts @@ -548,6 +548,18 @@ it.layer(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))( physicalProject.title, ); yield* runCli(["project", "remove", physicalProject.id, "--base-dir", baseDir]); + yield* withProjectService(baseDir, (projects) => + projects.update({ + commandId: CommandId.make("project-cli-archive-legacy-alias"), + projectId: project.id, + archived: true, + }), + ); + yield* runCli(["project", "add", aliasPath, "--base-dir", baseDir]); + const restored = (yield* readProjects(baseDir)).projects; + assert.lengthOf(restored, 1); + assert.equal(restored[0]?.id, project.id); + assert.isNull(restored[0]?.archivedAt); yield* runCli(["project", "rename", aliasPath, "Legacy", "--base-dir", baseDir]); assert.equal((yield* readProjects(baseDir)).projects[0]?.title, "Legacy"); yield* runCli(["project", "remove", `${aliasPath}${NodePath.sep}.`, "--base-dir", baseDir]); diff --git a/apps/server/src/cli/project.ts b/apps/server/src/cli/project.ts index ff1d1c0915187..02d774b4fd63a 100644 --- a/apps/server/src/cli/project.ts +++ b/apps/server/src/cli/project.ts @@ -503,9 +503,11 @@ const projectAddCommand = Command.make("add", { ) => Effect.Effect; }) { const workspaceRoot = yield* normalizeWorkspaceRootForProjectCommand(flags.workspaceRoot); - const existingProject = snapshot.projects.find( - (project) => project.deletedAt === null && project.workspaceRoot === workspaceRoot, - ); + const target = yield* findActiveProjectTarget({ + snapshot, + identifier: flags.workspaceRoot, + }).pipe(Effect.catchTags({ ProjectNotFoundError: () => Effect.undefined })); + const existingProject = snapshot.projects.find((project) => project.id === target?.id); // Re-adding an archived project's folder restores it, matching the clients. if (existingProject?.archivedAt != null) { const title = Option.isSome(flags.title) From 89920b2387cd58535e11dddd2b5a36c93cf23a23 Mon Sep 17 00:00:00 2001 From: maria-rcks Date: Wed, 18 Nov 2026 08:35:37 +0000 Subject: [PATCH 6/8] fix(server): keep project add arguments path based --- apps/server/src/cli/project.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/apps/server/src/cli/project.ts b/apps/server/src/cli/project.ts index 02d774b4fd63a..45f4c6d82f9a1 100644 --- a/apps/server/src/cli/project.ts +++ b/apps/server/src/cli/project.ts @@ -503,9 +503,12 @@ const projectAddCommand = Command.make("add", { ) => Effect.Effect; }) { const workspaceRoot = yield* normalizeWorkspaceRootForProjectCommand(flags.workspaceRoot); + const path = yield* Path.Path; const target = yield* findActiveProjectTarget({ snapshot, - identifier: flags.workspaceRoot, + identifier: path.resolve( + expandHomePath(flags.workspaceRoot, yield* HostProcess.HomeDirectory), + ), }).pipe(Effect.catchTags({ ProjectNotFoundError: () => Effect.undefined })); const existingProject = snapshot.projects.find((project) => project.id === target?.id); // Re-adding an archived project's folder restores it, matching the clients. From 99eac53b97f2aef462e20f9f463c6d40b9db0c6f Mon Sep 17 00:00:00 2001 From: maria-rcks Date: Wed, 18 Nov 2026 08:39:30 +0000 Subject: [PATCH 7/8] fix(server): normalize padded checkout aliases consistently --- apps/server/src/cli/project.test.ts | 2 +- apps/server/src/cli/project.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/apps/server/src/cli/project.test.ts b/apps/server/src/cli/project.test.ts index 666353ebbdde2..c0042a1aa514b 100644 --- a/apps/server/src/cli/project.test.ts +++ b/apps/server/src/cli/project.test.ts @@ -555,7 +555,7 @@ it.layer(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))( archived: true, }), ); - yield* runCli(["project", "add", aliasPath, "--base-dir", baseDir]); + yield* runCli(["project", "add", ` ${aliasPath} `, "--base-dir", baseDir]); const restored = (yield* readProjects(baseDir)).projects; assert.lengthOf(restored, 1); assert.equal(restored[0]?.id, project.id); diff --git a/apps/server/src/cli/project.ts b/apps/server/src/cli/project.ts index 45f4c6d82f9a1..72fc142a7be59 100644 --- a/apps/server/src/cli/project.ts +++ b/apps/server/src/cli/project.ts @@ -507,7 +507,7 @@ const projectAddCommand = Command.make("add", { const target = yield* findActiveProjectTarget({ snapshot, identifier: path.resolve( - expandHomePath(flags.workspaceRoot, yield* HostProcess.HomeDirectory), + expandHomePath(flags.workspaceRoot.trim(), yield* HostProcess.HomeDirectory), ), }).pipe(Effect.catchTags({ ProjectNotFoundError: () => Effect.undefined })); const existingProject = snapshot.projects.find((project) => project.id === target?.id); From 8b4887015ba796c5d432b7fbe2fb8ab78b5e41cd Mon Sep 17 00:00:00 2001 From: maria-rcks Date: Wed, 18 Nov 2026 12:47:34 +0000 Subject: [PATCH 8/8] fix(server): reject checkout roots that path contracts would trim --- apps/server/src/workspace/WorkspaceEntries.ts | 1 + .../src/workspace/WorkspacePaths.test.ts | 39 ++++++++++++------- apps/server/src/workspace/WorkspacePaths.ts | 24 +++++++++++- apps/server/src/ws.ts | 6 +++ 4 files changed, 55 insertions(+), 15 deletions(-) diff --git a/apps/server/src/workspace/WorkspaceEntries.ts b/apps/server/src/workspace/WorkspaceEntries.ts index ad1d2c2ae0ebe..304635ae46721 100644 --- a/apps/server/src/workspace/WorkspaceEntries.ts +++ b/apps/server/src/workspace/WorkspaceEntries.ts @@ -87,6 +87,7 @@ export const WorkspaceEntriesError = Schema.Union([ WorkspacePaths.WorkspaceRootCreateFailedError, WorkspacePaths.WorkspaceRootStatFailedError, WorkspacePaths.WorkspaceRootNotDirectoryError, + WorkspacePaths.WorkspaceRootUnsupportedPathError, WorkspaceSearchIndex.WorkspaceSearchIndexCreateFailed, WorkspaceSearchIndex.WorkspaceSearchIndexScanTimedOut, WorkspaceSearchIndex.WorkspaceSearchIndexSearchFailed, diff --git a/apps/server/src/workspace/WorkspacePaths.test.ts b/apps/server/src/workspace/WorkspacePaths.test.ts index 890e8994e6b5b..86ea26cd88cbb 100644 --- a/apps/server/src/workspace/WorkspacePaths.test.ts +++ b/apps/server/src/workspace/WorkspacePaths.test.ts @@ -23,22 +23,33 @@ const makeTempDir = Effect.fn("makeTempDir")(function* () { it.layer(layerTest)("WorkspacePathsLive", (it) => { describe("normalizeWorkspaceRoot", () => { - it.effect("creates missing directories when createIfMissing is enabled", () => - Effect.gen(function* () { - const workspacePaths = yield* WorkspacePaths.WorkspacePaths; - const fileSystem = yield* FileSystem.FileSystem; - const cwd = yield* makeTempDir(); - const path = yield* Path.Path; - const missingPath = path.join(cwd, "nested", "new-project"); + it.effect( + "creates missing roots and rejects aliases whose resolved paths cannot be preserved", + () => + Effect.gen(function* () { + const workspacePaths = yield* WorkspacePaths.WorkspacePaths; + const fileSystem = yield* FileSystem.FileSystem; + const cwd = yield* makeTempDir(); + const path = yield* Path.Path; + const missingPath = path.join(cwd, "nested", "new-project"); - const resolved = yield* workspacePaths.normalizeWorkspaceRoot(missingPath, { - createIfMissing: true, - }); - const stat = yield* fileSystem.stat(resolved); + const resolved = yield* workspacePaths.normalizeWorkspaceRoot(missingPath, { + createIfMissing: true, + }); + const stat = yield* fileSystem.stat(resolved); - expect(resolved).toBe(yield* fileSystem.realPath(missingPath)); - expect(stat.type).toBe("Directory"); - }), + expect(resolved).toBe(yield* fileSystem.realPath(missingPath)); + expect(stat.type).toBe("Directory"); + + if (HostProcess.Platform.defaultValue() !== "win32") { + const spacedRoot = path.join(cwd, "spaced "); + const aliasRoot = path.join(cwd, "alias"); + yield* fileSystem.makeDirectory(spacedRoot); + yield* fileSystem.symlink(spacedRoot, aliasRoot); + const error = yield* workspacePaths.normalizeWorkspaceRoot(aliasRoot).pipe(Effect.flip); + expect(error._tag).toBe("WorkspaceRootUnsupportedPathError"); + } + }), ); }); diff --git a/apps/server/src/workspace/WorkspacePaths.ts b/apps/server/src/workspace/WorkspacePaths.ts index cce1d39c6d1b7..43ee3b8230c3c 100644 --- a/apps/server/src/workspace/WorkspacePaths.ts +++ b/apps/server/src/workspace/WorkspacePaths.ts @@ -59,6 +59,18 @@ export class WorkspaceRootNotDirectoryError extends Schema.TaggedError()( + "WorkspaceRootUnsupportedPathError", + { + workspaceRoot: Schema.String, + normalizedWorkspaceRoot: Schema.String, + }, +) { + override get message(): string { + return `Workspace root resolves to a path ending in whitespace. Rename the directory before adding it: ${JSON.stringify(this.normalizedWorkspaceRoot)}`; + } +} + export class WorkspacePathOutsideRootError extends Schema.TaggedError()( "WorkspacePathOutsideRootError", { @@ -76,6 +88,7 @@ export const WorkspacePathsError = Schema.Union([ WorkspaceRootCreateFailedError, WorkspaceRootStatFailedError, WorkspaceRootNotDirectoryError, + WorkspaceRootUnsupportedPathError, WorkspacePathOutsideRootError, ]); export type WorkspacePathsError = typeof WorkspacePathsError.Type; @@ -92,6 +105,7 @@ export class WorkspacePaths extends Context.Service< | WorkspaceRootCreateFailedError | WorkspaceRootStatFailedError | WorkspaceRootNotDirectoryError + | WorkspaceRootUnsupportedPathError >; readonly resolveRelativePathWithinRoot: (input: { workspaceRoot: string; @@ -170,7 +184,7 @@ const make = Effect.gen(function* () { normalizedWorkspaceRoot, }); } - return yield* fileSystem.realPath(normalizedWorkspaceRoot).pipe( + const realRoot = yield* fileSystem.realPath(normalizedWorkspaceRoot).pipe( Effect.mapError( (cause) => new WorkspaceRootStatFailedError({ @@ -181,6 +195,14 @@ const make = Effect.gen(function* () { }), ), ); + // Existing path contracts trim strings, which would persist a different directory. + if (realRoot !== realRoot.trimEnd()) { + return yield* new WorkspaceRootUnsupportedPathError({ + workspaceRoot, + normalizedWorkspaceRoot: realRoot, + }); + } + return realRoot; }); const resolveRelativePathWithinRoot: WorkspacePaths["Service"]["resolveRelativePathWithinRoot"] = diff --git a/apps/server/src/ws.ts b/apps/server/src/ws.ts index bbe80f28e9681..ec2ccb7985a1a 100644 --- a/apps/server/src/ws.ts +++ b/apps/server/src/ws.ts @@ -608,6 +608,12 @@ function projectEntriesFailureContext(error: WorkspaceEntries.WorkspaceEntriesEr failure: "workspace_root_create_failed", normalizedCwd: error.normalizedWorkspaceRoot, }; + case "WorkspaceRootUnsupportedPathError": + return { + failure: "workspace_root_stat_failed", + normalizedCwd: error.normalizedWorkspaceRoot, + detail: error.message, + }; case "WorkspaceRootStatFailedError": return { failure: "workspace_root_stat_failed",