Is SonarCloud compatible with Jinja2 imports?

Hi, we are using SonarCloud and SonarLint for our Django/Jinja2 framework. Jinja2 uses lots of imports to build web pages, so often a page on its own will not have compliant HTML. For example, we might import the header to a table from a different file if it is repeated a lot.

We therefore get hundreds of errors because SonarCloud and SonarLint only read the file in isolation. Is there any way for it to parse the HTML as if the imports were made?

What’s the usual best practice in this case?

Hi @rob101 and welcome to the community!

Just to be sure I understand correctly, you are using jinja import statements to import macros and you get issues on your HTML code because it is syntactically not correct without executing these macros. Is that it?

Could you share some code and the rules which raise False Positives?

Thanks.

Hi Nicolas, thanks!

Sorry, my bad, I mean include statements. For example:

<table>
{{ include('partials/_tablehead.html') }}
<tbody>
   <td>...

Results in SonarCloud flagging a “major bug” Web:S5256 as tables should have headers. It does not read that the _tablehead.html includes a header so the table would pass this test when compiled.

Does that help clarify the problem?

Hi @rob101,

Thank you for clarifying your problem.

Sadly the HTML analyzer can’t resolve includes.
I would recommend to disable the rules which raise False Positives in your case. You can do this by creating your own Quality Profile for HTML.

Could you provide the list of rules which raise False Positives because of include? I’ll create a False Positive ticket. We could detect that include is used and disable the rule automatically these rules.

Hi Nicolas, it would be anything that breaks an HTML structure; e.g. Web:S5256

Just to report that is still not fixed and if you include your templated header in a page, sonar complains. You need to make your own custom profile.