Skip to content

[DependencyInjection] Decoupling cacheDir from buildDir breaks absolute paths in cacheDir #46176

Closed
@Sajito

Description

@Sajito

Symfony version(s) affected

5.4

Description

I'm trying to decouple the cache dir from the build dir. I want to keep the build dir in the project under var/cache, but move the runtime cache directory outside the project.

Since the parameter kernel.build_dir and the Kernel-method getBuildDir have been introduced, I just changed those methods.
The problem is, if the new cache dir and the project share the same root directory, the parameter kernel.cache_dir won't stay absolute, but instead be changed to be calculated dynamically.

My use case is, that I'm trying to pack a symfony console application as a phar. I want to keep the warmed cache inside the phar, while using a cache directory in /tmp path. This breaks during the build process using https://github.com/box-project/box, because box moves the files into an own temp-directory, before warming the cache.

I can think of some workarounds, but I still think this is a bug here.

How to reproduce

It's pretty simple to reproduce without a reproducer-repository.

Create a new skeleton-project somewhere in your home-directory, eg. /home/app/project. Minimal installation is enough.
Create a directory for the cache, eg. home/app/cache.

Change the Kernel in src/Kernel.php to look like this:

<?php

namespace App;

use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
use Symfony\Component\HttpKernel\Kernel as BaseKernel;

class Kernel extends BaseKernel
{
    use MicroKernelTrait;

    public function getCacheDir(): string
    {
        // hardcoded for simplicity reasons, make sure to change if your path is different
        return '/home/app/cache';
    }

    public function getBuildDir(): string
    {
        return parent::getCacheDir();
    }
}

Run bin/console cache:clear.

Now have a look at the App_KernelDevDebugContainer.php file in var/cache/dev/Container<hash>. Look into the method getDefaultParameters.

The parameter kernel.cache_dir will now be defined as (\dirname(__DIR__, 5).'/cache') instead of /home/app/cache.

Possible Solution

I found that this happens in the dependency injection component, specifically in this method:

https://github.com/symfony/dependency-injection/blob/dc25cfd99efda595f83a703e642b2db666b7adda/Dumper/PhpDumper.php#L2117-L2149

Unfortunately I really have no idea what's the best way to fix the issue. I think there should be a check somewhere, if the directory is actually inside the project directory, before using the dirname notation instead of the absolute path.

Additional Context

No response

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