Cláudio Ribeiro
Cláudio Ribeiro

Reputation: 1699

Vagrant & Symfony 3.3: Failed to remove directory

Trying to set up a Symfony 3.3 environment with Flex. Currently, when trying to require some packages like the following:

composer req annotations security orm template asset validator

Everything goes well, except on cache:clear I'm getting the following errors:

!!    [Symfony\Component\Filesystem\Exception\IOException]                         
!!    Failed to remove directory "/home/vagrant/Code/multi-user-gallery-blog/var/  
!!    cache/loca~/pools": rmdir(/home/vagrant/Code/multi-user-gallery-blog/var/ca  
!!    che/loca~/pools): Directory not empty. 

I already tried to remove the folders manually, but those are generated automatically in the installation process, and then Symfony cannot remove them.

I'm running this on Vagrant Homestead.

Any idea on how can I solve this problem?

Upvotes: 11

Views: 2586

Answers (2)

Nicolas
Nicolas

Reputation: 601

See my comment to the accepted answer, here is the code:

public function getCacheDir()
{
    if (in_array($this->environment, ['dev', 'test'])) {
        return '/tmp/cache/' .  $this->environment;
    }

    return parent::getCacheDir();
}

public function getLogDir()
{
    if (in_array($this->environment, ['dev', 'test'])) {
        return '/var/log/symfony/logs';
    }

    return parent::getLogDir();
}

Upvotes: 1

Cy Rossignol
Cy Rossignol

Reputation: 16827

This is an issue with the way Vagrant/VirtualBox maps shared folders between the host and guest machines. In Symfony projects, the cache directory is typically placed within the project directory that gets synced from the host machine.

We can change the cache directory to use a location within the guest machine's filesystem to avoid these problems by adding an override method to the app/AppKernel.php class:

class AppKernel extends Kernel 
{
    ...
    public function getCacheDir() 
    {
        if ($this->environment === 'local') {
            return '/tmp/symfony/cache';
        }

        return parent::getCacheDir();
    }
}

The example above demonstrates how we can set a custom cache directory for local development environments while retaining the default behavior for production. /tmp/symfony/cache is just an example. We can choose a location anywhere on the guest machine's filesystem that the application has permission to write to. Replace 'local' with the name of the environment the project uses for development.

Upvotes: 7

Related Questions