# Wrong "Base Dir" for SonarCloud analyze task (randomly)

**URL:** <https://community.sonarsource.com/t/wrong-base-dir-for-sonarcloud-analyze-task-randomly/110308>\
**Category:** SonarQube Cloud\
**Tags:** dotnet, coverage\
**Created:** [March 4, 2024, 1:39pm UTC](https://community.sonarsource.com/t/wrong-base-dir-for-sonarcloud-analyze-task-randomly/110308 "2024-03-04T13:39:01Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ChrisBets](https://avatars.discourse-cdn.com/v4/letter/c/ed655f/32.png) [@ChrisBets](https://community.sonarsource.com/u/ChrisBets)\
**Post date:** [March 4, 2024, 1:39pm UTC](https://community.sonarsource.com/t/wrong-base-dir-for-sonarcloud-analyze-task-randomly/110308/1 "2024-03-04T13:39:01Z")

</div>

- ALM: Azure DevOps
- CI system: Azure DevOps
- Scanner command:

```auto
  - task: SonarCloudPrepare@1
    displayName: Prepare SonarCloud analysis configuration
    inputs:
      SonarCloud: SonarCloud
      organization: $(sonarOrganization)
      scannerMode: MSBuild
      projectKey: $(sonarOrganization)_$(sonarProject)
      projectName: $(sonarProject)
      extraproperties:
        sonar.exclusions=src/**/Resources/*
        sonar.cpd.exclusions= **/Migrations/*,src/** Persistence/Migrations/*,src/*.DataSeeder/SeederImplementations/**
        sonar.coverage.exclusions= **/Migrations/*,src/*.DataSeeder/** ,src/*.Api/Program.cs,src/*.Domain/Exceptions/ **Exception.cs,src/** Persistence/Migrations/*,src/**/Persistence/Migrations/*
        sonar.coverageReportPaths="$(Build.SourcesDirectory)/coveragereport/SonarQube.xml"

  // build

  - task: SonarCloudAnalyze@1
    condition: succeededOrFailed()
    displayName: SonarCloud analyze

  - task: SonarCloudPublish@1
    displayName: SonarCloud publish Results
    condition: succeededOrFailed()
    inputs:
      pollingTimeoutSec: '300'

```

- Languages: C#
- Error observed:

Sometimes we get a lot of pull requestion annotations from Sonar for some of our repositories. With “a lot” I mean all build warnings are commented, instead of just for “new code”.

Also the comments are created on the wrong path:  
“This file no longer exists in the latest pull request changes. It may have been moved or deleted.”  
It has a prefix `/azp/_work/1/s/`, that shouldn’t be there (which is coming from the Azure Pipeline agent)

We only observed this issue in our Azure Function repos.

~~Today I could narrow it done one step more: it seems to be related to a specific nuget package reference.  
I had at least three repositories with the following change:  
 ~~

 ![image](https://europe1.discourse-cdn.com/sonarsource/uploads/sonarcommunity/original/3X/6/5/650f05a3719b5467e9bcfff18865c1a2400bf0fc.png)~~  
Resulting in a lot of comments  
  ![image](https://europe1.discourse-cdn.com/sonarsource/uploads/sonarcommunity/original/3X/7/9/794506c542debde4854ba8f0a041483524eeda9f.png)~~

~~Other PRs for those repositories didn’t have this issue.~~  
EDIT: today we had other PRs with the same issue. It seems to be random.

I could even find the relevant part in the SonarCloud Analyze log:  
PR with Issue:

```auto
INFO: Base dir: /

```

PR with no issue:

```auto
INFO: Base dir: /azp/_work/1/s

```

The PR build with issue also has the following warning

```auto
WARN: SCM provider autodetection failed. Please use "sonar.scm.provider" to define SCM of your project, or disable the SCM Sensor in the project settings.

```

which makes sense, as the base path is already not correct, so it can find the correct source folder.

The PR build without the issue therefore has some of log statements, that are not in the log for the PR build with the issue like:

```auto
INFO: 7 files ignored because of scm ignore settings
...
INFO: SCM collecting changed files in the branch
INFO: SCM collecting changed files in the branch (done) | time=148ms

```

which again makes sense and therefore resulting into the issue described above.

Either this is dependent on the build agent (don’t think so), or this is an issue in Sonar.

Any ideas?

---

<div class="post-metadata">

**Author:** ![ganncamp](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/ganncamp/32/1261_2.png) [@ganncamp](https://community.sonarsource.com/u/ganncamp)\
**Post date:** [March 5, 2024, 3:19pm UTC](https://community.sonarsource.com/t/wrong-base-dir-for-sonarcloud-analyze-task-randomly/110308/2 "2024-03-05T15:19:18Z")

</div>

Hi,

There seems to be a lot going on here. I think you’ve correctly identified with problem with the base directory identification and how that impacts SCM data collection and thus leads to old issues showing up in PRs.

So then I loop back to the beginning of your post, where you list (thank you) a project-specific pipeline(?) and then seem to indicate that you’re having the problem with multiple repositories.

So… I think we need to narrow this down.

> [@ChrisBets](#):
>
> for some of our repositories.  
> …  
> observed this issue in our Azure Function repos.  
> …  
> It seems to be random.

You see this

- Only Azure Function repos (Is Azure Function a project type?)
- Intermittently? Or is it reliable for certain projects? Certain projects when they land on certain build agents?
- Is the pipeline snippet you presented representative across all relevant projects?
- What happens before analysis? Is there any `cd`ing?

To answer your question, I don’t think this is on the analysis side. The SonarScanner is just acting on the data it’s been given.

&nbsp;  
Ann

---

<div class="post-metadata">

**Author:** ![ChrisBets](https://avatars.discourse-cdn.com/v4/letter/c/ed655f/32.png) [@ChrisBets](https://community.sonarsource.com/u/ChrisBets)\
**Post date:** [March 13, 2024, 2:07pm UTC](https://community.sonarsource.com/t/wrong-base-dir-for-sonarcloud-analyze-task-randomly/110308/3 "2024-03-13T14:07:06Z")

</div>

Hey @ganncamp !

Thanks for the response!  
Yes you are right, there is a lot of going on… unfortunately, it is quite hard to break it out from our complex system.

To answer your questions:

- Only Azure Function repos (Is Azure Function a project type?)
  - We have different kind of repos, mostly of them are either “services” (e. g. normal [ASP.NET](http://ASP.NET) Core Web APIs) or “functions” (We used the default template for those). They use the normal Snippet Microsoft.NET.Sdk, but the have some special properties and Visual Studio has a dedicated icon for them. So far, we experienced this issue only in repositories that we consider functions.

- Intermittently? Or is it reliable for certain projects? Certain projects when they land on certain build agents?
  - So far, it seems to be random. Sometimes it happens, sometimes it doesn’t. I already thought about the build agents too, they are however scaled jobs that run on our k8s cluster and they use all the same base image. Afaik, they are single use jobs.

- Is the pipeline snippet you presented representative across all relevant projects?
  - yes, however the build steps vary between service and functions (we use templates)

- What happens before analysis? Is there any `cd`ing?
  - this is a good hint, I’ll check. While it is a possibility, I’m still puzzled, why sometimes it works and sometimes it doesn’t. If it would be any `cd`ing, it should happen all the time.

Can you maybe tell me how the `SonarCloudAnalyze` gets the Base dir? Is there any way to force the value?

---

<div class="post-metadata">

**Author:** ![ganncamp](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/ganncamp/32/1261_2.png) [@ganncamp](https://community.sonarsource.com/u/ganncamp)\
**Post date:** [March 13, 2024, 3:56pm UTC](https://community.sonarsource.com/t/wrong-base-dir-for-sonarcloud-analyze-task-randomly/110308/4 "2024-03-13T15:56:42Z")

</div>

Hi,

`sonar.projectBaseDir` [defaults](https://docs.sonarsource.com/sonarcloud/advanced-setup/analysis-parameters/#analysis-scope) to the directory analysis is triggered from, but may be defaulted from the build for Maven, Gradle, and .NET.

I can also be explicitly overridden, which is not usually a good idea, but maybe what you need to do here.

&nbsp;  
Ann

---

<div class="post-metadata">

**Author:** ![ganncamp](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/ganncamp/32/1261_2.png) [@ganncamp](https://community.sonarsource.com/u/ganncamp)\
**Post date:** [March 15, 2024, 12:38pm UTC](https://community.sonarsource.com/t/wrong-base-dir-for-sonarcloud-analyze-task-randomly/110308/5 "2024-03-15T12:38:31Z")

</div>

Hi again,

I’ve just learned of this issue, which is probably what you’re facing:

> <https://github.com/SonarSource/sonar-scanner-msbuild/issues/1309>
>
> This is a spin-off from #1294
> 
> During the build of Azure Functions projects, a… temporary project is generated by \[ExtensionsCsprojGenerator\](https://github.com/Azure/azure-functions-dotnet-worker/blob/95a10c7aa95da2ba1122201ef78f5059733fa283/sdk/Sdk/ExtensionsCsprojGenerator.cs) outside of the project directory and this leads to the wrong computation of the project base path.
> 
> Example on Azure Pipelines (\[azure-pipelines-1.yml\](https://github.com/andrei-epure-sonarsource/ReproAzureFunctions/blob/master/azure-pipelines-1.yml) ; \[AZP logs - retained\](https://dev.azure.com/andreiepure/ReproAzureFunctions/\_build/results?buildId=6&view=logs&j=12f1170f-54f2-53f3-20dd-22fc7dff55f9) ; \[SonarCloud report\](https://sonarcloud.io/project/issues?resolved=false&types=CODE\_SMELL&id=ReproAzureFunctions)) , 
> 
> \# Actual behavior
> 
> The \`WorkerExtensions.csproj\` is created in the temp folder (on UNIX, \`/tmp/y3t0yxhk.0fg/WorkerExtensions.csproj\`). This leads to
> 
> \> INFO: Project key: 2022-08-05-ReproAzureFunctions
> INFO: Base dir: /
> INFO: Working dir: /home/vsts/work/1/.sonarqube/out/.sonar
> 
> See screenshot with the skewed project paths:
> 
> !\[image\](https://user-images.githubusercontent.com/38876598/184106813-dc20390a-c766-4980-8d13-8f19745022d1.png)
> 
> \# Expected behavior
> 
> The scanner should detect such anomalies and ignore them from analysis.
> 
> The path in the above screenshot should be \`ReproAzureFunctions/Function1.cs\`.
> 
> \# Work around
> 
> Manually specify the base path at the BEGIN step \`/d:sonar.projectBaseDi=...\`
> 
> \# Additional information
> 
> See the following comments:
> \- \[problems that might appear when fixing this issue\](https://github.com/SonarSource/sonar-scanner-msbuild/issues/1294#issuecomment-1209470201)
> \- \[details about ExtensionsCsprojGenerator\](https://github.com/SonarSource/sonar-scanner-msbuild/issues/1294#issuecomment-1209119716)

The listed workaround is to explicitly supply the correct base directory, so let me upgrade this:

> [@ganncamp](#):
>
> maybe what you need to do here.

to “_definitely_ what you need to do here.”

&nbsp;  
Ann

---

<div class="post-metadata">

**Author:** ![ChrisBets](https://avatars.discourse-cdn.com/v4/letter/c/ed655f/32.png) [@ChrisBets](https://community.sonarsource.com/u/ChrisBets)\
**Post date:** [March 19, 2024, 10:53am UTC](https://community.sonarsource.com/t/wrong-base-dir-for-sonarcloud-analyze-task-randomly/110308/6 "2024-03-19T10:53:09Z")

</div>

Hey @ganncamp !

Thanks for the response and the follow up!

We just rolled out the following fix in a view of our repositories and will continue to monitor, if this issue still persists.

```auto
  sonarExtraProperties: |
    sonar.projectBaseDir=$(Build.SourcesDirectory)

```

Would however of course be nice, if it would get fixed in the sonar scanner.
