Skip to content

Commit 60499a4

Browse files
committed
fix(web): match stripped chip paths case-insensitively on Windows
1 parent 2fba743 commit 60499a4

3 files changed

Lines changed: 67 additions & 5 deletions

File tree

‎apps/web/src/components/ChatMarkdown.tsx‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ import { projectEnvironment } from "../state/projects";
175175
import {
176176
claimWorkspaceBasenameLookup,
177177
needsWorkspaceBasenameLookup,
178+
pickStrippedWorkspaceMatch,
178179
pickWorkspaceBasenameMatch,
179180
stripRepeatedWorkspacePrefix,
180181
WORKSPACE_BASENAME_LOOKUP_LIMIT,
@@ -2579,11 +2580,12 @@ function useChatMarkdownState({
25792580
if (strippedPath === null) {
25802581
return pickWorkspaceBasenameMatch(workspaceRelativePath, result.value.entries);
25812582
}
2582-
// A real child folder named like the project keeps the literal path.
2583-
const paths = new Set(result.value.entries.map((entry) => entry.path));
2584-
return !paths.has(workspaceRelativePath.replaceAll("\\", "/")) && paths.has(strippedPath)
2585-
? strippedPath
2586-
: null;
2583+
return pickStrippedWorkspaceMatch(
2584+
workspaceRelativePath,
2585+
strippedPath,
2586+
cwd,
2587+
result.value.entries,
2588+
);
25872589
},
25882590
[cwd, environmentId, searchProjectEntries],
25892591
);

‎apps/web/src/workspaceBasenameLookup.test.ts‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { describe, expect, it } from "vite-plus/test";
33
import {
44
claimWorkspaceBasenameLookup,
55
needsWorkspaceBasenameLookup,
6+
pickStrippedWorkspaceMatch,
67
pickWorkspaceBasenameMatch,
78
stripRepeatedWorkspacePrefix,
89
} from "./workspaceBasenameLookup";
@@ -112,3 +113,39 @@ describe("stripRepeatedWorkspacePrefix", () => {
112113
expect(stripRepeatedWorkspacePrefix("physics", "/school/physics")).toBeNull();
113114
});
114115
});
116+
117+
describe("pickStrippedWorkspaceMatch", () => {
118+
const file = (path: string) => ({ path, kind: "file" as const });
119+
120+
it("opens the stripped path when only it exists", () => {
121+
expect(
122+
pickStrippedWorkspaceMatch("physics/notes/a.md", "notes/a.md", "/school/physics", [
123+
file("notes/a.md"),
124+
]),
125+
).toBe("notes/a.md");
126+
});
127+
128+
it("keeps the literal path when a real nested folder exists", () => {
129+
expect(
130+
pickStrippedWorkspaceMatch("physics/notes/a.md", "notes/a.md", "/school/physics", [
131+
file("notes/a.md"),
132+
file("physics/notes/a.md"),
133+
]),
134+
).toBeNull();
135+
});
136+
137+
it("matches casing loosely only on Windows", () => {
138+
const entries = [file("Notes/a.md")];
139+
expect(
140+
pickStrippedWorkspaceMatch(
141+
"physics\\notes\\a.md",
142+
"notes/a.md",
143+
"C:\\school\\physics",
144+
entries,
145+
),
146+
).toBe("Notes/a.md");
147+
expect(
148+
pickStrippedWorkspaceMatch("physics/notes/a.md", "notes/a.md", "/school/physics", entries),
149+
).toBeNull();
150+
});
151+
});

‎apps/web/src/workspaceBasenameLookup.ts‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,26 @@ export function stripRepeatedWorkspacePrefix(relativePath: string, cwd: string):
7474
}
7575
return null;
7676
}
77+
78+
/**
79+
* Resolves a stripped path against index entries. A real child folder named
80+
* like the project keeps the literal path, so this returns null when the
81+
* literal path is indexed too.
82+
*/
83+
export function pickStrippedWorkspaceMatch(
84+
literalPath: string,
85+
strippedPath: string,
86+
cwd: string,
87+
entries: ReadonlyArray<WorkspaceEntryCandidate>,
88+
): string | null {
89+
const caseInsensitive = isWindowsAbsolutePath(cwd);
90+
const key = (path: string) => {
91+
const normalized = path.replaceAll("\\", "/");
92+
return caseInsensitive ? normalized.toLowerCase() : normalized;
93+
};
94+
const pathsByKey = new Map(
95+
entries.filter((entry) => entry.kind === "file").map((entry) => [key(entry.path), entry.path]),
96+
);
97+
if (pathsByKey.has(key(literalPath))) return null;
98+
return pathsByKey.get(key(strippedPath)) ?? null;
99+
}

0 commit comments

Comments
 (0)