-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
BC break regarding relative paths in 1.25 due to realpath changes (#2127) #2145
Comments
The reproducer is not really interesting as it is just the result of the change. Where is it a problem for you? |
Actually the goal of the reproducer was to show the difference of behavior between 1.24 and 1.25 but it's not interesting actually. |
Also got affected by this BC after updating from Twig 1.24.1 to 1.25. |
AFAIK, Twig always documented that it expects an absolute path in the loader. (Btw, relative paths are fragile as they are relative to the current working dir, and it is easy to run the same codebase from different directories) |
I ran into this issue when loading twig files from a symfony console phar. |
#2149 is 70% duplicate. |
Inside the Drupal Console project we have the same issue inside a phar, as @kimpepper reported. We had to return to 1.23.1 and all works as expected. |
@stof I don't see any mention of relative paths or absolute paths in the loader (see e.g. http://twig.sensiolabs.org/doc/api.html#twig-loader-filesystem). Only the cache seems to explicitly ask for absolute paths. |
I don't know if this is related.. but since the update to 1.25, I am now getting this as soon as my app is hit Fatal error: Call to undefined method Twig_Node_Module::setFilename() in /vagrant/vendor/twig/twig/lib/Twig/Compiler.php on line 88 |
I have the same problem.
And my
My
Finally my
The error is:
I guest that the problem is in the If you disable the call of method
the template rendered. |
@fabpot the issue is that we always expected Twig_Loader_Filesystem to receive absolute paths when working on your refactoring and reviewing it, but the user base does not agree. |
@Omerta the workaround for now is to configure your loader with absolute paths (which is way more reliable than relying on the cwd when your code is called btw): $loader = new Twig_Loader_Filesystem(array(__DIR__.'/templates/', __DIR__.'/../shared/templates/'));
$twig = new Twig_Environment($loader);
$template = $twig->loadTemplate('form.html'); On a side note, you don't need to call |
@stof Converting relative paths to absolute ones is not a good idea. Let me explain why and some ideas on how to fix this issue. The main problem is that we are using the template path for two different things:
I've worked on a patch to fix that but it's somehow convoluted. But I'm wondering why we need the path for the cache key, it looks like using a normalized template name would be more than enough. Something like this: public function getCacheKey($name)
{
// will throw an exception if there is an issue finding this template
$this->findTemplate($name);
// this is unique and should be good as a cache key
return $this->normalizeName($name);
} Any drawback? |
@fabpot using the file name means that the cache is invalidated if you overwrite the template in a new folder (as Twig_Loader_Filesystem supports having multiple folders). So we need to involve the base folder being used in some way. Normalizing alone would break BC. |
@stof Here is my WIP: https://github.com/twigphp/Twig/compare/1.x...fabpot:loader-relative-paths?expand=1 What do you think? |
looks good, except that you should be careful about windows support (where the path separator in realpath will not be |
phars are taken into account as if |
This PR was merged into the 1.x branch. Discussion ---------- fixed the filesystem loader with relative paths closes #2145 TODO - [x] document the new `$rootPath` option - [x] add test with a `$rootPath` different from `getcwd()` - [x] test the cache key does not vary when `$rootPath` changes Commits ------- a343c92 fixed the filesystem loader with relative paths
The 1.25 version does not allow relative loading paths ($this->paths) in the filesystem loader anymore.
The new
normalizePath()
method is more strict thanrealpath()
as it does not allow relative paths (any extra../
will be ignored). It means that if you have a FilesystemLoader configured with a relative directory in the paths list, the result offindTemplate()
will be different than in the 1.24 version.The result will also be wrong because the
is_file()
check (https://github.com/twigphp/Twig/blob/1.x/lib/Twig/Loader/Filesystem.php#L200) will succeed but the returned path won't lead to an existing file, as a result a warning will be thrown bygetSource()
.Reproducer (expecting true with both 1.24 and 1.25):
The text was updated successfully, but these errors were encountered: