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

[DX] Enhance error when a use is missing #28905

Closed
lyrixx opened this issue Oct 17, 2018 · 10 comments
Closed

[DX] Enhance error when a use is missing #28905

lyrixx opened this issue Oct 17, 2018 · 10 comments
Labels
Debug DX DX = Developer eXperience (anything that improves the experience of using Symfony)

Comments

@lyrixx
Copy link
Member

lyrixx commented Oct 17, 2018

Description
I would like to enhance DX when theses errors occur:

(1/1) RuntimeException
Cannot autowire service "App\Controller\Api\TopicController": argument "$validator" of method "__construct()" has type "App\Controller\Api\ValidatorInterface" but this class was not found.

or

(1/1) InvalidArgumentExceptionCannot determine controller argument for "App\Controller\Api\TopicController::new()": the $request argument is type-hinted with the non-existent class or interface: "App\Controller\Api\Request". Did you forget to add a use statement?

or ...


 
We coul instead put in bold something like:

We think you should add use Symfony\Component\HttpFoundation\Request; in the file src/Foo/bar.php


I really think this would be really useful. So many people fall into theses issues during training and stray blocked :/

@stof
Copy link
Member

stof commented Oct 17, 2018

The question is, how do we know which class to suggest ? That's the hard part of this suggestion. We all agree that it would be a better error message, but we need to be able to know which class to suggest.

@lyrixx
Copy link
Member Author

lyrixx commented Oct 17, 2018

Some ideas:

  1. Search dynamically for service that have the same class name (without the namespace)
  2. Add a static list of common class (Request, Response,...)
  3. Search for potential class and list them all

@stof
Copy link
Member

stof commented Oct 17, 2018

The DebugClassLoader cannot search for services. The autoloader does not have access to the container (and giving it such access would be a nightmare to avoid memory leaks, and also to account for the fact that the autoloader is also used during the building of the container).
And how do you search for potential classes ? PHP only allows you to list the declared classes, i.e. the ones which have already been loaded in the process. There is no way to list all classes that your autoloader could load.

So in your ideas, I only see the second one as doable (but then, it is a pain to maintain, and only works for core classes)

@xabbuh xabbuh added Debug DX DX = Developer eXperience (anything that improves the experience of using Symfony) labels Oct 18, 2018
@stof
Copy link
Member

stof commented Oct 22, 2018

hmm, in the case of autowiring errors, the AutowiringPass may inspect the service to implement 1. though.

@lyrixx
Copy link
Member Author

lyrixx commented Oct 22, 2018

But even if there is no service associated with a class, we could propose a use, isn't it ?

@stof
Copy link
Member

stof commented Oct 22, 2018

The issue is still the same: how do you know about the class ?

@lyrixx
Copy link
Member Author

lyrixx commented Oct 25, 2018

we could simply search for all class in project that have the same class name and propose each FQCN

@xabbuh
Copy link
Member

xabbuh commented Oct 25, 2018

But you can only reliably get the names of already loaded classes. So chances are that you just cannot propose the right class name as this class wasn't autoloaded before.

@stof
Copy link
Member

stof commented Oct 25, 2018

@lyrixx you always cam back to the same issue: how do you know "all classes" ?

@javiereguiluz
Copy link
Member

Let's close this because it looks like a "dead end". The idea is very nice, but the implementation seems "impossible". Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Debug DX DX = Developer eXperience (anything that improves the experience of using Symfony)
Projects
None yet
Development

No branches or pull requests

4 participants