ds300 / patch-package

Fix broken node modules instantly 🏃🏽‍♀️💨
MIT License
10.36k stars 290 forks source link

Support for Bun #489

Open Strengthless opened 1 year ago

Strengthless commented 1 year ago

As mentioned in https://github.com/oven-sh/bun/issues/2336#issuecomment-1712458657,

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?

OnurGvnc commented 1 year ago

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 commented 1 year ago

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"
baphony commented 5 months ago

Still doesn't work with Bun 1.1.3

nobkd commented 5 months ago

@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: https://github.com/oven-sh/bun/issues/2336#issuecomment-2028402422

baphony commented 5 months ago

Thank you for your help @nobkd

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