Skip to content

Commit 1e20361

Browse files
fix(server): workspace search keeps backslashes in POSIX file names (#35672)
1 parent a98bc69 commit 1e20361

2 files changed

Lines changed: 47 additions & 16 deletions

File tree

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,31 @@ it.effect("fetches past a first page of Git-ignored results", () =>
113113
).pipe(Effect.provide(testLayer)),
114114
);
115115

116+
// A backslash is a file name character on Linux and macOS, so only Windows may treat it as a separator.
117+
it.effect.skipIf(HostProcess.Platform.defaultValue() === "win32")(
118+
"keeps a backslash in a POSIX folder name as part of the search result path",
119+
() =>
120+
Effect.scoped(
121+
Effect.gen(function* () {
122+
const items = [fileItem("dir\\one/report.txt")];
123+
const finder = {
124+
destroy: vi.fn(),
125+
waitForIndexReady: vi.fn(async () => ({ ok: true as const, value: true })),
126+
fileSearch: vi.fn(() => ({
127+
ok: true as const,
128+
value: { items, scores: [], totalMatched: 1, totalFiles: 1 },
129+
})),
130+
} as unknown as FileFinder;
131+
vi.spyOn(FileFinder, "create").mockReturnValueOnce({ ok: true, value: finder });
132+
133+
const searchIndex = yield* WorkspaceSearchIndex.make("/workspace/project");
134+
const result = yield* searchIndex.search("report", 50, "file");
135+
136+
expect(result.entries).toEqual([{ kind: "file", path: "dir\\one/report.txt" }]);
137+
}),
138+
),
139+
);
140+
116141
it.effect("continues whole-word searches after a filtered grep page", () =>
117142
Effect.scoped(
118143
Effect.gen(function* () {

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

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import type {
2222
ProjectSearchContentsResult,
2323
ProjectSearchEntriesResult,
2424
} from "@t3tools/contracts";
25+
import * as HostProcess from "@t3tools/shared/HostProcess";
2526
import { isProjectFaviconPath } from "@t3tools/shared/projectFavicon";
2627

2728
import * as VcsProcess from "../vcs/VcsProcess.ts";
@@ -120,8 +121,9 @@ export class WorkspaceSearchIndex extends Context.Service<
120121
}
121122
>()("t3/workspace/WorkspaceSearchIndex") {}
122123

123-
function toPosixPath(input: string): string {
124-
return input.replaceAll("\\", "/");
124+
// Only the host separator becomes `/`: on POSIX `\` is a file name character.
125+
function toPosixPath(input: string, hostIsWindows: boolean): string {
126+
return hostIsWindows ? input.replaceAll("\\", "/") : input;
125127
}
126128

127129
function trimDirectorySeparator(input: string): string {
@@ -133,8 +135,8 @@ function parentPathOf(input: string): string | undefined {
133135
return separatorIndex === -1 ? undefined : input.slice(0, separatorIndex);
134136
}
135137

136-
function toProjectEntry(item: MixedItem): ProjectEntry | null {
137-
const normalizedPath = trimDirectorySeparator(toPosixPath(item.item.relativePath));
138+
function toProjectEntry(item: MixedItem, hostIsWindows: boolean): ProjectEntry | null {
139+
const normalizedPath = trimDirectorySeparator(toPosixPath(item.item.relativePath, hostIsWindows));
138140
if (!normalizedPath) {
139141
return null;
140142
}
@@ -145,13 +147,13 @@ function toProjectEntry(item: MixedItem): ProjectEntry | null {
145147
};
146148
}
147149

148-
function toFileEntry(item: FileItem): ProjectEntry | null {
149-
const normalizedPath = trimDirectorySeparator(toPosixPath(item.relativePath));
150+
function toFileEntry(item: FileItem, hostIsWindows: boolean): ProjectEntry | null {
151+
const normalizedPath = trimDirectorySeparator(toPosixPath(item.relativePath, hostIsWindows));
150152
return normalizedPath ? { path: normalizedPath, kind: "file" } : null;
151153
}
152154

153-
function toDirectoryEntry(item: DirItem): ProjectEntry | null {
154-
const normalizedPath = trimDirectorySeparator(toPosixPath(item.relativePath));
155+
function toDirectoryEntry(item: DirItem, hostIsWindows: boolean): ProjectEntry | null {
156+
const normalizedPath = trimDirectorySeparator(toPosixPath(item.relativePath, hostIsWindows));
155157
return normalizedPath ? { path: normalizedPath, kind: "directory" } : null;
156158
}
157159

@@ -160,10 +162,11 @@ type SearchPage<I> = { readonly items: ReadonlyArray<I>; readonly totalMatched:
160162
function mapFileSearchResult(
161163
result: SearchPage<FileItem>,
162164
limit: number,
165+
hostIsWindows: boolean,
163166
imageOnly = false,
164167
): ProjectSearchEntriesResult {
165168
const entries = result.items.flatMap((item) => {
166-
const entry = toFileEntry(item);
169+
const entry = toFileEntry(item, hostIsWindows);
167170
return entry && (!imageOnly || isProjectFaviconPath(entry.path)) ? [entry] : [];
168171
});
169172
return {
@@ -175,9 +178,10 @@ function mapFileSearchResult(
175178
function mapDirectorySearchResult(
176179
result: SearchPage<DirItem>,
177180
limit: number,
181+
hostIsWindows: boolean,
178182
): ProjectSearchEntriesResult {
179183
const entries = result.items.flatMap((item) => {
180-
const entry = toDirectoryEntry(item);
184+
const entry = toDirectoryEntry(item, hostIsWindows);
181185
return entry ? [entry] : [];
182186
});
183187
const rootDirectoryCount = result.items.some((item) => item.relativePath.length === 0) ? 1 : 0;
@@ -190,10 +194,11 @@ function mapDirectorySearchResult(
190194
function mapMixedSearchResult(
191195
result: SearchPage<MixedItem>,
192196
limit: number,
197+
hostIsWindows: boolean,
193198
): { readonly entries: ProjectEntry[]; readonly truncated: boolean } {
194199
const entries: ProjectEntry[] = [];
195200
for (const item of result.items) {
196-
const entry = toProjectEntry(item);
201+
const entry = toProjectEntry(item, hostIsWindows);
197202
if (entry) {
198203
entries.push(entry);
199204
}
@@ -388,6 +393,7 @@ export const make = Effect.fn("WorkspaceSearchIndex.make")(function* (
388393
cwd: string,
389394
variant: WorkspaceSearchIndexVariant = "paths",
390395
) {
396+
const hostIsWindows = yield* HostProcess.isWindows;
391397
const finder = yield* Effect.acquireRelease(createFinder(cwd, variant), (finder) =>
392398
Effect.try({
393399
try: () => finder.destroy(),
@@ -519,7 +525,7 @@ export const make = Effect.fn("WorkspaceSearchIndex.make")(function* (
519525
(pageSize) => finder.mixedSearch("", { pageSize }),
520526
mixedItemPath,
521527
);
522-
const mapped = mapMixedSearchResult(result, WORKSPACE_INDEX_MAX_ENTRIES);
528+
const mapped = mapMixedSearchResult(result, WORKSPACE_INDEX_MAX_ENTRIES, hostIsWindows);
523529
const sortedEntries = withDirectoryAncestors(mapped.entries).toSorted((left, right) =>
524530
left.path.localeCompare(right.path),
525531
);
@@ -547,7 +553,7 @@ export const make = Effect.fn("WorkspaceSearchIndex.make")(function* (
547553
(size) => finder.fileSearch(query, { pageSize: size }),
548554
(item) => item.relativePath,
549555
);
550-
return mapFileSearchResult(result, limit, imageOnly);
556+
return mapFileSearchResult(result, limit, hostIsWindows, imageOnly);
551557
}
552558
if (kind === "directory") {
553559
const result = yield* searchVisible(
@@ -557,7 +563,7 @@ export const make = Effect.fn("WorkspaceSearchIndex.make")(function* (
557563
(size) => finder.directorySearch(query, { pageSize: size }),
558564
(item) => item.relativePath,
559565
);
560-
return mapDirectorySearchResult(result, limit);
566+
return mapDirectorySearchResult(result, limit, hostIsWindows);
561567
}
562568
const result = yield* searchVisible(
563569
query,
@@ -566,7 +572,7 @@ export const make = Effect.fn("WorkspaceSearchIndex.make")(function* (
566572
(size) => finder.mixedSearch(query, { pageSize: size }),
567573
mixedItemPath,
568574
);
569-
return mapMixedSearchResult(result, limit);
575+
return mapMixedSearchResult(result, limit, hostIsWindows);
570576
});
571577

572578
const search: WorkspaceSearchIndex["Service"]["search"] = Effect.fn(
@@ -626,7 +632,7 @@ export const make = Effect.fn("WorkspaceSearchIndex.make")(function* (
626632
// fff reports hits past its 512-char lineContent cap with no ranges, so whole words can't be verified there.
627633
if (input.wholeWord && matchRanges.length === 0) continue;
628634
matches.push({
629-
path: toPosixPath(match.relativePath),
635+
path: toPosixPath(match.relativePath, hostIsWindows),
630636
lineNumber: match.lineNumber,
631637
lineContent: match.lineContent,
632638
matchRanges,

0 commit comments

Comments
 (0)