Skip to content

Commit 69ba22a

Browse files
committed
QL handbook: bring library path documentation up to date
1 parent 23532ae commit 69ba22a

File tree

1 file changed

+76
-5
lines changed

1 file changed

+76
-5
lines changed

docs/language/ql-handbook/language.rst

Lines changed: 76 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,79 @@ 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 an ordered list of directory locations that is used
49+
in for resolving module imports, described below. It 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+
tooling also allows specifying it explicitly on the command line for a
53+
particular invocation, though that is that is rarely done, and only
54+
useful in very special situation. 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, containing a single top-level tag named
67+
``queries``, which has a ``language`` attribute set to the identifier
68+
of the active database scheme (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+
its content is described in the CodeQL CLI documentation. This file
74+
will not be recognized when using older QL tooling that is not based
75+
on the CodeQL CLI (that is, LGTM.com, LGTM Enterprise, Odasa, QL for
76+
Eclipse, and QL 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+
The query directory itself becomes the first element of the library
83+
path.
84+
85+
In old tooling that doesn't recognize ``qlpack.yml``, the default
86+
value of the rest of the library path is hard-coded in the tooling for
87+
each supporting language. It contains directories within the Odasa
88+
distribution that define the default CodeQL libraries for the selected
89+
language. Which language to use depends on the ``language`` attibute
90+
of the ``queries.xml`` file if not overridden with a ``--language``
91+
option to Odasa.
92+
93+
On the other hand, the CodeQL CLI and newer tooling based on it (e.g.,
94+
GitHub Code Scanning and the Visual Stidio Code extension for CodeQL)
95+
constructs a default library path using QL packs. For each QL pack
96+
added to the language path, the QL packs named in its
97+
``libraryPathDependencies`` will be subsequently added to the library
98+
path, and the process continues until all packs have been
99+
resolved. The actual library path consists of the root directories of
100+
the selected QL packs. This process depends on a mechanism for finding
101+
QL packs by pack name, as described in the CodeQL CLI documentation.
102+
103+
When the query directory contains a ``queries.xml`` file but no
104+
``qlpack.yml``, the QL pack resolution behaves as if it defines a QL
105+
pack with no name and a single library-path dependency named
106+
``legacy-libraries-LANGUAGE`` where ``LANGUAGE`` is taken from
107+
``queries.xml``. The ``github/codeql`` repository provides packs with
108+
names following this pattern, which themselves depend on the actual
109+
CodeQL libraries for each language.
110+
111+
When the query directory contains neither ``queries.xml`` nor
112+
``qlpack.yml`` it will be considered to be a QL pack with no name and
113+
no library dependencies. This causes the library path to consist of
114+
*only* the query directory itself, which is not generally useful --
115+
but will suffice to run toy examples of QL code that don't actually
116+
use information from the database.
117+
45118
Name resolution
46119
---------------
47120

@@ -162,11 +235,9 @@ For selection identifiers (``a::b``):
162235

163236
For qualified identifiers (``a.b``):
164237

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).
238+
- Build up a list of *candidate search paths*, consisting of the
239+
current file's directory, and each of the directories on the
240+
*library path* (in order).
170241

171242
- 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.
172243

0 commit comments

Comments
 (0)