[WARNING] Failed to process thymeleaf template x.html. It will be ignored. Cause is logged in DEBUG

Hi,

Sonar can’t really process any Thymeleaf templates.
The only related issue that I could find was this one: Thymeleaf errors appears

Sonar generates a Warning for all of our Thymeleaf templates.
Relevant part: ParseException: Cannot evaluate expression "#{titlePrefix}".

The templates are designed to receive information from the external environment, so I’m unsure what type of Thymeleaf templates Sonar typically processes.

[DEBUG] src/main/resources/templates/done.html recognized as thymeleaf template
[WARNING] Failed to process thymeleaf template src/main/resources/templates/done.html. It will be ignored. Cause is logged in DEBUG level.
[DEBUG] Exception when processing src/main/resources/templates/done.html
org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: "<!doctype html>
...
	at org.thymeleaf.templateparser.markup.AbstractMarkupTemplateParser.parse(na:554)
	at org.thymeleaf.templateparser.markup.AbstractMarkupTemplateParser.parseString(na:2242)
	at com.sonar.security.frontend.java.C.A.A(na:393)
	at com.sonar.security.frontend.java.C.A.A(na:563)
	at com.sonar.security.frontend.java.C.C.processTemplate(na:1293)
	at com.sonar.security.frontend.java.C.C.execute(na:350)
	at org.sonar.scanner.sensor.AbstractSensorWrapper.analyse(AbstractSensorWrapper.java:67)
	at org.sonar.scanner.sensor.AbstractSensorsExecutor.executeSensors(AbstractSensorsExecutor.java:40)
	at org.sonar.scanner.sensor.ModuleSensorsExecutor.lambda$execute$1(ModuleSensorsExecutor.java:44)
	at org.sonar.scanner.sensor.ModuleSensorsExecutor.withModuleStrategy(ModuleSensorsExecutor.java:62)
	at org.sonar.scanner.sensor.ModuleSensorsExecutor.execute(ModuleSensorsExecutor.java:44)
	at org.sonar.scanner.scan.SpringModuleScanContainer.doAfterStart(SpringModuleScanContainer.java:74)
	at org.sonar.scanner.spring.SpringComponentContainer.startComponents(SpringComponentContainer.java:206)
	at org.sonar.scanner.spring.SpringComponentContainer.execute(SpringComponentContainer.java:185)
	at org.sonar.scanner.scan.SpringProjectScanContainer.scan(SpringProjectScanContainer.java:219)
	at org.sonar.scanner.scan.SpringProjectScanContainer.scanRecursively(SpringProjectScanContainer.java:214)
	at org.sonar.scanner.scan.SpringProjectScanContainer.doAfterStart(SpringProjectScanContainer.java:182)
	at org.sonar.scanner.spring.SpringComponentContainer.startComponents(SpringComponentContainer.java:206)
	at org.sonar.scanner.spring.SpringComponentContainer.execute(SpringComponentContainer.java:185)
	at org.sonar.scanner.bootstrap.SpringScannerContainer.doAfterStart(SpringScannerContainer.java:509)
	at org.sonar.scanner.bootstrap.SpringScannerContainer.startComponents(SpringScannerContainer.java:443)
	at org.sonar.scanner.spring.SpringComponentContainer.execute(SpringComponentContainer.java:185)
	at org.sonar.scanner.bootstrap.SpringGlobalContainer.doAfterStart(SpringGlobalContainer.java:149)
	at org.sonar.scanner.spring.SpringComponentContainer.startComponents(SpringComponentContainer.java:206)
	at org.sonar.scanner.spring.SpringComponentContainer.execute(SpringComponentContainer.java:185)
	at org.sonar.scanner.bootstrap.ScannerMain.runScannerEngine(ScannerMain.java:143)
	at org.sonar.scanner.bootstrap.ScannerMain.run(ScannerMain.java:58)
	at org.sonar.scanner.bootstrap.ScannerMain.main(ScannerMain.java:42)
Caused by: org.attoparser.ParseException: Cannot evaluate expression "#{titlePrefix}". Message externalization expressions can only be evaluated in a template-processing environment (as a part of an in-template expression) where processing context is an implementation of class java.lang.Class, which it isn't (com.sonar.security.frontend.java.C.A$_B)
	at org.attoparser.MarkupParser.parseDocument(na:1604)
	at org.attoparser.MarkupParser.parse(na:1731)
	at org.thymeleaf.templateparser.markup.AbstractMarkupTemplateParser.parse(na:3548)
	... 27 common frames omitted
Caused by: org.thymeleaf.exceptions.TemplateProcessingException: Cannot evaluate expression "#{titlePrefix}". Message externalization expressions can only be evaluated in a template-processing environment (as a part of an in-template expression) where processing context is an implementation of class java.lang.Class, which it isn't (com.sonar.security.frontend.java.C.A$_B)
	at org.thymeleaf.standard.expression.MessageExpression.executeMessageExpression(na:2800)
	at org.thymeleaf.standard.expression.SimpleExpression.executeSimple(na:92)
	at org.thymeleaf.standard.expression.Expression.execute(na:2145)
	at org.thymeleaf.standard.expression.AdditionExpression.executeAddition(na:3421)
	at org.thymeleaf.standard.expression.ComplexExpression.executeComplex(na:196)
	at org.thymeleaf.standard.expression.Expression.execute(na:1507)
	at org.thymeleaf.standard.expression.AdditionExpression.executeAddition(na:3421)
	at org.thymeleaf.standard.expression.ComplexExpression.executeComplex(na:196)
	at org.thymeleaf.standard.expression.Expression.execute(na:1507)
	at org.thymeleaf.standard.expression.Expression.execute(na:1875)
	at org.thymeleaf.standard.expression.Expression.execute(na:1728)
	at com.sonar.security.frontend.java.C.A$_A.A(na:1753)
	at com.sonar.security.frontend.java.C.A$_A.E(na:307)
	at java.base/java.util.Optional.ifPresent(Unknown Source)
	at com.sonar.security.frontend.java.C.A$_A.B(na:243)
	at com.sonar.security.frontend.java.C.A$_A.handleOpenElement(na:1875)
	at org.thymeleaf.engine.TemplateHandlerAdapterMarkupHandler.handleOpenElementEnd(na:3496)
	at org.thymeleaf.templateparser.markup.InlinedOutputExpressionMarkupHandler$InlineMarkupAdapterPreProcessorHandler.handleOpenElementEnd(na:344)
	at org.thymeleaf.standard.inline.OutputExpressionInlinePreProcessorHandler.handleOpenElementEnd(na:2191)
	at org.thymeleaf.templateparser.markup.InlinedOutputExpressionMarkupHandler.handleOpenElementEnd(na:1765)
	at org.attoparser.HtmlElement.handleOpenElementEnd(na:687)
	at org.attoparser.HtmlMarkupHandler.handleOpenElementEnd(na:1295)
	at org.attoparser.MarkupEventProcessorHandler.handleOpenElementEnd(na:1590)
	at org.attoparser.ParsingElementMarkupUtil.parseOpenElement(na:1248)
	at org.attoparser.MarkupParser.parseBuffer(na:3251)
	at org.attoparser.MarkupParser.parseDocument(na:1031)
	... 29 common frames omitted

Example template:

<!doctype html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">

<head>
  <title th:text="#{titlePrefix}">App</title>
...

Q:

  • Is this normal?
  • Do you recommend to exclude all Thyemleaf templates from scanning?
  • If there is another scanner that can process these files, how to suppress the warnings?

Best regards,
Andrei

Hey @tehnicaorg , and thank you for the report!

In general, we do support Thymeleaf templates, incuding externalization expressions such as #{titlePrefix}.

I suspect that you found a bug somewhere in our parsing engine, which causes the analysis to skip that file and log a warning. In order to investigate, a reproducer would be helpful.

I did test the standalone snippet you provided:

<!doctype html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">

<head>
  <title th:text="#{titlePrefix}">App</title>
...

However, this template parses just fine (provided I close the tags, of course) and does not log a warning in my tests, so I cannot reproduce the problem from this code.

Please note: The message Cannot evaluate expression "#{titlePrefix}" should be taken with a grain of salt. It could be that the parsing failed elsewhere, then the error cascaded up and ultimately produced a misleading error message.

Would you be able to share the file done.html with me? I will send you a private message so you do not have to post it publicly.

To answer your questions:

No, this should not happen.

No, Thymeleaf templates are supported and do not need to be generally excluded. That said, if there is a parsing problem for some specific Thymeleaf template files, you could temporarily exclude those specific files from analysis to suppress the warnings until we fix the underlying root cause. Due to the parsing problem, these files are ignored anyway, so excluding them at least gets rid of the warnings.

There is no other scanner that can process the files. As for suppressing the warnings, see above.

Thank you again for the report, it is very much appreciated and helps us improve! :folded_hands:

Thanks for your reply, @Malte and sorry for being misleading with my test case.
I’ve reproduced now the issue with a minimal case:

<!doctype html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
  <title th:text="#{titlePrefix} + 'x'"></title>
</head>
<body>
</body>
</html>

Thank you, Andrei, that’s perfect!

I will investigate this and come back to you as soon as possible. :slight_smile:

Hello @tehnicaorg ,

Thank you for reporting this and for taking the time to put together this clear, minimal reproducer! It made investigating the issue quick and straightforward. :folded_hands:

I can confirm this is indeed a bug on our side: it’s caused by how we handle the concatenation, which currently triggers the exception you ran into.

I’ve created an internal ticket to track and fix it, and I’ll follow up with you as soon as it’s implemented and released. :blush:

Thanks again for helping us improve!