Search code examples
javascripttypescriptsonarqubegithub-actionssonarqube-scan

Ts and tsx files are not getting analysed by Sonarqube via Github actions for master branch analysis


My setup

What am I trying to achieve?

I’m trying to run sonarqube scanner on PR and master branch for a web app written in typescript and javascript.

What problem am I facing?

ts and tsx files are getting analysed for PR (branch) analysis but when the analysis is run on master branch the ts and tsx files are shown as un-analysed.

code from a PR enter image description here

code from master branch analysis enter image description here

What have I tried so far to achieve this

Sample action for master branch analysis:

sonarqube:
    runs-on: ubuntu-latest
    needs: [lint, test]
    steps:
      - uses: actions/checkout@v2
        with:
          fetch-depth: 0
      - name: SonarQube Scan
        uses: SonarSource/[email protected]
        env:
          SONAR_TOKEN: XX
          SONAR_HOST_URL: XX
        with:
          args: >
            -Dsonar.verbose=true
            -Dsonar.sources=./src/
            -Dsonar.tests=./test/
            -Dsonar.projectKey=my_key
            -Dsonar.eslint.reportPaths=./reports/eslint.json
            -Dsonar.css.stylelint.reportPaths=/reports/stylelint.json
            -Dsonar.javascript.lcov.reportPaths=./reports/lcov.info

We have also ensured that tsconfig.json has the necessary includes

 "include": [
    "**/*.ts",
    "**/*.tsx",
    "**/*.css",
  ],

Don’t know what’s going wrong.


Solution

  • The issue was related to an option useUnknownInCatchVariables that was being used as compiler option in tsconfig.json.

    When the scanner was run in debug mode an error was being thrown due to which tsx files were not getting scanned.

    06:09:36.583 INFO: Using /github/workspace/./tsconfig.json from sonar.typescript.tsconfigPath property
    06:09:36.744 ERROR: Unknown compiler option 'useUnknownInCatchVariables'.
    

    Based on https://community.sonarsource.com/t/typescript-useunknownincatchvariables-compiler-option-causes-main-branch-to-show-up-empty/49349/4?u=abhijeet_vaikar thread, we have to:

    • Either remove that option from tsconfig.json
    • Create a separate tsconfig.json without that option.
    • Upgrade sonarqube server to the latest version.