ux: [TwigComponent] Twig linter does not recognize anonymous components

When I run the following command

bin/console lint:twig templates/ --format=github

I got this message

Unknown component "common:atoms:badge:default". And no matching anonymous component template was found.

About this issue

  • Original URL
  • State: closed
  • Created 10 months ago
  • Comments: 16 (4 by maintainers)

Commits related to this issue

Most upvoted comments

Thank you @ker0x @aszenz & @ebitkov . All your messages helped me understand the problem(s).

There are in fact two different problems we need to address.

1/ Internal difference between function and tag

We do not check the component name during the compilation when called in {{ component('FooBarBaz') }} function, but we do when called in tag {% component 'FooBarBaz' %}

Self-closing “HTML” component are transformed into function call during prelex … that’s why you can type <twig:WhateverYouWant /> and it will always seem valid to the lint command.

On the contrary, every “tag” component is validated, so the componentFactory is called, allowing us to check the existence of the component. BUT…

2/ Runtime TemplateComponentFinder instanciation

During the lint, there is indeed a switch of Loader…

$realLoader = $this->twig->getLoader();
        try {
            $temporaryLoader = new ArrayLoader([$file => $template]);
            $this->twig->setLoader($temporaryLoader);
            $nodeTree = $this->twig->parse($this->twig->tokenize(new Source($template, $file)));
            $this->twig->compile($nodeTree);
            $this->twig->setLoader($realLoader);

https://github.com/symfony/symfony/blob/6.4/src/Symfony/Bridge/Twig/Command/LintCommand.php

So as the TemplateComponentFinder has not be instanciated before, when it call environment::getLoader(), the loader it uses then to check if a template exists… is the empty one set in the command… which does not know the template.

We have a couple of options there, but i’d like to remove the Environment dependency, and replace it with a LoaderInterface (the twig.loader service), removing this kind of runtime-effect.

WDYT ?

@smnandre yes, the component works fine. So far the error only occurs when linting the templates (not sure, if @ker0x and @aszenz refer to the same bug, tho).

Same here, components are working fine. It’s just on linting them that I get errors.

But it breaks my CI, which checks the templates before pushing to production. It’s not “system breaking”, but still annoying.

I totally agree and i’d love to take a look when i have time… In the meantime,if you have some time too, feel free to start one and i’ll join you (or anyone here with a bit of time) !

the “.html” part is as far as I know a “best-practice” thing and shouldn’t break anything. Because of that I actually didn’t noticed the “typo”, since it worked fine without 😃

Just to give you a heads-up, in 2.13, with the new anonymous mapping system (optional, but will be the main system in 3.0), only the “.html.twig” will be mapped

Having the same error. Made a quick symfony repo to reproduce it: https://github.com/ebitkov/symfony-ux-twig-component-lint-error

Running symfony console lint:twig throws an exception in ComponentFactory line 244.

Interestingly, if you leave the component empty (<twig:button/>) no error is thrown.