twigphp / twigphp/Twig

Twig cache contains absolute paths

Open
#3,218 1 comment 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
PHP
Stars
8.4k
Forks
1.3k
Avg merge
1d 13h
Merged PRs (30d)
37

Description

I noticed this in a Symfony app but while trying to see where the error was, I noticed that it's actually Twig core behaviour.
Yet reproducing it is probably easier using Symfony Flex:

  1. composer create-project symfony/skeleton . 4.4.*
  2. composer req twig
  3. APP_ENV=prod ./bin/console cache:warmup

You'll see that the cache contains files along the following lines:

<?php

use Twig\Environment;
use Twig\Error\LoaderError;
use Twig\Error\RuntimeError;
use Twig\Extension\SandboxExtension;
use Twig\Markup;
use Twig\Sandbox\SecurityError;
use Twig\Sandbox\SecurityNotAllowedTagError;
use Twig\Sandbox\SecurityNotAllowedFilterError;
use Twig\Sandbox\SecurityNotAllowedFunctionError;
use Twig\Source;
use Twig\Template;

/* @Framework/Form/time_widget.html.php */
class __TwigTemplate_884348209061bf9effea81fda27c890a9a31886d0f62de99211558053cc7ec90 extends \Twig\Template
{
    private $source;
    private $macros = [];

   [...]

    public function getSourceContext()
    {
        return new Source("", "@Framework/Form/time_widget.html.php", "/absolute/path/to/project/vendor/symfony/framework-bundle/Resources/views/Form/time_widget.html.php");
    }
}

Actually this is the only place where I noticed absolute paths in a typical Symfony cache which causes issues if e.g. you have a chrooted SSH user and a different path in the web process.

Not sure how to fix it though. The FilesystemLoader seems to provide absolute file paths by contract.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the issue with the Symfony Flex commands in the report and inspect the generated cache files. Start by reading the FilesystemLoader contract and tracing how its absolute path reaches Source in the cached template. Done means agreeing on and testing a cache-path behavior that works across the different process paths described.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.