Skip to content

Support for Bun #489

Open
Open
@Strengthless

Description

@Strengthless

As mentioned in oven-sh/bun#2336 (comment),

Attempting to create a patch using bunx patch-package fails. The patch-package outputs the following error:

**ERROR** No package-lock.json, npm-shrinkwrap.json, or yarn.lock file.

You must use either npm@>=5, yarn, or npm-shrinkwrap to manage this project's
dependencies.

However, if you have pre-existing patches in the ./patches directory, you can successfully apply them using the
bunx patch-package command (or via package.json -> scripts -> "postinstall": "patch-package")

As a temporary workaround to generate a patches/package+name+version.patch file

Run npm install to generate a package-lock.json file (temporarily, we'll remove it later)
Make the necessary modifications inside node-modules/
Execute npx patch-package to create the patches/package+name+version.patch file
Run rm package-lock.json && bun install

Bun has just released their 1.0 version, and its popularity seems to be growing rapidly. Will there ever be support for Bun?

Activity

OnurGvnc

OnurGvnc commented on Sep 9, 2023

@OnurGvnc

After a quick look, I see the getPackageResolution() function needs to handle bun.lockb.

The bun.lockb file is a binary file, and I did some research on how to read it.

I looked into how the bun-vscode extension handles displaying the content of bun.lockb. You can see their approach here:
https://github.com/oven-sh/bun/blob/ffe4f561a3af53b9f5a41c182de55d7199b5d692/packages/bun-vscode/src/features/lockfile.ts#L39

When running the command bun ./bun.lockb, it outputs the binary file's content as a string.

I think the output looks like yarn v1, but I'm not sure.

import { spawn } from 'node:child_process'
import lockfile from '@yarnpkg/lockfile'

const { parse: parseYarnLockFile } = lockfile

//
;(async () => {
  const lockFileString = await readLockfile('./bun.lockb')
  console.log(lockFileString)
  const parsedYarnLockFile = parseYarnLockFile(lockFileString)
  console.log(parsedYarnLockFile.object)
})()

/**
 * @param {string} lockFilePath
 * @returns {Promise<string>}
 */
function readLockfile(lockFilePath) {
  return new Promise((resolve, reject) => {
    const process = spawn('bun', [lockFilePath], {
      stdio: ['ignore', 'pipe', 'pipe'],
    })
    let stdout = ''
    process.stdout.on('data', (/** @type {Buffer} */ data) => {
      stdout += data.toString()
    })
    let stderr = ''
    process.stderr.on('data', (/** @type {Buffer} */ data) => {
      stderr += data.toString()
    })
    process.on('error', (error) => {
      reject(error)
    })
    process.on('exit', (code) => {
      if (code === 0) {
        resolve(stdout)
      } else {
        reject(new Error(`Bun exited with code: ${code}\n${stderr}`))
      }
    })
  })
}
  ...
  'wrangler@^3.7.0': [Object: null prototype] {
    version: '3.7.0',
    resolved: 'https://registry.npmjs.org/wrangler/-/wrangler-3.7.0.tgz',
    integrity: 'sha512-7823G5U7WwDIkqaZrxSh/BQ/pxA4WIX3R9GwYfh+MYJj+k5s56KGQ+K/NmY/JbgZsxVEHDjhoYzqDqJebQMZeg==',
    dependencies: [Object: null prototype] {
      '@cloudflare/kv-asset-handler': '^0.2.0',
      '@esbuild-plugins/node-globals-polyfill': '^0.2.3',
      '@esbuild-plugins/node-modules-polyfill': '^0.2.2',
      'blake3-wasm': '^2.1.5',
      chokidar: '^3.5.3',
      esbuild: '0.17.19',
      miniflare: '3.20230904.0',
      nanoid: '^3.3.3',
      'path-to-regexp': '^6.2.0',
      selfsigned: '^2.0.1',
      'source-map': '^0.7.4',
      'xxhash-wasm': '^1.0.1'
    },
    optionalDependencies: [Object: null prototype] { fsevents: '~2.3.2' }
  },
  'zod@^3.11.x': [Object: null prototype] {
    version: '3.22.2',
    resolved: 'https://registry.npmjs.org/zod/-/zod-3.22.2.tgz',
    integrity: 'sha512-wvWkphh5WQsJbVk1tbx1l1Ly4yg+XecD+Mq280uBGt9wa5BKSWf4Mhp6GmrkPixhMxmabYY7RbzlwVP32pbGCg=='
  },
  'zod@^3.19.1': [Object: null prototype] {
    version: '3.22.2',
    resolved: 'https://registry.npmjs.org/zod/-/zod-3.22.2.tgz',
    integrity: 'sha512-wvWkphh5WQsJbVk1tbx1l1Ly4yg+XecD+Mq280uBGt9wa5BKSWf4Mhp6GmrkPixhMxmabYY7RbzlwVP32pbGCg=='
  },
  'zod@^3.20.2': [Object: null prototype] {
    version: '3.22.2',
    resolved: 'https://registry.npmjs.org/zod/-/zod-3.22.2.tgz',
    integrity: 'sha512-wvWkphh5WQsJbVk1tbx1l1Ly4yg+XecD+Mq280uBGt9wa5BKSWf4Mhp6GmrkPixhMxmabYY7RbzlwVP32pbGCg=='
  },
  'zod-form-data@^2.0.1': [Object: null prototype] {
    version: '2.0.1',
    resolved: 'https://registry.npmjs.org/zod-form-data/-/zod-form-data-2.0.1.tgz',
    integrity: 'sha512-4jcsj3vFyFGINLLHEmehfOPKdcw+HqV65RwsV2IdyLHp9wpvGJRVXWg1yY8sq0ASEbQfTVBRtI7LcDGv3Qpj8g=='
  }
}
Strengthless

