Working Directory at SonarScanner MSBuild isn't dynamic

  • SonarScanner.MSBuild 4.7.1

  • Scenario:

    • When you begin and create the first outputs of the scan (For example the build).
    • For some reason, before the unit testing, you need to change the folder location.
    • When you try to run the tests and end the scan, it fails (because the saved location it’s from the begin step).
  • What I did:

    • First was to find a command to change the working directory properly after the begin step and before the end step but I could not find it in any documentation.

    • Second was to change the working dir parameter inside the .sonarqube/conf to the new location and it doesn’t work very well because it worked just for a copied folder, if you move it to a new location or copy it to a new server in a different location, it’ll not work.

    • Third one was to change all the generated files with the old location of the working dir parameter to the new one and it returns:

      ERROR:Caused by: unexpected error while parsing protobuf file: .sonarqube\out\0\output-cs\file-metadata.pb ERROR: Caused by: Protocol message end-group tag did not match expected tag. ERROR: The SonarQube Scanner did not complete successfully
      

Do you guys know a better solution to this problem?

Thanks,
Schieck.

hi @RicardoS and welcome to our community forum!

Our recommendation is to run all commands from the same folder. What’s the specific scenario for needing to change the directory between analysis steps?

1 Like

Hi @Andrei_Epure, thanks!

In my case, in Gitlab Runners:

Job 1 -> Build the app in server x in folder a, begin the SonarQube analysis.
Job 2 -> Test the app in server y in folder b, end the SonarQube analysis.

Why I don’t build and test in the same job, solving the issue?
Because if the package changes in the middle of a pipeline is not true that the same build artifact is the same as the SonarQube regenerated artifact, creating a space for possible vulnerabilities in the deploy, also repeat the same step more times is not a good thing to do, for bigger projects repeat the build can be a lot of time. Some compliance rules are also in the game, forcing to run build and tests in separated servers.

I don’t know how hard it is or if it’s possible.

But, changing the paths to use the variable of working dir instead of hard coding it in all the configurations, maybe can solve this type of problem.

Hi @RicardoS,

You can’t change the directory without a significant effort on your side. The full absolute path is not only used in the analysis configuration but also in many other places when processing analysis data, metrics and issues.

To really do this, you’d need to:

  • Move all files to another server/path, including .sonarqube directory
  • Search through all files in .sonarqube directory and
    • Do a simple plain text replace for *.txt and *.xml files
    • Open each *.pb and *.ucfg Google Protobuf binary file, do a replace in all string fields and save the new protobuf content to original location

AFAIK it should be possible to read and write protobuf messages without knowledge of their structure, but I haven’t tried that. If you’ll do this, try to write an universal approach. The Protobuf structure of these internal files can change in the future.