Skip to content

armgit / E2B

Public Imported from GitHub
0

Open-source, secure environment with real-world tools for enterprise-grade agents.

Fix empty file + add tests

Jakub Novakf2bec0544fb3
diff --git a/packages/js-sdk/src/sandbox/filesystem/index.ts b/packages/js-sdk/src/sandbox/filesystem/index.ts
index ccadaacce..df505d073 100644
--- a/packages/js-sdk/src/sandbox/filesystem/index.ts
+++ b/packages/js-sdk/src/sandbox/filesystem/index.ts
@@ -133,6 +133,11 @@ export class Filesystem {
return new Uint8Array(res.data as ArrayBuffer)
}
+ // When the file is empty, res.data is parsed as `{}`. This is a workaround to return an empty string.
+ if (res.response.headers.get('content-length') === '0') {
+ return ''
+ }
+
return res.data
}
diff --git a/packages/js-sdk/tests/sandbox/files/read.test.ts b/packages/js-sdk/tests/sandbox/files/read.test.ts
index e9c26969c..7479e9df5 100644
--- a/packages/js-sdk/tests/sandbox/files/read.test.ts
+++ b/packages/js-sdk/tests/sandbox/files/read.test.ts
@@ -17,3 +17,11 @@ sandboxTest('read non-existing file', async ({ sandbox }) => {
await expect(sandbox.files.read(filename)).rejects.toThrowError(NotFoundError)
})
+
+sandboxTest('empty file', async ({ sandbox }) => {
+ const filename = 'empty-file.txt'
+
+ await sandbox.commands.run(`touch ${filename}`)
+ const content = await sandbox.files.read(filename)
+ expect(content).toBe('')
+})
diff --git a/packages/python-sdk/tests/async/sandbox_async/files/test_read.py b/packages/python-sdk/tests/async/sandbox_async/files/test_read.py
index 83cce7894..5aec6b80d 100644
--- a/packages/python-sdk/tests/async/sandbox_async/files/test_read.py
+++ b/packages/python-sdk/tests/async/sandbox_async/files/test_read.py
@@ -17,3 +17,12 @@ async def test_read_non_existing_file(async_sandbox: AsyncSandbox):
with pytest.raises(NotFoundException):
await async_sandbox.files.read(filename)
+
+
+async def test_read_empty_file(async_sandbox: AsyncSandbox):
+ filename = "empty_file.txt"
+ content = ""
+
+ await async_sandbox.commands.run(f"touch {filename}")
+ read_content = await async_sandbox.files.read(filename)
+ assert read_content == content
diff --git a/packages/python-sdk/tests/sync/sandbox_sync/files/test_read.py b/packages/python-sdk/tests/sync/sandbox_sync/files/test_read.py
index e010ecbf0..d889e806d 100644
--- a/packages/python-sdk/tests/sync/sandbox_sync/files/test_read.py
+++ b/packages/python-sdk/tests/sync/sandbox_sync/files/test_read.py
@@ -16,3 +16,12 @@ def test_read_non_existing_file(sandbox):
with pytest.raises(NotFoundException):
sandbox.files.read(filename)
+
+
+def test_read_empty_file(sandbox):
+ filename = "empty_file.txt"
+ content = ""
+
+ sandbox.commands.run(f"touch {filename}")
+ read_content = sandbox.files.read(filename)
+ assert read_content == content