If a site has unused_modules installed as a module then installing unused_moduhttps://www.drupal.org/node/2755351#les as a drush command and running drush @site unused-modules or drush @sites audit_extensions (if used in conjunction with site_audit as suggested/implemented in https://www.drupal.org/node/2529540) throws a PHP fatal error:

Fatal error: Cannot redeclare _unused_modules_get_modules_by_project() (previously declared in /home/user/.drush/commands/unused_modules/unused_modules.inc:20) in /home/user/projects/sandbox.local/sites/all/modules/unused_modules/unused_modules.inc on line 58

Also, if unused_modules is installed as a drush command, running drush @sites en unused_modules throws the same error.

This is bad for people who might want to use unused_modules as a drush command in platforms where you can't know in advance whether sites have unused_modules installed already.

  • unused_modules versions tested (as drush command): 7.x-1.5 and 7.x-1.x-dev
  • unused_modules versions tested (as site module): 7.x-1.5

Comments

erickbj created an issue. See original summary.

ndf’s picture

Status: Active » Closed (works as designed)

For UI the module must be installed in the installation off-course, but yeah I can understand that you would like to install this in .drush too.

I don't have an idea how to solve this in the module though. In PHP you cannot redeclare a function twice:

All functions and classes in PHP have the global scope - they can be called outside a function even if they were defined inside and vice versa.
PHP does not support function overloading, nor is it possible to undefine or redefine previously-declared functions.

source: http://php.net/manual/en/functions.user-defined.php

So in this case I think you must choose; enable the module in the site or only in .drush.

Please re-open if you see this different!

ndf’s picture

Status: Closed (works as designed) » Needs work

There is a site I worked on that has the described setup with registry-rebuild. The registry-rebuild module is installed both in .drush and in the sites/all/modules. And the drush command works as expected.

So it must be possible for this module too.

erickbj’s picture

Checking out the latest version for registry_rebuild I don't see how that could be enabled as a module (there's no .module or .info files in the package). There's only registry_rebuild.drush.inc and the "site" version of that file (i.e., what is put into sites/all/modules or similar path) is never used when registry_rebuild is installed as a drush command/extension too. I might be mistaken but I think that's why having RR both installed as a drush command/extension and site module doesn't break things.

Regarding unused_modules I think for now it'd be a matter of avoiding including unused_modules.inc more than once - the attached patch does exactly that. We can't trust require_once to avoid this inclusion more than once because files paths (from the drush installed version to the site installed version) will vary in most cases.

erickbj’s picture

Status: Needs work » Needs review

Changing status to "needs review".

Status: Needs review » Needs work

The last submitted patch, 4: unused_modules-drush_include_files-2755351-4-D7.patch, failed testing.

ricardo.peressinotti’s picture

I tested the patch but the issue still happening.

ricardo.peressinotti’s picture

Assigned: Unassigned » ricardo.peressinotti
Issue summary: View changes
Status: Needs work » Reviewed & tested by the community
StatusFileSize
new191.84 KB

In my last test I forgot to apply the patch in the drush command folder, I applyed only on site module folder.

Now I did the test applying the patch for both folders (drush command and site module). Everthing is working correctely.

Don't forget to apply the path in both folders (~/.drush/commands and ../sites/all/modules/unused_modules)! It's realy important to do this!

Check my terminal messages:
Successful

Thanks @erickbj

ricardo.peressinotti’s picture

Issue summary: View changes
Status: Reviewed & tested by the community » Needs review

Changing status to "needs review".

ricardo.peressinotti’s picture

Status: Needs review » Reviewed & tested by the community

The patch is working! :)

For more information, please check the comment #8.

Thanks @erickbj

  • ndf committed 4bd9a45 on 7.x-1.x
    Issue #2755351 by erickbj, reviewed by ricardo.peressinotti : Fatal...
larruda’s picture

Status: Reviewed & tested by the community » Fixed

I've contacted @ndf and he committed the patch!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

ndf’s picture