patch-package icon indicating copy to clipboard operation
patch-package copied to clipboard

feat: add preliminary support for Bun

Open Strengthless opened this issue 2 years ago • 10 comments
trafficstars

Closes #489.

Changelogs:

  • Added --use-bun flag, similar to the current --use-yarn flag.
  • Added parseBunLockfile.ts to convert bun.lockb into yarn v1 lockfiles.
  • Updated getPackageResolution.ts to handle bun conversions before parsing.
  • Updated detectPackageManager.ts to detect bun, bun workspaces and multiple lockfiles.
  • Updated README with setup instructions for Bun.

Caveats:

  • If user has multiple lockfiles, patch-package uses npm by default. However, in the rare case where only bun.lockb and yarn.lock files exist, we have to resort to using yarn. This behaviour has been documented in the CLI and README (see https://github.com/ds300/patch-package/pull/490/commits/0491b915e20e9e5305c336ae565098bef608c9b5).
  • ~~Currently awaiting confirmation on how Bun handles postinstalls, for writing up the README about bun setup. (Will bun remove execute postinstall scripts? If not, bun users should also install postinstall-postinstall just like yarn v1 users.)~~
    • Confirmed by https://github.com/ds300/patch-package/pull/490#issuecomment-1712882619.
    • Fixed by https://github.com/ds300/patch-package/pull/490/commits/7482db96c1a4e59c3ecace0409330e0c24b125a7.
  • Currently awaiting confirmation on how Bun workspaces work, for writing up the README about bun workspaces setup (Is it similar to yarn workspaces, where users need to "repeat the setup process for the child package" if they want to patch un-hoisted packages?)

Temporary workaround (while this PR is unmerged):

  • Citing https://github.com/oven-sh/bun/issues/2336#issuecomment-1712458657,

    Attempting to create a patch using bunx patch-package <package-name> fails.

    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 bun install --yarn it just converts bun.lockb into a yarn.lock. (or bun ./bun.lockb > ./yarn.lock)
    • make the necessary modifications inside node-modules/<package-name>
    • bunx patch-package <package-name>
    • rm yarn.lock
    • package.json -> scripts -> "postinstall": "bunx patch-package" https://github.com/ds300/patch-package

Strengthless avatar Sep 10 '23 16:09 Strengthless

I tried bun remove <package-name> and saw that it executes the postinstall script.

❯ bun remove zod
bun remove v1.0.0 (822a00c4)
patch-package 8.0.0
Applying patches...
@remix-run/[email protected] ✔
@remix-run/[email protected] ✔
 - zod

 1 packages removed [38.00ms]

OnurGvnc avatar Sep 10 '23 17:09 OnurGvnc

This PR is now ready to merge, with only two caveats as mentioned in https://github.com/ds300/patch-package/pull/490#issue-1889202083.

@ds300 Can you please review this PR? :)

Strengthless avatar Sep 10 '23 20:09 Strengthless

Looking forward to this :) Been using pnpm for a while and been missing patch-package - so having it work with bun will be yummy!

Hoping this will work in monorepos as well!

robertherber avatar Oct 16 '23 18:10 robertherber

any news on this?

lovlyx avatar Jan 06 '24 14:01 lovlyx

Wish I could use patch-package to patch patch-package with this patch 😂 I guess I just need to fork the repo.

rikur avatar Jan 12 '24 00:01 rikur

Until this is merged, would you consider publishing this fork on npm @Strengthless?

robertherber avatar Jan 31 '24 23:01 robertherber

LGTM!

erickreutz avatar Feb 07 '24 19:02 erickreutz

It's happening!

Using this method for now https://github.com/oven-sh/bun/issues/2336#issuecomment-1928032858

medv avatar Feb 09 '24 06:02 medv

@ds300 I understand you probably have lots going on - but having this merged would be a key enabler! 🙏🙂

robertherber avatar Mar 19 '24 08:03 robertherber

Until this is merged, would you consider publishing this fork on npm @Strengthless?

@robertherber I am trying to use this method right now and published it under @miblanchard/[email protected] until this is merged and published.

miblanchard avatar Mar 21 '24 00:03 miblanchard