Skip to content

Normalize the template name in FilesystemLoader::getSourceContext() - #4985

Merged
fabpot merged 2 commits into
twigphp:3.xfrom
KalimeroMK:filesystem-loader-normalize-source-name
Nov 18, 2026
Merged

fabpot merged 2 commits into
twigphp:3.xfrom
KalimeroMK:filesystem-loader-normalize-source-name

Conversation

@KalimeroMK

Copy link
Copy Markdown
Contributor

Fixes the root cause of symfony/symfony#61099: findTemplate() normalizes names for lookup and getCacheKey() is path-based, so the same template gets compiled through different spellings — e.g. App\Controller\HomeAction.html.twig at runtime vs App/Controller/HomeAction.html.twig from CLI cache warmup — but the Source kept the name as passed, making getTemplateName() (and _self) depend on which spelling compiled the template first. The Source now always carries the canonical forward-slash name. Includes a regression test.

findTemplate() normalizes names for lookup and getCacheKey() is
path-based, so the same template can be compiled through different
spellings (e.g. 'App\\Controller\\Foo.html.twig' at runtime vs
'App/Controller/Foo.html.twig' from CLI cache warmup). The Source kept
the name as passed, making getTemplateName() (and `_self`) depend on
which spelling compiled the template first. Normalize the name stored
in the Source so it is always the canonical forward-slash form.

Refs symfony/symfony#61099
}

return new Source(file_get_contents($path), $name, $path);
return new Source(file_get_contents($path), $this->normalizeName($name), $path);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I didn't see that there was already a normalize function. Let me try this.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had to copy the private normalizeName() function in my custom FilesystemLoader class, but it works.

@fabpot

fabpot commented Nov 18, 2026

Copy link
Copy Markdown
Contributor

Thank you @KalimeroMK.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

3 participants