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

feat: make sure the APCU autoloader prefix is deterministic when a composer.lock file is available #11964

Closed
wants to merge 1 commit into from

Conversation

drupol
Copy link
Contributor

@drupol drupol commented May 5, 2024

Hello!

Context PR: #11663

This PR make sure the APCU prefix is deterministic only when 2 conditions are met:

  1. When a composer.lock file is available
  2. When there's no --apcu-autoloader-prefix command line option set.

It will be predictable by reusing the content-hash key from composer.lock file.

@drupol drupol marked this pull request as ready for review May 5, 2024 07:18
@drupol drupol force-pushed the deterministic-apcu-autoloader branch 3 times, most recently from 914b3bd to acbdcb5 Compare May 5, 2024 07:36
@drupol drupol force-pushed the deterministic-apcu-autoloader branch from acbdcb5 to 675e57c Compare May 5, 2024 07:42
@fredden
Copy link
Contributor

fredden commented May 6, 2024

This sounds unsafe. Please can you test and confirm this is safe when classes managed by the application, but auto-loaded by Composer are modified / added / removed? For example, in this repository, dump out an auto-loader (php bin/composer install with the relevant flags), then run some Composer command (eg, composer show) to prime the APCu cache, then modify a PHP file in src/Composer/ (eg, src/Composer/Factory.php) and dump out an auto-loader (php bin/composer install with relevant flags) -- this simulates a "deployment", then run the same Composer command (eg, composer show) and witness if the cached or modified file runs.

composer/composer.json

Lines 76 to 80 in 675e57c

"autoload": {
"psr-4": {
"Composer\\": "src/Composer/"
}
},

If you want to make the autoloader prefix deterministic (instead of random as it currently is), then you'll need to ensure that you're hashing more than just the contents of vendor/, but also taking the autoload/autoload-dev sections in the root into account.

@drupol
Copy link
Contributor Author

drupol commented May 6, 2024

This sounds unsafe.

Can you please develop further? What do you mean and why using that is unsafe?
For example, compared to any other string manually set through --apcu-autoloader-prefix why would this would not be valid?

@fredden
Copy link
Contributor

fredden commented May 6, 2024

Unsafe in that I think this may result in running old code from memory (ACPu) when new code has been deployed on disk.

@drupol
Copy link
Contributor Author

drupol commented May 6, 2024

Please can you test and confirm this is safe when classes managed by the application, but auto-loaded by Composer are modified / added / removed? For example, in this repository, dump out an auto-loader (php bin/composer install with the relevant flags), then run some Composer command (eg, composer show) to prime the APCu cache, then modify a PHP file in src/Composer/ (eg, src/Composer/Factory.php) and dump out an auto-loader (php bin/composer install with relevant flags) -- this simulates a "deployment", then run the same Composer command (eg, composer show) and witness if the cached or modified file runs.

So you're asking me to modify a file from vendor/ after running composer install ? This is unlikely to happen in any scenario, why would one do that?

@fredden
Copy link
Contributor

fredden commented May 6, 2024

So you're asking me to modify a file from vendor/ after running composer install ? This is unlikely to happen in any scenario, why would one do that?

No, I'm saying that there are files in the project itself other than in vendor/ (eg, src/ or app/ or tests/) which are auto-loaded via Composer. Only the contents of vendor/ are "protected" by the content-hash.

@fredden
Copy link
Contributor

fredden commented May 7, 2024

Only the contents of vendor/ are "protected" by the content-hash.

Actually, even this is wrong. The content-hash only changes when (the value of certain keys within) composer.json changes. Running composer update to upgrade packages does not modify the content-hash.

The steps/scenario I described above should also apply to changing package versions via composer update (eg composer update --prefer-lowest).

If you want a deterministic value, I think you'll need to generate one for this use case specifically.

@Seldaek
Copy link
Member

Seldaek commented May 27, 2024

IMO if you use --apcu and want reproducible builds then you have to specify a meaningfully-unique prefix yourself. Otherwise it is bound to end up badly if we just reuse the lock hash. By default we do not include the apcu prefix so I think this is acceptable.

@Seldaek Seldaek closed this May 27, 2024
@drupol drupol deleted the deterministic-apcu-autoloader branch May 27, 2024 13:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants