Dotnet CLI SonarScanner is failing with dotnet 8.0 image

Hi,

I am facing the issue while scanning the ASP DOTNET Core 8 project using the dotnet cli global tool.

SonarQube Server: Enterprise EditionVersion 10.0
Sonar Scanner: dotnet-sonarscanner 6.2.0
Error:

Unhandled exception. System.InvalidOperationException: There was an error generating the XML document.
      ---> System.ArgumentException: ' ', hexadecimal value 0x1B, is an invalid character.
        at System.Xml.XmlEncodedRawTextWriter.WriteElementTextBlock(Char* pSrc, Char* pSrcEnd)
        at System.Xml.XmlEncodedRawTextWriter.WriteString(String text)
        at System.Xml.XmlWellFormedWriter.WriteString(String text)
        at System.Xml.Serialization.XmlSerializationWriter.WriteElementString(String localName, String ns, String value, XmlQualifiedName xsiType)
        at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterAnalysisConfig.Write6_AnalysisConfig(String n, String ns, AnalysisConfig o, Boolean isNullable, Boolean needType)
        at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterAnalysisConfig.Write7_AnalysisConfig(Object o)
        at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
        at System.Reflection.MethodBaseInvoker.InvokeDirectByRefWithFewArgs(Object obj, Span`1 copyOfArgs, BindingFlags invokeAttr)
        --- End of inner exception stack trace ---
        at System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter, Object o, XmlSerializerNamespaces namespaces, String encodingStyle, String id)
        at SonarScanner.MSBuild.Common.Serializer.Write[T](T model, TextWriter writer)
        at SonarScanner.MSBuild.Common.Serializer.SaveModel[T](T model, String fileName)
        at SonarScanner.MSBuild.Common.AnalysisConfig.Save(String fileName)
        at SonarScanner.MSBuild.PreProcessor.AnalysisConfigGenerator.GenerateFile(ProcessedArgs localSettings, BuildSettings buildSettings, Dictionary`2 additionalSettings, IDictionary`2 serverProperties, List`1 analyzersSettings, String sonarQubeVersion)
        at SonarScanner.MSBuild.PreProcessor.PreProcessor.DoExecute(ProcessedArgs localSettings)
        at SonarScanner.MSBuild.PreProcessor.PreProcessor.Execute(IEnumerable`1 args)
        at SonarScanner.MSBuild.BootstrapperClass.PreProcess()
        at SonarScanner.MSBuild.BootstrapperClass.Execute()
        at SonarScanner.MSBuild.Program.Execute(String[] args, ILogger logger)
        at SonarScanner.MSBuild.Program.Execute(String[] args)
        at SonarScanner.MSBuild.Program.Main(String[] args)
        at SonarScanner.MSBuild.Program.<Main>(String[] args)

How to resolve this issue?

Detailed log with verbose

 SonarScanner for MSBuild 6.2
11:53:11     Using the .NET Core version of the Scanner for MSBuild
11:53:11     Default properties file was found at /root/.nuget/packages/dotnet-sonarscanner/6.2.0/tools/netcoreapp3.1/any/SonarQube.Analysis.xml
11:53:11     Loading analysis properties from /root/.nuget/packages/dotnet-sonarscanner/6.2.0/tools/netcoreapp3.1/any/SonarQube.Analysis.xml
11:53:11     sonar.verbose=true was specified - setting the log verbosity to 'Debug'
11:53:11     Pre-processing started.
11:53:11     Preparing working directories...
11:53:11     Using environment variables to determine the download directory...
11:53:12     06:23:12.019  06:23:11.999  Loading analysis properties from /root/.nuget/packages/dotnet-sonarscanner/6.2.0/tools/netcoreapp3.1/any/SonarQube.Analysis.xml
11:53:12     06:23:12.019  06:23:12.019  sonar.verbose=true was specified - setting the log verbosity to 'Debug'
11:53:12     06:23:12.023  Updating build integration targets...
11:53:12     06:23:12.028  Installed SonarQube.Integration.ImportBefore.targets to /root/.local/share/Microsoft/MSBuild/4.0/Microsoft.Common.targets/ImportBefore
11:53:12     06:23:12.029  Installed SonarQube.Integration.ImportBefore.targets to /root/.local/share/Microsoft/MSBuild/10.0/Microsoft.Common.targets/ImportBefore
11:53:12     06:23:12.03  Installed SonarQube.Integration.ImportBefore.targets to /root/.local/share/Microsoft/MSBuild/11.0/Microsoft.Common.targets/ImportBefore
11:53:12     06:23:12.03  Installed SonarQube.Integration.ImportBefore.targets to /root/.local/share/Microsoft/MSBuild/12.0/Microsoft.Common.targets/ImportBefore
11:53:12     06:23:12.031  Installed SonarQube.Integration.ImportBefore.targets to /root/.local/share/Microsoft/MSBuild/14.0/Microsoft.Common.targets/ImportBefore
11:53:12     06:23:12.032  Installed SonarQube.Integration.ImportBefore.targets to /root/.local/share/Microsoft/MSBuild/15.0/Microsoft.Common.targets/ImportBefore
11:53:12     06:23:12.032  Installed SonarQube.Integration.ImportBefore.targets to /root/.local/share/Microsoft/MSBuild/Current/Microsoft.Common.targets/ImportBefore
11:53:12     06:23:12.033  Installed SonarQube.Integration.ImportBefore.targets to /root/Microsoft/MSBuild/15.0/Microsoft.Common.targets/ImportBefore
11:53:12     06:23:12.034  Installed SonarQube.Integration.ImportBefore.targets to /root/Microsoft/MSBuild/Current/Microsoft.Common.targets/ImportBefore
11:53:12     06:23:12.036  Installed SonarQube.Integration.targets to /app/buildagent/work/3cea31a8a2e2aa95/src/.sonarqube/bin/targets
11:53:12     06:23:12.036  Creating config and output folders...
11:53:12     06:23:12.037  Creating directory: /app/buildagent/work/3cea31a8a2e2aa95/src/.sonarqube/conf
11:53:12     06:23:12.037  Creating directory: /app/buildagent/work/3cea31a8a2e2aa95/src/.sonarqube/out
11:53:12     06:23:12.054  Fetching server version...
11:53:12     06:23:12.056  Downloading from https://sonarqube.devtools.zsservices.com/api/server/version...
11:53:12     06:23:12.557  Response received from https://sonarqube.devtools.zsservices.com/api/server/version...
11:53:12     06:23:12.56  Checking if the server version is supported...
11:53:12     06:23:12.565  Checking validity of server license...
11:53:12     06:23:12.565  Downloading from https://sonarqube.devtools.zsservices.com/api/editions/is_valid_license...
11:53:12     06:23:12.585  Response received from https://sonarqube.devtools.zsservices.com/api/editions/is_valid_license...
11:53:12     06:23:12.658  Fetching analysis configuration settings...
11:53:12     06:23:12.666  Fetching properties for project '#########'...
11:53:12     06:23:12.667  Downloading from https://#####/api/settings/values?component=######...
11:53:12     06:23:12.699  Response received from https://######/api/settings/values?component=######...
11:53:12     06:23:12.713  Downloading from https://######/api/languages/list...
11:53:12     06:23:12.725  Response received from https://######/api/languages/list...
11:53:12     06:23:12.733  Fetching quality profile for project '######'...
11:53:12     06:23:12.733  Downloading from https://######/api/qualityprofiles/search?project=######...
11:53:12     06:23:12.774  Response received from https://######/api/qualityprofiles/search?project=######...
11:53:12     06:23:12.78  Fetching rules for quality profile '######'...
11:53:12     06:23:12.78  Downloading from https://######/api/rules/search?f=repo,name,severity,lang,internalKey,templateKey,params,actives&ps=500&qprofile=######&p=1...
11:53:12     06:23:12.893  Response received from https://######/api/rules/search?f=repo,name,severity,lang,internalKey,templateKey,params,actives&ps=500&qprofile=######&p=1...
11:53:12     06:23:12.91  Local analyzer cache: /app/buildagent/temp/buildTmp/.sonarqube/resources
11:53:12     06:23:12.914  Writing Roslyn generated ruleset to /app/buildagent/work/3cea31a8a2e2aa95/src/.sonarqube/conf/Sonar-cs.ruleset...
11:53:12     06:23:12.935  Writing Roslyn generated ruleset to /app/buildagent/work/3cea31a8a2e2aa95/src/.sonarqube/conf/Sonar-cs-none.ruleset...
11:53:12     06:23:12.939  Provisioning analyzer assemblies for cs...
11:53:12     06:23:12.94  Installing required Roslyn analyzers...
11:53:12     06:23:12.94  Processing plugin: csharp version 8.55.0.65544
11:53:12     06:23:12.992  Cache miss: plugin files were not found in the local cache
11:53:12     06:23:12.993  Fetching resource for plugin: csharp, version 8.55.0.65544. Resource: SonarAnalyzer-8.55.0.65544.zip
11:53:12     06:23:12.993  Downloading SonarAnalyzer-8.55.0.65544.zip to /app/buildagent/temp/buildTmp/.sonarqube/resources/0
11:53:12     06:23:12.994  Downloading file to /app/buildagent/temp/buildTmp/.sonarqube/resources/0/SonarAnalyzer-8.55.0.65544.zip...
11:53:12     06:23:12.994  Downloading from https://######/static/csharp/SonarAnalyzer-8.55.0.65544.zip...
11:53:13     06:23:13.032  Response received from https://######/static/csharp/SonarAnalyzer-8.55.0.65544.zip...
11:53:13     06:23:13.041  Extracting files to /app/buildagent/temp/buildTmp/.sonarqube/resources/0...
11:53:13     06:23:13.087  Processing plugin: vbnet version 8.55.0.65544
11:53:13     06:23:13.102  Cache miss: plugin files were not found in the local cache
11:53:13     06:23:13.102  Fetching resource for plugin: vbnet, version 8.55.0.65544. Resource: SonarAnalyzer-8.55.0.65544.zip
11:53:13     06:23:13.102  Downloading SonarAnalyzer-8.55.0.65544.zip to /app/buildagent/temp/buildTmp/.sonarqube/resources/1
11:53:13     06:23:13.102  Downloading file to /app/buildagent/temp/buildTmp/.sonarqube/resources/1/SonarAnalyzer-8.55.0.65544.zip...
11:53:13     06:23:13.102  Downloading from https://######/static/vbnet/SonarAnalyzer-8.55.0.65544.zip...
11:53:13     06:23:13.114  Response received from https://######/static/vbnet/SonarAnalyzer-8.55.0.65544.zip...
11:53:13     06:23:13.115  Extracting files to /app/buildagent/temp/buildTmp/.sonarqube/resources/1...
11:53:13     06:23:13.136  Processing plugin: securitycsharpfrontend version 10.0.0.20234
11:53:13     06:23:13.138  Cache miss: plugin files were not found in the local cache
11:53:13     06:23:13.138  Fetching resource for plugin: securitycsharpfrontend, version 10.0.0.20234. Resource: SonarAnalyzer.Security-10.0.0.20234.zip
11:53:13     06:23:13.138  Downloading SonarAnalyzer.Security-10.0.0.20234.zip to /app/buildagent/temp/buildTmp/.sonarqube/resources/2
11:53:13     06:23:13.138  Downloading file to /app/buildagent/temp/buildTmp/.sonarqube/resources/2/SonarAnalyzer.Security-10.0.0.20234.zip...
11:53:13     06:23:13.14  Downloading from https://######/static/securitycsharpfrontend/SonarAnalyzer.Security-10.0.0.20234.zip...
11:53:13     06:23:13.147  Response received from https://######/static/securitycsharpfrontend/SonarAnalyzer.Security-10.0.0.20234.zip...
11:53:13     06:23:13.149  Extracting files to /app/buildagent/temp/buildTmp/.sonarqube/resources/2...
11:53:13     06:23:13.17  Writing Roslyn analyzer additional file to /app/buildagent/work/3cea31a8a2e2aa95/src/.sonarqube/conf/cs/SonarLint.xml...
11:53:13     06:23:13.171  Fetching quality profile for project '######'...
11:53:13     06:23:13.172  Downloading from https://######/api/qualityprofiles/search?project=######...
11:53:13     06:23:13.211  Response received from https://######/api/qualityprofiles/search?project=######...
11:53:13     06:23:13.213  Fetching rules for quality profile 'AYJ-KBq2XOwVvgx9jUmr'...
11:53:13     06:23:13.213  Downloading from https://######/api/rules/search?f=repo,name,severity,lang,internalKey,templateKey,params,actives&ps=500&qprofile=AYJ-KBq2XOwVvgx9jUmr&p=1...
11:53:13     06:23:13.265  Response received from https://######/api/rules/search?f=repo,name,severity,lang,internalKey,templateKey,params,actives&ps=500&qprofile=AYJ-KBq2XOwVvgx9jUmr&p=1...
11:53:13     06:23:13.271  Local analyzer cache: /app/buildagent/temp/buildTmp/.sonarqube/resources
11:53:13     06:23:13.272  Writing Roslyn generated ruleset to /app/buildagent/work/3cea31a8a2e2aa95/src/.sonarqube/conf/Sonar-vbnet.ruleset...
11:53:13     06:23:13.273  Writing Roslyn generated ruleset to /app/buildagent/work/3cea31a8a2e2aa95/src/.sonarqube/conf/Sonar-vbnet-none.ruleset...
11:53:13     06:23:13.274  Provisioning analyzer assemblies for vbnet...
11:53:13     06:23:13.274  Installing required Roslyn analyzers...
11:53:13     06:23:13.274  Processing plugin: csharp version 8.55.0.65544
11:53:13     06:23:13.275  Cache hit: using plugin files from /app/buildagent/temp/buildTmp/.sonarqube/resources/0
11:53:13     06:23:13.275  Processing plugin: vbnet version 8.55.0.65544
11:53:13     06:23:13.276  Cache hit: using plugin files from /app/buildagent/temp/buildTmp/.sonarqube/resources/1
11:53:13     06:23:13.277  Writing Roslyn analyzer additional file to /app/buildagent/work/3cea31a8a2e2aa95/src/.sonarqube/conf/vbnet/SonarLint.xml...
11:53:13     06:23:13.281  Processing analysis cache
11:53:13     06:23:13.284  Incremental PR analysis: Base branch parameter was not provided.
11:53:13     06:23:13.284  Cache data is empty. A full analysis will be performed.
Unhandled exception. System.InvalidOperationException: There was an error generating the XML document.
      ---> System.ArgumentException: ' ', hexadecimal value 0x1B, is an invalid character.
        at System.Xml.XmlEncodedRawTextWriter.WriteElementTextBlock(Char* pSrc, Char* pSrcEnd)
        at System.Xml.XmlEncodedRawTextWriter.WriteString(String text)
        at System.Xml.XmlWellFormedWriter.WriteString(String text)
        at System.Xml.Serialization.XmlSerializationWriter.WriteElementString(String localName, String ns, String value, XmlQualifiedName xsiType)
        at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterAnalysisConfig.Write6_AnalysisConfig(String n, String ns, AnalysisConfig o, Boolean isNullable, Boolean needType)
        at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterAnalysisConfig.Write7_AnalysisConfig(Object o)
        at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
        at System.Reflection.MethodBaseInvoker.InvokeDirectByRefWithFewArgs(Object obj, Span`1 copyOfArgs, BindingFlags invokeAttr)
        --- End of inner exception stack trace ---
        at System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter, Object o, XmlSerializerNamespaces namespaces, String encodingStyle, String id)
        at SonarScanner.MSBuild.Common.Serializer.Write[T](T model, TextWriter writer)
        at SonarScanner.MSBuild.Common.Serializer.SaveModel[T](T model, String fileName)
        at SonarScanner.MSBuild.Common.AnalysisConfig.Save(String fileName)
        at SonarScanner.MSBuild.PreProcessor.AnalysisConfigGenerator.GenerateFile(ProcessedArgs localSettings, BuildSettings buildSettings, Dictionary`2 additionalSettings, IDictionary`2 serverProperties, List`1 analyzersSettings, String sonarQubeVersion)
        at SonarScanner.MSBuild.PreProcessor.PreProcessor.DoExecute(ProcessedArgs localSettings)
        at SonarScanner.MSBuild.PreProcessor.PreProcessor.Execute(IEnumerable`1 args)
        at SonarScanner.MSBuild.BootstrapperClass.PreProcess()
        at SonarScanner.MSBuild.BootstrapperClass.Execute()
        at SonarScanner.MSBuild.Program.Execute(String[] args, ILogger logger)
        at SonarScanner.MSBuild.Program.Execute(String[] args)
        at SonarScanner.MSBuild.Program.Main(String[] args)
        at SonarScanner.MSBuild.Program.<Main>(String[] args)

