Skip to content

Commit d737b57

Browse files
committed
Merge and update about-ql-packs with about-codeql-packs
This is the first of a series of commits around updating packaging docs. `about-ql-packs.rst` is outdated. All relevant information has been moved to about-codeql-packs.rst`.
1 parent 4f93f2b commit d737b57

File tree

3 files changed

+194
-278
lines changed

3 files changed

+194
-278
lines changed

docs/codeql/codeql-cli/about-codeql-packs.rst

Lines changed: 190 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
About CodeQL packs
44
==================
55

6-
.. include:: ../reusables/beta-note-package-management.rst
7-
86
CodeQL packs are used to create, share, depend on, and run CodeQL queries and libraries. You can publish your own CodeQL packs and download packs created by others. CodeQL packs contain queries, library files, query suites, and metadata.
97

108
There are two types of CodeQL packs: query packs and library packs.
@@ -14,11 +12,18 @@ There are two types of CodeQL packs: query packs and library packs.
1412

1513
You can use the package management commands in the CodeQL CLI to create CodeQL packs, add dependencies to packs, and install or update dependencies. For more information, see ":ref:`Creating and working with CodeQL packs <creating-and-working-with-codeql-packs>`." You can also publish and download CodeQL packs using the CodeQL CLI. For more information, see ":doc:`Publishing and using CodeQL packs <publishing-and-using-codeql-packs>`."
1614

15+
16+
The standard CodeQL packages for all supported languages are published in the `GitHub package registry <https://github.com/orgs/codeql/packages>`__.`
17+
The `CodeQL repository <https://github.com/github/codeql>`__ contains sources for the standard CodeQL packs for all supported languages.
18+
19+
.. _codeql-pack-structure:
20+
1721
CodeQL pack structure
1822
---------------------
1923

20-
A CodeQL pack must contain a file called ``qlpack.yml`` in its root directory. In the ``qlpack.yml`` file, the ``name:`` field must have a value that follows the format of ``<scope>/<pack>``, where ``<scope>`` is the GitHub organization or user account that the pack will be published to and ``<pack>`` is the name of the pack. The other
21-
files and directories within the pack should be logically organized. For example, typically:
24+
A CodeQL pack must contain a file called ``qlpack.yml`` in its root directory. In the ``qlpack.yml`` file, the ``name:`` field must have a value that follows the format of ``<scope>/<pack>``, where ``<scope>`` is the GitHub organization or user account that the pack will be published to and ``<pack>`` is the name of the pack. Additionally, query packs contain a ``codeql-pack.lock.yml`` file that contains the resolved dependencies of the pack. This file is generated during a call to the ``codeql pack install`` command, is not meant to be edited by hand, and should be added to your version control system.
25+
26+
The other files and directories within the pack should be logically organized. For example, typically:
2227

2328
- Queries are organized into directories for specific categories.
2429
- Queries for specific products, libraries, and frameworks are organized into
@@ -27,21 +32,17 @@ files and directories within the pack should be logically organized. For example
2732
About ``qlpack.yml`` files
2833
--------------------------
2934

30-
When executing query-related commands, CodeQL first looks in siblings of the installation directory (and their subdirectories) for ``qlpack.yml`` files.
31-
Then it checks the package cache for CodeQL packs which have been downloaded. This means that when you are developing queries locally, the local packages
35+
When executing query-related commands, CodeQL first looks in siblings of the installation directory (and their subdirectories) for ``qlpack.yml`` files.
36+
Then it checks the package cache for CodeQL packs which have been downloaded. This means that when you are developing queries locally, the local packages
3237
in the installation directory override packages of the same name in the package cache, so that you can test your local changes.
3338

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

38-
The contents of the CodeQL pack (queries or libraries used in CodeQL analysis) is
39-
included in the same directory as ``qlpack.yml``, or its subdirectories.
43+
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.
4044

41-
The location of ``qlpack.yml`` defines the library path for the content
42-
of the CodeQL pack. That is, for all ``.ql`` and ``.qll`` files in the pack,
43-
CodeQL will resolve all import statements relative to the ``qlpack.yml`` at the
44-
pack's root.
45+
The location of ``qlpack.yml`` defines the library path 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 ``qlpack.yml`` at the pack's root.
4546

4647
.. _codeqlpack-yml-properties:
4748

@@ -59,19 +60,41 @@ The following properties are supported in ``qlpack.yml`` files.
5960
- Required
6061
- Purpose
6162
* - ``name``
62-
- ``octo-org/security-queries``
63+
- ``name: octo-org/security-queries``
6364
- All packs
64-
- The scope, where the CodeQL pack is published, and the name of the pack defined using alphanumeric characters and hyphens. It must be unique as CodeQL cannot differentiate between CodeQL packs with identical names. Name components cannot start or end with a hyphen. Additionally, a period is not allowed in pack names at all. Use the pack name to specify queries to run using ``database analyze`` and to define dependencies between QL packs (see examples below).
65+
- The scope, where the CodeQL pack is published, and the name of the pack defined using alphanumeric characters and hyphens. It must be unique as CodeQL cannot differentiate between CodeQL packs with identical names. Use the pack name to specify queries to run using ``database analyze`` and to define dependencies between QL packs (see examples below).
6566
* - ``version``
66-
- ``0.0.0``
67+
- ``version: 0.0.0``
6768
- All packs
68-
- A version range for this CodeQL pack. This must be a valid semantic version that meets the `SemVer v2.0.0 specification <https://semver.org/spec/v2.0.0.html>`__.
69+
- A semantic version for this CodeQL pack that must adhere to the `SemVer v2.0.0 specification <https://semver.org/spec/v2.0.0.html>`__.
6970
* - ``dependencies``
70-
- ``codeql/javascript-all: ^1.2.3``
71-
- Optional
72-
- The names and version ranges of any CodeQL packs that this pack depends on, as a mapping. This gives the pack access to any libraries, database schema, and query suites defined in the dependency. For more information, see `SemVer ranges <https://docs.npmjs.com/cli/v6/using-npm/semver#ranges>`__ in the NPM documentation.
71+
- .. code-block:: yaml
72+
73+
dependencies:
74+
codeql/cpp-all: ^0.0.2
75+
76+
- Packs that define CodeQL package dependencies on other packs
77+
- A map from pack references to the semantic version range that is compatible with this pack. Supported from CLI version v2.6.0 and onwards.
78+
* - ``defaultSuiteFile``
79+
- ``defaultSuiteFile: cpp-code-scanning.qls``
80+
- Packs that export a set of default queries to run
81+
- The path to a query suite file containing all of the queries that are run by default when this pack is passed to the ``codeql database analyze`` command. Supported from CLI version v2.6.0 and onwards.
82+
* - ``defaultSuite``
83+
- .. code-block:: yaml
84+
85+
defaultSuite:
86+
queries: .
87+
exclude:
88+
precision: medium
89+
90+
- Packs that export a set of default queries to run
91+
- An inlined query suite containing all of the queries that are run by default when this pack is passed to the ``codeql database analyze`` command. Supported from CLI version v2.6.0 and onwards.
92+
* - ``library``
93+
- ``library: true``
94+
- Library packs
95+
- A boolean value that indicates whether this pack is a library pack. Library packs do not contain queries and are not compiled. Query packs can ignore this field or explicitly set it to ``false``.
7396
* - ``suites``
74-
- ``octo-org-query-suites``
97+
- ``suites: octo-org-query-suites``
7598
- Optional
7699
- The path to a directory in the pack that contains the query suites you want to make known to the CLI, defined relative to the pack directory. QL pack users can run "well-known" suites stored in this directory by specifying the pack name, without providing their full path. This is not supported for CodeQL packs downloaded from a package registry. For more information about query suites, see ":doc:`Creating CodeQL query suites <creating-codeql-query-suites>`."
77100
* - ``extractor``
@@ -82,23 +105,166 @@ The following properties are supported in ``qlpack.yml`` files.
82105
- ``.``
83106
- Optional for test packs
84107
- The path to a directory within the pack that contains tests, defined relative to the pack directory. Use ``.`` to specify the whole pack. Any queries in this directory are run as tests when ``test run`` is run with the ``--strict-test-discovery`` option. These queries are ignored by query suite definitions that use ``queries`` or ``qlpack`` instructions to ask for all queries in a particular pack.
85-
* - ``dbscheme``
108+
* - ``dbscheme`` TODO: Internal only. Delete?
86109
- ``semmlecode.python.dbscheme``
87110
- Core language packs only
88111
- The path to the :ref:`database schema <codeql-database-schema>` for all libraries and queries written for this CodeQL language (see example below).
89-
* - ``upgrades``
112+
* - ``upgrades`` TODO: Internal only. Delete?
90113
- ``.``
91114
- Core language packs only
92-
- The path to a directory within the pack that contains upgrade scripts, defined relative to the pack directory. The ``database upgrade`` action uses these scripts to update databases that were created by an older version of an extractor so they're compatible with the current extractor (see `Upgrade scripts for a language <#upgrade-scripts-for-a-language>`__ below.)
115+
- The path to a directory within the pack that contains upgrade scripts, defined relative to the pack directory. The ``database upgrade`` action uses these scripts to upgrade databases that were created by an older version of an extractor or downgrade databases that were created by a newer version of the extractor so they're compatible with the current extractor (see `Upgrade scripts for a language <#upgrade-scripts-for-a-language>`__ below.)
93116
* - ``authors``
94117
- ``example@github.com``
95118
- All packs
96119
- Metadata that will be displayed on the packaging search page in the packages section of the account that the CodeQL pack is published to.
97120
* - ``licenses``
98121
- ``(LGPL-2.1 AND MIT)``
99122
- All packs
100-
- Metadata that will be displayed on the packaging search page in the packages section of the account that the CodeQL pack is published to. For a list of allowed licenses, see `SPDX License List <https://spdx.org/licenses/>`__ in the SPDX Specification.
123+
- Metadata that will be displayed on the packaging search page in the packages section of the account that the CodeQL pack is published to. For a list of allowed licenses, see `SPDX License List <https://spdx.org/licenses/>`__ in the SPDX Specification.
101124
* - ``description``
102125
- ``Human-readable description of the contents of the CodeQL pack.``
103126
- All packs
104127
- Metadata that will be displayed on the packaging search page in the packages section of the account that the CodeQL pack is published to.
128+
129+
.. _about-codeql-pack-lock:
130+
131+
About ``codeql-pack.lock.yml`` files
132+
------------------------------------
133+
134+
``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`` 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 anyone running ``codeql pack install`` on this the pack will always retrieve the same versions of dependencies even if newer compatible versions exist. Only query packs can have ``codeql-pack.lock.yml`` files. This is because library packs are not compiled independently of query packs. Without being compiled, there is no need to specify precise versions of their dependencies.
135+
136+
For example, if a ``qlpack.yml`` file contains the following dependencies:
137+
138+
.. code-block:: yaml
139+
140+
dependencies:
141+
codeql/cpp-all: ^0.1.2
142+
my-user/my-lib: ^0.2.3
143+
other-dependency/from-source: "*"
144+
145+
The ``codeql-pack.lock.yml`` file will contain something like the following:
146+
147+
.. code-block:: yaml
148+
149+
dependencies:
150+
codeql/cpp-all:
151+
version: 0.1.4
152+
my-user/my-lib:
153+
version: 0.2.4
154+
my-user/transitive-dependency:
155+
version: 1.2.4
156+
157+
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.
158+
159+
.. _custom-codeql-packs:
160+
161+
Examples of custom CodeQL packs
162+
-------------------------------
163+
164+
When you write custom queries or tests, you should save them in custom CodeQL packs. For simplicity, try to organize each pack logically. For more information, see `CodeQL pack structure <#codeql-pack-structure>`__. Save files for queries and tests in separate packs and, where possible, organize custom packs into specific folders for each target language. This is particuarly useful if you intend to publish your CodeQL packs so they can be shared with others or used in GitHub `Code scanning <https://docs.github.com/en/code-security/secure-coding/automatically-scanning-your-code-for-vulnerabilities-and-errors/about-code-scanning>`__.
165+
166+
CodeQL packs for custom queries
167+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
168+
169+
A custom QL pack for queries must include a ``qlpack.yml`` file at the pack root, containing ``name``, ``version``, and ``dependencies`` properties. If the pack contains query suites, you can use the ``suites`` property to define their location. Query suites defined here are called "well-known" suites, and can be used on the command line by referring to their name only, rather than their full path. For more information about query suites, see ":doc:`Creating CodeQL query suites <creating-codeql-query-suites>`."
170+
171+
For example, a ``qlpack.yml`` file for a QL pack featuring custom C++ queries
172+
and libraries may contain:
173+
174+
.. code-block:: yaml
175+
176+
name: my-github-user/my-custom-queries
177+
version: 1.2.3
178+
dependencies:
179+
codeql/cpp-all: ^0.1.2
180+
suites: my-custom-suites
181+
182+
where ``codeql/cpp-all`` is the name of the QL 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``.
183+
184+
CodeQL packs for custom test files
185+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
186+
187+
For custom CodeQL packs containing test files, you also need to include an
188+
``extractor`` property so that the ``test run`` command knows how to create test
189+
databases. You may also wish to specify the ``tests`` property.
190+
191+
.. include:: ../reusables/test-qlpack.rst
192+
193+
For more information about running tests, see ":doc:`Testing custom queries
194+
<testing-custom-queries>`."
195+
196+
.. _standard-ql-packs:
197+
198+
Examples of CodeQL packs in the CodeQL repository
199+
---------------------------------------------~~~~
200+
201+
Each of the languages in the CodeQL repository has four main CodeQL packs:
202+
203+
- Core library pack for the language, with the :ref:`database schema <codeql-database-schema>`
204+
used by the language, and CodeQL libraries, and queries at ``ql/<language>/ql/lib``
205+
- Core query pack for the language that includes the default queries for the language, along
206+
with their query suites at ``ql/<language>/ql/src``
207+
- Tests for the core language libraries and queries at ``ql/<language>/ql/test``
208+
- Example queries for the language at ``ql/<language>/ql/examples``
209+
210+
Core library pack
211+
~~~~~~~~~~~~~~~~~
212+
213+
The ``qlpack.yml`` file for a core library pack uses the following properties:
214+
``name``, ``version``, ``dbscheme``.
215+
The ``dbscheme`` property should only be defined in the core QL
216+
pack for a language.
217+
218+
For example, the ``qlpack.yml`` file for `C/C++ analysis libraries
219+
<https://github.com/github/codeql/blob/main/cpp/ql/lib/qlpack.yml>`__
220+
contains:
221+
222+
.. code-block:: yaml
223+
224+
name: codeql/cpp-all
225+
version: x.y.z-dev
226+
dbscheme: semmlecode.cpp.dbscheme
227+
library: true
228+
upgrades: upgrades
229+
230+
Core query pack
231+
~~~~~~~~~~~~~~~
232+
233+
The ``qlpack.yml`` file for a core query pack uses the following properties:
234+
``name``, ``version``, ``suites``, ``defaultSuiteFile``, ``dependencies`` .
235+
236+
For example, the ``qlpack.yml`` file for `C/C++ analysis queries
237+
<https://github.com/github/codeql/blob/main/cpp/ql/lib/qlpack.yml>`__
238+
contains:
239+
240+
.. code-block:: yaml
241+
242+
name: codeql/cpp-queries
243+
version: x.y.z-dev
244+
dependencies:
245+
codeql/cpp-all: "*"
246+
codeql/suite-helpers: "*"
247+
suites: codeql-suites
248+
defaultSuiteFile: codeql-suites/cpp-code-scanning.qls
249+
250+
This query pack depends on ``codeql/cpp-all`` and ``codeql/suite-helpers``. Since these dependencies are resolved from source, it does not matter what version of the CodeQL pack they are compatible with.
251+
252+
Tests for the core QL pack
253+
~~~~~~~~~~~~~~~~~~~~~~~~~~
254+
255+
The ``qlpack.yml`` file for the tests for the core QL packs use the following properties: ``name``, ``dependencies``, ``extractor``, ``tests``.
256+
257+
For example, the ``qlpack.yml`` file for `C/C++ analysis tests
258+
<https://github.com/github/codeql/blob/main/cpp/ql/test/qlpack.yml>`__
259+
contains:
260+
261+
.. code-block:: yaml
262+
263+
name: codeql/cpp-tests
264+
dependencies:
265+
codeql/cpp-all: "*"
266+
codeql/cpp-queries: "*"
267+
extractor: cpp
268+
tests: .
269+
270+
The ``dependencies`` property always specifies the core QL pack. The ``extractor`` property specifies that all the tests will use the same C++ extractor to create the database for the tests. The ``tests`` property specifies the location of the tests. In this case, the tests are in the root folder (and all sub-folders) of the pack. Note that there is no ``version`` property for the tests pack since this pack is not meant to be published.

0 commit comments

Comments
 (0)