Skip to content

Commit 88483a5

Browse files
fix(server): finish short reads before returning workspace text (#14989)
Co-authored-by: SAPHID <[email protected]> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
1 parent 123e5ba commit 88483a5

2 files changed

Lines changed: 86 additions & 14 deletions

File tree

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

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,23 @@ import { vi } from "vite-plus/test";
2323

2424
vi.mock("node:fs/promises", async (importOriginal) => {
2525
const actual = await importOriginal<typeof NodeFSP>();
26-
return { ...actual, realpath: vi.fn(actual.realpath) };
26+
return { ...actual, realpath: vi.fn(actual.realpath), open: vi.fn(actual.open) };
27+
});
28+
29+
const injectShortReads = Effect.fn("injectShortReads")(function* (target: string) {
30+
const native = yield* Effect.promise(() => vi.importActual<typeof NodeFSP>("node:fs/promises"));
31+
const open = vi.mocked(NodeFSP.open).mockImplementation(async (path, flags, mode) => {
32+
const handle = await native.open(path, flags, mode);
33+
if (path === target) {
34+
const read = handle.read.bind(handle);
35+
Object.defineProperty(handle, "read", {
36+
value: (buffer: Uint8Array, offset: number, length: number, position: number) =>
37+
read(buffer, offset, Math.min(length, 2), position),
38+
});
39+
}
40+
return handle;
41+
});
42+
yield* Effect.addFinalizer(() => Effect.sync(() => open.mockImplementation(native.open)));
2743
});
2844

2945
const UNKNOWN_WORKSPACE_PREFIX = "t3code-unknown-workspace-";
@@ -162,6 +178,57 @@ it.layer(layerTest, { excludeTestServices: true })("WorkspaceFileSystemLive", (i
162178
});
163179

164180
describe("readFile", () => {
181+
it.effect("reads UTF-8 files relative to the workspace root", () =>
182+
Effect.gen(function* () {
183+
const workspaceFileSystem = yield* WorkspaceFileSystem.WorkspaceFileSystem;
184+
const cwd = yield* makeTempDir;
185+
yield* writeTextFile(cwd, "src/index.ts", "export const answer = 42;\n");
186+
187+
const result = yield* workspaceFileSystem.readFile({
188+
cwd,
189+
relativePath: "src/index.ts",
190+
});
191+
192+
expect(result).toEqual({
193+
relativePath: "src/index.ts",
194+
contents: "export const answer = 42;\n",
195+
byteLength: 26,
196+
truncated: false,
197+
});
198+
}),
199+
);
200+
201+
it.effect("finishes short reads before decoding UTF-8 text", () =>
202+
Effect.gen(function* () {
203+
const files = yield* WorkspaceFileSystem.WorkspaceFileSystem;
204+
const fs = yield* FileSystem.FileSystem;
205+
const cwd = yield* makeTempDir;
206+
const contents = "A café 🐈\n";
207+
yield* writeTextFile(cwd, "short.txt", contents);
208+
const target = yield* fs.realPath(`${cwd}/short.txt`);
209+
yield* injectShortReads(target);
210+
expect(yield* files.readFile({ cwd, relativePath: "short.txt" })).toEqual({
211+
relativePath: "short.txt",
212+
contents,
213+
byteLength: Buffer.byteLength(contents),
214+
truncated: false,
215+
});
216+
}),
217+
);
218+
219+
it.effect("rejects binary bytes delivered after an initial short read", () =>
220+
Effect.gen(function* () {
221+
const files = yield* WorkspaceFileSystem.WorkspaceFileSystem;
222+
const fs = yield* FileSystem.FileSystem;
223+
const cwd = yield* makeTempDir;
224+
yield* writeTextFile(cwd, "short.txt", "ab\0cd");
225+
const target = yield* fs.realPath(`${cwd}/short.txt`);
226+
yield* injectShortReads(target);
227+
const error = yield* files.readFile({ cwd, relativePath: "short.txt" }).pipe(Effect.flip);
228+
expect(error).toBeInstanceOf(WorkspaceFileSystem.WorkspaceBinaryFileError);
229+
}),
230+
);
231+
165232
it.effect("reads host files outside the workspace root by absolute path", () =>
166233
Effect.gen(function* () {
167234
const workspaceFileSystem = yield* WorkspaceFileSystem.WorkspaceFileSystem;

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

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -405,19 +405,24 @@ export const make = Effect.gen(function* () {
405405

406406
const bytesToRead = Math.min(stat.size, PROJECT_READ_FILE_MAX_BYTES);
407407
const buffer = Buffer.alloc(bytesToRead);
408-
const { bytesRead } = yield* Effect.tryPromise({
409-
try: () => handle.read(buffer, 0, bytesToRead, 0),
410-
catch: (cause) =>
411-
new WorkspaceFileSystemOperationError({
412-
workspaceRoot: input.cwd,
413-
relativePath: input.relativePath,
414-
resolvedPath: realTargetPath,
415-
operationPath: realTargetPath,
416-
operation: "read",
417-
cause,
418-
}),
419-
});
420-
const fileBytes = buffer.subarray(0, bytesRead);
408+
let offset = 0;
409+
while (offset < bytesToRead) {
410+
const { bytesRead } = yield* Effect.tryPromise({
411+
try: () => handle.read(buffer, offset, bytesToRead - offset, offset),
412+
catch: (cause) =>
413+
new WorkspaceFileSystemOperationError({
414+
workspaceRoot: input.cwd,
415+
relativePath: input.relativePath,
416+
resolvedPath: realTargetPath,
417+
operationPath: realTargetPath,
418+
operation: "read",
419+
cause,
420+
}),
421+
});
422+
if (bytesRead <= 0) break;
423+
offset += bytesRead;
424+
}
425+
const fileBytes = buffer.subarray(0, offset);
421426
if (fileBytes.includes(0)) {
422427
return yield* new WorkspaceBinaryFileError({
423428
workspaceRoot: input.cwd,

0 commit comments

Comments
 (0)