Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion packages/server/src/server/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1580,7 +1580,11 @@ export class Session {
): Promise<PersistedWorkspaceRecord | null> {
const normalizedCwd = await this.resolveWorkspaceDirectory(cwd, options);
const workspaces = await this.workspaceRegistry.list();
return workspaces.find((workspace) => workspace.cwd === normalizedCwd) ?? null;
return (
workspaces.find(
(workspace) => normalizePersistedWorkspaceId(workspace.cwd) === normalizedCwd,
) ?? null
);
}

private async resolveWorkspaceDirectory(
Expand Down
19 changes: 11 additions & 8 deletions packages/server/src/server/workspace-directory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,26 +215,29 @@ export class WorkspaceDirectory {

resolveRegisteredWorkspaceIdForCwd(cwd: string, workspaces: PersistedWorkspaceRecord[]): string {
const normalizedCwd = normalizeWorkspaceId(cwd);
const exact = workspaces.find((workspace) => workspace.cwd === normalizedCwd);
const exact = workspaces.find(
(workspace) => normalizeWorkspaceId(workspace.cwd) === normalizedCwd,
);
if (exact) {
return exact.workspaceId;
}

const userHome = homedir();
let bestMatch: PersistedWorkspaceRecord | null = null;
const userHome = normalizeWorkspaceId(homedir());
let bestMatch: { workspace: PersistedWorkspaceRecord; normalizedCwd: string } | null = null;
for (const workspace of workspaces) {
if (workspace.cwd === userHome) continue;
if (workspace.archivedAt) continue;
const prefix = workspace.cwd.endsWith(sep) ? workspace.cwd : `${workspace.cwd}${sep}`;
const normalizedWsCwd = normalizeWorkspaceId(workspace.cwd);
if (normalizedWsCwd === userHome) continue;
const prefix = normalizedWsCwd.endsWith(sep) ? normalizedWsCwd : `${normalizedWsCwd}${sep}`;
if (!normalizedCwd.startsWith(prefix)) {
continue;
}
if (!bestMatch || workspace.cwd.length > bestMatch.cwd.length) {
bestMatch = workspace;
if (!bestMatch || normalizedWsCwd.length > bestMatch.normalizedCwd.length) {
bestMatch = { workspace, normalizedCwd: normalizedWsCwd };
}
}

return bestMatch?.workspaceId ?? normalizedCwd;
return bestMatch?.workspace.workspaceId ?? normalizedCwd;
}

async listDescriptors(): Promise<WorkspaceDescriptorPayload[]> {
Expand Down
38 changes: 38 additions & 0 deletions packages/server/src/server/workspace-registry-model.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import { mkdirSync, realpathSync, rmSync, symlinkSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";

import { describe, expect, test, vi } from "vitest";

import {
Expand Down Expand Up @@ -197,3 +201,37 @@ describe("git worktree grouping", () => {
).toBe("worktree");
});
});

describe("normalizeWorkspaceId", () => {
const testDir = join(tmpdir(), "paseo-test-normalize");

afterEach(() => {
try {
rmSync(testDir, { recursive: true, force: true });
} catch {
// ignore
}
});

test("resolves symlinks to their real path", () => {
const realDir = join(testDir, "real-dir");
const linkPath = join(testDir, "link-to-dir");
mkdirSync(realDir, { recursive: true });
symlinkSync(realDir, linkPath);

expect(normalizeWorkspaceId(linkPath)).toBe(realpathSync(realDir));
});

test("returns resolved path when no symlink is involved", () => {
expect(normalizeWorkspaceId("/tmp")).toBe(realpathSync("/tmp"));
});

test("returns resolved path for non-existent paths (realpathSync fails gracefully)", () => {
const result = normalizeWorkspaceId("/nonexistent/path");
expect(result).toBe("/nonexistent/path");
});

test("returns empty string unchanged", () => {
expect(normalizeWorkspaceId("")).toBe("");
});
});
8 changes: 7 additions & 1 deletion packages/server/src/server/workspace-registry-model.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { realpathSync } from "node:fs";
import { resolve } from "node:path";

import type {
Expand Down Expand Up @@ -32,7 +33,12 @@ export function normalizeWorkspaceId(cwd: string): string {
if (!trimmed) {
return cwd;
}
return resolve(trimmed);
const resolved = resolve(trimmed);
try {
return realpathSync(resolved);
} catch {
return resolved;
}
}

export function deriveWorkspaceId(cwd: string, checkout: ProjectCheckoutLitePayload): string {
Expand Down