From 7c7275cfdb8af3f5fb8fe7d14e6925c4843f2396 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Sun, 14 Sep 2025 11:40:43 -0500 Subject: [PATCH] Allow reading files from any directory on the VFS --- src/lib/commands/cat.test.ts | 34 ++++++++++++++++++++---- src/lib/commands/cat.ts | 14 ++++++---- src/lib/tools/TextEditorViewTool.test.ts | 24 +++++++++++++++++ src/lib/tools/TextEditorViewTool.ts | 16 ++++++----- 4 files changed, 72 insertions(+), 16 deletions(-) diff --git a/src/lib/commands/cat.test.ts b/src/lib/commands/cat.test.ts index 69b80b1f..5e37015b 100644 --- a/src/lib/commands/cat.test.ts +++ b/src/lib/commands/cat.test.ts @@ -35,7 +35,7 @@ describe('CatCommand', () => { it('should return error when no arguments provided', async () => { const result = await catCommand.execute([], testCwd); - + expect(result.exitCode).toBe(1); expect(result.stderr).toContain('missing file operand'); expect(result.stdout).toBe(''); @@ -96,12 +96,36 @@ describe('CatCommand', () => { expect(result.stdout).toBe(''); }); - it('should reject absolute paths', async () => { + it('should handle absolute paths', async () => { + const fileContent = 'Absolute path content'; + vi.mocked(mockFS.stat).mockResolvedValue({ + isDirectory: () => false, + isFile: () => true, + }); + vi.mocked(mockFS.readFile).mockResolvedValue(fileContent); + const result = await catCommand.execute(['/absolute/path.txt'], testCwd); - expect(result.exitCode).toBe(1); - expect(result.stderr).toContain('absolute paths are not supported'); - expect(result.stdout).toBe(''); + expect(result.exitCode).toBe(0); + expect(result.stdout).toBe(fileContent); + expect(result.stderr).toBe(''); + expect(mockFS.readFile).toHaveBeenCalledWith('/absolute/path.txt', 'utf8'); + }); + + it('should handle Windows-style absolute paths', async () => { + const fileContent = 'Windows path content'; + vi.mocked(mockFS.stat).mockResolvedValue({ + isDirectory: () => false, + isFile: () => true, + }); + vi.mocked(mockFS.readFile).mockResolvedValue(fileContent); + + const result = await catCommand.execute(['C:\\temp\\file.txt'], testCwd); + + expect(result.exitCode).toBe(0); + expect(result.stdout).toBe(fileContent); + expect(result.stderr).toBe(''); + expect(mockFS.readFile).toHaveBeenCalledWith('C:\\temp\\file.txt', 'utf8'); }); it('should handle permission errors', async () => { diff --git a/src/lib/commands/cat.ts b/src/lib/commands/cat.ts index eb409072..c16d17a6 100644 --- a/src/lib/commands/cat.ts +++ b/src/lib/commands/cat.ts @@ -34,12 +34,16 @@ export class CatCommand implements ShellCommand { for (const filePath of args) { try { - // Handle absolute paths - if (filePath.startsWith('/') || filePath.startsWith('\\') || /^[A-Za-z]:[\\/]/.test(filePath)) { - return createErrorResult(`${this.name}: absolute paths are not supported: ${filePath}`); - } + // Handle both absolute and relative paths + let absolutePath: string; - const absolutePath = join(cwd, filePath); + if (filePath.startsWith('/') || filePath.startsWith('\\') || /^[A-Za-z]:[\\/]/.test(filePath)) { + // It's an absolute path - use it directly + absolutePath = filePath; + } else { + // It's a relative path - resolve it relative to the current working directory + absolutePath = join(cwd, filePath); + } // Check if path exists and is a file const stats = await this.fs.stat(absolutePath); diff --git a/src/lib/tools/TextEditorViewTool.test.ts b/src/lib/tools/TextEditorViewTool.test.ts index 2d9175e2..a4aff297 100644 --- a/src/lib/tools/TextEditorViewTool.test.ts +++ b/src/lib/tools/TextEditorViewTool.test.ts @@ -13,10 +13,12 @@ class MockFS implements JSRuntimeFS { this.dirs.add('/project/.git'); this.dirs.add('/project/src'); this.dirs.add('/project/node_modules'); + this.dirs.add('/tmp'); this.files.set('/project/package.json', '{"name": "test"}'); this.files.set('/project/src/index.ts', 'console.log("hello");'); this.files.set('/project/.gitignore', 'node_modules\n.git\n'); + this.files.set('/tmp/uploaded-file.txt', 'This is an uploaded file'); } async readFile(path: string): Promise; @@ -173,4 +175,26 @@ describe('TextEditorViewTool', () => { expect(result).toBe('console.log("hello");'); }); + + it('should handle absolute paths', async () => { + const result = await tool.execute({ path: '/tmp/uploaded-file.txt' }); + + expect(result).toBe('This is an uploaded file'); + }); + + it('should handle absolute directory paths', async () => { + const result = await tool.execute({ path: '/tmp' }); + + expect(result).toContain('uploaded-file.txt'); + }); + + it('should handle absolute paths with line filtering', async () => { + const result = await tool.execute({ + path: '/tmp/uploaded-file.txt', + start_line: 1, + end_line: 1 + }); + + expect(result).toBe('This is an uploaded file'); + }); }); \ No newline at end of file diff --git a/src/lib/tools/TextEditorViewTool.ts b/src/lib/tools/TextEditorViewTool.ts index 1e80a43d..f690bbf6 100644 --- a/src/lib/tools/TextEditorViewTool.ts +++ b/src/lib/tools/TextEditorViewTool.ts @@ -19,7 +19,7 @@ export class TextEditorViewTool implements Tool { readonly inputSchema = z.object({ path: z.string().describe( - 'Path of the file or directory to view, eg "src/index.ts" or "src/"', + 'Path of the file or directory to view, eg "src/index.ts" or "src/" for relative paths, or "/tmp/file.txt" for absolute paths', ), start_line: z.number().min(1).optional().describe( "Starting line number (1-indexed, inclusive). If provided, only show lines from this line onwards.", @@ -38,12 +38,16 @@ export class TextEditorViewTool implements Tool { const { path, start_line, end_line } = args; try { - // Check for absolute paths and provide helpful error - if (path.startsWith('/') || path.startsWith('\\') || /^[A-Za-z]:[\\/]/.test(path)) { - throw new Error(`āŒ Absolute paths are not supported.\n\nThe path "${path}" appears to be an absolute path.\n\nšŸ’” Please use a relative path instead. Examples:\n- "src/index.ts" (relative to current directory)\n- "./src/index.ts" (explicit relative path)\n- "../other-project/src/index.ts" (relative to parent directory)\n\nCurrent working directory: ${this.cwd}`); - } + // Handle both absolute and relative paths + let absolutePath: string; - const absolutePath = join(this.cwd, path); + if (path.startsWith('/') || path.startsWith('\\') || /^[A-Za-z]:[\\/]/.test(path)) { + // It's an absolute path - use it directly + absolutePath = path; + } else { + // It's a relative path - resolve it relative to the current working directory + absolutePath = join(this.cwd, path); + } // Check if the path is a directory const stats = await this.fs.stat(absolutePath);