Skip to content

Running Dart Analyzer#

To run Dart Analyzer as a client-side tool:

  1. Enable Dart Analyzer and configure the corresponding code patterns on your repository Code patterns page.

  2. Enable Run analysis on your build server on your repository Settings, tab General, Repository analysis on your server.

    This setting enables Codacy to wait for the results of the local analysis before resuming the analysis of your commits.

    Run analysis on your build server

  3. Set up an API token to authenticate on Codacy:

    • If you're setting up one repository, obtain a repository API token and set the following environment variable to specify your repository API token:

      export CODACY_PROJECT_TOKEN=<your repository API token>
      
    • If you're setting up multiple repositories, obtain an account API Token and set the following environment variable to specify the account API token:

      export CODACY_API_TOKEN=<your account API token>
      

    Warning

    Never write API tokens to your configuration files and keep your API tokens well protected, as they grant owner permissions to your projects on Codacy.

    It's a best practice to store API tokens as environment variables. Check the documentation of your CI/CD platform on how to do this.

Dart SDK and dependencies#

The Codacy Analysis CLI runs dart analyze from a Dart SDK. It uses the SDK that Codacy manages for you, if you have installed it, and otherwise the dart command on your PATH:

  • Codacy-managed SDK: Add --install-dependencies to analyze and the CLI downloads the pinned Dart SDK, about 210 MB, into ~/.codacy/tools/dartanalyzer/.
  • Your own SDK: Install Dart, or Flutter, which bundles Dart, and make sure dart is on your PATH.

Before analyzing, resolve your project's packages with dart pub get, or flutter pub get for a Flutter project. The CLI never runs it for you. Without the resolved packages, every package: import is unresolvable: the CLI warns and skips the checks that depend on them for that run.

Manual configuration#

Install the Codacy Analysis CLI and initialize the repository. Initialization writes .codacy/codacy.config.json, which analyze requires: pulling it from Codacy Cloud with init --remote means the local run uses the same Dart Analyzer patterns as your repository's Code patterns page.

npm i -g @codacy/analysis-cli
codacy-analysis init --remote <gh|gl|bb> <ORGANIZATION> <REPOSITORY>

Then resolve your packages, run the analysis, and upload the results:

dart pub get
codacy-analysis analyze --tool dartanalyzer --output-format sarif --output dartanalyzer.sarif
codacy-analysis upload dartanalyzer.sarif --commit $COMMIT_SHA

If you're using an account API token, you must also provide the --repository flag with your provider, organization, and repository name. You can obtain these values from the URL of your repository dashboard on Codacy:

dart pub get
codacy-analysis analyze --tool dartanalyzer --output-format sarif --output dartanalyzer.sarif
codacy-analysis upload dartanalyzer.sarif --commit $COMMIT_SHA --repository <gh|gl|bb> <ORGANIZATION> <REPOSITORY>

Note

--tool matches against the tool IDs in your .codacy/codacy.config.json, and only tools listed there run. If --tool dartanalyzer doesn't match your setup, check that file for the ID it recorded.

GitHub Action#

The Codacy Analysis CLI GitHub Action runs the same codacy-analysis CLI as the manual configuration above. If your repository has no committed .codacy/codacy.config.json, the action fetches the repository's configuration from Codacy with your token.

On pull_request events, the action uploads the results for the pull request's head commit. Check out that commit with ref: as in the examples below, otherwise line numbers can point at the wrong lines.

The examples set up Dart and resolve the packages before running the action. For a Flutter project, use a Flutter setup action and flutter pub get instead.

Using a project token#

- uses: actions/checkout@v4
  with:
    ref: ${{ github.event.pull_request.head.sha || github.sha }}

- uses: dart-lang/setup-dart@v1

- run: dart pub get

- name: Run Codacy Analysis CLI
  uses: codacy/analysis-cli-action@v1
  with:
    project-token: ${{ secrets.CODACY_PROJECT_TOKEN }}
    tool: dartanalyzer
    upload: true

Using an account API token#

The action takes the provider, organization, and repository from the workflow. Set provider, owner, and repository only if the repository is on Codacy under different names.

- uses: actions/checkout@v4
  with:
    ref: ${{ github.event.pull_request.head.sha || github.sha }}

- uses: dart-lang/setup-dart@v1

- run: dart pub get

- name: Run Codacy Analysis CLI
  uses: codacy/analysis-cli-action@v1
  with:
    api-token: ${{ secrets.CODACY_API_TOKEN }}
    tool: dartanalyzer
    upload: true

Tokens should be stored in the repository secrets.

Was this page helpful?

Your feedback helps us improve the documentation.