Anki/docs/development.md
Damien Elmes 5e0a761b87
Move away from Bazel (#2202)
(for upgrading users, please see the notes at the bottom)

Bazel brought a lot of nice things to the table, such as rebuilds based on
content changes instead of modification times, caching of build products,
detection of incorrect build rules via a sandbox, and so on. Rewriting the build
in Bazel was also an opportunity to improve on the Makefile-based build we had
prior, which was pretty poor: most dependencies were external or not pinned, and
the build graph was poorly defined and mostly serialized. It was not uncommon
for fresh checkouts to fail due to floating dependencies, or for things to break
when trying to switch to an older commit.

For day-to-day development, I think Bazel served us reasonably well - we could
generally switch between branches while being confident that builds would be
correct and reasonably fast, and not require full rebuilds (except on Windows,
where the lack of a sandbox and the TS rules would cause build breakages when TS
files were renamed/removed).

Bazel achieves that reliability by defining rules for each programming language
that define how source files should be turned into outputs. For the rules to
work with Bazel's sandboxing approach, they often have to reimplement or
partially bypass the standard tools that each programming language provides. The
Rust rules call Rust's compiler directly for example, instead of using Cargo,
and the Python rules extract each PyPi package into a separate folder that gets
added to sys.path.

These separate language rules allow proper declaration of inputs and outputs,
and offer some advantages such as caching of build products and fine-grained
dependency installation. But they also bring some downsides:

- The rules don't always support use-cases/platforms that the standard language
tools do, meaning they need to be patched to be used. I've had to contribute a
number of patches to the Rust, Python and JS rules to unblock various issues.
- The dependencies we use with each language sometimes make assumptions that do
not hold in Bazel, meaning they either need to be pinned or patched, or the
language rules need to be adjusted to accommodate them.

I was hopeful that after the initial setup work, things would be relatively
smooth-sailing. Unfortunately, that has not proved to be the case. Things
frequently broke when dependencies or the language rules were updated, and I
began to get frustrated at the amount of Anki development time I was instead
spending on build system upkeep. It's now about 2 years since switching to
Bazel, and I think it's time to cut losses, and switch to something else that's
a better fit.

The new build system is based on a small build tool called Ninja, and some
custom Rust code in build/. This means that to build Anki, Bazel is no longer
required, but Ninja and Rust need to be installed on your system. Python and
Node toolchains are automatically downloaded like in Bazel.

This new build system should result in faster builds in some cases:

- Because we're using cargo to build now, Rust builds are able to take advantage
of pipelining and incremental debug builds, which we didn't have with Bazel.
It's also easier to override the default linker on Linux/macOS, which can
further improve speeds.
- External Rust crates are now built with opt=1, which improves performance
of debug builds.
- Esbuild is now used to transpile TypeScript, instead of invoking the TypeScript
compiler. This results in faster builds, by deferring typechecking to test/check
time, and by allowing more work to happen in parallel.

As an example of the differences, when testing with the mold linker on Linux,
adding a new message to tags.proto (which triggers a recompile of the bulk of
the Rust and TypeScript code) results in a compile that goes from about 22s on
Bazel to about 7s in the new system. With the standard linker, it's about 9s.

Some other changes of note:

- Our Rust workspace now uses cargo-hakari to ensure all packages agree on
available features, preventing unnecessary rebuilds.
- pylib/anki is now a PEP420 implicit namespace, avoiding the need to merge
source files and generated files into a single folder for running. By telling
VSCode about the extra search path, code completion now works with generated
files without needing to symlink them into the source folder.
- qt/aqt can't use PEP420 as it's difficult to get rid of aqt/__init__.py.
Instead, the generated files are now placed in a separate _aqt package that's
added to the path.
- ts/lib is now exposed as @tslib, so the source code and generated code can be
provided under the same namespace without a merging step.
- MyPy and PyLint are now invoked once for the entire codebase.
- dprint will be used to format TypeScript/json files in the future instead of
the slower prettier (currently turned off to avoid causing conflicts). It can
automatically defer to prettier when formatting Svelte files.
- svelte-check is now used for typechecking our Svelte code, which revealed a
few typing issues that went undetected with the old system.
- The Jest unit tests now work on Windows as well.

If you're upgrading from Bazel, updated usage instructions are in docs/development.md and docs/build.md. A summary of the changes:

- please remove node_modules and .bazel
- install rustup (https://rustup.rs/)
- install rsync if not already installed  (on windows, use pacman - see docs/windows.md)
- install Ninja (unzip from https://github.com/ninja-build/ninja/releases/tag/v1.11.1 and
  place on your path, or from your distro/homebrew if it's 1.10+)
- update .vscode/settings.json from .vscode.dist
2022-11-27 15:24:20 +10:00

5.7 KiB

Anki development

Packaged betas

For non-developers who want to try beta versions, the easiest way is to use a packaged version - please see:

https://betas.ankiweb.net/

Pre-built Python wheels

Pre-built Python packages are available on PyPI. They are useful if you wish to:

  • Run Anki from a local Python installation without building it yourself
  • Get code completion when developing add-ons
  • Make command line scripts that modify .anki2 files via Anki's Python libraries

You will need the 64 bit version of Python 3.9 or 3.10 installed. 3.9 is recommended, as Anki has only received minimal testing on 3.10 so far, and some dependencies have not been fully updated yet. On Windows, currently only 3.9 will work. You can install Python from python.org or from your distro.

For further instructions, please see https://betas.ankiweb.net/#via-pypipip. Note that in the provided commands, --pre tells pip to fetch alpha/beta versions. If you remove --pre, it will download the latest stable version instead.

Building from source

On all platforms, you will need to install:

  • Rustup (https://rustup.rs/). The Rust version pinned in rust-toolchain.toml will be automatically downloaded if not yet installed. If removing that file to use a distro-provided Rust, newer Rust versions will typically work for building but may fail tests; older Rust versions may not work at all.
  • Ninja (unzip from https://github.com/ninja-build/ninja/releases/tag/v1.11.1 and place on your path, or from your distro if it's 1.10+)

Platform-specific requirements:

Running Anki during development

From the top level of Anki's source folder:

./run

(.\run on Windows)

This will build Anki and run it in place.

The first build will take a while, as it downloads and builds a bunch of dependencies. When the build is complete, Anki will automatically start.

Running tests/checks

To run all tests at once, from the top-level folder:

./ninja check

(tools\ninja check on Windows).

You can also run specific checks. For example, if you see during the checks that check:svelte:editor is failing, you can use ./ninja check:svelte:editor to re-run that check, or ./ninja check:svelte to re-run all Svelte checks.

Fixing formatting

When formatting issues are reported, they can be fixed with

./ninja format
./ninja fix

Fixing clippy issues

cargo clippy --fix

Optimized builds

The ./run command will create a non-optimized build by default. This is faster to compile, but will mean Anki will run slower.

To run Anki in optimized mode, use:

./tools/runopt

Or set RELEASE=1.

Building redistributable wheels

The ./run method described in the platform-specific instructions is a shortcut for starting Anki directly from the build folder. For regular study, it's recommended you build Python wheels and then install them into your own python venv. This is also a good idea if you wish to install extra tools from PyPi that Anki's build process does not use.

To build wheels on Mac/Linux:

./tools/build

(on Windows, \tools\build.bat)

The generated wheels are in out/wheels. You can then install them by copying the paths into a pip install command. Follow the steps on the beta site, but replace the pip install --upgrade --pre aqt[qt6] line with something like:

/my/pyenv/bin/pip install --upgrade out/wheels/*.whl

(On Windows you'll need to list out the filenames manually instead of using a wildcard).

You'll also need to install PyQt:

$ /my/pyenv/bin/pip install pyqt6 pyqt6-webengine

or

$ my/pyenv/bin/pip install pyqt5 pyqtwebengine

Cleaning up build files

Apart from submodule checkouts, most build files go into the out/ folder (and node_modules on Windows). You can delete that folder for a clean build, or to free space.

Cargo, yarn and pip all cache downloads of dependencies in a shared cache that other builds on your system may use as well. If you wish to clear up those caches, they can be found in ~/.rustup, ~/.cargo and ~/.cache/{yarn,pip}.

If you invoke Rust outside of the build scripts (eg by running cargo, or with Rust Analyzer), output files will go into target/ unless you have overriden the default output location.

IDEs

Please see this separate page for setting up an editor/IDE.

Making changes to the build

See this page

Environmental Variables

If ANKIDEV is set before starting Anki, some extra log messages will be printed on stdout, and automatic backups will be disabled - so please don't use this except on a test profile. It is automatically enabled when using ./run.

If TRACESQL is set, all SQL statements will be printed as they are executed.

If LOGTERM is set before starting Anki, warnings and error messages that are normally placed in the collection2.log file will also be printed on stdout.

If ANKI_PROFILE_CODE is set, Python profiling data will be written on exit.

Binary Bundles

Anki's official binary packages are created with ./ninja bundle. The bundling process was created specifically for the official builds, and is provided as-is; we are unfortunately not able to provide assistance with any issues you may run into when using it.

Mixing development and study

You may wish to create a separate profile with File>Switch Profile for use during development. You can pass the arguments "-p [profile name]" when starting Anki to load a specific profile.

If you're using PyCharm:

  • right click on the "run" file in the root of the PyCharm Anki folder
  • click "Edit 'run'..." - in Script options and enter: "-p [dev profile name]" without the quotes
  • click "Ok"