Skip to content

[Dependency-Injection] OPcache preload-file not being generated #34897

Closed
@Kleinkind

Description

@Kleinkind

Hello :)

Symfony version(s) affected: >= 4.4.0

Description
We tried to enable opcache preloading on php 7.4 but the App_KernelProdContainer.preload.php file is not being generated. For our cache we use a custom directory outside of the project-directory. This is accomplished by overriding getCacheDir in the Kernel. If we use the default implementation of getCacheDir the generated cache contains the preload file.

How to reproduce

  • Override the Kernels getCacheDir-Function and return a custom cache-dir outside of the projects path.
public function getCacheDir(): string
{
	# different from kernel.project_dir
    return '/var/application/cache/'.$this->environment;
}
  • run bin/console cache:clear --env=prod
  • no preload-file present under /var/application/cache/prod/

Possible Solution
In the PhpDumper-Class the autoload-file for the project is determined in the getAutoloadFile-method. The autoloader-file is only returned if it's path matches the $this->targetDirRegex, which is based on the target directory for the cache. This regex would match any autoloader contained in two directories higher than the cache-dir itself. Therefore it works if your cache is stored in %PROJECT_DIR%/var/cache/prod, but not if it is stored somewhere else.
The determination of the autoloader should therefore also/only include any files found under kernel.project_dir.
If my assumptions are correct I would happily provide a pull-request and try to fix this.

Additional context

  • We use /var/application/cache for our cache and /var/www/symfony for the project itself.
  • We use docker's bind-mount to mount the project into the container (on dev, in production the code is contained in the image) and a standard docker volume to share the cache between different services.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions