Describe the bug
Attempting to run any drush commands on an existing project with a PolicyCommands.php file results in an unrecoverable error due to using Drush 8 rather than a local (to the project) Drush 9:
PHP Fatal error: Class 'Drush\Commands\DrushCommands' not found in /var/www/html/drush/Commands/PolicyCommands.php on line 11
Drush command terminated abnormally due to an unrecoverable error. [error]
Error: Class 'Drush\Commands\DrushCommands' not found in /var/www/html/drush/Commands/PolicyCommands.php, line 11
Failed to execute command [drush status]: Failed to run docker-compose [-f /Users/tanc/Sites/l.ethicalconsumer/gitroot/.ddev/docker-compose.yaml exec -T web drush status], err='exit status 255', stdout='
Fatal error: Class 'Drush\Commands\DrushCommands' not found in /var/www/html/drush/Commands/PolicyCommands.php on line 11
', stderr='PHP Fatal error: Class 'Drush\Commands\DrushCommands' not found in /var/www/html/drush/Commands/PolicyCommands.php on line 11
Drush command terminated abnormally due to an unrecoverable error. [error]
Error: Class 'Drush\Commands\DrushCommands' not found in /var/www/html/drush/Commands/PolicyCommands.php, line 11'
To Reproduce
Steps to reproduce the behavior:
DrushCommandsddev exec drush statusExpected behavior
I would expect the locally installed (to the project) drush 9 to be used, not drush 8.
Version and configuration information (please complete the following information):
Client:
Version: 18.06.0-ce
API version: 1.38
Go version: go1.10.3
Git commit: 0ffa825
Built: Wed Jul 18 19:05:26 2018
OS/Arch: darwin/amd64
Experimental: false
Server:
Engine:
Version: 18.06.0-ce
API version: 1.38 (minimum version 1.12)
Go version: go1.10.3
Git commit: 0ffa825
Built: Wed Jul 18 19:13:46 2018
OS/Arch: linux/amd64
Experimental: true
domain ddev.local
cli v1.0.0
web drud/ddev-webserver:v1.0.0
db drud/ddev-dbserver:v1.0.0
dba drud/phpmyadmin:v1.0.0
router drud/ddev-router:v1.0.0
commit v1.0.0
APIVersion: v1.0.0
name: ec
type: drupal8
docroot: web
php_version: "7.1"
router_http_port: "80"
router_https_port: "443"
xdebug_enabled: false
additional_hostnames: []
additional_fqdns: []
provider: default
Additional context
After removing the PolicyCommands.php file and running ddev exec drush --version I can see that Drush 8 is still being used rather than the local Drush 9:
Drush Version : 8.1.15
How do I get the container to pick up Drush 9?
Could you take a look at this with a fresh drupal-composer build? When I do a plain drupal-composer build I get
$ ddev ssh
uid_501@6664b00d33b8:/var/www/html/web$ drush --version
Drush Commandline Tool 9.4-dev-g564cae9
I'm pretty sure you either didn't run composer install or you don't have require-dev in your composer.json ?
And that appears to be the first error you see: PHP Fatal error: Class 'Drush\Commands\DrushCommands' not found in /var/www/html/drush/Commands/PolicyCommands.php on line 11
Drush command terminated abnormally due to an unrecoverable error.
Thanks for getting back to me on this. While I sort out a fresh project and test it, can you clarify why I'd need to re-run composer install? As this is an existing working project that I can successfully use with both Lando and Docker for Drupal I would assume it would also work with ddev. But there must be something slightly unusual about the set up or my lack of ddev knowledge.
My composer.json has drush as a standard require and I can confirm it is installed in vendor/drush/drush. The only potential oddity is that I have symlinks in bin pointing to the location of the binaries, configured in composer.json with:
"config": {
"bin-dir": "bin/",
Does ddev have drush launcher installed?
The classic reason you'd need a composer install is that you didn't run it yet. People do that all the time, and the result is no vendor directory. So that's why I suggested that.
ddev uses the classic drush8 launcher in /usr/local/bin. It finds the project drush and uses it.
However, if your setup confuses the drush launcher, things won't work. If it can't find your project's drush, it won't work.
You can change the environment to add a PATH to your project's drush if you want and you're committed to using something a little different. The technique in https://stackoverflow.com/questions/50109020/how-can-i-set-environment-variables-inside-ddevs-containers would work, and you could path directly to your drush and not require the drush launcher.
All I'm suggesting is that you do this composer create-project drupal-composer/drupal-project:8.x-dev my-drupal8-site --stability dev --no-interaction and test the behavior. I think you'll find it works fine; then the issue is debugging your composer.json setup.
I'm having the exact same problem on a fresh install, based on my composer.json starter. Anything strike you as odd or different about this composer file: https://gitlab.agile.coop/agile-public/drupal-8-starter/raw/master/composer.json
Confirmed that using drupal-project works fine, drush 9 is found.
I'm afraid I'm no composer expert, but it seems you need to debug your composer.json, or just make it more "normal". If you have suggestions after studying the difference, we'll listen.
I'll see if I can figure out what might be causing the issue and report back.
Ok, if I remove "bin-dir": "bin/", from composer.json everything works fine, Drush 9 is found. So something is expecting to find all the binary symlinks in vendor/bin rather than just bin (which we specify for convenience).
Congrats on that. You may want to add double-symlinks (symlink into bin from vendor/bin) so that drush can find the site-installed version? AFAICT this has nothing to do with ddev and everything to do with drush, drush launcher, and composer right?
AFAICT this has nothing to do with ddev and everything to do with drush, drush launcher, and composer right?
Possibly, but drush launcher solves the problem:
$ ddev exec drush --version
PHP Fatal error: Class 'Drush\Commands\DrushCommands' not found in /var/www/html/drush/Commands/PolicyCommands.php on line 11
$ ddev ssh
$ wget --quiet -O drush.phar https://github.com/drush-ops/drush-launcher/releases/download/0.6.0/drush.phar
$ chmod +x drush.phar
$ sudo mv drush.phar /usr/local/bin/drush
$ exit
$ ddev exec drush --version
Drush Launcher Version: 0.6.0
Drush Commandline Tool 9.0.0
Is there a reason you don't install it? Looking through the docs there doesn't seem to be a hook to run commands inside a container when you create them, only when you start them, otherwise that could be a way of installing drush launcher.
We do in fact install drush v8 in /usr/local/bin, and it does in fact run the site-local version of drush on a normal composer install. AFAICT this is a problem with your configuration, (or are you running composer on Windows and not getting symlinks properly?)
However, we probably will switch from drush 8 to drush-launcher.
I'm happy to close this as there is a solution to getting drush working (stick to the 'normal' composer configuration). I think drush launcher makes sense for ddev to install as it allows people some choice as to where they symlink their binaries from which is not completely unusual practice (see config in step 3 in the 'Drupal Extension' installation instructions) and is an allowed composer config setting:
"config": {
"bin-dir": "bin/"
}
Thanks, I'm sure drush launcher is in our future. Drush is getting more and more complicated; we'll seek some Drupal users' consensus on the way forward. I updated https://github.com/drud/ddev/issues/1039 to add this concern to it.
For others who find this. I had to do a composer/require drush/drush:~9 on my codebase + the steps in https://github.com/drud/ddev/issues/1028#issuecomment-410150840 to swap out the drush launcher. This was for a native drupal8 ddev project type.