CodeQL query packs reference

Understand the compatibility, contents, and structure of CodeQL packs.

CodeQL pack compatibility

When a query pack is published, it includes pre-compiled representations of all the queries in it to increase analysis speed. However, if the version of CodeQL that performs the analysis is over 6 months newer than the version that ran codeql pack publish, it may be necessary to compile the queries from source during analysis, slowing the process significantly.

A pack published by the latest public release of CodeQL will be useable by the version of CodeQL that is used by code scanning and GitHub Actions, even though that is often a slightly older release.

If your analysis contains lines like the following, then CodeQL is successfully using precompiled queries:

[42/108] Loaded /long/path/to/query/Filename.qlx.

If your analysis instead contains lines that look like the following, then CodeQL manually recompiled the queries from source:

Compiling query plan for /long/path/to/query/Filename.ql.
[42/108 comp 25s] Compiled /long/path/to/query/Filename.ql.

To help users of your query pack benefit from pre-compiled queries, we recommend using a recent release of CodeQL to publish your packs. Additionally, you should publish a fresh version of your pack with an updated CodeQL version every 6 months.

If you publish query packs with the intention of using them on a GitHub Enterprise Server installation that uses its bundled CodeQL binaries, use the same CodeQL version to run codeql pack publish.

qlpack.yml files

When executing query-related commands, CodeQL first looks in siblings of the installation directory (and their subdirectories) for qlpack.yml files, then checks the package cache for downloaded CodeQL packs. This means that when your local packages in the installation directory override packages of the same name in the package cache, so you can test your local changes.

The metadata in each qlpack.yml file tells CodeQL how to compile any queries in the pack, what libraries the pack depends on, and where to
find query suite definitions.

The contents of the CodeQL pack (queries or libraries used in CodeQL analysis) is included in the same directory as qlpack.yml, or its subdirectories.

The directory containing the qlpack.yml file serves as the root directory for the content of the CodeQL pack. That is, for all .ql and .qll files in the pack, CodeQL will resolve all import statements relative to the directory containing the qlpack.yml file at the pack’s root.

qlpack.yml properties

The following properties are supported in qlpack.yml files.

name

version

dataExtensions

dependencies

defaultSuiteFile

defaultSuite

extensionTargets

groups

library

suites

tests

extractor

authors

license

description

libraryPathDependencies

dbscheme

upgrades

warnOnImplicitThis

codeql-pack.lock.yml files

codeql-pack.lock.yml files store the versions of the resolved transitive dependencies of a CodeQL pack. This file is created by the codeql pack install command if it does not already exist and should be added to your version control system. The dependencies section of the qlpack.yml file contains version ranges that are compatible with the pack. The codeql-pack.lock.yml file locks the versions to precise dependencies. This ensures that running codeql pack install on this the pack will always retrieve the same versions of dependencies even if newer compatible versions exist.

For example, if a qlpack.yml file contains the following dependencies:

dependencies:
  codeql/cpp-all: ^0.1.2
  my-user/my-lib: ^0.2.3
  other-dependency/from-source: "*"

The codeql-pack.lock.yml file will contain something like the following:

dependencies:
  codeql/cpp-all:
    version: 0.1.4
  my-user/my-lib:
    version: 0.2.4
  my-user/transitive-dependency:
    version: 1.2.4

The codeql/cpp-all dependency is locked to version 0.1.4. The my-user/my-lib dependency is locked to version 0.2.4. The my-user/transitive-dependency, which is a transitive dependency and is not specified in the qlpack.yml file, is locked to version 1.2.4. The other-dependency/from-source is absent from the lock file since it is resolved from source. This dependency must be available in the same CodeQL workspace as the pack. For more information about CodeQL workspaces and resolving dependencies from source, see CodeQL workspaces.

In most cases, the codeql-pack.lock.yml file is only relevant for query packs since library packs are non-executable and usually do not need their transitive dependencies to be fixed. The exception to this is for library packs that contain tests. In this case, the codeql-pack.lock.yml file is used to ensure that the tests are always run with the same versions of dependencies to avoid spurious failures when there are mismatched dependencies.

Example custom CodeQL packs

You should save files for custom queries and tests in separate packs, and organize custom packs into specific folders for each target language.

CodeQL packs for custom libraries

A custom CodeQL pack containing custom C++ libraries, with no queries or tests, may have a qlpack.yml file containing:

name: my-github-user/my-custom-libraries
version: 1.2.3
library: true
dependencies:
  codeql/cpp-all: ^0.1.2

where codeql/cpp-all is the name of the CodeQL pack for C/C++ analysis included in the CodeQL repository. The version range ^0.1.2 indicates that this pack is compatible with all versions of codeql/cpp-all that are greater than or equal to 0.1.2 and less than 0.2.0. Any CodeQL library file (a file with a .qll extension) defined in this pack will be available to queries defined in any query pack that includes this pack in its dependencies block.

The library property indicates that this pack is a library pack and does not contain any queries.

CodeQL packs for custom queries

A custom CodeQL pack containing custom C++ queries and libraries may have a qlpack.yml file containing:

name: my-github-user/my-custom-queries
version: 1.2.3
dependencies:
  codeql/cpp-all: ^0.1.2
  my-github-user/my-custom-libraries: ^1.2.3

where codeql/cpp-all is the name of the CodeQL pack for C/C++ analysis included in the CodeQL repository. The version range ^0.1.2 indicates that this pack is compatible with all versions of codeql/cpp-all that are greater than or equal to 0.1.2 and less than 0.2.0. my-github-user/my-custom-libraries is the name of a CodeQL pack containing custom CodeQL libraries for C++. Any CodeQL library file (a file with a .qll extension) defined in this pack will be available to queries in the my-github-user/my-custom-queries pack.

CodeQL packs for custom tests

For custom CodeQL packs containing test files, you also need to include an
extractor property so that the test run command knows how to create test
databases. You may also wish to specify the tests property.

The following qlpack.yml file states that my-github-user/my-query-tests depends on my-github-user/my-custom-queries at a version greater than or equal to 1.2.3 and less than 2.0.0. It also declares that the CLI should use the Java extractor when creating test databases. The tests: . line declares that all .ql files in the pack should be run as tests when codeql test run is run with the --strict-test-discovery option. Typically, test packs do not contain a version property. This prevents you from accidentally publishing them.

name: my-github-user/my-query-tests
dependencies:
  my-github-user/my-custom-queries: ^1.2.3
extractor: java-kotlin
tests: .

For more information about running tests, see Testing custom queries.

Example CodeQL packs in the CodeQL repository

Each of the languages in the CodeQL repository has four main CodeQL packs:

Core library pack

Here is an example qlpack.yml file for the C/C++ analysis libraries
core language pack:

name: codeql/cpp-all
version: x.y.z-dev
dbscheme: semmlecode.cpp.dbscheme
library: true
upgrades: upgrades

Some extra notes on the following properties:

Core query pack

Here is an example qlpack.yml file for C/C++ analysis queries
core query pack:

name: codeql/cpp-queries
version: x.y.z-dev
dependencies:
    codeql/cpp-all: "*"
    codeql/suite-helpers: "*"
suites: codeql-suites
defaultSuiteFile: codeql-suites/cpp-code-scanning.qls

Some extra notes on the following properties:

Tests for the core CodeQL pack

Here is an example qlpack.yml file for C/C++ analysis tests
core test pack:

name: codeql/cpp-tests
dependencies:
  codeql/cpp-all: "*"
  codeql/cpp-queries: "*"
extractor: cpp
tests: .

Some extra notes on the following properties: