-
-
Notifications
You must be signed in to change notification settings - Fork 76
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
[Scoper] Scoping autoloaded files result in an autoload conflict #298
Comments
Hi! Thanks for the report. It's nice to see it being tried on Wordpress :) I admit I'm a bit confused.
Since it is not a function from the global namespace, unless explicitly configured otherwise, I would expect this function to be scoped. If it is the case, then it means somewhere a piece of code has not been scoped properly and is still using the old reference (which can be an expected case. Unfortunately due to PHP nature PHP-Scoper won't be able to be perfect and will sometimes need some help via the patchers).
What I would find weird/confusing there is why would it be a problem for the scoped version and not the unscoped one? Also if the hash file is the same it means the content is the same, so indeed there shouldn't be a need to include it twice should it? |
Yes, it's not a function from the global namespace, but since it is defined in the The hash is the same even though the contents are not. Scoped
Unscoped
So whatever |
Hm this is very weird then. Has the autoloader been dumped again after the scoping? |
Yes, i've done a lot of testing with it. I've tried adding even more code to the Does not change a thing, unfortunately. |
I think it would be best to try to narrow down the issue in a reproducible repo to ease the debugging. It may be a bug in Composer or it means Composer is relying on the hash from the |
I'll create a repo for you 👍 |
The repository: If you run into any problems or need any more information just let me know :) |
👍 I'll try to take a look ASAP but it's likely gonna take a couple of weeks, this month is quite busy for me :/ |
Not a problem. We can hack in a solution if it becomes really pressing. Happy that you are willing to take a look! |
Yep, manipulating the package name fixes it indeed. Very weird choice to do it just based on that. They should also compare content-size or something. |
I'd hash the whole |
it's for the dumping the autoloader so I don't think it would be a big deal |
Would using |
I'm not sure. But I think it's a Composer issue so might be better to create an issue and look for a solution there. If not achievable, maybe we can look for a workaround in PHP-Scoper but I would like to avoid that if possible. |
@sandervanhooft no its needs to hash the file contents so it can deal with out situation. @theofidry Could you open an issue at Composer? I think it will carry more weight if you do it as the owner / maintainer of the humbug. |
So as per the discussion in composer/composer#7942, I changed my mind and I think it makes sense for PHP-Scoper to change the names of the scoped packages. So I guess a solution would be to also change the packages names in The concerned code is this one I think: https://github.com/humbug/php-scoper/tree/ddcf428/src/Scoper/Composer |
I agree with that approach, makes more sense since this will only happen when file contents of required files get scoped or altered. It's not a composer bug. Sounds like a good solution! |
I've experimented with this a bit and couldn't find a simple way to solve this within scoper. For the time being I've went to parsing But this is then a 3 step process:
Any ideas how to best move forward here? |
I think fixing the autoloader itself is a must avoid: we are jumping in an incredibly more complex solution if we do so. I'm quite busy at the moment so I can't take a proper look but maybe @naderman has an idea? @naderman to avoid the whole thread, the issue here is that:
The result is that one file is loaded (e.g. the non scoped Guzzle one) and the scoped one won't be loaded since has the same hash as the non-scoped one. Related Composer issue: composer/composer#7942 |
Understood - however I am not sure how to catch this in scoper - when one is supposed to dump the autoloader after using scoper. What solved it for me was this simple script"
|
Maybe a way would be to collect the included scoped files and append them to the dumped autoloader? |
I'm here with the same problem, I have a WordPress plugin bundling the scoped AWS SDK, but it loads a functions file before another plugin with the SDK. @theofidry have you had any further thoughts on this? @patrickjahns is #298 (comment) still your working solution? Update: Thanks @patrickjahns - works a treat! |
No I couldn't really check. One would need to check if, when the scoped file is appended to the |
Thinking about this again, a possible solution would be to have a
And then when dumping the PHP-Scoper autoloader we can append all those files there and they should not be in the regular Composer I can help out if one desires to implement this, but I'm unlikely to work on this in a foreseeable future |
Any momentum left here? The issue prevents us from using psalm.phar on any code using symfony/polyfill-80. |
Hi Have come across this issue aswell and awaiting a fix. In the meantime, I've added a custom patcher that bypasses this issue. //...
return [
'patchers' => [
static function (string $filePath, string $prefix, string $content): string {
$path = dirname( __FILE__ ) . DIRECTORY_SEPARATOR . implode( DIRECTORY_SEPARATOR, [ 'vendor','composer','autoload_real.php' ] );
if ( 0 === strcasecmp( $filePath, $path ) ) {
$content = str_replace( '\'__composer_autoload_files\'', '\'__composer_autoload_files_myapplication\'', $content );
}
return $content;
},
],
]; |
Note for myself: the best lead is to update the |
Closed by #774 |
hello, in a WordPress context, my plugin is scoped, the other one is not, we both depend on guzzle. With PHP scoper 0.18.3 it seems that the autoloaded guzzle file of the other plugin is still not loaded. Is this to be expected? I run |
@tkfv would you have a reproducer by any chance? |
I'll be closing this issue for now. If you experience any problem please open a new issue with a reproducer so that I can investigate 🙏 |
In [0.18.0-rc.0](https://github.com/humbug/php-scoper/releases/tag/0.18.0-rc.0), more specifically in #298, the composer autoload global variable is completely reset. This is actually a problem for when there is excluded files, as they now count as not loaded and if included again in another project, will be loaded, which can cause problems.
I seem to be running into this (well, something similar!) again. My
I used PHP-Scoper v0.18.7. My
Now, the thing is, I have another plugin of which the When I first encountered this issue (which was fixed by moving to PHP-Scoper v0.18.x), it was the first line ( This time, however, it seems to be the second line ( I'm guessing this is because I used the same filename/relative path in different plugins. (The contents of both files, and their namespaces, are very different, though.) In fact, if I rename the first file (to But now I'm worried that any other WordPress plugin using such a filename/path (and Composer or PHP-Scoper) could cause a (future) conflict. Or is this just how Composer works, nothing to do with PHP-Scoper? In that case, I should probably just include the file directly (I'm going to need it anyway), and I guess I learned something today. But then why does it work for the |
Never mind, this thing is now also (again) failing: |
And the issue seems gone now that I moved from the |
Edit to avoid to summarise the state of this issue.
When scoping a package which has autoloaded files, the scoped files will keep the same hash. As a result if your current application/library is using the scoped package and has files with the same hash, only one file will be loaded which results in undesirable behaviours.
It appears the hash strategy is not something we can change composer/composer#7942 and changing the package name of the scoped packages has undesirable side effects so it is not a solution either.
@jarrodbell #298 (comment) and @ondrejmirtes #298 (comment) made it work by changing it after the composer dump process as part of the scoping build process.
The discussion is carrying from that point to figure out if there is a way PHP-Scoper could fix it by itself without further actions from the user.
Bug report
We are using php-scoper in our client to generate a release for integrators to be used in for example Wordpress. When it is used there, there are often more packages with Guzzle via composer.
When this happens they will get the exception:
Uncaught Error: Call to undefined function GuzzleHttp\choose_handler()
This is because the corresponding
guzzlehttp/guzzle/src/functions_include.php
is not loaded for the second implementation due to the composer file require hash being the same.Below there is an example of our package being ran alongside another guzzle package.
This is a var_dump inside the composer_real.php where the functions_include is being required if the file hash is not already loaded.
So this causes only the first implementation it's
choose_handler
function to be available.I'm not really sure if this bug should be reported here, but i'm curious to your expert opinion about this. Might it be worth it to also scope the files that outta be required by composer? To avoid some of them not being loaded? As manipulating the hash in above example would fix the problem.
The text was updated successfully, but these errors were encountered: