Skip to content

Commit c7e5f96

Browse files
committed
fix(server): preserve POSIX filenames in native search results
1 parent 182851b commit c7e5f96

2 files changed

Lines changed: 45 additions & 15 deletions

File tree

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

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import type {
2020
ProjectSearchEntriesResult,
2121
} from "@t3tools/contracts";
2222
import { isWorkspaceImagePreviewPath } from "@t3tools/shared/filePreview";
23+
import { HostProcessPlatform } from "@t3tools/shared/hostProcess";
2324

2425
import * as WorkspaceSearchIndexService from "./WorkspaceSearchIndexService.ts";
2526

@@ -35,8 +36,8 @@ const CONTENT_SEARCH_TIME_BUDGET_MS = 250;
3536
const CONTENT_SEARCH_MAX_MATCHES_PER_FILE = 100;
3637
const CONTENT_SEARCH_MAX_CANDIDATES = 25_000;
3738

38-
function toPosixPath(input: string): string {
39-
return input.replaceAll("\\", "/");
39+
function toPosixPath(input: string, platform: NodeJS.Platform): string {
40+
return platform === "win32" ? input.replaceAll("\\", "/") : input;
4041
}
4142

4243
function trimDirectorySeparator(input: string): string {
@@ -48,8 +49,8 @@ function parentPathOf(input: string): string | undefined {
4849
return separatorIndex === -1 ? undefined : input.slice(0, separatorIndex);
4950
}
5051

51-
function toProjectEntry(item: MixedItem): ProjectEntry | null {
52-
const normalizedPath = trimDirectorySeparator(toPosixPath(item.item.relativePath));
52+
function toProjectEntry(item: MixedItem, platform: NodeJS.Platform): ProjectEntry | null {
53+
const normalizedPath = trimDirectorySeparator(toPosixPath(item.item.relativePath, platform));
5354
if (!normalizedPath) {
5455
return null;
5556
}
@@ -60,23 +61,24 @@ function toProjectEntry(item: MixedItem): ProjectEntry | null {
6061
};
6162
}
6263

63-
function toFileEntry(item: FileItem): ProjectEntry | null {
64-
const normalizedPath = trimDirectorySeparator(toPosixPath(item.relativePath));
64+
function toFileEntry(item: FileItem, platform: NodeJS.Platform): ProjectEntry | null {
65+
const normalizedPath = trimDirectorySeparator(toPosixPath(item.relativePath, platform));
6566
return normalizedPath ? { path: normalizedPath, kind: "file" } : null;
6667
}
6768

68-
function toDirectoryEntry(item: DirItem): ProjectEntry | null {
69-
const normalizedPath = trimDirectorySeparator(toPosixPath(item.relativePath));
69+
function toDirectoryEntry(item: DirItem, platform: NodeJS.Platform): ProjectEntry | null {
70+
const normalizedPath = trimDirectorySeparator(toPosixPath(item.relativePath, platform));
7071
return normalizedPath ? { path: normalizedPath, kind: "directory" } : null;
7172
}
7273

7374
function mapFileSearchResult(
7475
result: SearchResult,
7576
limit: number,
77+
platform: NodeJS.Platform,
7678
imageOnly = false,
7779
): ProjectSearchEntriesResult {
7880
const entries = result.items.flatMap((item) => {
79-
const entry = toFileEntry(item);
81+
const entry = toFileEntry(item, platform);
8082
return entry && (!imageOnly || isWorkspaceImagePreviewPath(entry.path)) ? [entry] : [];
8183
});
8284
return {
@@ -88,9 +90,10 @@ function mapFileSearchResult(
8890
function mapDirectorySearchResult(
8991
result: DirSearchResult,
9092
limit: number,
93+
platform: NodeJS.Platform,
9194
): ProjectSearchEntriesResult {
9295
const entries = result.items.flatMap((item) => {
93-
const entry = toDirectoryEntry(item);
96+
const entry = toDirectoryEntry(item, platform);
9497
return entry ? [entry] : [];
9598
});
9699
const rootDirectoryCount = result.items.some((item) => item.relativePath.length === 0) ? 1 : 0;
@@ -103,10 +106,11 @@ function mapDirectorySearchResult(
103106
function mapMixedSearchResult(
104107
result: MixedSearchResult,
105108
limit: number,
109+
platform: NodeJS.Platform,
106110
): { readonly entries: ProjectEntry[]; readonly truncated: boolean } {
107111
const entries: ProjectEntry[] = [];
108112
for (const item of result.items) {
109-
const entry = toProjectEntry(item);
113+
const entry = toProjectEntry(item, platform);
110114
if (entry) {
111115
entries.push(entry);
112116
}
@@ -267,6 +271,7 @@ export const make = Effect.fn("WorkspaceSearchIndex.make")(function* (
267271
cwd: string,
268272
variant: WorkspaceSearchIndexService.WorkspaceSearchIndexVariant = "paths",
269273
) {
274+
const platform = yield* HostProcessPlatform;
270275
const finder = yield* Effect.acquireRelease(createFinder(cwd, variant), (finder) =>
271276
Effect.try({
272277
try: () => finder.destroy(),
@@ -356,6 +361,7 @@ export const make = Effect.fn("WorkspaceSearchIndex.make")(function* (
356361
const mapped = mapMixedSearchResult(
357362
result,
358363
WorkspaceSearchIndexService.WORKSPACE_INDEX_MAX_ENTRIES,
364+
platform,
359365
);
360366
const sortedEntries = withDirectoryAncestors(mapped.entries).toSorted((left, right) =>
361367
left.path.localeCompare(right.path),
@@ -377,18 +383,18 @@ export const make = Effect.fn("WorkspaceSearchIndex.make")(function* (
377383
const result = yield* runSearch(query, pageSize, "fileSearch", () =>
378384
finder.fileSearch(query, { pageSize }),
379385
);
380-
return mapFileSearchResult(result, limit, imageOnly);
386+
return mapFileSearchResult(result, limit, platform, imageOnly);
381387
}
382388
if (kind === "directory") {
383389
const result = yield* runSearch(query, pageSize, "directorySearch", () =>
384390
finder.directorySearch(query, { pageSize }),
385391
);
386-
return mapDirectorySearchResult(result, limit);
392+
return mapDirectorySearchResult(result, limit, platform);
387393
}
388394
const result = yield* runSearch(query, pageSize, "mixedSearch", () =>
389395
finder.mixedSearch(query, { pageSize }),
390396
);
391-
return mapMixedSearchResult(result, limit);
397+
return mapMixedSearchResult(result, limit, platform);
392398
});
393399

394400
const searchContents: WorkspaceSearchIndexService.WorkspaceSearchIndex["Service"]["searchContents"] =
@@ -429,7 +435,7 @@ export const make = Effect.fn("WorkspaceSearchIndex.make")(function* (
429435
);
430436
if (matchRanges.length === 0) continue;
431437
pageMatches.push({
432-
path: toPosixPath(match.relativePath),
438+
path: toPosixPath(match.relativePath, platform),
433439
lineNumber: match.lineNumber,
434440
lineContent: match.lineContent,
435441
matchRanges,

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,30 @@ it.layer(TestLayer, { excludeTestServices: true })("WorkspaceEntries", (it) => {
193193
60_000,
194194
);
195195

196+
it.effect.skipIf(HostProcessPlatform.defaultValue() === "win32")(
197+
"preserves literal backslashes in POSIX search results",
198+
() =>
199+
Effect.gen(function* () {
200+
const cwd = yield* makeTempDir();
201+
const relativePath = "src/foo\\bar.ts";
202+
yield* writeTextFile(cwd, relativePath, "synthetic content\n");
203+
const workspaceEntries = yield* WorkspaceEntries.WorkspaceEntries;
204+
expect(
205+
(yield* workspaceEntries.search({ cwd, query: "foo", limit: 10 })).entries,
206+
).toContainEqual({ path: relativePath, kind: "file" });
207+
expect(
208+
(yield* workspaceEntries.searchContents({
209+
cwd,
210+
query: "synthetic",
211+
limit: 10,
212+
caseSensitive: true,
213+
wholeWord: false,
214+
useRegex: false,
215+
})).matches,
216+
).toContainEqual(expect.objectContaining({ path: relativePath }));
217+
}),
218+
);
219+
196220
it.effect("returns the complete cached workspace index", () =>
197221
Effect.gen(function* () {
198222
const cwd = yield* makeTempDir();

0 commit comments

Comments
 (0)