ds300 / patch-package

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

vercel deployment #326

Open RichardJohnn opened 3 years ago

RichardJohnn commented 3 years ago

Is there a trick to using this with vercel like there is for heroku? I always see this in the vercel build log:

19:59:19.800 | Applying patches...
-- | --
19:59:19.803 | **ERROR** Failed to apply patch for package rich-markdown-editor at path
19:59:19.804 |  
19:59:19.804 | node_modules/rich-markdown-editor
19:59:19.804 | This error was caused because patch-package cannot apply the following patch file:
19:59:19.804 | patches/rich-markdown-editor+11.10.0.patch
19:59:19.804 | Try removing node_modules and trying again. If that doesn't work, maybe there was
19:59:19.804 | an accidental change made to the patch file? Try recreating it by manually
19:59:19.804 | editing the appropriate files and running:
19:59:19.804 |  
19:59:19.804 | patch-package rich-markdown-editor
19:59:19.804 |  
19:59:19.804 | If that doesn't work, then it's a bug in patch-package, so please submit a bug
19:59:19.804 | report. Thanks!
19:59:19.804 | https://github.com/ds300/patch-package/issues
19:59:19.804 |  
19:59:19.805 | ---
19:59:19.805 | patch-package finished with 1 error(s).

while locally it works great. thank you

henriquesaias commented 3 years ago

I had some patches already and there was no problem but after applying one today I'm experiencing the same issue:

I can yarn build locally and it will compile successfully but when I run vercel --prod I get


--
14:35:15.633 | [1/4] Resolving packages...
14:35:16.055 | success Already up-to-date.
14:35:16.057 | $ patch-package
14:35:16.205 | patch-package 6.4.7
14:35:16.205 | Applying patches...
14:35:16.209 | @types/react-places-autocomplete@7.2.6 ✔
14:35:16.211 | **ERROR** Failed to apply patch for package react-dropzone-uploader at path
14:35:16.211 |  
14:35:16.211 | node_modules/react-dropzone-uploader
14:35:16.211 | This error was caused because patch-package cannot apply the following patch file:
14:35:16.212 | patches/react-dropzone-uploader+2.11.0.patch
14:35:16.212 | Try removing node_modules and trying again. If that doesn't work, maybe there was
14:35:16.212 | an accidental change made to the patch file? Try recreating it by manually
14:35:16.212 | editing the appropriate files and running:
14:35:16.212 |  
14:35:16.212 | patch-package react-dropzone-uploader
14:35:16.212 |  
14:35:16.212 | If that doesn't work, then it's a bug in patch-package, so please submit a bug
14:35:16.212 | report. Thanks!
14:35:16.213 | https://github.com/ds300/patch-package/issues
14:35:16.213 |  
14:35:16.213 | ---
14:35:16.213 | patch-package finished with 1 error(s).
14:35:16.226 | error Command failed with exit code 1.
14:35:16.226 | info Visit https://yarnpkg.com/en/docs/cli/install for documentation about this command.
14:35:16.250 | Error: Command "yarn install" exited with 1```

Please help!
mononoke-choi commented 2 years ago

i've been using patched package without any problem on vercel until today. after modifying additional lines i'm facing same issue. it can only be built on local successfully in my package.json i added this config to use patch-package on vercel "config": { "unsafe-perm": true }, and then appended "--unsafe-perm" flag to vercel install command

12:59:45.025 | patch-package 6.4.7 12:59:45.026 | Applying patches... 12:59:45.032 | ERROR Failed to apply patch for package baseui at path 12:59:45.032 |   12:59:45.032 | node_modules/baseui 12:59:45.032 | This error was caused because patch-package cannot apply the following patch file: 12:59:45.033 | patches/baseui+9.116.3.patch 12:59:45.033 | Try removing node_modules and trying again. If that doesn't work, maybe there was 12:59:45.033 | an accidental change made to the patch file? Try recreating it by manually 12:59:45.033 | editing the appropriate files and running: 12:59:45.033 |   12:59:45.033 | patch-package baseui 12:59:45.033 |   12:59:45.033 | If that doesn't work, then it's a bug in patch-package, so please submit a bug 12:59:45.033 | report. Thanks! 12:59:45.034 | https://github.com/ds300/patch-package/issues 12:59:45.034 |   12:59:45.034 | --- 12:59:45.034 | patch-package finished with 1 error(s).

i wanna your advice thank you :)

suyoin commented 2 years ago

Same issue is happening to me. It seems to be happening when multiple files' diffs are in the same patch file.

reck753 commented 2 years ago

Hi, I also got an issue with vercel deployment, but a little different scenario than this issue.

I have got a monorepo setup, with one of the packages being a Next application. The issue is that, this specific package, doesn't pick up the patches from the root of the monorepo, nor does the postinstall script in the root package.json which does the package patching. If I would add the postinstall script to the specific package, I would get the error from patch-package that, the libraries I am trying to patch, are not present in the node_modules. This is true, the libraries are not present in the scoped node_modules of that package, because they get hoisted to the root node_modules of monorepo. I got a hacky solution, that I would rather not use, which involves nohoisting libraries that I am patching. This could lead to few issues, that is why I am not willing to use it.

Does anyone have some advice? Thanks in advance.

zacharyliu commented 2 years ago

I also ran into this issue on Vercel with a patch that touched multiple source files.

If you just need to patch two files, a hacky workaround is to split the patch file into two files. patch-package allows using either + or : to separate the package name from the version: https://github.com/ds300/patch-package/blob/5c2c92bf504885fba4840870a23fc8999c00e572/src/PackageDetails.ts#L51

So you can create two patch files, each affecting a single file, and both will be applied to the same package:

Might be worth a feature request to support multiple patch files, since it might be useful in general.

Benhawkins18 commented 2 years ago

I have found out through testing that vercel is not clearing out the nodes_module. You can verify this by making a change via patch package, deploying the change, then when you try to revert it still has all the old changes applied to the now removed patch. This is why once you've created a patch you cannot seem to change it.

JoiGud commented 1 year ago

Having almost the same issue:


Applying patches...
--
13:43:04.926 |  
13:43:04.926 | **ERROR** Failed to apply patch for package canvas-scroll-clip at path
13:43:04.926 |  
13:43:04.926 | node_modules/canvas-scroll-clip
13:43:04.926 |  
13:43:04.926 | This error was caused because patch-package cannot apply the following patch file:
13:43:04.927 |  
13:43:04.927 | patches/canvas-scroll-clip+1.3.1.patch
13:43:04.927 |  
13:43:04.927 | Try removing node_modules and trying again. If that doesn't work, maybe there was
13:43:04.927 | an accidental change made to the patch file? Try recreating it by manually
13:43:04.927 | editing the appropriate files and running:
13:43:04.927 |  
13:43:04.927 | patch-package canvas-scroll-clip
13:43:04.927 |  
13:43:04.927 | If that doesn't work, then it's a bug in patch-package, so please submit a bug
13:43:04.927 | report. Thanks!
13:43:04.927 |  
13:43:04.927 | https://github.com/ds300/patch-package/issues
13:43:04.927 |  
13:43:04.927 |  
13:43:04.928 | ---
13:43:04.928 | patch-package finished with 1 error(s).

I need to press redeploy every time 😓

Might opt to forking the npm package and installing the fork instead 🤷

ansonyuu commented 1 year ago

Ran into a similar issue! From this thread, we know that Vercel supports patch-package as of Jan 3rd, 2023. The error may be on the patch creation side. Here is the order of operations for what I would try.

  1. Make sure you're making patches for the right file. Although the package I was patching mentioned to make changes in dist, I actually had to do so in index.js
  2. Avoid having patches for multiple files in one patch file. If multiple are generated, delete one of them and test deploying. Be sure to remove any empty lines or spaces near the beginning of your .patch file.
  3. Manually redeploy on Vercel

Hope this helps!

BrianHung commented 1 year ago

Reversing then applying seemed to fix for me:

    "postinstall": "cd ../.. && npx patch-package --reverse && npx patch-package --error-on-fail"
luixo commented 1 year ago

This is still an issue. Pushing a patch change breaks the patching as it tries to patch already patched (with a previous patch version) file.

vvo commented 1 year ago

Hey there, just had this issue too, here's how I solved it on Vercel:

"postinstall": "rm -rf node_modules/package-name && npm i --ignore-scripts && npx patch-package",
bombillazo commented 11 months ago

Im guessing this is a cache issue since redeploying with no cache solves this for us...

bombillazo commented 11 months ago

Expanding on @vvo 's suggestion, I created a bash script to auto-delete any related deps without hardcoding the dependency names, useful also if you have multiple patched dependencies as we do:

# For each file in the format <dependency_name>+<version>.patch
for PATCH_FILE in ./patches/*+*.patch; do
    # Check if file exists to avoid issues with the wildcard in case no files match
    if [[ -f "$PATCH_FILE" ]]; then
        # Extract dependency name
        DEP_NAME=$(basename "$PATCH_FILE" | cut -d'+' -f1)

        # Delete the dependency from node_modules
        if [[ -d "node_modules/$DEP_NAME" ]]; then
            echo "Deleting node_modules/$DEP_NAME ..."
            rm -rf "node_modules/$DEP_NAME"
        else
            echo "$DEP_NAME not found in node_modules!"
        fi
    fi
done

In our package.json:

 "postinstall": ". ./scripts/vercel-clear-patches.sh && npm i --ignore-scripts && npx patch-package"

Tested and works!

cheapsteak commented 2 months ago

@bombillazo Thanks so much that bash script works!

😭 build times doubled though but I guess that's to be expected 😆

if I understand correctly, the problem occurs when attempting to run a patch a dep that already had a different patch applied to it from a prior run

so clearing vercel's cache when updating an existing patch would be an alternative solution here