Hey there.

Can you share the command you’re running to execute analysis, and any additional configuration?

0x1B represents an escape character that can’t be encoded in an XML file… so we have to figure out where that’s coming from :thinking:

Command:
dotnet sonarscanner begin /k:"zssd_oe_field_suggestions_AYKRKMHLnp_pTET0X7MN" /d:"sonar.token=%sonar.token%" /d:"sonar.host.url=%sonar.host%" /d:"sonar.branch.name=%vcsroot.branch%" /d:sonar.cs.opencover.reportsPaths=./results/coverage/**/coverage.opencover.xml /v:"%productVersion%" /d:sonar.verbose=true
The same command works fine on the Windows machine and when I switch the solution back to dotnet 6.0. There is no change in command in all scenarios.

Hello @Mukul_Garg

Can you provide us the SonarQubeAnalysisConfig.xml file generated during the successful run with dotnet 6.0?
You can find it in the folder where you ran the scanner in the .sonarqube\conf\SonarQubeAnalysisConfig.xml directory.
We write many values from many sources into that XML file, and one of them seems to have an invalid character. Knowing where that value originates from would be interesting so we can handle such a case properly.

Hi Martin,

the SonarQubeAnalysisConfig.xml is attached here.
SonarQubeAnalysisConfig.txt (18.8 KB)

