drupal-console
drupal-console copied to clipboard
generate:module gives Fatal error: Call to a member function getPath()
[ ISSUE-GROUP ] Short description
The ISSUE-GROUP should be one of: generate:module
Problem/Motivation
Just set up console and tried to run gm command.
drupal generate:module
--module="Qleva"
--machine-name="qleva"
--module-path="/modules"
--description="Custom functions for Qleva mini-site."
--core="8.x"
--package="Qleva"
--module-file
gives: Fatal error: Call to a member function getPath() on null in E:\www\drupal8a\vendor\drupal\console\src\Generator\ComposerGenerator.php on line 46
Details to include:
- Why are we doing this? Above all, a summary should explain why a change is needed, in a few short sentences.
How to reproduce
Include steps related how to reproduce.
Details to include:
- Drupal version (you can obtain this by running
drupal site:status
). 8.7.3 - Console version (you can obtain this by running
composer show | grep drupal/console
). 1.9.1 - Console Launcher version (you can obtain this by running outside of a drupal site
drupal --version
). 1.9.1 - Steps to reproduce
drupal generate:module
--module="Qleva"
--machine-name="qleva"
--module-path="/modules"
--description="Custom functions for Qleva mini-site."
--core="8.x"
--package="Qleva"
--module-file
I ran just gm command with no options and simply answered questions.. this worked. Guessing one of the arguments no longer matches the documentation. Perhaps a better error message would be useful?
Hi @liquidcms The problem is that you are using an absolute path that doesn't exist in the --module-path
option. You can use the absolute path if the folder is valid
If you try
drupal generate:module \
--module="Qleva" \
--machine-name="qleva" \
--module-path="modules" \
--description="Custom functions for Qleva mini-site." \
--core="8.x" \
--package="Qleva" \
--module-file \
--no-interaction
Everything will work fine, I will change the examples in the languages repository.
Thank you.
I got this error too, even I don't use the absolute module path. The module files generate successfully except the composer.json file. What to do :(
bash-4.4# su - application -c "cd /app/web/sites && /usr/local/bin/drupal gm"
// Welcome to the Drupal module generator
Enter the new module name:
> Che Migration
Enter the module machine name [che_migration]:
>
Enter the module Path [modules/custom]:
> profiles/custom/che/modules/custom
Enter module description [My Awesome Module]:
> Che project's migration functionalities.
Enter package name [Custom]:
> Che
Enter Drupal Core version [8.x]:
>
Do you want to generate a .module file? (yes/no) [yes]:
>
Define module as feature (yes/no) [no]:
>
Do you want to add a composer.json file to your module? (yes/no) [yes]:
>
Would you like to add module dependencies? (yes/no) [no]:
> yes
Module dependencies separated by commas (i.e. context, panels):
> migrate
Do you want to generate a unit test class? (yes/no) [yes]:
>
Do you want to generate a themeable template? (yes/no) [yes]:
> no
Do you want proceed with the operation? (yes/no) [yes]:
>
// generate:composer
Error: Call to a member function getPath() on null in /app/vendor/drupal/console/src/Generator/ComposerGenerator.php on line 46 #0 /app/vendor/drupal/console/src/Command/Generate/ComposerCommand.php(410): Drupal\Console\Generator\ComposerGenerator->generate(Array)
#1 /app/vendor/symfony/console/Command/Command.php(255): Drupal\Console\Command\Generate\ComposerCommand->execute(Object(Symfony\Component\Console\Input\ArrayInput), Object(Drupal\Console\Core\Style\DrupalStyle))
#2 /app/vendor/drupal/console-core/src/EventSubscriber/CallCommandListener.php(76): Symfony\Component\Console\Command\Command->run(Object(Symfony\Component\Console\Input\ArrayInput), Object(Drupal\Console\Core\Style\DrupalStyle))
#3 /app/vendor/symfony/event-dispatcher/EventDispatcher.php(214): Drupal\Console\Core\EventSubscriber\CallCommandListener->callCommands(Object(Symfony\Component\Console\Event\ConsoleTerminateEvent), 'console.termina...', Object(Symfony\Component\EventDispatcher\EventDispatcher))
#4 /app/vendor/symfony/event-dispatcher/EventDispatcher.php(44): Symfony\Component\EventDispatcher\EventDispatcher->doDispatch(Array, 'console.termina...', Object(Symfony\Component\Console\Event\ConsoleTerminateEvent))
#5 /app/vendor/symfony/console/Application.php(1008): Symfony\Component\EventDispatcher\EventDispatcher->dispatch('console.termina...', Object(Symfony\Component\Console\Event\ConsoleTerminateEvent))
#6 /app/vendor/symfony/console/Application.php(254): Symfony\Component\Console\Application->doRunCommand(Object(Drupal\Console\Command\Generate\ModuleCommand), Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#7 /app/vendor/drupal/console-core/src/Application.php(188): Symfony\Component\Console\Application->doRun(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#8 /app/vendor/drupal/console/src/Application.php(64): Drupal\Console\Core\Application->doRun(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#9 /app/vendor/symfony/console/Application.php(147): Drupal\Console\Application->doRun(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#10 /app/vendor/drupal/console/bin/drupal.php(89): Symfony\Component\Console\Application->run()
#11 /app/vendor/drupal/console/bin/drupal(4): require('/app/vendor/dru...')
#12 {main}
Error: Call to a member function getPath() on null in Drupal\Console\Generator\ComposerGenerator->generate() (line 46 of /app/vendor/drupal/console/src/Generator/ComposerGenerator.php).
I found why my problem, the path profiles/custom/che/modules/custom
dosen't exist.
Please make sure that the path inputed had created.
@solody thank for your report, could you create a new issue, thanks in advance
I'm back again, I found that it's a bug actually.
This time, the path profiles/custom/che/modules/custom
is actually exist, and the error still occur.
I encountered this same problem from my Drupal root folder. Changing the module path from module/custom to web/module/custom fixed the problem.
Encountered this again and was able to fix by not specifying dependencies. Didn't need to include 'web' for the module path.
For me it works if no dependencies are specified.
For me it works if no dependencies are specified.
this was my work around as well.
For me clearly the problem was trying to create composer.json. Answering no to relevant question allowed for the operation to complete.
Actually, the error occures if BOTH composer.json and dependencies are selected. It happens with the parameters specified and in interactive mode too.
(-n option used to avoid extra questions)
This works, creates a composer file, no dependencies
drupal generate:module -n --module="AAA" --machine-name="aaa" --module-path="modules/custom" --description="AAA" --core="8.x" --package="Custom" --module-file --test --twigtemplate --composer
This also works, adds dependencies, no composer file
drupal generate:module -n --module="BBB" --machine-name="bbb" --module-path="modules/custom" --description="BBB" --core="8.x" --package="Custom" --module-file --test --twigtemplate --dependencies user
This fails and does not create the composer.json file
drupal generate:module -n --module="CCC" --machine-name="ccc" --module-path="modules/custom" --description="CCC" --core="8.x" --package="Custom" --module-file --test --twigtemplate --composer --dependencies user
Composer 1.9.3 PHP 7.2.28
I am also running into this. Here is ComposerGenerator::generate():
public function generate(array $parameters)
{
$machineName = $parameters['machine_name'];
$this->renderFile(
'module/composer.json.twig',
$this->extensionManager->getModule($machineName)
->getPath() . '/composer.json',
$parameters
);
}
$machineName
Is the value of the --machine-name=
parameter I presume, and this $this->extensionManager->getModule($machineName)
is returning a null value because the module you are creating doesn't exist in the list of modules at the time it is called.
I don't have any answers, I just ran into this and attempted to trace it down.
Also confirmed that this appears to work when not using the dependencies
option.
I just created a pull request to try to address this. The PR doesn't really fix the issue, but it provides some more helpful information. Running the generator will appear as this on error instead of the stack trace you see now:
[ERROR] Unable to load module: "my_module" from extension manager. This may be an unresolved issue in the
module generator. This will prevent the generator from creating the composer.json file. Try calling the command
without setting dependencies. See https://github.com/hechoendrupal/drupal-console/issues/4118
Generated or updated files
Generation path: /vagrant/web
1 - /modules/custom/my_module/my_module.info.yml
2 - /modules/custom/my_module/my_module.features.yml
3 - /modules/custom/my_module/my_module.module
4 - /modules/custom/my_module/tests/src/Functional/LoadTest.php
5 - /modules/custom/my_module/my_module.module
6 - /modules/custom/my_module/templates/stripe-webform-discount.html.twig
Generated lines: 91
Also note, if you want to try re-running the command, you will need to delete the previously generated files.
Isn't it funny when you google an issue and first result in Google is the post you made a year ago.
anyway, still getting this error a year later. Looking through comments above i was able to get it working by removing --composer and then also answering no when it asked if i wanted to include dependencies.
Also, when using module_path as "/modules/custom", i thought it was odd that it was trying to create the module in D:\Git\Git\modules\custom when i ran the command from the docroot of my site: E:/web/drupal8/web. When i specified absolute path it put the new module in the correct location (E:/www/drupal8/web/modules/custom).
Hi, I was working with Drupal Console and when I was using the drupal gm command (generate module) I got this error message:
// generate:composer
Error: Call to a member function getPath() on null in /var/www/html/vendor/drupal/console/src/Generator/ComposerGenerator.php on line 46 #0 /var/www/html/vendor/drupal/console/src/Command/Generate/ComposerCommand.php(410): Drupal\Console\Generator\ComposerGenerator->generate(Array)
#1 /var/www/html/vendor/symfony/console/Command/Command.php(255): Drupal\Console\Command\Generate\ComposerCommand->execute(Object(Symfony\Component\Console\Input\ArrayInput), Object(Drupal\Console\Core\Style\DrupalStyle))
#2 /var/www/html/vendor/drupal/console-core/src/EventSubscriber/CallCommandListener.php(76): Symfony\Component\Console\Command\Command->run(Object(Symfony\Component\Console\Input\ArrayInput), Object(Drupal\Console\Core\Style\DrupalStyle))
My context: Generating a new Drupal Custom module in a interactive way, yes to composer.json file and yes to dependencies. My stack:
- Ubuntu 20.04.1 LTS
- Docker 19.03.13
- Docker-Compose 1.24.1
- DDEV 1.15.3
- Drupal Core 9.0.6
- Drupal Console 1.9.5
- PHP 7.3.20-1+0~20200710.65+debian10~1.gbpc9cbeb
Hey guys,
As @johnbburg mentioned the composer.json is generated through through ComposerGenerator::generate(). But the function $this->extensionManager->getModule($machineName)->getPath() . '/composer.json'
is responsible for setting the path.
So you can only generate a composer.json IF you generated the module in the modules folder of your drupal root where it is discoverable for Drupal.
This logic is faulty because (I think most) developers generate their modules outside of the webroot so they can require their module through composer itself. This is the correct procedure to keep dependency management intact.
My proposal is to add the --module-path option that is defined in: https://github.com/hechoendrupal/drupal-console/blob/e582d885aadcf57694043dc8b02acd151f8fdf98/src/Command/Generate/ModuleCommand.php#L122-L127
As an optional option to: https://github.com/hechoendrupal/drupal-console/blob/master/src/Command/Generate/ComposerCommand.php#L61
And if this option is set use that path, instead of the (empty) path provided by the extensionManager.
So for now, if you want to generate modules with a composer.json it's best to generate it in the default location modules/custom so Drupal can provide the path in which to generate the composer.json. After that you can just move the module to where you prefer it.
I'll see if I can provide a patch for you guys in the coming days.
I have not tried that PR, but this fails even when in modules/custom:
drupal generate:module --module="Yeego Connect: Color" \
--machine-name="yeegoconnect_color" \
--module-path="modules/custom" \
--description="Color API enhancements for Yeego." \
--core="9.x" \
--package="Yeego Connect" \
--module-file \
--composer \
--test
It's worth noting that it did generate all of the expected files except for composer.json. It is thus slightly different than (I think) described by @verbruggenalex but that solution may well resolve it. I'm not in an ideal position to test that at the moment so this is purely informational. :)
I got this error too, even I don't use the absolute module path. The module files generate successfully except the composer.json file. What to do :(
bash-4.4# su - application -c "cd /app/web/sites && /usr/local/bin/drupal gm" // Welcome to the Drupal module generator Enter the new module name: > Che Migration Enter the module machine name [che_migration]: > Enter the module Path [modules/custom]: > profiles/custom/che/modules/custom Enter module description [My Awesome Module]: > Che project's migration functionalities. Enter package name [Custom]: > Che Enter Drupal Core version [8.x]: > Do you want to generate a .module file? (yes/no) [yes]: > Define module as feature (yes/no) [no]: > Do you want to add a composer.json file to your module? (yes/no) [yes]: > Would you like to add module dependencies? (yes/no) [no]: > yes Module dependencies separated by commas (i.e. context, panels): > migrate Do you want to generate a unit test class? (yes/no) [yes]: > Do you want to generate a themeable template? (yes/no) [yes]: > no Do you want proceed with the operation? (yes/no) [yes]: > // generate:composer Error: Call to a member function getPath() on null in /app/vendor/drupal/console/src/Generator/ComposerGenerator.php on line 46 #0 /app/vendor/drupal/console/src/Command/Generate/ComposerCommand.php(410): Drupal\Console\Generator\ComposerGenerator->generate(Array) #1 /app/vendor/symfony/console/Command/Command.php(255): Drupal\Console\Command\Generate\ComposerCommand->execute(Object(Symfony\Component\Console\Input\ArrayInput), Object(Drupal\Console\Core\Style\DrupalStyle)) #2 /app/vendor/drupal/console-core/src/EventSubscriber/CallCommandListener.php(76): Symfony\Component\Console\Command\Command->run(Object(Symfony\Component\Console\Input\ArrayInput), Object(Drupal\Console\Core\Style\DrupalStyle)) #3 /app/vendor/symfony/event-dispatcher/EventDispatcher.php(214): Drupal\Console\Core\EventSubscriber\CallCommandListener->callCommands(Object(Symfony\Component\Console\Event\ConsoleTerminateEvent), 'console.termina...', Object(Symfony\Component\EventDispatcher\EventDispatcher)) #4 /app/vendor/symfony/event-dispatcher/EventDispatcher.php(44): Symfony\Component\EventDispatcher\EventDispatcher->doDispatch(Array, 'console.termina...', Object(Symfony\Component\Console\Event\ConsoleTerminateEvent)) #5 /app/vendor/symfony/console/Application.php(1008): Symfony\Component\EventDispatcher\EventDispatcher->dispatch('console.termina...', Object(Symfony\Component\Console\Event\ConsoleTerminateEvent)) #6 /app/vendor/symfony/console/Application.php(254): Symfony\Component\Console\Application->doRunCommand(Object(Drupal\Console\Command\Generate\ModuleCommand), Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput)) #7 /app/vendor/drupal/console-core/src/Application.php(188): Symfony\Component\Console\Application->doRun(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput)) #8 /app/vendor/drupal/console/src/Application.php(64): Drupal\Console\Core\Application->doRun(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput)) #9 /app/vendor/symfony/console/Application.php(147): Drupal\Console\Application->doRun(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput)) #10 /app/vendor/drupal/console/bin/drupal.php(89): Symfony\Component\Console\Application->run() #11 /app/vendor/drupal/console/bin/drupal(4): require('/app/vendor/dru...') #12 {main} Error: Call to a member function getPath() on null in Drupal\Console\Generator\ComposerGenerator->generate() (line 46 of /app/vendor/drupal/console/src/Generator/ComposerGenerator.php).
just try hole thing same except [composer.json]->no: