Skip to content

Commit ed1ef79

Browse files
committed
Fix path resolution for absolute workspace paths
Updated the Editor's resolve method to correctly handle absolute paths that start with the workspace root, preventing duplication of the root in resolved paths. Added a test to verify correct behavior when creating files with absolute paths within the workspace.
1 parent cfad673 commit ed1ef79

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

packages/sandbox/src/openai/index.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -403,8 +403,16 @@ export class Editor implements OpenAIEeditor {
403403
}
404404

405405
private resolve(relativePath: string): string {
406+
// If the path already starts with the root, strip it to get the relative part
407+
let pathToProcess = relativePath;
408+
if (relativePath.startsWith(this.root)) {
409+
pathToProcess = relativePath.slice(this.root.length);
410+
// Remove leading slash if present after stripping root
411+
pathToProcess = pathToProcess.replace(/^\//, '');
412+
}
413+
406414
// Remove leading ./ or / if present, then join with root
407-
const normalized = relativePath.replace(/^\.\//, '').replace(/^\//, '');
415+
const normalized = pathToProcess.replace(/^\.\//, '').replace(/^\//, '');
408416
const resolved = normalized ? `${this.root}/${normalized}` : this.root;
409417

410418
// Normalize path separators first

packages/sandbox/tests/openai-shell-editor.test.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,5 +403,32 @@ describe('Editor', () => {
403403
);
404404
expect(mockSandbox.writeFile).not.toHaveBeenCalled();
405405
});
406+
407+
it('should handle absolute paths within workspace', async () => {
408+
applyDiffMock.mockReturnValueOnce('content');
409+
410+
const mockSandbox: MockSandbox = {
411+
mkdir: vi.fn().mockResolvedValue(undefined),
412+
writeFile: vi.fn().mockResolvedValue(undefined)
413+
};
414+
415+
const editor = new Editor(
416+
mockSandbox as unknown as Sandbox,
417+
'/workspace'
418+
);
419+
const operation = {
420+
type: 'create_file',
421+
path: '/workspace/file.txt',
422+
diff: 'content'
423+
} as Extract<ApplyPatchOperation, { type: 'create_file' }>;
424+
425+
await editor.createFile(operation);
426+
427+
expect(mockSandbox.writeFile).toHaveBeenCalledWith(
428+
'/workspace/file.txt', // Not /workspace/workspace/file.txt
429+
'content',
430+
{ encoding: 'utf-8' }
431+
);
432+
});
406433
});
407434
});

0 commit comments

Comments
 (0)