Hi, @Mukul_Garg,

Thank you for providing the file. I looked at it but couldn’t find anything unusual in it. It also contains no U+001B <Escape> (ESC) Unicode Character characters.
I was able to reproduce a similar error in a test project. The error is raised if a string property contains the 0x1B character:

var model = new MyClass()
{
    Test = "\u001B",
};
var serializer = new XmlSerializer(typeof(MyClass));
serializer.Serialize(xmlWriter, model); // Exception raised here

Unfortunately, the error details do not include details about the specific property that caused the error (You can not tell from the exception that Test is the property with the malformed string).

As long as we can not tell which of the properties is causing this, it is hard to decide how to handle this case correctly. Do you know what may cause an 0x1B to appear in any of the settings in SonraQubeAnalysisConfig in your environment?

This is the same difficulty I am also facing. The error does not contain the source detail. I am also not able to find the 0x1B character in any configuration values. The same configuration is working fine with Windows 10. But when I am running it on TeamCity under dotnet 8.0 official docker image, it is failing.

I see. Without a reproducer on our side it is hard to decide what to do in this case. We write a lot of properties from different sources into the file and it is consumed by different components during a scan. We can not easily change how all the strings are stored there (e.g. by base64 encoding them or by removing invalid XML characters). This may cause problems in all kinds of our subsystems.
I’m just speculating, but one possibility could be problems with the encoding of files. Do you provide any custom configuration files to the scanner either via comand line parameter or via SonarQube settings? If so, it may use Windows-1252 - Wikipedia encoding which might not be properly detected on your TeamCity server (and might be interpreted as UTF-8).