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

Wrong error message for dependency problem in container #29019

Closed
emil-nasso opened this issue Jul 2, 2019 · 1 comment · Fixed by #29180
Closed

Wrong error message for dependency problem in container #29019

emil-nasso opened this issue Jul 2, 2019 · 1 comment · Fixed by #29180
Labels

Comments

@emil-nasso
Copy link

  • Laravel Version: v5.8.7
  • PHP Version: 7.3
  • Database Driver & Version: -

Description:

I ran into a situation where the container gives me an error message pointing me in the wrong direction. In the scenario, there are two dependencies that have strings as arguments to the constructor. One of the dependencies are declared with a contextual binding ( a when-call) but the other one is not.

The container gives me the following error message in the example below: Unresolvable dependency resolving [Parameter #0 [ <required> string $a ]] in class App\Console\Commands\Secondary. This dependency is not unresolvable as it has been declared.

I would expect the error message to instead be: Unresolvable dependency resolving [Parameter #0 [ <required> string $c ]] in class App\Console\Commands\DeepTwo. Adding

Declaring DeepTwo as a singleton solves the problem, making Main resolvable via the container.

The problem only seems to occur when the contructor argument in DeepOne has a default value. If I remove the = null i get the correct error message from the container.

Please let me know if this is not reproducible or if I can help in any other way.

Steps To Reproduce:

Run this as a command:

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Container\Container;

class Main
{
    public function __construct(DeepOne $deepOne, Secondary $secondary)
    { }
}

class DeepOne
{
    // Removing the `= null` in here will result in the correct error-message
   // being shown.
    public function __construct(DeepTwo $c = null)
    { }
}

class DeepTwo
{
    public function __construct(string $a)
    { }
}

class Secondary
{
    public function __construct(string $a, string $b)
    { }
}

class ContainerBug extends Command
{
    protected $signature = 'bug:demo';
    protected $description = 'Container bug demonstration';

    public function handle()
    {
        $container = Container::getInstance();


        $container->when(Main::class)
            ->needs(Secondary::class)
            ->give(function () {
                return new Secondary('a', 'b');
            });

        // Uncommenting this resolves the unresolved dependency
        // $container->singleton(DeepTwo::class, function () {
        //     return new DeepTwo('a');
        // });

        $res  = $container->make(Main::class);

        $this->info('done, without errors');
    }
}
@driesvints
Copy link
Member

This indeed seems like a bug to me. Thanks for reporting. Appreciating any help with figuring this out.

@driesvints driesvints added the bug label Jul 2, 2019
Organizm238 added a commit to Organizm238/framework that referenced this issue Jul 15, 2019
Organizm238 added a commit to Organizm238/framework that referenced this issue Jul 15, 2019
taylorotwell pushed a commit to illuminate/container that referenced this issue Jul 16, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants