Sub topic 13
Performance tuning and troubleshooting failed scans
This chapter assumes you can read a CodeQL workflow, know what init and analyze do, and understand build modes and the configuration file from earlier chapters. It covers how to make scans faster and how to diagnose the failures you will meet most often.
Where scan time goes
A CodeQL run has three costly phases: building the database, evaluating queries, and uploading results. Most tuning targets the first two.
Tuning for speed
Apply these in order. Each is cheap and reversible.
- Use
build-mode: nonewhere it is supported. For JavaScript/TypeScript and Python, no build is needed, so the database is created straight from source. Only compiled languages needmanualorautobuild. - Exclude code you do not own. Tests, vendored dependencies and generated files add extraction and query time. Use
paths-ignorein the configuration file:
name: "Faster CodeQL configuration"
paths-ignore:
- '**/test/**'
- '**/node_modules/**'
- '**/dist/**'- Use the smallest suite that meets your policy.
security-extendedruns more queries than the default suite, so it takes longer. Choose it deliberately, not by habit. - Run languages in parallel. The matrix from the earlier workflow already does this. Keep
fail-fast: falseso one failing language does not cancel the others. - Use a larger runner for big repositories. Query evaluation is memory-hungry. If a job is killed or very slow, more RAM often helps more than anything else.
- Trim the triggers. Scan pull requests and the default branch, and run the full scan on a schedule. Avoid triggering on every branch.
Common failures and fixes
| Symptom | Likely cause | Fix |
|---|---|---|
| "Resource not accessible by integration" on upload | Missing `security-events: write`, or a pull request from a fork with a read-only token | Add the permission to the job. For forks, scan after merge or accept reduced results. |
| "CodeQL detected no source code" | Wrong `languages` value, or `paths-ignore` excluded everything | Check the language identifier, such as `javascript-typescript`, and review ignore patterns. |
| Build step fails for a compiled language | `build-mode: manual` with a command that does not build the whole project | Run the same commands locally on a clean checkout, then copy them into the workflow. |
| Few or no alerts for compiled code | Build did not compile the code, so little was extracted | Make sure the build is a full build, not a cached or incremental one. |
| Job times out or is killed | Large repository or too little memory | Exclude paths, split by language, or use a larger runner. |
| Alerts appear under the wrong tool | Missing or duplicate `category` | Give each analysis a unique `category`, as in the matrix example. |
A troubleshooting routine
- Open the failed run and find the first red step. Later errors are often side effects.
- Read the
initlog to confirm the languages and build mode CodeQL selected. - For build failures, reproduce the build commands locally.
- For upload failures, check
permissionsfirst, then thecategory. - Add debug logging if the cause is still unclear. Re-run the job with debug logging enabled, which adds detail to the CodeQL steps.
- Re-run after each fix, and confirm the Security tab shows the new analysis.
Exercise
A scan of a Java repository succeeds in 4 minutes but reports almost no alerts, though the project is large. The workflow uses build-mode: manual with a build step that runs make clean. What is the likely problem, and how would you confirm it?
Show answer
The build step only cleans and does not compile the code, so CodeQL extracts almost nothing. Confirm it by checking the log for the number of files extracted, then change the step to run a full build such as make clean && make all.
Exercise
Name two configuration changes that shorten scan time without removing any language from the scan.
Show answer
Add paths-ignore entries for tests and dependencies in the configuration file, and use the default query suite instead of security-extended. Using a larger runner also helps.