Analysis tries to read all tsconfig files in repo

We have a TypeScript monorepo (managed by rush.js and pnpm) with about 60 packages in it. We are hosting in Azure DevOps and using the SonarCloud tasks to run analysis on each project as part of our build pipelines.

Basically for each package in the repo we have a pipeline that runs and calls Sonar Cloud to analyze just the files in that package. In other words, even though the pipeline has access to the whole repo, SC is limited to just the files in the package at issue. This is done via a pipeline config like this:

- task: SonarCloudPrepare@2
    displayName: "Prepare SonarCloud"
    inputs:
      SonarCloud: 'SonarCloud'
      organization: 'myOrg'
      scannerMode: 'CLI'
      configMode: 'manual'
      cliProjectKey: 'my_package_key'
      cliProjectName: 'TS My Package'
      cliSources: './path/to/myPackage'
      extraProperties: |
        sonar.project.monorepo.enabled=true
        sonar.exclusions=**/__tests__/**/*,**/__mocks__/**/*,**/public/**/*,
        sonar.javascript.lcov.reportPaths=path/to/myPackage/coverage/lcov.info

//OTHER PIPELINE STUFF

  - task: SonarCloudAnalyze@2
    displayName: 'SonarCloud analysis'

  - task: SonarCloudPublish@2
    displayName: 'Publish SonarCloud analysis'
    inputs:
      pollingTimeoutSec: "300"

The issue we have is that when the SonarCloudAnalyze step runs it appears to try to parse ever single tsconfig.json file in the whole repo, even those outside the “cliSources” path. That generates log entries like this (for every package in the repo):

20:38:43.448 INFO  TypeScript configuration file /__w/8/s/path/to/someOtherPackage/tsconfig.json
20:38:43.693 INFO  Starting analysis with current program
20:38:43.693 INFO  Analyzed 0 file(s) with current program

That in and of itself isn’t really a problem. That parsing happens very quickly and the actual analysis only runs on the specified package.

The problem comes in however because some of the tsconfig.json files in our monorepo are invalid. Specifically we have sample files or deprecated files that live in places where they will never actually get parsed and so never cause any problems BUT SonarCloud insists on trying to parse them which leads to errors in our pipelines.

Is there any way to avoid this behavior, i.e., to not have Sonar Cloud try to read EVERY tsconfig in the repo, but only those in the cliSources directory?

1 Like

Hello @samd,

Welcome back to the Sonar community, and thank you for your feedback!

It seems that the automatic lookup of TSConfig files is not working well with the invalid TSConfig files in your monorepo. You can instruct the analysis to consider specific TSConfig files by using the property sonar.typescript.tsconfigPaths. This property expects a comma-delimited list of paths to TSConfig files, and paths can use wildcards, for example, src/**/tsconfig.json.

To set this property for your SonarCloud project, go to Administration > General Settings > Languages > JavaScript/TypeScript > TypeScript tsconfig.json location.

Hope this helps,
Yassin

Thank you @Yassin

This was very helpful. We are able to point that property to just the config files in the package/project directory. That seems to clear up the error. Thanks!

1 Like

I’m happy to hear that, @samd! :slightly_smiling_face: