composer-patches: Could not apply patch! Skipping. The error was: Cannot apply patch

For some reason I cannot use this plugin. I thought its a user rights thing so I gave 777 to the whole folder. Still it does not work.

The patch is ok. I just was able to include it by using phpstorm.

Thanks for help

$ composer update
    1/1:	http://packagist.org/p/provider-latest$40d29078e846e543e38d2771c7c700e88dfe049917735d752cc3fa12fa9f051d.json
    Finished: success: 1, skipped: 0, failure: 0, total: 1
Gathering patches for root package.
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 1 install, 0 updates, 0 removals
Gathering patches for root package.
Gathering patches for dependencies. This might take a minute.
  - Installing drupal/paragraphs (dev-1.x bedd55b): Cloning bedd55bbac from cache
  - Applying patches for drupal/paragraphs
    https://www.drupal.org/files/issues/embed_paragraph_content-2848878-43.patch (Add startup configuration for PHP server)
   Could not apply patch! Skipping. The error was: Cannot apply patch https://www.drupal.org/files/issues/embed_paragraph_content-2848878-43.patch
    https://www.drupal.org/files/issues/2868155-16.patch (Issue #2868155: new hooks for editing paragraphs subforms)

> Drupal\Core\Composer\Composer::vendorTestCodeCleanup
Writing lock file
Generating autoload files
> Drupal\Core\Composer\Composer::preAutoloadDump
> Drupal\Core\Composer\Composer::ensureHtaccess

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 3
  • Comments: 51 (14 by maintainers)

Commits related to this issue

Most upvoted comments

For those who are looking for exact reason why the patch cannot be applied, try running composer install -vvv which means debug level of verbosity. It should give you insight on what’s happening. In my case the error was only on the server and pretty stupid and self explanatory :

- Applying patches for ... ... patch '-p1' --no-backup-if-mismatch -d '...' < '...' Executing command (CWD): patch '-p1' --no-backup-if-mismatch -d '..' < '...' sh: patch: command not found

Some of the time problem maybe OS does not have patch module install. sudo yum install patch can solve this problem sometime.

I am thinking that we need a PR to output a non-verbose-mode error with a failed return code if the patch binary does not exist.

I had the problem in a GitHub Action. While the patch could without any problem be applied on the local system it was not working on in the GitHub Action. When installing with composer install -vvv the error message was sh: patch: not found. With the help of https://github.com/php-actions/composer/issues/98#issuecomment-1551934062 I realized that the problem was my own Docker Image: The apt package patch was missing.

Sometimes the patch might be missing in Linux Use the below command to install patch and try again sudo yum install patch

I’m not sure what the github standard is compared to a regular patch file, but I started comparing the diff that was working to the one from authorize.net and noticed that the github patch for the other hotfix used an “a/” prefix on the old and a “b/” prefix on the new files. I tried adding “a/” and “b/” in front of the respective lines in my modified authorize patch file and it appears to have worked.

For those who are looking for exact reason why the patch cannot be applied, try running composer install -vvv which means debug level of verbosity. It should give you insight on what’s happening. In my case the error was only on the server and pretty stupid and self explanatory :

- Applying patches for ... ... patch '-p1' --no-backup-if-mismatch -d '...' < '...' Executing command (CWD): patch '-p1' --no-backup-if-mismatch -d '..' < '...' sh: patch: command not found

This

Some of the time problem maybe OS does not have patch module install. sudo yum install patch can solve this problem sometime.

and this really saved me after a few hours of debugging why composer wasn’t installing certain files!

The patch needs to be relative to the root of the package that you’re trying to patch (lh-hsts). In your patch, basically just remove all instances of web/wp-content/plugins/lh-hsts/ and it should start working again.

Removing instances of web/wp-content/plugins/lh-hsts/ in the patch file still gives Could not apply patch! Skipping. The error was: Cannot apply patch lh-hsts-get-home-url.patch and Cannot apply patch Use get_home_url in lh-hsts plugin (lh-hsts-get-home-url.patch)!

My patch file is in the git root along with composer.json. The file I am trying to patch, in the lh-hsts package, is web/wp-content/plugins/lh-hsts/lh-hsts.php.

The patch file, with relative paths compared to the package installation at web/wp-content/plugins/lh-hsts/, is:

From 78502e76a598b65d67ae6ea6216f1319debc59e9 Mon Sep 17 00:00:00 2001
From: ataylorme <andrew@ataylor.me>
Date: Wed, 12 Sep 2018 16:16:35 -0700
Subject: [PATCH 1/1] Use get_home_url instead of get_site_url in lh-hsts plugin

---
 lh-hsts.php | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/lh-hsts.php b/lh-hsts.php
index 434da5e..10ae32e 100644
--- a/lh-hsts.php
+++ b/lh-hsts.php
@@ -43,7 +43,7 @@ if (!class_exists('LH_HSTS_Plugin')) {
 
 			$this->uri = $_SERVER['REQUEST_URI'];
 			$this->domain = $_SERVER['HTTP_HOST'];
-			$this->current_domain = get_site_url();
+			$this->current_domain = get_home_url();
 
 			add_action( 'send_headers', array($this, "add_header"));
 		}
-- 
2.15.2 (Apple Git-101.1)


The patch file no longer works with patch using paths relative to the package installation rather than the git root, where the patch file is.

@ucola You shouldn’t need to create a new repo for the files. You should commit files that aren’t managed by composer to the project repository. Changes to those files should be applied directly without composer-patches.

@fgm

They work on some machines and not others, with the same composer.json/composer.lock.

Very likely due to differing versions of patch and/or git. Thanks for the detailed information. I’ll look through it and see if there’s anything that stands out as a reason why things would be going sideways.

@ataylorme The patch needs to be relative to the root of the package that you’re trying to patch (lh-hsts). In your patch, basically just remove all instances of web/wp-content/plugins/lh-hsts/ and it should start working again.