TypeScript
fix(server): finish short reads before returning workspace text (#14989)
Alex Southwell88483a5cf056
diff --git a/apps/server/src/workspace/WorkspaceFileSystem.test.ts b/apps/server/src/workspace/WorkspaceFileSystem.test.tsindex 0f252d600a5..786d55dbe05 100644--- a/apps/server/src/workspace/WorkspaceFileSystem.test.ts+++ b/apps/server/src/workspace/WorkspaceFileSystem.test.ts@@ -23,7 +23,23 @@ import { vi } from "vite-plus/test";vi.mock("node:fs/promises", async (importOriginal) => {const actual = await importOriginal<typeof NodeFSP>();- return { ...actual, realpath: vi.fn(actual.realpath) };+ return { ...actual, realpath: vi.fn(actual.realpath), open: vi.fn(actual.open) };+});++const injectShortReads = Effect.fn("injectShortReads")(function* (target: string) {+ const native = yield* Effect.promise(() => vi.importActual<typeof NodeFSP>("node:fs/promises"));+ const open = vi.mocked(NodeFSP.open).mockImplementation(async (path, flags, mode) => {+ const handle = await native.open(path, flags, mode);+ if (path === target) {+ const read = handle.read.bind(handle);+ Object.defineProperty(handle, "read", {+ value: (buffer: Uint8Array, offset: number, length: number, position: number) =>+ read(buffer, offset, Math.min(length, 2), position),+ });+ }+ return handle;+ });+ yield* Effect.addFinalizer(() => Effect.sync(() => open.mockImplementation(native.open)));});const UNKNOWN_WORKSPACE_PREFIX = "t3code-unknown-workspace-";@@ -162,6 +178,57 @@ it.layer(layerTest, { excludeTestServices: true })("WorkspaceFileSystemLive", (i});describe("readFile", () => {+ it.effect("reads UTF-8 files relative to the workspace root", () =>+ Effect.gen(function* () {+ const workspaceFileSystem = yield* WorkspaceFileSystem.WorkspaceFileSystem;+ const cwd = yield* makeTempDir;+ yield* writeTextFile(cwd, "src/index.ts", "export const answer = 42;\n");++ const result = yield* workspaceFileSystem.readFile({+ cwd,+ relativePath: "src/index.ts",+ });++ expect(result).toEqual({+ relativePath: "src/index.ts",+ contents: "export const answer = 42;\n",+ byteLength: 26,+ truncated: false,+ });+ }),+ );++ it.effect("finishes short reads before decoding UTF-8 text", () =>+ Effect.gen(function* () {+ const files = yield* WorkspaceFileSystem.WorkspaceFileSystem;+ const fs = yield* FileSystem.FileSystem;+ const cwd = yield* makeTempDir;+ const contents = "A café 🐈\n";+ yield* writeTextFile(cwd, "short.txt", contents);+ const target = yield* fs.realPath(`${cwd}/short.txt`);+ yield* injectShortReads(target);+ expect(yield* files.readFile({ cwd, relativePath: "short.txt" })).toEqual({+ relativePath: "short.txt",+ contents,+ byteLength: Buffer.byteLength(contents),+ truncated: false,+ });+ }),+ );++ it.effect("rejects binary bytes delivered after an initial short read", () =>+ Effect.gen(function* () {+ const files = yield* WorkspaceFileSystem.WorkspaceFileSystem;+ const fs = yield* FileSystem.FileSystem;+ const cwd = yield* makeTempDir;+ yield* writeTextFile(cwd, "short.txt", "ab\0cd");+ const target = yield* fs.realPath(`${cwd}/short.txt`);+ yield* injectShortReads(target);+ const error = yield* files.readFile({ cwd, relativePath: "short.txt" }).pipe(Effect.flip);+ expect(error).toBeInstanceOf(WorkspaceFileSystem.WorkspaceBinaryFileError);+ }),+ );+it.effect("reads host files outside the workspace root by absolute path", () =>Effect.gen(function* () {const workspaceFileSystem = yield* WorkspaceFileSystem.WorkspaceFileSystem;diff --git a/apps/server/src/workspace/WorkspaceFileSystem.ts b/apps/server/src/workspace/WorkspaceFileSystem.tsindex e611d2bd71a..489fedc2dce 100644--- a/apps/server/src/workspace/WorkspaceFileSystem.ts+++ b/apps/server/src/workspace/WorkspaceFileSystem.ts@@ -405,19 +405,24 @@ export const make = Effect.gen(function* () {const bytesToRead = Math.min(stat.size, PROJECT_READ_FILE_MAX_BYTES);const buffer = Buffer.alloc(bytesToRead);- const { bytesRead } = yield* Effect.tryPromise({- try: () => handle.read(buffer, 0, bytesToRead, 0),- catch: (cause) =>- new WorkspaceFileSystemOperationError({- workspaceRoot: input.cwd,- relativePath: input.relativePath,- resolvedPath: realTargetPath,- operationPath: realTargetPath,- operation: "read",- cause,- }),- });- const fileBytes = buffer.subarray(0, bytesRead);+ let offset = 0;+ while (offset < bytesToRead) {+ const { bytesRead } = yield* Effect.tryPromise({+ try: () => handle.read(buffer, offset, bytesToRead - offset, offset),+ catch: (cause) =>+ new WorkspaceFileSystemOperationError({+ workspaceRoot: input.cwd,+ relativePath: input.relativePath,+ resolvedPath: realTargetPath,+ operationPath: realTargetPath,+ operation: "read",+ cause,+ }),+ });+ if (bytesRead <= 0) break;+ offset += bytesRead;+ }+ const fileBytes = buffer.subarray(0, offset);if (fileBytes.includes(0)) {return yield* new WorkspaceBinaryFileError({workspaceRoot: input.cwd,