Skip to content

Commit d2d235d

Browse files
authored
Merge pull request github#3476 from hmakholm/pr/module-res-update
QL language specification: bring library path documentation up to date
2 parents e5480e4 + c2c70d7 commit d2d235d

File tree

1 file changed

+74
-5
lines changed

1 file changed

+74
-5
lines changed

docs/language/ql-handbook/language.rst

Lines changed: 74 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,76 @@ A QL program can be *evaluated* (see `Evaluation <#evaluation>`__) to produce a
4242

4343
For a QL program to be *valid*, it must conform to a variety of conditions that are described throughout this specification; otherwise the program is said to be *invalid*. An implementation of QL must detect all invalid programs and refuse to evaluate them.
4444

45+
Library path
46+
------------
47+
48+
The library path is an ordered list of directory locations. It is used
49+
for resolving module imports (see `Module resolution <#module-resolution>`__). The library path is not strictly
50+
speaking a core part of the QL language, since different
51+
implementations of QL construct it in slightly different ways. Most QL
52+
tools also allow you to explicitly specify the library path on the command line for a
53+
particular invocation, though that is rarely done, and only
54+
useful in very special situations. This section describes the default
55+
construction of the library path.
56+
57+
First, determine the *query directory* of the ``.ql`` file being
58+
compiled. Starting with the directory containing the ``.ql`` file, and
59+
walking up the directory structure, each directory is checked for a
60+
file called ``queries.xml`` or ``qlpack.yml``. The first directory
61+
where such a file is found is the query directory. If there is no such
62+
directory, the directory of the ``.ql`` file itself is the query
63+
directory.
64+
65+
A ``queries.xml`` file that defines a query directory must always
66+
contain a single top-level tag named
67+
``queries``, which has a ``language`` attribute set to the identifier
68+
of the active database schema (for example, ``<queries
69+
language="java"/>``).
70+
71+
A ``qlpack.yml`` file defines a `QL pack
72+
<https://help.semmle.com/codeql/codeql-cli/reference/qlpack-overview.html>`__.
73+
The content of a ``qlpack.yml`` file is described in the CodeQL CLI documentation. This file
74+
will not be recognized when using legacy tools that are not based
75+
on the CodeQL CLI (that is, LGTM.com, LGTM Enterprise, ODASA, CodeQL for
76+
Eclipse, and CodeQL for Visual Studio).
77+
78+
If both a ``queries.xml`` and a ``qlpack.yml`` exist in the same
79+
directory, the latter takes precedence (and the former is assumed to
80+
exist for compatibility with older tooling).
81+
82+
In legacy QL tools that don't recognize ``qlpack.yml`` files, the default
83+
value of the library path for
84+
each supported language is hard-coded. The tools contain directories within the ODASA
85+
distribution that define the default CodeQL libraries for the selected
86+
language. Which language to use depends on the ``language`` attribute
87+
of the ``queries.xml`` file if not overridden with a ``--language``
88+
option to the ODASA CLI.
89+
90+
On the other hand, the CodeQL CLI and newer tools based on it (such as
91+
GitHub Code Scanning and the CodeQL extension for Visual Studio Code)
92+
construct a library path using QL packs. For each QL pack
93+
added to the library path, the QL packs named in its
94+
``libraryPathDependencies`` will be subsequently added to the library
95+
path, and the process continues until all packs have been
96+
resolved. The actual library path consists of the root directories of
97+
the selected QL packs. This process depends on a mechanism for finding
98+
QL packs by pack name, as described in the `CodeQL CLI documentation <https://help.semmle.com/codeql/codeql-cli.html>`__.
99+
100+
When the query directory contains a ``queries.xml`` file but no
101+
``qlpack.yml``, the QL pack resolution behaves as if it defines a QL
102+
pack with no name and a single library path dependency named
103+
``legacy-libraries-LANGUAGE`` where ``LANGUAGE`` is taken from
104+
``queries.xml``. The ``github/codeql`` repository provides packs with
105+
names following this pattern, which themselves depend on the actual
106+
CodeQL libraries for each language.
107+
108+
When the query directory contains neither a ``queries.xml`` nor
109+
``qlpack.yml`` file, it is considered to be a QL pack with no name and
110+
no library dependencies. This causes the library path to consist of
111+
*only* the query directory itself. This is not generally useful,
112+
but it suffices for running toy examples of QL code that don't
113+
use information from the database.
114+
45115
Name resolution
46116
---------------
47117

@@ -162,11 +232,10 @@ For selection identifiers (``a::b``):
162232

163233
For qualified identifiers (``a.b``):
164234

165-
- Define the *current file* as the file the import directive occurs in.
166-
167-
- Determine the current file's *query directory*, if any. Starting with the directory containing the current file, and walking up the directory structure, each directory is checked for a file called ``queries.xml``, containing a single top-level tag named ``queries``, which has a ``language`` attribute set to the identifier of the active database scheme (for example, ``<queries language="java"/>``). The closest enclosing directory is taken as the current file's query directory.
168-
169-
- Build up a list of *candidate search paths*, consisting of the current file's directory, the current file's query directory (if one was determined in the previous step), and the list of directories making up the library path (in order).
235+
- Build up a list of *candidate search paths*, consisting of the
236+
current file's directory, then the *query directory* of the current
237+
file, and finally each of the directories on the
238+
`library path <#library-path>`__ (in order).
170239

171240
- Determine the first candidate search path that has a *matching* QLL file for the import directive's qualified name. A QLL file in a candidate search path is said to match a qualified name if, starting from the candidate search path, there is a subdirectory for each successive qualifier in the qualified name, and the directory named by the final qualifier contains a file whose base name matches the qualified name's base name, with the addition of the file extension ``.qll``. The file and directory names are matched case-sensitively, regardless of whether the filesystem is case-sensitive or not.
172241

0 commit comments

Comments
 (0)