Hoa/Core errors when loaded twice
In our scenario, we have a project that depends on Hoa\Bench.
We also have globally installed packages (composer global), in particular phpunit and other Hoa dependencies.
The phpunit binary is executed globally for our package. This means that there are two composer autoloaders active, each load a copy of Hoa\Core, even though the global one does not require it.
This causes this error:
phpunit --configuration tests/phpunit.xml
Hoa main file (Core.php) must be included once.
The root cause for this, is that Core.php is loaded regardless of it it's ever used or not.
Source: https://github.com/fruux/sabre-vobject/issues/238
It would be nice if there is a solution to this =) Hope all is well!
Hello :-),
If you replace https://github.com/hoaproject/Core/blob/master/Core.php#L52 by a simple return statement, does it work?
It's not going to be the solution. Just testing stuff.
Confirmed to work for my use-case. Put that return statement a bit higher up though ;)
@evert Yup sorry, here https://github.com/hoaproject/Core/blob/df1f75e6a98d52b879bf36bc1ea8744433458f09/Core.php#L46.
So it works. Great. But an exit seems better since it shows a unexpected workflow or file load. What do you think is better? Being silent is… good for new comers but not good to make something clean. It's always the “strict vs. friendly” debate…
Please @hoaproject/hoackers, raise your voice!
Actually why do you need to keep an exit or require in hoa itself ? Why can't it be kept the responsibility of composer or other autoloaders to handle this ?
@harikt Because if Composer or another project sucks at loading correctly different dependencies (I am not blaming anyone, it just happens sometimes), the developper will not notice. This exit is bad but it shows that something wrong is happening. It is a guard. But it makes the user experience bad sometimes and people are blaming Hoa at the end… which is really bad for us. We are the good guys here :-(.
@Hywan not sure what you mean by composer not loading correctly.
Do you have any example to show the psr-0, psr-4 autoloaders are not working correctly?
See the usecase of @evert right above. It is possible to load the same file twice if present in two different locations (so it's not the same file but it contains the same content).
Actually that is a good point. But I have a doubt.
I hope we are using autoloader to load than requiring ourselves.
1 ) Assuming Hoa\Core\Core is the class trying to load. 2 ) Autoloader checks if class is loaded or not and try to load from ( global / different ) path. 3 ) It loads the file 4 ) Now we don't need to care the location of the file it loads right ?
May be I am missing something. Sorry if I am asking silly doubts just ignore and don't get frustrated with my comments.
Thank you.
We load Core.php with the files Composer's property. So the class_exists is not used here.
Thank you for the clarification @Hywan .
It's very close to what we talked about here: https://github.com/composer/composer/pull/3794.
Wouldn't it be better though if Core is only loaded if it's actually used? This might be a larger change, but do you really need it? Perhaps some of it's functionality can be moved to functions that can get optionally included, on demand.
I agree with @evert here.
It seems to me ( blame me I don't really know if that breaks anything ) that we can include a file which have the functions defined below https://github.com/hoaproject/Core/blob/df1f75e6a98d52b879bf36bc1ea8744433458f09/Core.php#L474 .
And the class Core is just a psr-4 classloader. So I believe that change bring one thing, and we can make sure it is not loaded any where else. So many of the require will go away and giving the autoloader the complete control. There can be pitfalls though I am not seeing / experiencing.
We heavily rely on the hoa:// protocol and we need to register it automatically, so the files Composer's property is required. With the split of Hoa\Core, I am planning to create Hoa/Protocol and to files this library instead of the whole Hoa\Core code.
This is broken when using HOA (2.0) under Symfony 2.8+ as a composer dependency (the Sensio installer for building the app/bootstrap.php.cache file will terminate early due to the HOA exit call).
I'm guessing it's because it see's 'HOA' defined more than once, as it gets redefined as the composer process continues to run.
I agree with adding a 'return', not an exit, or at the very least throwing an exception (so it can be left up to the programmer to catch it, or let it fall through and terminate with a stack trace).
Using a raw exit doesn't make sense.
Also, the error message is not good, it implies (to me) that the file was not included at all, when the intent is to inform the user that the file was included more than once.
It seems updating the hoa\websocket dependency to 3.x from 2.x fixes it, however remaining on the 2.x version, per semantic versioning, should be a viable option. Was something changed that breaks backwards compatability with it?
Hoa/Core has been split, see http://blog.hoa-project.net/posts/36-the-nucleus-releases-serie.html.
Please, can you test with hoa/websocket ~3.0. I am guessing the issue will still be present. There was no BC break here as far as I know @ahungry.
What the app/bootstrap.php.cache file?
Actually, we are now throwing an exception. No more exit. See https://github.com/hoaproject/Consistency/blob/c7919e409ca68858f8cceadb7565b9f622575249/Prelude.php#L42-L44.
@Hywan question why does it actually need the exception.. Isn't it can be dealt with composer?
May be you are providing the whole library to be downloaded without composer ?
@Hywan never mind you have answered earlier to my same question :) .
Sorry for the trouble. Just got the mail and was checking :) .
No problem :-]. We are no longer checking if Hoa exists or not. We just let PHP cries out. We are just checking if Hoa can run now.