ds300 / patch-package

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

Fails to create patch file for complex package version #278

Open davidhousedev opened 3 years ago

davidhousedev commented 3 years ago

Thanks for your work on this! I really hope we can start using it in our project. We are currently using a canary build of a package, gatsby-source-contentful and installing it using a non-standard version. However, when I try to create a patch, there is a failure finding associated data in the lock file.

$ yarn patch-package gatsby-source-contentful
yarn run v1.22.5
$ /Users/davidhouse/Code/sandbox/frontend/butcherbox-frontend/node_modules/.bin/patch-package gatsby-source-contentful
patch-package 6.2.2
• Creating temporary folder
Error: Can't find lockfile entry for gatsby-source-contentful
    at Object.getPackageResolution (/Users/davidhouse/Code/sandbox/frontend/butcherbox-frontend/node_modules/patch-package/dist/getPackageResolution.js:41:19)
    at Object.makePatch (/Users/davidhouse/Code/sandbox/frontend/butcherbox-frontend/node_modules/patch-package/dist/makePatch.js:49:66)
    at /Users/davidhouse/Code/sandbox/frontend/butcherbox-frontend/node_modules/patch-package/dist/index.js:48:25
    at Array.forEach (<anonymous>)
    at Object.<anonymous> (/Users/davidhouse/Code/sandbox/frontend/butcherbox-frontend/node_modules/patch-package/dist/index.js:47:22)
    at Module._compile (internal/modules/cjs/loader.js:1076:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1097:10)
    at Module.load (internal/modules/cjs/loader.js:941:32)
    at Function.Module._load (internal/modules/cjs/loader.js:782:14)
    at Module.require (internal/modules/cjs/loader.js:965:19)
/Users/davidhouse/Code/sandbox/frontend/butcherbox-frontend/node_modules/patch-package/dist/makePatch.js:183
        throw e;
        ^

Error: Can't find lockfile entry for gatsby-source-contentful
    at Object.getPackageResolution (/Users/davidhouse/Code/sandbox/frontend/butcherbox-frontend/node_modules/patch-package/dist/getPackageResolution.js:41:19)
    at Object.makePatch (/Users/davidhouse/Code/sandbox/frontend/butcherbox-frontend/node_modules/patch-package/dist/makePatch.js:49:66)
    at /Users/davidhouse/Code/sandbox/frontend/butcherbox-frontend/node_modules/patch-package/dist/index.js:48:25
    at Array.forEach (<anonymous>)
    at Object.<anonymous> (/Users/davidhouse/Code/sandbox/frontend/butcherbox-frontend/node_modules/patch-package/dist/index.js:47:22)
    at Module._compile (internal/modules/cjs/loader.js:1076:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1097:10)
    at Module.load (internal/modules/cjs/loader.js:941:32)
    at Function.Module._load (internal/modules/cjs/loader.js:782:14)
    at Module.require (internal/modules/cjs/loader.js:965:19)
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

It also fails if I try to supply the explicit version as it is shown in the lock file:

$ yarn patch-package 'gatsby-source-contentful@^2.3.55-next.21'
yarn run v1.22.5
$ /Users/davidhouse/Code/sandbox/frontend/butcherbox-frontend/node_modules/.bin/patch-package gatsby-source-contentful@^2.3.55-next.21
patch-package 6.2.2
No such package gatsby-source-contentful@^2.3.55-next.21

  File not found: /Users/davidhouse/Code/sandbox/frontend/butcherbox-frontend/node_modules/gatsby-source-contentful@^2.3.55-next.21/package.json
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

package.json

"gatsby-source-contentful": "^2.3.55-next.21",

yarn.lock

gatsby-source-contentful@^2.3.55-next.21:
  version "2.3.55-next.21"
  resolved "https://registry.yarnpkg.com/gatsby-source-contentful/-/gatsby-source-contentful-2.3.55-next.21.tgz#0e8f15f87061c094efcbd1f02dc6a87389c72b54"
  integrity sha512-tXWbaA7ByB5QbQs6if4LsnlN11H0F5VZn/oBFfWh1lI5Cb7U+GxfszxWZ/9Wp7U8jOAKaNdZq3BsNIXvXPCA1A==
  dependencies:
    "@babel/runtime" "^7.11.2"
    "@contentful/rich-text-react-renderer" "^14.1.1"
    "@contentful/rich-text-types" "^14.1.1"
    "@hapi/joi" "^15.1.1"
    axios "^0.20.0"
    base64-img "^1.0.4"
    bluebird "^3.7.2"
    chalk "^4.1.0"
    contentful "^7.14.7"
    fs-extra "^9.0.1"
    gatsby-core-utils "1.3.23"
    gatsby-plugin-utils "0.2.33"
    gatsby-source-filesystem "2.3.34"
    is-online "^8.5.0"
    json-stringify-safe "^5.0.1"
    lodash "^4.17.20"
    node-fetch "^2.6.1"
    progress "^2.0.3"
    qs "^6.9.4"
