SonarQube:TypeScript baseUrl 和相对路径配置 => 问题“依赖关系应该是明确的”(typescript:S4328)

fle*_*ryc 7 typescript sonarqube

语境

  • 使用的 ALM:Azure DevOps
  • 使用的 CI 系统:Azure DevOps
  • 适用时使用的扫描仪命令(屏蔽私人详细信息):
  - task: SonarCloudPrepare@1
    inputs:
      SonarCloud: 'SonarCloud-Clement'
      organization: 'assoconnect'
      scannerMode: 'CLI'
      configMode: 'manual'
      cliProjectKey: 'assoconnect_frontend'
      cliProjectName: 'frontend'
      cliProjectVersion: '$(Build.BuildNumber)'
      cliSources: 'src'
      extraProperties: |
        sonar.tests=src
        sonar.test.inclusions=**/__tests__/**
        sonar.coverage.exclusions=src/pages/**
        sonar.junit.reportPaths=junit.xml
        sonar.typescript.lcov.reportPaths=coverage/lcov.info
        sonar.typescript.tsconfigPath=tsconfig.json
    displayName: 'Prepare SonarQube analysis'

  - task: SonarCloudAnalyze@1
    displayName: 'Run SonarQube analysis'

  - task: SonarCloudPublish@1
    inputs:
      pollingTimeoutSec: '300'
    displayName: 'Publish SonarQube analysis result'
Run Code Online (Sandbox Code Playgroud)
  • 存储库的语言:TypeScript

观察到的问题

对于我们所有的imports文件,我们都有“依赖关系应该是明确的(typescript:S4328).tsx ”问题的实例。

我们在文件中配置了baseUrlhttps://www.typescriptlang.org/docs/handbook/module-resolution.html#base-urltsconfig.json,这样我们就不必处​​理相对路径。WebStorm 似乎对这个配置没问题,并且 linter 没有显示任何消息。但是在Sonar中,当我们导入一个模块时,由于没有./前置,它认为它是一个外部模块并在依赖项中查找它。

例子

  • 我们的布局是:
src/
|___pages/
|   |___Index/
|       |___index.tsx
|       |___type.tsx
|___App.tsx
package.json
tsconfig.json
Run Code Online (Sandbox Code Playgroud)
  • 文件内容
// package.json
// nothing path-related, only external dependencies
...
Run Code Online (Sandbox Code Playgroud)
// tsconfig.json
{
  "compilerOptions": {
    "baseUrl": "./src/",
...
Run Code Online (Sandbox Code Playgroud)
  - task: SonarCloudPrepare@1
    inputs:
      SonarCloud: 'SonarCloud-Clement'
      organization: 'assoconnect'
      scannerMode: 'CLI'
      configMode: 'manual'
      cliProjectKey: 'assoconnect_frontend'
      cliProjectName: 'frontend'
      cliProjectVersion: '$(Build.BuildNumber)'
      cliSources: 'src'
      extraProperties: |
        sonar.tests=src
        sonar.test.inclusions=**/__tests__/**
        sonar.coverage.exclusions=src/pages/**
        sonar.junit.reportPaths=junit.xml
        sonar.typescript.lcov.reportPaths=coverage/lcov.info
        sonar.typescript.tsconfigPath=tsconfig.json
    displayName: 'Prepare SonarQube analysis'

  - task: SonarCloudAnalyze@1
    displayName: 'Run SonarQube analysis'

  - task: SonarCloudPublish@1
    inputs:
      pollingTimeoutSec: '300'
    displayName: 'Publish SonarQube analysis result'
Run Code Online (Sandbox Code Playgroud)
src/
|___pages/
|   |___Index/
|       |___index.tsx
|       |___type.tsx
|___App.tsx
package.json
tsconfig.json
Run Code Online (Sandbox Code Playgroud)

Q.C*_*.CY 1

您可以尝试像这样添加特定路径sonar.sources

// in my tsconfig
"paths": {
      "src": [
        "./src/*"
      ],
    },

// in my sonar-project.properties
sonar.sources=src/components,src/navigation,src/screens,src/store,src/types,src/utils
Run Code Online (Sandbox Code Playgroud)

它对我有用,在声纳扫描报告中“依赖性应该是明确的”问题被删除