Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Namespace problems with Site-Wide Drush Commands #4641

Closed
AdamPS opened this issue Jan 19, 2021 · 6 comments
Closed

Namespace problems with Site-Wide Drush Commands #4641

AdamPS opened this issue Jan 19, 2021 · 6 comments

Comments

@AdamPS
Copy link
Contributor

AdamPS commented Jan 19, 2021

Describe the bug
Docs for Site-Wide Drush Commands state to use a namespace Drush\Commands. However this namespace isn't registered as an auto-loader, so it' doesn't work to try and use files in this directory.

To Reproduce
Create a site-wide command in $PROJECT_ROOT/drush/Commands/ExampleCommands.php that extends from a base class in the same directory.

Expected behavior
It works.

Actual behavior
The base class cannot be found.

Workaround
Add a line to your own composer.json.

  "autoload": {
    "psr-4": {
      "Drush\\Commands\\": "web/drush/Commands/"
    }
  },

System Configuration

Q A
Drush version? 10..3.6
Drupal version? Any
PHP version 7.4.3
OS? Linux
@AdamPS
Copy link
Contributor Author

AdamPS commented Jan 19, 2021

Proposed fix is to alter drush composer.json as per the workaround. I can create a PR if you would accept this change.

@Sweetchuck
Copy link
Contributor

Sweetchuck commented Jan 23, 2021

I have been using site-wide Drush commands for a while, and definitely something wrong with the Command file detection.

But my workaround is the following:
File layout (relative from project root):

  • ./drush/drush.yml
  • ./drush/drush.app.yml
  • ./drush/drush.local.yml
  • ./drush/Commands/contrib/foo/composer.json
  • ./drush/Commands/contrib/foo/Commands/drush.yml
  • ./drush/Commands/contrib/foo/Commands/foo/FooBarCommands.php

File content ./drush/drush.yml

drush:
  paths:
    config:
      - '${drush.vendor-dir}/../drush/Commands/contrib/foo/Commands'
      - '${drush.vendor-dir}/../drush/drush.app.yml'
      - '${drush.vendor-dir}/../drush/drush.local.yml'
    include:
      - '${drush.vendor-dir}/../drush/Commands/contrib/foo'

File content ./drush/drush.app.yml

options:
  root: '${drush.vendor-dir}/../docroot'

File content ./drush/Commands/contrib/foo/composer.json

{
    "name": "drupal/foo",
    "type": "drupal-drush",
    "autoload": {
        "psr-4": {
            "Drush\\Commands\\foo\\": "Commands/foo/",
            "Drush\\Generators\\foo\\": "Generators/foo/",
            "Drupal\\foo\\": "src/"
        }
    }
}

File content ./drush/Commands/contrib/foo/Commands/foo/FooBarCommands.php

<?php

namespace Drush\Commands\foo;

class FooBarCommands {}

alias d='vendor/bin/drush --config=drush @app.local'
d list --filter=foo

it works, but far from ideal.
I have to deal with all the "config" and "include" manually, and for some reason I have to add the "--config=drush" option to the CLI as well.

@Sweetchuck
Copy link
Contributor

Commands and Generator detection related issues:

@greg-1-anderson
Copy link
Member

See the Example Drush Extension. This example does not add an entry to autoload in its composer.json, but it could, similar to the suggestion in the OP, but in the extension composer.json rather than the site composer.json.

I don't think we should put an entry in Drush to autoload extension class files though.

@AdamPS
Copy link
Contributor Author

AdamPS commented Jan 24, 2021

It's fine by me to resolve this with documentation. The two actions below would have resolved my uncertainty meaning I didn't raise an issue here.

  1. Alter the composer.json of the example drush extension
  2. Alter the help page for command authoring

@weitzman
Copy link
Member

weitzman commented Apr 2, 2021

Pretty big improvement merged in #4696.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants