Deployer unable to find composer installed recipes
| Q | A |
|---|---|
| Issue Type | Question |
| Deployer Version | 6.0.3 |
| Local Machine OS | WSL |
| Remote Machine OS | CentOS 7 |
Description
Deployer fails to find and require recipes installed via the method included in the third party recipe instructions
Steps to reproduce
Install recipes:
composer require deployer/recipes --dev
Add a recipe as per docs:
require 'recipe/cachetool.php';
Run deployment:
dep deploy production
OR
php deployer.phar deploy production
Result:
PHP Warning: require(recipe/cachetool.php): failed to open stream: No such file or directory in /mnt/d/web/deploy.php on line 3
PHP Fatal error: require(): Failed opening required 'recipe/cachetool.php' (include_path='phar:///mnt/d/web/deployer.phar/bin/../:.:/usr/share/php') in /mnt/d/web/blog.ib3.uk/deploy.php on line 3
Content of deploy.php
<?php
namespace Deployer;
require 'recipe/composer.php';
require 'recipe/cachetool.php';
// Project name
set('application', 'Example');
// Project repository
set('repository', '[email protected]:foo');
// Hosts
host('xxx')
->stage('production')
->user('deploy')
->set('deploy_path', '/var/www')
->set('cachetool', '127.0.0.1:9000');
// Shared files/dirs between deploys
add('shared_dirs', [
'web/app/upgrade',
'web/app/uploads',
'web/app/wfcache',
'web/app/wflogs'
]);
// Copy config
task('enable_prod_config', function() {
run('cp "{{release_path}}/.env.prod" "{{release_path}}/.env"');
})->onStage('production');
after('deploy:update_code', 'enable_prod_config');
// Clear opcache
after('deploy:symlink', 'cachetool:clear:opcache');
// [Optional] Allocate tty for git clone. Default value is false.
set('git_tty', true);
// [Optional] if deploy fails automatically unlock.
after('deploy:failed', 'deploy:unlock');
Worked around the issue thus:
ini_set('include_path', ini_get('include_path') . PATH_SEPARATOR . 'vendor/deployer/recipes');
Should that be part of the documtation or is something broken?
I think it's a bug
Can you find and fix it?
I can’t see how Deployer was designed to find the composer installed recipes. Presumably, it needs to know where the vendor directory is (read composer.json/assume default?) and to add the path to those recipes to the include path? Or the expectation on the user is to simply provide the full path to the third party recipes. Perhaps, if Deployer is run and installed using the local composer method, it would find the recipes?
It's using include path.
I am having this problem as well.
Perhaps recipes should be redesigned in how they are implemented? My initial thought would be to do something similar to how Symfony handles extension – simple abstract classes and interfaces.
Deployer already uses Symfony Console, so why not utilize the class autoloading of composer? It will make extension much easier I think.
@jordanbrauer what do u suggest?
At a minimum update the documentation to show that the include path needs to include the deployer recipes directory. As it stands all the examples and documents suggest it "just works" without, when it doesn't.
Hmm, maybe better to fix include path of all repos? :) I'll do it.
@antonmedv I would handle recipes the same way Symfony handles their single components and autoloading.
- Have it so that a user only needs to require
deployerphp/deployerfor the core functionality (common recipe) - Any additional recipes should be required as singletons (again, akin to Symfony single components), e.g.,
deployerphp/rsync- This allows users to select only the recipes they really need for their project
- To use a recipe, simply require/use the file/namespace in your deployer file (again, see usage of Symfony standalone components if you need an idea of what I mean)
Ideally, this would mean ditching the use of require "recipe/rsync.php"; in favour of use Deployer\Recipe\Rsync;. Since deployer seems to work off of global functions, using use will work fine for importing.
Would this still work if like me, you've installed the deployer.phar in to the project root and not via composer? That is the first installation method shown on the install page.
Good question – I am sure that there would be a way to make it work yes. As an example, when you add entries to the "scripts" key of a composer.json file, Composer will update it's set of commands, adding yours to it (try it by running composer list, and you will see your commands).
My point is, is that if a PHP CLI application is installed globally via a .phar rather than as a local composer binary, the application can still be extended for a local instance only.
I think we should keep it simple and do not reinvent the wheel. Use what already in php and composer.
I am not sure I understand @antonmedv? None of what I mentioned requires reinventing the wheel and uses default composer autoloading with native PHP imports (use). 😮
It's impossible to autoload tasks. In php and composer we can autoload only classes (not even functions, but you can use function ).
I think something like this will be good:
require 'vendor/autoload.php'; // Or move it to deployer and automatically detect
require 'common.php'
require 'slack.php'
All recipes have to provide autoload.php file with set_include_path to composer.json.
Ahh, you are right! My bad.
I think that user should be responsible for handling the inclusion of their autoload file. This way they are able to bootstrap however they please, even if they are not using composer.
From my experience doing require 'vendor/autoload.php'; from deploy.php level can make a conflicts with deployer packages.
The reason is that composer uses spl_autoload_register(..., true, true); The important is the second 'true' param in spl_autoload_register function. Its "prepend" and means the classes searching for this spl_autoload_register will be done before other registered ealier (by composer autoload from deployer in that case). That means that if deployer will want to create symfony console object then it will use class from root project vendors and not from itself vendors (inside phar). That of course can lead to problems if there are different version of symfony console for deployer phar and for root project.
The solution I know is to make light loader and register it with spl_autoload_register but without the third "prepend" param set to true. Then if there are the same libraries used in deployer and root project - deployer will use itself library first.
The example of such loader which uses information generated by composer itself is in this little library: https://github.com/sourcebroker/deployer-loader
I'd be glad to hear for better solution. So far its working for me well.
I think it will be cool to have possibility to include recipes like this without worried about autoload:
require 'common.php'
require 'slack.php'
Deployer already has autoload.php detection https://github.com/deployphp/deployer/blob/master/bin/dep#L52-L56
So it should work if proper include paths will be configured.
In case when deployer installed globally and locally, local version should be used (dep automatically will switch to local version). In this case recipes installed by composer will be used and this is correct.
So now, then using phar one think should be done is requiring projects autoload.php
As another solution: fork all used symfony components and rename namespace.
@antonmedv
How the "deployer already has autoload.php detection" should work then ?
I can not create object from my vendor class if I have no require 'vendor/autoload.php' (or my autoload) in deploy.php. So for my understanding the autoload from root project is not loaded.