TypeScript tsconfig.json detection fundamentally broken

Currently, for determining which tsconfig.json to use when analyzing a .ts file, SonarJS uses the following logic:

Not only does this code have bugs, it is also unsuitable for certain valid TypeScript projects.

The bugs are as follows:

  1. parseConfigHost requires a member readFile with value ts.sys.readFile, extends will not work otherwise
  2. After calling parseJsonConfigFileContent, the result’s errors member needs to be checked for errors (this would’ve lead to the discovery of bug 1)

Now, the rest of SonarJS hinges on parsed.fileNames actually containing all files. However, this is not the case at least for modern Angular! It uses the “solution-style tsconfig”.

This results in the following tsconfig.json:

{
  "files": [],
  "references": [
    {
      "path": "./tsconfig.app.json"
    },
    {
      "path": "./tsconfig.spec.json"
    },
    {
      "path": "./e2e/tsconfig.json"
    }
  ]
}

The files array is intentionally empty. This will never return anything in fileNames after being parsed.

Of course, there’s also tsconfig.app.json, which we could specify manually. However, this file contains the following:

{
  "extends": "./tsconfig.base.json",
  "compilerOptions": {
    "outDir": "./out-tsc/app",
    "types": []
  },
  "files": [
    "src/main.ts",
    "src/polyfills.ts"
  ],
  "include": [
    "src/**/*.d.ts"
  ]
}

This means it will result in exactly two files in fileNames: main.ts and polyfills.ts. Also not suitable for SonarJS.

What SonarJS needs is an option to just use that tsconfig.json. Alternatively, it could also just use the closest tsconfig.json by default, which may not be entirely accurate but is still better than refusing to analyze anything at all.

Additionally, SonarJS should support TypeScript Project References.

@fuzzykiller,

thanks for reporting this issue with detailed analysis. We created this ticket to handle it https://github.com/SonarSource/SonarJS/issues/2051

In the meantime, Angular 10.1 went back to “regular” tsconfig.json, which means Sonar can scan the files again.

1 Like

SonarQube works with solution-style tsconfig with this setting:

sonar.typescript.tsconfigPath=tsconfig.lint.json

You need to point into a tsconfig which includes the src folder (or whatever directory with the code):
For instance, I’m reusing the same one that I have for Eslint:

{
  "extends": "./tsconfig.base.json",
  "compilerOptions": {
    "noEmit": true
  },
  "include": ["src"]
}

The issue now is located at SonarLint, it tries to read tsconfig.json and because it does not contain anything it skips all the files and the errors are not displayed in the IDE:

[Info  - 11:26:36.639] Found 1 tsconfig.json file(s): [/Users/gllamas/projects/recall2-POC-starter/tsconfig.json]
[Debug - 11:26:36.698] /Users/gllamas/projects/recall2-POC-starter/src/app/app.component.ts matched NO_CONFIG
[Info  - 11:26:36.699] 1 source files to be analyzed
[Info  - 11:26:36.699] Skipping 1 files with no tsconfig.json
[Debug - 11:26:36.700] Skipped files: [uri=file:///Users/gllamas/projects/recall2-POC-starter/src/app/app.component.ts]

If we had a property for specifying the tsconfig path it would be solved.

We released new version of SonarQube 8.5 which includes the fix in JS analyzer to address this issue. Please give it a try and report any issues you encounter.