deployer icon indicating copy to clipboard operation
deployer copied to clipboard

Use `--classmap-authoritative` on `composer install`

Open Schrank opened this issue 3 years ago • 11 comments

We had a recent discussion on Twitter, read the complete thread, it is interesting for non-Magento folks as well: https://twitter.com/PeterJaap/status/1481166381332807680?s=20

https://getcomposer.org/doc/articles/autoloader-optimization.md#optimization-level-2-a-authoritative-class-maps

This option says that if something is not found in the classmap, then it does not exist and the autoloader should not attempt to look on the filesystem according to PSR-4 rules.

This option makes the autoloader always return very quickly. On the flipside it also means that in case a class is generated at runtime for some reason, it will not be allowed to be autoloaded. If your project or any of your dependencies does that then you might experience "class not found" issues in production. Enable this with care.

I think "generating classes on runtime" is an edge case, were (as @peterjaap wrote on Twitter) "typos or wrong casing in class names vs file names." is not an edge case.

So what I think we should implement at least is an option to use optimization level 2/A or 2/B as per composer documentation and/or even set it as default.

Schrank avatar Jan 12 '22 08:01 Schrank

Make sense. Let’s add it before v7 release.

antonmedv avatar Jan 12 '22 08:01 antonmedv

Wanted to leave my vote for making option 2/A (authoritative class maps) the default :hand:

sprankhub avatar Jan 12 '22 08:01 sprankhub

When this is implemented, the respective option should also be added to the dump-autoload calls in the Magento 2 recipe:

https://github.com/deployphp/deployer/blob/fb6f2154ef36f03e4e4b0644206b06722ee09fd8/recipe/magento2.php#L75-L77

sprankhub avatar Jan 12 '22 08:01 sprankhub

Any volunteers to implement?

antonmedv avatar Jan 12 '22 11:01 antonmedv

@peterjaap @sprankhub If you have time, feel free. I'm currently drowning and don't see light.

Schrank avatar Jan 12 '22 12:01 Schrank

When this is implemented, the respective option should also be added to the --dump-autoload calls in the Magento 2 recipe:

https://github.com/deployphp/deployer/blob/fb6f2154ef36f03e4e4b0644206b06722ee09fd8/recipe/magento2.php#L75-L77

Checked the rest of the codebase, Magento 2 is the only recipe we need to edit!

Schrank avatar Jan 12 '22 12:01 Schrank

Checked the rest of the codebase, Magento 2 is the only recipe we need to edit!

You only checked for composer dump-autoload calls, right? However, this would also need to be added to composer install calls (and theoretically to composer update commands, but they should not be present here ^^).

Any volunteers to implement?

I could have a look when I have some time, but I will need some help with testing. Plus, it should be clear how exactly this should be implemented before we start: Will we make --classmap-authoritative the new default? Should it be possible to disable it? Or should it be the other way round, so that you need to enable it if you want to use it?

sprankhub avatar Jan 12 '22 12:01 sprankhub

Users already can override composer_options. Let’s just add it as new default.

antonmedv avatar Jan 12 '22 13:01 antonmedv

You only checked for composer dump-autoload calls, right? However, this would also need to be added to composer install calls (and theoretically to composer update commands, but they should not be present here ^^).

Nope, I searched for {{bin/composer}} ^^

https://twitter.com/IvanChepurnyi/status/1481279298057162755?s=20

Can we check wether apcu is installed? If it is, we better use it 🙈 And if not we use classmaps?

Schrank avatar Jan 12 '22 15:01 Schrank

Yeah the question is how willing @antonmedv is to potentially break other peoples' systems :stuck_out_tongue: --classmap-authoritative can indeed break systems. The issue with this is that it cannot be detected during the deployment, but only on the (potentially live) system after the deployment. Hence, maybe the solution suggested by @Schrank makes sense? If APCu is enabled, use it and if it is not, we use --classmap-authoritative (or nothing). What do you think, @antonmedv?

By the way, many in the Magento community used a deployer-based recipe by @jalogut before and there, --apcu is used:

https://github.com/jalogut/magento2-deployer-plus/blob/a8c4432a68a4ac442541f250577e6049abaed52b/recipe/magento_2_1/files.php#L17

sprankhub avatar Jan 14 '22 07:01 sprankhub

+1 for making --classmap-authoritative the default.

trsteel88 avatar Feb 03 '22 11:02 trsteel88