From 0d9e5f397ea493d2921531e2d7231bf15264e1c3 Mon Sep 17 00:00:00 2001 From: suatsulun Date: Wed, 9 Sep 2026 12:04:08 +0300 Subject: [PATCH 1/3] fix(cli): stage downloads on the destination filesystem --- packages/cli/src/lib/installations.ts | 3 +-- packages/cli/test/installations.test.ts | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) create mode 100644 packages/cli/test/installations.test.ts diff --git a/packages/cli/src/lib/installations.ts b/packages/cli/src/lib/installations.ts index 19abe049..1388ef31 100644 --- a/packages/cli/src/lib/installations.ts +++ b/packages/cli/src/lib/installations.ts @@ -1,6 +1,5 @@ import { createWriteStream } from 'node:fs' import { chmod, cp, mkdir, readFile, rename, rm, symlink, writeFile } from 'node:fs/promises' -import { tmpdir } from 'node:os' import { basename, dirname, extname, join } from 'node:path' import { Readable } from 'node:stream' import { pipeline } from 'node:stream/promises' @@ -216,7 +215,7 @@ export async function downloadArtifact(url: string, destinationDir: string): Pro if (!response.ok || !response.body) throw new Error(`Download returned ${response.status} ${response.statusText}`) const filename = filenameFromResponse(response) ?? (basename(new URL(response.url).pathname) || `beeper-download-${Date.now()}`) const finalPath = join(destinationDir, filename) - const tmpPath = join(tmpdir(), `${filename}.${process.pid}.${Date.now()}.tmp`) + const tmpPath = join(destinationDir, `${filename}.${process.pid}.${Date.now()}.tmp`) await writeResponseToFile(response, tmpPath) await rename(tmpPath, finalPath) return finalPath diff --git a/packages/cli/test/installations.test.ts b/packages/cli/test/installations.test.ts new file mode 100644 index 00000000..59f44f6e --- /dev/null +++ b/packages/cli/test/installations.test.ts @@ -0,0 +1,25 @@ +import { expect, it } from 'bun:test' +import { mkdtemp, readdir, readFile, rm } from 'node:fs/promises' +import { join } from 'node:path' +import { fileURLToPath } from 'node:url' +import { downloadArtifact } from '../src/lib/installations.js' + +it('installs a complete download on the destination filesystem', async () => { + // The checkout can be on a different filesystem from the system temp directory. + const destination = await mkdtemp(join(fileURLToPath(new URL('..', import.meta.url)), '.download-test-')) + const server = Bun.serve({ + hostname: '127.0.0.1', + port: 0, + fetch: () => new Response('server artifact'), + }) + try { + const artifact = await downloadArtifact(new URL('/beeper-server.tar.gz', server.url).href, destination) + + expect(artifact).toBe(join(destination, 'beeper-server.tar.gz')) + expect(await readFile(artifact, 'utf8')).toBe('server artifact') + expect(await readdir(destination)).toEqual(['beeper-server.tar.gz']) + } finally { + server.stop(true) + await rm(destination, { recursive: true, force: true }) + } +}) From 2e9de0129fb65e09fe7eb21dcd207c322e6ed7c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?batuhan=20i=C3=A7=C3=B6z?= Date: Fri, 25 Sep 2026 15:39:08 +0000 Subject: [PATCH 2/3] test(cli): prove downloads skip the system temp directory Poison TMPDIR instead of relying on the checkout and /tmp being on different filesystems, so the regression test fails on a single-filesystem CI runner too, and stop writing test directories into the package checkout. --- packages/cli/test/installations.test.ts | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/packages/cli/test/installations.test.ts b/packages/cli/test/installations.test.ts index 59f44f6e..71b38187 100644 --- a/packages/cli/test/installations.test.ts +++ b/packages/cli/test/installations.test.ts @@ -1,12 +1,14 @@ import { expect, it } from 'bun:test' import { mkdtemp, readdir, readFile, rm } from 'node:fs/promises' +import { tmpdir } from 'node:os' import { join } from 'node:path' -import { fileURLToPath } from 'node:url' import { downloadArtifact } from '../src/lib/installations.js' -it('installs a complete download on the destination filesystem', async () => { - // The checkout can be on a different filesystem from the system temp directory. - const destination = await mkdtemp(join(fileURLToPath(new URL('..', import.meta.url)), '.download-test-')) +it('installs a complete download without staging it in the system temp directory', async () => { + const destination = await mkdtemp(join(tmpdir(), 'beeper-download-')) + const originalTmpdir = process.env.TMPDIR + // An unusable temp directory fails the same way a temp directory on another filesystem does. + process.env.TMPDIR = join(destination, 'unavailable') const server = Bun.serve({ hostname: '127.0.0.1', port: 0, @@ -19,7 +21,10 @@ it('installs a complete download on the destination filesystem', async () => { expect(await readFile(artifact, 'utf8')).toBe('server artifact') expect(await readdir(destination)).toEqual(['beeper-server.tar.gz']) } finally { + if (originalTmpdir === undefined) delete process.env.TMPDIR + else process.env.TMPDIR = originalTmpdir server.stop(true) await rm(destination, { recursive: true, force: true }) } }) + From 85f5090806e45679223e9ef45169bbb34da487a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?batuhan=20i=C3=A7=C3=B6z?= Date: Fri, 25 Sep 2026 15:39:08 +0000 Subject: [PATCH 3/3] fix(cli): remove partial downloads from the install directory Now that downloads stage next to the final artifact, a failed or interrupted download would leave a .tmp file in the install directory. --- packages/cli/src/lib/installations.ts | 9 +++++++-- packages/cli/test/installations.test.ts | 22 ++++++++++++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/packages/cli/src/lib/installations.ts b/packages/cli/src/lib/installations.ts index 1388ef31..1f42d5a3 100644 --- a/packages/cli/src/lib/installations.ts +++ b/packages/cli/src/lib/installations.ts @@ -216,8 +216,13 @@ export async function downloadArtifact(url: string, destinationDir: string): Pro const filename = filenameFromResponse(response) ?? (basename(new URL(response.url).pathname) || `beeper-download-${Date.now()}`) const finalPath = join(destinationDir, filename) const tmpPath = join(destinationDir, `${filename}.${process.pid}.${Date.now()}.tmp`) - await writeResponseToFile(response, tmpPath) - await rename(tmpPath, finalPath) + try { + await writeResponseToFile(response, tmpPath) + await rename(tmpPath, finalPath) + } catch (error) { + await rm(tmpPath, { force: true }) + throw error + } return finalPath } diff --git a/packages/cli/test/installations.test.ts b/packages/cli/test/installations.test.ts index 71b38187..bb7787cd 100644 --- a/packages/cli/test/installations.test.ts +++ b/packages/cli/test/installations.test.ts @@ -1,5 +1,6 @@ import { expect, it } from 'bun:test' import { mkdtemp, readdir, readFile, rm } from 'node:fs/promises' +import { createServer, type Socket } from 'node:net' import { tmpdir } from 'node:os' import { join } from 'node:path' import { downloadArtifact } from '../src/lib/installations.js' @@ -28,3 +29,24 @@ it('installs a complete download without staging it in the system temp directory } }) +it('removes the partial file when a download is interrupted', async () => { + const destination = await mkdtemp(join(tmpdir(), 'beeper-download-')) + let connection: Socket | undefined + const server = createServer(socket => { + connection = socket + socket.write('HTTP/1.1 200 OK\r\nContent-Length: 1000\r\n\r\npartial') + }) + await new Promise(resolve => server.listen(0, '127.0.0.1', resolve)) + const { port } = server.address() as { port: number } + try { + const download = downloadArtifact(`http://127.0.0.1:${port}/beeper-server.tar.gz`, destination) + while ((await readdir(destination)).length === 0) await Bun.sleep(5) + connection?.destroy() + + await expect(download).rejects.toThrow() + expect(await readdir(destination)).toEqual([]) + } finally { + server.close() + await rm(destination, { recursive: true, force: true }) + } +})