davidhousedev commented 3 years ago

Ah! Never mind. It looks like the canary build is no longer necessary as it's been merged into 4.0.0 of the package I'm using. I should be able to use this package without an issue now, so no rush on my end. But I'll leave this open in case this use case is applicable to others.

bigorangemachine commented 3 years ago

For any time-travellers :D

I think whats happening is that it checks the package.json not the package-lock.json (or yarn.lock?) for the dependancies. So if its a nested dependancies you need to install the module directly into your package.json

So my package was foo but it had a patch in @foo/variant-react-native (which also had node_modules/foo and node_modules/@foo.

So I just did a npm install --save-dev @foo/variant-react-native

OFC I didn't want to install everything so I just targeted the directory. Sadly this overwrites your changes so be sure to keep the file open in your IDE and undo the change or don't update the file when you focus on your IDE again.

Tagging @ds300 -> patch-package should do some sniffing the lock file? Or note in the readme they may need to directly install the package or JSON surgery their package.json?

jthn commented 3 years ago

Actually, I think there may be an issue when installing with yarn tags.

I installed a package @aws-amplify/auth@unstable.

When it is written to the yarn.lock file, yarn (v1.22.5) is adding a git hash to the end of the version. So, my package.json is reads:

    "@aws-amplify/auth": "^3.4.18-unstable.10",

whereas my yarn.lock reads:

"@aws-amplify/auth@3.4.18-unstable.10+74dc3b12f"

Not sure if this is an across the board scenario, but in my case, I had to change the comparison in getPackageResolution to be:

k.startsWith(packageDetails.name + "@") &&
        installedVersion.startsWith(v.version),

...in other news, it appears that it isn't picking up my changes. I made them in a lib-esm directory inside of the package.

fracture91 commented 3 years ago

I got this same "Can't find lockfile entry" error in a slightly different situation. patch-package 6.2.2

I've installed a package directly from a github tarball - here's what it looks like in package.json:

"mdx-prism": "https://github.com/doamatto/mdx-prism/archive/v0.3.2.tar.gz",

That package has an entry in yarn.lock like this:

"mdx-prism@https://github.com/doamatto/mdx-prism/archive/v0.3.2.tar.gz":
  version "0.3.1"
  resolved "https://github.com/doamatto/mdx-prism/archive/v0.3.2.tar.gz#cd4d6240dbd67031f3f8c56304d3dd034e447f25"
  dependencies:
    ...

Note the version "0.3.1" there - the project's package.json file says the version is 0.3.1+fork.0.1.0: https://github.com/doamatto/mdx-prism/blob/22ca7f91cc3e86096d0ff5bf20cb511ad00b028c/package.json#L3 That mismatch might be a yarn bug? Not sure

Anyway, running yarn run patch-package mdx-prism gives me the same error message as in the OP. I manually changed the version in yarn.lock to "0.3.1+fork.0.1.0" and the error went away, yay! It successfully generates a patch file called mdx-prism+0.3.1+fork.0.1.0.patch.

Unfortunately, I get a version mismatch warning when applying the patch, but it's good enough for me since everything still works in CI:

Warning: patch-package detected a patch file version mismatch

  Don't worry! This is probably fine. The patch was still applied
  successfully. Here's the deets:

  Patch file created for

    mdx-prism@0.3.1+fork.0.1.0

  applied to

    mdx-prism@0.3.1

  At path...

TLDR: make sure the version in yarn.lock matches what's in the dependency's package.json and maybe you'll have better luck with patching

MarufSharifi commented 3 years ago

same problem with patch package when i want to patch changes to react-native-google-places-autocomplete

viewerw commented 2 years ago

try remove node_modules, then npm install