Sub topic 6
Scanning several languages and choosing a build mode
This chapter assumes you can read a GitHub Actions workflow and know what the init and analyze steps of the CodeQL action do. It covers how to scan more than one language in one repository and how to pick a build mode for each.
Why languages are analyzed separately
CodeQL builds one database per language. A repository with JavaScript, Python and Java therefore needs three analyses. The usual way to run them is a matrix job. GitHub Actions starts one job per matrix entry, and each job runs init, an optional build, and analyze for a single language.
Running languages in separate jobs has three benefits:
- A failure in one language does not hide results from the others.
- Slow languages run in parallel with fast ones.
- Each language can use its own build mode.
Set fail-fast: false so that one failing job does not cancel the rest.
The language identifiers
The languages input takes CodeQL's own names, which are not always the names you use in conversation.
| Languages in your repository | Identifier |
|---|---|
| JavaScript and TypeScript | `javascript-typescript` |
| Python | `python` |
| Java and Kotlin | `java-kotlin` |
| C and C++ | `c-cpp` |
| C# | `csharp` |
| Go | `go` |
| Ruby | `ruby` |
| Swift | `swift` |
| GitHub Actions workflows | `actions` |
JavaScript and TypeScript share one identifier, and so do Java and Kotlin. A wrong identifier makes the init step fail, so check the list in the CodeQL documentation when you add a language.
The three build modes
A compiled language needs CodeQL to see how the code is built. The build-mode input controls this.
| Mode | What happens | Typical use |
|---|---|---|
| `none` | CodeQL reads the source without building it. | Interpreted languages, and compiled languages where it is supported. |
| `autobuild` | CodeQL guesses the build command and runs it. | Simple projects with a standard layout. |
| `manual` | You write the build step yourself. | Custom build systems, code generation, special toolchains. |
Interpreted languages such as JavaScript, TypeScript, Python and Ruby need no build, so use none. Some compiled languages, including Java, Kotlin and C#, also support none. A scan without a build is easier to set up, but it can miss code that only exists after generation or that depends on build-time configuration. C and C++ need a real build, and Go and Swift have their own requirements, so check the current support for your language before choosing none.
A matrix with mixed build modes
The matrix below carries a build-mode value for each language. The init step reads it, and a build step runs only when the mode is manual.
name: CodeQL
on:
push:
branches: [main]
pull_request:
branches: [main]
jobs:
analyze:
name: Analyze (${{ matrix.language }})
runs-on: ubuntu-latest
permissions:
security-events: write
contents: read
actions: read
strategy:
fail-fast: false
matrix:
include:
- language: javascript-typescript
build-mode: none
- language: python
build-mode: none
- language: c-cpp
build-mode: manual
steps:
- name: Check out repository
uses: actions/checkout@v4
- name: Initialize CodeQL
uses: github/codeql-action/init@v4
with:
languages: ${{ matrix.language }}
build-mode: ${{ matrix.build-mode }}
- name: Build (compiled languages only)
if: matrix.build-mode == 'manual'
run: |
make clean
make all
- name: Perform CodeQL analysis
uses: github/codeql-action/analyze@v4
with:
category: "/language:${{ matrix.language }}"The build step sits between init and analyze. CodeQL watches the build that runs in that window, so the build must happen after init and before analyze.
The category value gives each language its own identity in the results. Without it, results from one job can overwrite results from another.
Common errors
- The build runs but CodeQL finds no code. The build probably ran before
init, or it was fully cached so nothing compiled. Run a clean build. autobuildfails. It could not guess your build. Switch tomanualand write the commands.- A language is listed but never scanned. Check the spelling of the identifier and that the matrix entry sets
language. - Results from different languages replace each other. Add a unique
categoryper job.
Exercise
Your repository contains Python and a C++ library built with make. Which matrix entries and build modes do you write, and where does the build step go?
Show answer
Add two entries: language: python with build-mode: none, and language: c-cpp with build-mode: manual. Put a step that runs make between the init and analyze steps, with if: matrix.build-mode == 'manual' so it only runs for C++.