Qodana CLI

repository·main·Indexed 42 days ago

https://github.com/jetbrains/qodana-cli

A cross-platform command-line tool for running JetBrains Qodana linters. It enables static analysis of codebases locally or in CI/CD environments using Docker or Podman containers. Key features include project initialization via `qodana init`, performing analysis with `qodana scan`, viewing HTML reports with `qodana show`, and uploading results to Qodana Cloud using `qodana send`.

Tokens
8.1K
Snippets
34
Records
40
Agent score
53%

What's inside Qodana CLI

  1. Configure Qodana using `qodana.yaml`

    main
    While the CLI supports various flags, the recommended way to configure Qodana or specific checks is to use a qodana.yaml file in your project root. This ensures consistent configuration across different environments, such as your local machine and CI/CD pipelines.
  2. Why use Qodana CLI instead of raw Docker commands?

    main

    While Qodana linters are distributed as Docker images, running them manually via docker run requires complex configuration of absolute paths, volume mounts, port forwarding, and user permissions (e.g., -u $(id -u):$(id -g) on Linux) to avoid permission issues or OOM (Out of Memory) errors on Windows/macOS.

    The Qodana CLI simplifies this by acting as a tailored wrapper around the Docker Engine API, managing the complexities of container orchestration, volume mapping, and resource handling for you.

  3. Install the Qodana CLI

    main

    The Qodana CLI is distributed as a binary. Depending on your operating system, use one of the following methods:

    macOS and Linux

    • Homebrew (Recommended):
      brew install jetbrains/utils/qodana
    • Installer Script:
      curl -fsSL https://jb.gg/qodana-cli/install | bash
      To install a specific version like nightly:
      curl -fsSL https://jb.gg/qodana-cli/install | bash -s -- nightly

    Windows

    • Winget (Recommended):
      winget install -e --id JetBrains.QodanaCLI
    • Chocolatey:
      choco install qodana
    • Scoop:
      scoop bucket add jetbrains https://github.com/JetBrains/scoop-utils
      scoop install qodana

    Manual Installation

    You can download the latest binary or apt/rpm/deb packages from the official releases page.

  4. Run Qodana inspections with `qodana scan`

    main

    To analyze your project, run the scan command from your project root.

    By default, Qodana runs using a container (Docker or Podman). If you want to use a specific linter, use the --linter option.

    Key behaviors:

    • Caching: Subsequent runs are faster because Qodana saves a cache in ./<userCacheDir>/JetBrains/<linter>/cache.
    • Results: The latest report and artifacts (like qodana.sarif.json and logs) are saved to ./<userCacheDir>/JetBrains/<linter>/results.

    User Cache Directories by OS:

    • macOS: ~/Library/Caches/
    • Linux: ~/.cache/
    • Windows: %LOCALAPPDATA%\
    # Basic scan
    qodana scan
    
    # Scan with a specific linter and show report immediately
    qodana scan --linter jetbrains/qodana-js --show-report
  5. Use the Qodana CLI

    main

    The qodana command is the entrypoint for the Qodana CLI. If no specific command is provided, the CLI defaults to the scan command.

    Security Note: Running the tool as root is discouraged and will trigger a warning. It is recommended to run Qodana as a regular user.

    Default Behavior:

    • If you run qodana without arguments, it will display the help message.
    • If you run qodana <args>, it will attempt to execute the command matching <args> or default to scan if no valid command is found.
    # Basic usage (defaults to scan)
    qodana
    
    # Explicitly running a command
    qodana scan
  6. Configure Qodana analysis execution mode

    main

    Qodana can run in different modes: natively using an IDE or within a Docker container.

    • Docker Mode: Use --linter to specify the linter (e.g., qodana-jvm) and --within-docker=true. The image is automatically selected based on the linter. Alternatively, use --image to specify a specific Docker image, which automatically sets --within-docker=true.
    • Native Mode: Use --ide to specify a native product code (e.g., pycharm, idea) to run without a container. Note that --ide is incompatible with --linter, --env, --volume, --user, and --skip-pull.

    Note: The --linter flag was previously used to define a Docker image; this behavior is deprecated. Use --linter and --within-docker=true or --image instead.

    # Run with a specific linter in Docker
    qodana scan --linter=qodana-jvm --within-docker=true
    
    # Run using a specific Docker image
    qodana scan --image=jetbrains/qodana-jvm
    
    # Run natively using an IDE (not compatible with --linter)
    qodana scan --ide=pycharm
    
    # Run natively by disabling Docker mode for a linter
    qodana scan --linter=qodana-jvm --within-docker=false
  7. Use the Qodana CLI

    main

    The Qodana CLI is the primary entrypoint for running static analysis on your projects. It provides commands to initialize projects, perform scans, and manage analysis configurations. The CLI is structured into subcommands, with scan being the primary command for running analysis and init used for project preparation.

    # Example of the general CLI usage pattern
    # (Note: specific command flags are defined in the command submodules)
    qodana scan [options]
  8. Reference: `qodana scan` options

    main

    A complete list of flags available for the qodana scan command.

      -l, --linter string             Defines the linter to be used for analysis. Default value is determined based on project files.
                                      Available values: qodana-jvm-community, qodana-jvm, qodana-jvm-android, qodana-android, qodana-php, qodana-python-community, qodana-python, qodana-js, qodana-cdnet, qodana-dotnet, qodana-ruby, qodana-cpp, qodana-go, qodana-rust, qodana-clang, qodana-poly.
          --within-docker string      Defines if analysis is performed within a docker container or not.
                                      Set to 'false' for performing analysis in native mode. Set to 'true' for performing analysis within a docker container.
          --image string              Defines an image to be used for analysis execution.
          -i, --project-dir string     Root directory of the inspected project (default ".")
          --repository-root string   Path to the root of the Git repository.
          -o, --results-dir string    Override directory to save Qodana inspection results to
          --cache-dir string         Override cache directory
          -r, --report-dir string    Override directory to save Qodana HTML report to
          --print-problems           Print all found problems by Qodana in the CLI output
          --code-climate              Generate a Code Climate report in SARIF format
          --bitbucket-insights        Send the results BitBucket Code Insights
          --clear-cache               Clear the local Qodana cache before running the analysis
          -w, --show-report          Serve HTML report on port
          --port int                  Port to serve the report on (default 8080)
          --config string             Set a custom configuration file instead of 'qodana.yaml'
          -a, --analysis-id string    Unique report identifier (GUID) to be used by Qodana Cloud
          -b, --baseline string       Provide the path to an existing SARIF report to be used in the baseline state calculation
          --baseline-include-absent  Include in the output report the results from the baseline run that are absent in the current run
          --fail-threshold string     Set the number of problems that will serve as a quality gate
          -d, --only-directory string Directory inside the project-dir directory must be inspected
          -n, --profile-name string   Profile name defined in the project
          -p, --profile-path string   Path to the profile file
          --apply-fixes               Apply all available quick-fixes, including cleanup
          --cleanup                   Run project cleanup
          --timeout int               Qodana analysis time limit in milliseconds
          --timeout-exit-code int     Exit code used when timeout is reached
          --diff-start string         Commit to start a diff run from
          --diff-end string           Commit to end a diff run on
          -e, --env stringArray      Define additional environment variables for the Qodana container
          -v, --volume stringArray   Define additional volumes for the Qodana container
          -u, --user string           Override user inside the Qodana container
  9. Configure a project with `qodana init` options

    main

    The qodana init command prepares a project by generating a qodana.yaml file. Use the following options to customize the initialization process:

          --config string        Set a custom configuration file instead of 'qodana.yaml'. Relative paths in the configuration will be based on the project directory.
      -f, --force                Force initialization (overwrite existing valid qodana.yaml)
      -h, --help                 help for init
      -i, --project-dir string   Root directory of the project to configure (default ".")
    
    Options inherited from parent commands:
          --disable-update-checks   Disable check for updates
          --log-level string        Set log-level for output (default "error")
  10. Calculate lines of code with `qodana cloc`

    main

    The cloc command provides project statistics such as languages and lines of code, powered by boyter/scc. This is intended for general project statistics, whereas qodana contributors should be used for pricing-related calculations.

    Options:

    • -i, --project-dir <stringArray>: Path to the project directory. Can be specified multiple times. If omitted, the current directory is used.
    • -o, --output <string>: Output format. Supported values: tabular (default), wide, json, csv, csv-stream, cloc-yaml, html, html-table, sql, sql-insert, openmetrics.
    qodana cloc -i ./my-project -o json
  11. View Qodana HTML reports with `qodana show`

    main

    After running a scan, you can view the results in a browser using the show command. This command locates the HTML report within the results directory.

    To quickly open the directory containing the latest report, use the -d flag.

    # Open the HTML report in your default browser
    qodana show
    
    # Open the directory containing the latest report
    qodana show -d