Scanner command used when applicable: scannerMode: ‘dotnet’
Languages of the repository: c#, TS, JS, json, xml
Projects are private
Error observed:
During the SonarQube Cloud Publish, we see:
[INFO] SonarQube Cloud: Analysis succeeded with warning: There are problems with file encoding in the source code. Please check the scanner logs for more details.
We also see this warning inside the SonarQube Cloud portal for the projects.
We think this might cause our projects to be scanned incorrectly, since they no longer have any lines of code.
Where do I find these logs or can you please look them up for us? So we can learn which files it is the scanner cannot read.
I might have found a log statement in the SonarQube Cloud Analysis that tells it cannot parse an ico file, trying to ignore that one now.
It is not a file we have added lately, I am fairly sure the scanner somehow found it back in august and have failed since, but we did not see that this warning appeared and stripped alot of code from our analysis.
Why would you ever let the SonarQube Cloud Publish task complete with success when it is reporting that there is a problem with the analysis?
You can see it has picked up the ignore of ico and I tried multiple for obj folder
Still I see in the logs afterwards:
WARN: Invalid character encountered in file /mnt/vss/_work/1/s/REDACTED/obj/Release/net8.0/apphost at line 1 for encoding UTF-8. Please fix file content or configure the encoding to be used using property 'sonar.sourceEncoding'.
WARN: Invalid character encountered in file /mnt/vss/_work/1/s/REDACTED/src/favicon.ico at line 1 for encoding UTF-8. Please fix file content or configure the encoding to be used using property 'sonar.sourceEncoding'.
Hey @Mikkel, normally excluding should get rid of the warning, yes. Could you share the full debug-level logs? I want to see the context in which these invalid character warnings occur.
Aha, so it sounds like the problem is that your files are being miscategorized as test instead of main code, and sonar.exclusions (Excluded sources: ...) applies only to main code. So the correct exclusion property would be sonar.test.exclusions.
However, what you should do is not to switch to sonar.test.exclusions, but to fix the miscategorization of main code as test. In .NET, this sometimes happens because certain dependencies implictly signal that a project is a test project instead of main. You can read more about it here. You should look for logs (debug level) evidencing the categorization of projects as test. The simplest fix is to just explicitly mark it as non-test.
Thank you, I found the nuget package which had the xunit reference by running the dotnet build with -v diag.
For the question if it is possible to detect warnings during the SonarQube Cloud Publish task, is there any recommendations? I would really not let a PR continue with warnings, since it can invalidate the Quality Gate completly if all code suddenly is marked as Test code.
It’s not really possible out of the box in the Publish task, although you could write your own check in the Azure pipeline. If you want to prevent the possibility of this happening, your best bet is to explicitly categorize the projects as test or non-test.