diff --git a/gdf.js b/gdf.js index 8005ded..8624987 100755 --- a/gdf.js +++ b/gdf.js @@ -614,11 +614,17 @@ export class GDF { get bunVersion() { if (this.#packager !== 'bun') return + const pinned = this.#pj.packageManager?.match(/^bun@(\d+\.\d+\.\d+)(?:\+[^ ]+)?$/)?.[1] + if (pinned) return pinned + try { - return execSync('bun --version', { encoding: 'utf8' }) + const version = execSync('bun --version', { encoding: 'utf8' }) .match(/\d+\.\d+\.\d+/)?.[0] + if (version) return version } catch { } + + return '1' } // classic version of yarn (installed by default) diff --git a/test/gdf.test.js b/test/gdf.test.js index 381c478..3a816ba 100644 --- a/test/gdf.test.js +++ b/test/gdf.test.js @@ -338,6 +338,22 @@ describe('gdf: packagers', function() { expect(sandbox().read('Dockerfile')).to.include('ARG PNPM_VERSION=9.1.2') }) + it('generates a usable Bun image without a local Bun installation', async function() { + sandbox().write('bun.lock', '') + await sandbox().run({ packageManager: 'bun@1.2.20' }) + expect(sandbox().read('Dockerfile')).to.include('ARG BUN_VERSION=1.2.20\n') + + await sandbox().run({}) + expect(sandbox().read('Dockerfile')).to.include('ARG BUN_VERSION=1\n') + }) + + it('prefers the project Bun version over the installed version', async function() { + sandbox().stub('bun', 'echo 1.1.20\n') + sandbox().write('bun.lock', '') + await sandbox().run({ packageManager: 'bun@1.2.20' }) + expect(sandbox().read('Dockerfile')).to.include('ARG BUN_VERSION=1.2.20\n') + }) + for (const lockfile of ['bun.lockb', 'bun.lock']) { it(`uses bun with a ${lockfile}`, async function() { sandbox().stub('bun', 'echo 1.1.20\n')