diff --git a/apps/server/src/workspace/WorkspaceFileSystem.test.ts b/apps/server/src/workspace/WorkspaceFileSystem.test.ts index c58dd10311..5c1b694ea0 100644 --- a/apps/server/src/workspace/WorkspaceFileSystem.test.ts +++ b/apps/server/src/workspace/WorkspaceFileSystem.test.ts @@ -137,8 +137,14 @@ it.layer(TestLayer, { excludeTestServices: true })("WorkspaceFileSystemLive", (i error.message, `Workspace file operation 'workspaceFileSystem.readFile' failed for 'image.bin' in '${cwd}'.`, ); - assert.ok(error.cause instanceof Error); - assert.strictEqual((error.cause as Error).message, "Binary files cannot be previewed as text."); + assert.strictEqual( + (error.cause as { readonly _tag?: string })._tag, + "WorkspaceReadFileBinaryFileError", + ); + assert.strictEqual( + (error.cause as { readonly message?: string }).message, + "Binary files cannot be previewed as text.", + ); }), ); @@ -163,9 +169,12 @@ it.layer(TestLayer, { excludeTestServices: true })("WorkspaceFileSystemLive", (i error.message, `Workspace file operation 'workspaceFileSystem.readFile' failed for 'linked-secret.txt' in '${cwd}'.`, ); - assert.ok(error.cause instanceof Error); assert.strictEqual( - (error.cause as Error).message, + (error.cause as { readonly _tag?: string })._tag, + "WorkspaceReadFileResolvedOutsideRootError", + ); + assert.strictEqual( + (error.cause as { readonly message?: string }).message, "Workspace file path resolves outside the project root.", ); }), diff --git a/apps/server/src/workspace/WorkspaceFileSystem.ts b/apps/server/src/workspace/WorkspaceFileSystem.ts index 6f30d9f282..5af54b2a9d 100644 --- a/apps/server/src/workspace/WorkspaceFileSystem.ts +++ b/apps/server/src/workspace/WorkspaceFileSystem.ts @@ -24,6 +24,46 @@ import * as WorkspacePaths from "./WorkspacePaths.ts"; const PROJECT_READ_FILE_MAX_BYTES = 1024 * 1024; +class WorkspaceReadFileResolvedOutsideRootError extends Schema.TaggedErrorClass()( + "WorkspaceReadFileResolvedOutsideRootError", + { + cwd: Schema.String, + relativePath: Schema.String, + realWorkspaceRoot: Schema.String, + realTargetPath: Schema.String, + }, +) { + override get message(): string { + return "Workspace file path resolves outside the project root."; + } +} + +class WorkspaceReadFileNotFileError extends Schema.TaggedErrorClass()( + "WorkspaceReadFileNotFileError", + { + cwd: Schema.String, + relativePath: Schema.String, + fileType: Schema.String, + }, +) { + override get message(): string { + return "Workspace path is not a file."; + } +} + +class WorkspaceReadFileBinaryFileError extends Schema.TaggedErrorClass()( + "WorkspaceReadFileBinaryFileError", + { + cwd: Schema.String, + relativePath: Schema.String, + nulByteOffset: Schema.Number, + }, +) { + override get message(): string { + return "Binary files cannot be previewed as text."; + } +} + export class WorkspaceFileSystemError extends Schema.TaggedErrorClass()( "WorkspaceFileSystemError", { @@ -96,22 +136,40 @@ export const make = Effect.gen(function* () { path.isAbsolute(relativeRealPath) ) { return yield* Effect.fail( - new Error("Workspace file path resolves outside the project root."), + new WorkspaceReadFileResolvedOutsideRootError({ + cwd: input.cwd, + relativePath: target.relativePath, + realWorkspaceRoot, + realTargetPath, + }), ); } const handle = yield* fileSystem.open(realTargetPath, { flag: "r" }); const stat = yield* handle.stat; if (stat.type !== "File") { - return yield* Effect.fail(new Error("Workspace path is not a file.")); + return yield* Effect.fail( + new WorkspaceReadFileNotFileError({ + cwd: input.cwd, + relativePath: target.relativePath, + fileType: stat.type, + }), + ); } const byteLength = Number(stat.size); const bytesToRead = Math.min(byteLength, PROJECT_READ_FILE_MAX_BYTES); const buffer = new Uint8Array(bytesToRead); const bytesRead = yield* handle.read(buffer); const fileBytes = buffer.subarray(0, Number(bytesRead)); - if (fileBytes.includes(0)) { - return yield* Effect.fail(new Error("Binary files cannot be previewed as text.")); + const nulByteOffset = fileBytes.indexOf(0); + if (nulByteOffset !== -1) { + return yield* Effect.fail( + new WorkspaceReadFileBinaryFileError({ + cwd: input.cwd, + relativePath: target.relativePath, + nulByteOffset, + }), + ); } const contents = new TextDecoder("utf-8").decode(fileBytes); return {