"How do I apply a patch with Composer?" is one of the most-viewed Drupal questions on Stack Exchange, and the plugin behind the usual answer, cweagans/composer-patches, changed its workflow in version 2. This site upgraded to version 2.0 in August, as one of the build-tool upgrades before Drupal 11. The site applies no patches at all, which made the upgrade itself trivial and makes it a good place to see what version 2 adds before you need it.
The upgrade
The change in composer.json was two constraints: composer/installers from ^1.9 to ^2.3, and cweagans/composer-patches from ^1.7 to ^2.0. Both are build tooling: they decide where packages are installed and whether patches are applied, and they never run on a live request. The test afterwards was correspondingly practical: modules and themes still landed in the right directories, Drupal's configuration status was clean, and the article-publishing pipeline still worked.
The new file: patches.lock.json
The visible difference after upgrading is a new file next to composer.json. On this site it looks like this:
{"_hash": "6142bfcb...","patches": []
The plugin's documentation describes patches.lock.json as the mechanism it now uses to keep a known-good list of patches to apply to the project: a hash, and an array of patch definitions. With no patches defined, the array is empty, and that's the whole file. This repository commits it next to composer.lock, for the same reason: it's the record of what a clean install should produce.
A leftover from version 1
The site's composer.json still has "enable-patching": true in its extra section. In version 1, that setting had a specific meaning: the README said a project that supplies no patches of its own needs it to accept patches defined by its dependencies. Version 2's configuration documentation doesn't list it among its settings, so on this site it's a harmless leftover. I mention it because it's the kind of line people copy from old answers into new projects without knowing what it did.
Adding the first patch, version 2 style
When this site does need a patch, the version 2 workflow is short. Patches are still defined in the extra.patches section of composer.json, or in a separate patches.json file, which is the plugin's default name for one. The familiar format from version 1 still works, an object of descriptions and URLs per package:
"extra": { "patches": { "drupal/some_module": {"Fix the thing (issue #1234)": "https://www.drupal.org/files/issues/....patch"} } }
The documentation also describes an expanded format, a list of objects per package, which can carry more than a description and a URL.
What's new is what you run after changing the list. The documentation names two commands for exactly that situation:
composer patches-relockre-discovers the patches and writes them topatches.lock.json.composer patches-repatchdeletes and reinstalls the patched dependencies, so the patches are applied again.
Then commit composer.json and patches.lock.json together. Anyone else running composer install gets the patches from the lock file, the same way they get package versions from composer.lock.
Why a lock file for patches at all
Patches are usually URLs, and the file at a URL can change or disappear. A committed lock file of resolved patch definitions makes the list of patches a recorded, reviewable state, instead of something each install works out again from the configuration. It's the same idea as composer.lock, applied to the one part of a Drupal build that version 1 left floating.
For a site with no patches, none of this does anything yet. The useful part of upgrading early is that when the first patch arrives, it arrives into the workflow the plugin documents now.
Leave a comment
Comments are reviewed before they appear.