Debian Codegraph @

python/docs/index.rst

raw
1 =================================
2 Using third-party Python packages
3 =================================
4
5 Mach and its associated commands have a variety of 3rd-party Python dependencies. Many of these
6 are vendored in ``third_party/python``, while others are installed at runtime via ``pip``.
7
8 The dependencies of Mach itself can be found at ``python/sites/mach.txt``. Mach commands
9 may have additional dependencies which are specified at ``python/sites/<site>.txt``.
10
11 For example, the following Mach command would have its 3rd-party dependencies declared at
12 ``python/sites/foo.txt``.
13
14 .. code:: python
15
16 @Command(
17 "foo-it",
18 virtualenv_name="foo",
19 )
20 # ...
21 def foo_it_command():
22 import specific_dependency
23
24 The format of ``<site>.txt`` files are documented further in the
25 :py:class:`~mach.requirements.MachEnvRequirements` class.
26
27 Adding a Python package
28 =======================
29
30 There's two ways of using 3rd-party Python dependencies:
31
32 * :ref:`pip install the packages <python-pip-install>`. Python dependencies with native code must
33 be installed using ``pip``. This is the recommended technique for adding new Python dependencies.
34 * :ref:`Vendor the source of the Python package in-tree <python-vendor>`. Dependencies of the Mach
35 core logic or of building Firefox itself must be vendored.
36
37 .. note::
38
39 For dependencies that meet both restrictions (dependency of Mach/build, *and* has
40 native code), see the :ref:`mach-and-build-native-dependencies` section below.
41
42 .. _python-pip-install:
43
44 ``pip install`` the package
45 ~~~~~~~~~~~~~~~~~~~~~~~~~~~
46
47 To add a ``pip install``-d package dependency, add it to your site's
48 ``python/sites/<site>.txt`` manifest file:
49
50 .. code:: text
51
52 ...
53 pypi:new-package==<version>
54 ...
55
56 .. note::
57
58 Some tasks are not permitted to use external resources, and for those we can
59 publish packages to an internal PyPI mirror.
60 See `how to upload to internal PyPI <https://wiki.mozilla.org/ReleaseEngineering/How_To/Upload_to_internal_Pypi>`_
61 for more details.
62
63 .. _python-vendor:
64
65 Vendoring Python packages
66 ~~~~~~~~~~~~~~~~~~~~~~~~~
67
68 To vendor a Python package run ``./mach vendor python --add
69 <package>~=<major>.<minor>``. This will add your dependency to
70 ``third_party/python/pyproject.toml`` then begin the re-vendoring process
71 for all dependencies. The `pyproject.toml` is used by ``uv`` to create a
72 lockfile (``uv.lock``) that ensures all the dependencies are compatible.
73 This lockfile is then used to generate a ``third_party/python/requirements.txt``
74 which is then used by ``pip`` to download all dependencies into the
75 ``third_party/python`` directory.
76
77 .. note::
78 The dependency you are attempting to add may not be compatible with what's
79 already vendored. In this case, the lockfile generation/dependency
80 resolution will fail with an error message along the lines of ``No
81 solution found when resolving dependencies:``. You may be able to get
82 around this by pinning your dependency to a newer or older version. If
83 that doesn't work you can try modifying the pin(s) of the already vendored
84 dependency(ies) that are causing the conflict(s).
85
86 Beware that this is a rather painful process. Changing the version of an
87 already vendored dependency may break functionality somewhere in the codebase.
88 This means that even if you get ``uv`` to make a compatible lockfile, you
89 may have caused a breakage somewhere else that ``uv`` cannot foresee. It is
90 your responsibility to fix anything you break, otherwise your changes will be
91 rejected or backed out if the issue isn't discovered until after landing.
92
93 If you change pins for packages to workaround issues, please add comments in the
94 ``third_party/python/pyproject.toml`` for each necessary pin indicating why it's
95 needed and which dependency(ies) need it. Doing so will make it much easier for
96 the next person that comes along trying to do the same thing.
97
98 After the ``./mach vendor python`` completes successfully, you'll need to add that package
99 and any new transitive dependencies (you'll see them added in ``third_party/python/requirements.txt``)
100 to the associated site's dependency manifest in ``python/sites/<site>.txt``:
101
102 .. code:: text
103
104 ...
105 vendored:third_party/python/new-package
106 vendored:third_party/python/new-package-dependency-foo
107 vendored:third_party/python/new-package-dependency-bar
108 ...
109
110 To remove a vendored package run ``./mach vendor python --remove <package>``. This re-creates the lockfile
111 with that dependency removed (along with any transitive dependencies that aren't shared) and re-vendor
112 everything.
113
114 .. note::
115 - You can add or remove multiple packages at the same time: ``./mach vendor python --add <package_one> --add <package_two>``
116 - If desired, you can add/remove dependencies manually in the ``third_party/python/pyproject.toml``. Once you've made your changes, just run ``./mach vendor python`` without the ``--add`` and/or ``--remove`` arguments.
117
118 After the ``./mach vendor python`` completes successfully you'll need to remove the package and transitive
119 dependencies from all the site manifest files (``python/sites/<site>.txt``) that used the removed package(s).
120
121 .. note::
122
123 The following policy applies to **ALL** vendored packages:
124
125 * Vendored PyPI libraries **MUST NOT** be modified
126 * Vendored libraries **SHOULD** be released copies of libraries available on
127 PyPI.
128
129 * When considering manually vendoring a package, discuss the situation with
130 the ``#build`` team to ensure that other, more maintainable options are exhausted.
131
132 .. note::
133
134 We require that it is possible to build Firefox using only a checkout of the source,
135 without depending on a package index. This ensures that building Firefox is
136 deterministic and dependable, avoids packages from changing out from under us,
137 and means we’re not affected when 3rd party services are offline. We don't want a
138 DoS against PyPI or a random package maintainer removing an old tarball to delay
139 a Firefox chemspill. Therefore, packages required by Mach core logic or for building
140 Firefox itself must be vendored.
141
142 If the vendored dependencies in the ``third_party/python/pyproject.toml`` are not pinned with
143 ``==``, they can be automatically upgraded. You can upgrade either a single package,
144 or all packages.
145
146 To upgrade an individual unpinned package just run ``./mach vendor python --upgrade-package <package>``. You can also update
147 multiple specific packages at the same time: ``./mach vendor python --upgrade-package <package_one> --upgrade-package <package_two>``
148
149 To upgrade all unpinned packages just run ``./mach vendor python --upgrade``.
150
151 For both cases the process is essentially the same. ``uv`` is invoked and it will determine if there is/are
152 newer versions available. If there aren't any compatible upgrades available then nothing will be vendored. If
153 there are, then everything will be re-vendored.
154
155 .. note::
156 If an upgrade adds new transitive dependencies, you will need to add them to the site(s) manifest files
157 (the same as you need to when adding a new package).
158
159 By default ``./mach vendor python`` only fully runs if changes are detected in the ``uv.lock`` file. If you
160 want to force the full vendor to run, just add ``--force``.
161
162
163 If the package contains optional native dependencies, they won't be compiled as
164 part of the vendored package. It is however possible to prefer the pypi version
165 which may contain the native bits, while allowing to fallback to the vendored
166 version:
167
168 .. code:: text
169
170 ...
171 vendored-fallback:pypi-package-name:third_party/python/new-package:explanation
172 ...
173
174
175 .. _mach-and-build-native-dependencies:
176
177 Mach/Build Native 3rd-party Dependencies
178 ========================================
179
180 There are cases where Firefox is built without being able to ``pip install``, but where
181 native 3rd party Python dependencies enable optional functionality. This can't be solved
182 by vendoring the platform-specific libraries, as then each one would have to be stored
183 multiple times in-tree according to how many platforms we wish to support.
184
185 Instead, this is solved by pre-installing such native packages onto the host system
186 in advance, then having Mach attempt to use such packages directly from the system.
187 This feature is only viable in very specific environments, as the system Python packages
188 have to be compatible with Mach's vendored packages.
189
190 .. note:
191
192 All of these native build-specific dependencies **MUST** be optional requirements
193 as to support the "no strings attached" builds that only use vendored packages.
194
195 To control this behaviour, the ``MACH_BUILD_PYTHON_NATIVE_PACKAGE_SOURCE`` environment
196 variable can be used:
197
198 .. list-table:: ``MACH_BUILD_PYTHON_NATIVE_PACKAGE_SOURCE``
199 :header-rows: 1
200
201 * - ``MACH_BUILD_PYTHON_NATIVE_PACKAGE_SOURCE``
202 - Behaviour
203 * - ``"pip"``
204 - Mach will ``pip install`` all needed dependencies from PyPI at runtime into a Python
205 virtual environment that's reused in future Mach invocations.
206 * - ``"none"``
207 - Mach will perform the build using only vendored packages. No Python virtual environment
208 will be created for Mach.
209 * - ``"system"``
210 - Mach will use the host system's Python packages as part of doing the build. This option
211 allows the usage of native Python packages without leaning on a ``pip install`` at
212 build-time. This is generally slower because the system Python packages have to
213 be asserted to be compatible with Mach. Additionally, dependency lockfiles are ignored,
214 so there's higher risk of breakage. Finally, as with ``"none"``, no Python virtualenv
215 environment is created for Mach.
216 * - ``<unset>``
217 - Same behaviour as ``"pip"`` if ``MOZ_AUTOMATION`` isn't set. Otherwise, uses
218 the same behaviour as ``"none"``.
219
220 There's a couple restrictions here:
221
222 * ``MACH_BUILD_PYTHON_NATIVE_PACKAGE_SOURCE`` only applies to the top-level ``"mach"`` site,
223 the ``"common"`` site and the ``"build"`` site. All other sites will use ``pip install`` at
224 run-time as needed.
225
226 * ``MACH_BUILD_PYTHON_NATIVE_PACKAGE_SOURCE="system"`` is not allowed when using any site other
227 than ``"mach"``, ``"common"`` or ``"build"``, because:
228
229 * As described in :ref:`package-compatibility` below, packages used by Mach are still
230 in scope when commands are run, and
231 * The host system is practically guaranteed to be incompatible with commands' dependency
232 lockfiles.
233
234 The ``MACH_BUILD_PYTHON_NATIVE_PACKAGE_SOURCE`` environment variable fits into the following use
235 cases:
236
237 Mozilla CI Builds
238 ~~~~~~~~~~~~~~~~~
239
240 We need access to the native packages of ``zstandard`` and ``psutil`` to extract archives and
241 get OS information respectively. Use ``MACH_BUILD_PYTHON_NATIVE_PACKAGE_SOURCE="system"``.
242
243 Mozilla CI non-Build Tasks
244 ~~~~~~~~~~~~~~~~~~~~~~~~~~
245
246 We generally don't want to create a Mach virtual environment to avoid redundant processing,
247 but it's ok to ``pip install`` for specific command sites as needed, so leave
248 ``MACH_BUILD_PYTHON_NATIVE_PACKAGE_SOURCE`` unset (``MOZ_AUTOMATION`` implies the default
249 behaviour of ``MACH_BUILD_PYTHON_NATIVE_PACKAGE_SOURCE="none"``).
250
251 In cases where native packages *are* needed by Mach, use
252 ``MACH_BUILD_PYTHON_NATIVE_PACKAGE_SOURCE="pip"``.
253
254 Downstream CI Builds
255 ~~~~~~~~~~~~~~~~~~~~
256
257 Sometimes these builds happen in sandboxed, network-less environments, and usually these builds
258 don't need any of the behaviour enabled by installing native Python dependencies.
259 Use ``MACH_BUILD_PYTHON_NATIVE_PACKAGE_SOURCE="none"``.
260
261 Gentoo Builds
262 ~~~~~~~~~~~~~
263
264 When installing Firefox via the package manager, Gentoo generally builds it from source rather than
265 distributing a compiled binary artifact. Accordingly, users doing a build of Firefox in this
266 context don't want stray files created in ``~/.mozbuild`` or unnecessary ``pip install`` calls.
267 Use ``MACH_BUILD_PYTHON_NATIVE_PACKAGE_SOURCE="none"``.
268
269 Firefox Developers
270 ~~~~~~~~~~~~~~~~~~
271
272 Leave ``MACH_BUILD_PYTHON_NATIVE_PACKAGE_SOURCE`` unset so that all Mach commands can be run,
273 Python dependency lockfiles are respected, and optional behaviour is enabled by installing
274 native packages.
275
276 .. _package-compatibility:
277
278 Package compatibility
279 =====================
280
281 Mach requires that all commands' package requirements be compatible with those of Mach itself.
282 (This is because functions and state created by Mach are still usable from within the commands, and
283 they may still need access to their associated 3rd-party modules).
284
285 However, it is OK for Mach commands to have package requirements which are incompatible with each
286 other. This allows the flexibility for some Mach commands to depend on modern dependencies while
287 other, more mature commands may still only be compatible with a much older version.
288
289 .. note::
290
291 Only one version of a package may be vendored at any given time. If two Mach commands need to
292 have conflicting packages, then at least one of them must ``pip install`` the package instead
293 of vendoring.
294
295 If a Mach command's dependency conflicts with a vendored package, and that vendored package
296 isn't needed by Mach itself, then that vendored dependency should be moved from
297 ``python/sites/mach.txt`` to its associated environment.