Strengthless commented on Sep 10, 2023

@Strengthless
Author

I think the output looks like yarn v1, but I'm not sure.

I believe so. It says yarn lockfile v1 in the output header.

strengthless@Kams-Air test-repository % bun ./bun.lockb
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1
# bun ./bun.lockb --hash: 4D7D58732D6A801C-feca8c93e240e793-C2755E71EE387323-f6946a04e0df29e1


"@0no-co/graphql.web@^1.0.1":
  version "1.0.4"
  resolved "https://registry.npmjs.org/@0no-co/graphql.web/-/graphql.web-1.0.4.tgz"
  integrity sha512-W3ezhHGfO0MS1PtGloaTpg0PbaT8aZSmmaerL7idtU5F7oCI+uu25k+MsMS31BVFlp4aMkHSrNRxiD72IlK8TA==

"@ampproject/remapping@^2.2.0":
  version "2.2.1"
  resolved "https://registry.npmjs.org/@ampproject/remapping/-/remapping-2.2.1.tgz"
  integrity sha512-lFMjJTrFL3j7L9yBxwYfCq2k6qqwHyzuUl/XBnif78PWTJYyL/dfowQHWE3sp6U6ZzqWiiIZnpTMO96zhkjwtg==
  dependencies:
    "@jridgewell/gen-mapping" "^0.3.0"
    "@jridgewell/trace-mapping" "^0.3.9"
linked a pull request that will close this issue on Sep 10, 2023
baphony

baphony commented on Apr 12, 2024

@baphony

Still doesn't work with Bun 1.1.3

nobkd

nobkd commented on Apr 12, 2024

@nobkd

@baphony
You can run bun install --yarn or add

[install.lockfile]
print = "yarn"

to your bunfig.toml to avoid this issue.
This adds a yarn.lock additionally to your bun.lockb on install.
This is just a workaround, but prevents this issue from happening.

Reference: oven-sh/bun#2336 (comment)

baphony

baphony commented on Apr 12, 2024

@baphony

Thank you for your help @nobkd

That didn't work, but removing the yarn.lock generated by yarn did work :)

added a commit that references this issue on Nov 2, 2024
nathanchere

nathanchere commented on Jan 28, 2025

@nathanchere

FYI As of bun 1.2, the default lock file format has switched from binary to text-based.

abumalick

abumalick commented on Jan 29, 2025

@abumalick

Bun has it’s own patching mechanism now
https://bun.sh/docs/install/patch

jasons42

jasons42 commented on Jan 29, 2025

@jasons42

Bun has it’s own patching mechanism now
https://bun.sh/docs/install/patch

My experience with bun's patching mechanism is that it is so buggy as to be unusable currently. There are many open issues with it. I would definitely still be interested in seeing support from the patch-package side.

wottpal

wottpal commented on Mar 8, 2025

@wottpal

Same experience as @jasons42!

evelant

evelant commented on May 5, 2025

@evelant

Here's my branch with support for bun's newer text lockfile. I committed the built dist so it can be installed direct from github like "patch-package": "github:evelant/patch-package",. I'll clean it up and submit a PR when I can find time but this works for me for now https://github.com/evelant/patch-package/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Support for Bun Β· Issue #489 Β· ds300/patch-package