Editing a file located in the vendor directory is generally considered a bad practice. However, in Magento 2, this situation happens more often than we would like. In production, a blocking bug, an unmaintained third-party dependency or an inconsistent behavior can require an immediate fix, long before an official patch is released.
In this context, the right approach is not to edit the vendor directly. Instead, using Composer patches allows you to apply fixes in a clean, reproducible and maintainable way. In this article, we will see how to easily generate vendor patches without writing diffs by hand, while staying aligned with Magento best practices.
First step: install the required dependencies
To manage and generate patches efficiently, two Composer packages are required.
composer require cweagans/composer-patches
composer require symplify/vendor-patches --dev
On the one hand, cweagans/composer-patches allows Composer to automatically apply patches during composer install and composer update commands.
On the other hand, symplify/vendor-patches provides a dedicated command to generate patch files from local modifications.
By combining these two tools, you avoid manual diff creation and significantly reduce the risk of errors.
Modify the vendor file in a controlled way
Once the dependencies are installed, you can safely work on the target file.
Start by navigating to the vendor directory and identifying the file that needs to be fixed. Before making any changes, duplicate this file and rename the copy with a .old extension.
Example:
trucbidule.php becomes trucbidule.php.old
Then, edit the original trucbidule.php file and apply your changes. At this stage, immediately test your fix locally. This step ensures that the modification actually solves the initial problem.
The goal here is intentionally simple: work directly on the executed code, without worrying about the patch itself yet.
Generate the patch automatically
Once your changes are validated, generating the patch only requires a single command.
From the project root, run:
vendor/bin/vendor-patches generate
The tool automatically compares the modified file with its .old version. It then generates the corresponding diff and creates a patch file inside the patches directory.
As a result, you get a ready-to-use patch without manual diff writing or risky copy-paste operations.
Verify patch application
Before considering the fix complete, you must verify that the patch is correctly applied.
To do so, remove the vendor directory or at least the affected dependency. Then run:
composer install
If everything is correctly configured, Composer automatically applies the patch and the fix is present again. At this point, you have a versioned, reproducible fix that integrates perfectly with your deployment process.
Finally, make sure to commit and push the patches directory to your Git repository. Without this step, the fix will be lost on other environments.
Patching a vendor file in Magento 2 is not a bad practice in itself, as long as it is done properly. By combining composer-patches and vendor-patches, you implement a reliable, maintainable solution that fits real-world production constraints.
This approach allows you to fix critical issues quickly while keeping your Magento project stable, clean and aligned with professional development standards.
