Skip to content

Commit 5e0a761

Browse files
authored
Move away from Bazel (ankitects#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
1 parent 88362ba commit 5e0a761

File tree

1,052 files changed

+10897
-47724
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

1,052 files changed

+10897
-47724
lines changed

.bazelignore

-2
This file was deleted.

.bazelrc

-47
This file was deleted.

.bazelversion

-1
This file was deleted.

.buildkite/linux/docker/Dockerfile.amd64

+14-2
Original file line numberDiff line numberDiff line change
@@ -44,21 +44,33 @@ RUN apt-get update && apt install --yes gnupg ca-certificates && \
4444
portaudio19-dev \
4545
python3-dev \
4646
rsync \
47+
unzip \
4748
zstd \
4849
&& rm -rf /var/lib/apt/lists/*
4950

50-
5151
RUN curl -L https://github.com/bazelbuild/bazelisk/releases/download/v1.7.4/bazelisk-linux-amd64 \
5252
-o /usr/local/bin/bazel \
5353
&& chmod +x /usr/local/bin/bazel
54-
5554
RUN ln -sf /usr/bin/python3 /usr/bin/python
5655

5756
RUN mkdir -p /etc/buildkite-agent/hooks && chown -R user /etc/buildkite-agent
5857

5958
COPY buildkite.cfg /etc/buildkite-agent/buildkite-agent.cfg
6059
COPY environment /etc/buildkite-agent/hooks/environment
6160

61+
RUN curl -LO https://github.com/ninja-build/ninja/releases/download/v1.11.1/ninja-linux.zip \
62+
&& unzip ninja-linux.zip \
63+
&& chmod +x ninja \
64+
&& mv ninja /usr/bin \
65+
&& rm ninja-linux.zip
66+
67+
RUN mkdir /state/rust && chown user /state/rust
68+
6269
USER user
70+
71+
ENV CARGO_HOME=/state/rust/cargo
72+
ENV RUSTUP_HOME=/state/rust/rustup
73+
RUN curl https://sh.rustup.rs -sSf | sh -s -- -y --no-modify-path --default-toolchain none
74+
6375
WORKDIR /code/buildkite
6476
ENTRYPOINT ["/usr/bin/buildkite-agent", "start"]

.buildkite/linux/docker/Dockerfile.arm64

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
FROM debian:11-slim
22

33
ARG DEBIAN_FRONTEND="noninteractive"
4-
ENV PYTHON_SITE_PACKAGES=/usr/lib/python3/dist-packages/
4+
ENV PYTHONPATH=/usr/lib/python3/dist-packages
55

66
RUN useradd -d /state -m -u 998 user
77

@@ -46,13 +46,13 @@ RUN apt-get update && apt install --yes gnupg ca-certificates && \
4646
python3-dev \
4747
rsync \
4848
# -- begin only required for arm64/debian11
49+
ninja-build \
4950
clang-format \
5051
python-is-python3 \
5152
python3-pyqt5.qtwebengine \
5253
# -- end only required for arm64/debian11
5354
&& rm -rf /var/lib/apt/lists/*
5455

55-
5656
RUN curl -L https://github.com/bazelbuild/bazelisk/releases/download/v1.10.1/bazelisk-linux-arm64 \
5757
-o /usr/local/bin/bazel \
5858
&& chmod +x /usr/local/bin/bazel
@@ -64,6 +64,13 @@ RUN mkdir -p /etc/buildkite-agent/hooks && chown -R user /etc/buildkite-agent
6464
COPY buildkite.cfg /etc/buildkite-agent/buildkite-agent.cfg
6565
COPY environment /etc/buildkite-agent/hooks/environment
6666

67+
RUN mkdir /state/rust && chown user /state/rust
68+
6769
USER user
70+
71+
ENV CARGO_HOME=/state/rust/cargo
72+
ENV RUSTUP_HOME=/state/rust/rustup
73+
RUN curl https://sh.rustup.rs -sSf | sh -s -- -y --no-modify-path --default-toolchain none
74+
6875
WORKDIR /code/buildkite
6976
ENTRYPOINT ["/usr/bin/buildkite-agent", "start"]

.buildkite/linux/docker/buildkite.cfg

+1
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ build-path="/state/build"
44
hooks-path="/etc/buildkite-agent/hooks"
55
no-plugins=true
66
no-local-hooks=true
7+
no-git-submodules=true

.buildkite/linux/entrypoint

+5-13
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,11 @@ set -e
66
echo "--- Checking CONTRIBUTORS"
77
.buildkite/linux/check_contributors
88

9-
BAZEL="bazel --output_user_root=/state/bazel --output_base=/state/bazel/anki"
10-
BUILDARGS="--config=ci --disk_cache=/state/bazel/disk --repository_cache=/state/bazel/repo"
11-
129
echo "+++ Building and testing"
10+
ln -sf out/node_modules .
1311

14-
# move existing node_modules into tree
15-
test -e /state/node_modules && mv /state/node_modules .
16-
17-
$BAZEL test $BUILDARGS ... //rslib/linkchecker
18-
19-
echo "--- Running lints"
20-
python tools/copyright_headers.py
12+
export PATH="$PATH:/state/rust/cargo/bin"
13+
export BUILD_ROOT=/state/build
14+
export ONLINE_TESTS=1
2115

22-
echo "--- Cleanup"
23-
# if tests succeed, back up node_modules folder
24-
mv node_modules /state/
16+
./ninja pylib/anki qt/aqt check

.buildkite/linux/release-entrypoint

+6-10
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,13 @@
22

33
set -e
44

5-
# move existing node_modules into tree
6-
test -e /state/node_modules && mv /state/node_modules .
5+
export PATH="$PATH:/state/rust/cargo/bin"
6+
export BUILD_ROOT=/state/build
7+
export RELEASE=1
8+
ln -sf out/node_modules .
79

810
if [ $(uname -m) = "aarch64" ]; then
9-
./tools/build
11+
./ninja wheels:anki
1012
else
11-
./tools/bundle
13+
./ninja bundle
1214
fi
13-
14-
rm -rf /state/dist
15-
mv .bazel/out/dist /state
16-
17-
# if tests succeed, back up node_modules folder
18-
mv node_modules /state/

.buildkite/mac/entrypoint

+2-13
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,7 @@ set -e
44

55
STATE=$(pwd)/../state/anki-ci
66
mkdir -p $STATE
7-
BAZEL="bazel --output_user_root=$STATE/bazel --output_base=$STATE/bazel/anki"
8-
BUILDARGS="--config=ci --experimental_convenience_symlinks=ignore"
97

108
echo "+++ Building and testing"
11-
12-
# move existing node_modules into tree
13-
test -e $STATE/node_modules && mv $STATE/node_modules .
14-
15-
$BAZEL test $BUILDARGS ...
16-
17-
echo "--- Building wheels"
18-
$BAZEL build wheels
19-
20-
# if tests succeed, back up node_modules folder
21-
mv node_modules $STATE/
9+
ln -sf out/node_modules .
10+
BUILD_ROOT=$STATE/build ./ninja pylib/anki qt/aqt wheels check

.buildkite/windows/entrypoint.bat

+9-15
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,16 @@
1-
set BAZEL=\bazel\bazel.exe --output_user_root=\bazel\ankici --output_base=\bazel\ankici\base
2-
set BUILDARGS=--config=ci
1+
set PATH=c:\cargo\bin;%PATH%
32

43
echo +++ Building and testing
54

6-
if exist \bazel\node_modules (
7-
move \bazel\node_modules .\node_modules
5+
if exist \buildkite\state\out (
6+
move \buildkite\state\out .
87
)
9-
10-
call %BAZEL% test %BUILDARGS% ...
11-
IF %ERRORLEVEL% NEQ 0 (
12-
echo checking ts build
13-
call %BAZEL% build //ts/... || (
14-
echo ts build failed, cleaning up build products
15-
call %BAZEL% run tools:cleanup_js
16-
)
17-
18-
exit /B 1
8+
if exist \buildkite\state\node_modules (
9+
move \buildkite\state\node_modules .
1910
)
2011

12+
call tools\ninja build pylib/anki qt/aqt check || exit /b 1
13+
2114
echo --- Cleanup
22-
move node_modules \bazel\node_modules
15+
move out \buildkite\state\
16+
move node_modules \buildkite\state\

.cargo/config.toml

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[env]
2+
STRINGS_JSON = { value = "out/rslib/i18n/strings.json", relative = true }
3+
# build script will append .exe if necessary
4+
PROTOC = { value = "out/extracted/protoc/bin/protoc", relative = true }
5+
PYO3_NO_PYTHON = "1"
6+
MACOSX_DEPLOYMENT_TARGET = "10.13.4"
7+
8+
[term]
9+
color = "always"

.config/hakari.toml

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
hakari-package = "workspace-hack"
2+
dep-format-version = "2"
3+
resolver = "2"
4+
5+
[traversal-excludes]
6+
third-party = [
7+
{ name = "reqwest", git = "https://github.com/ankitects/reqwest.git", rev = "7591444614de02b658ddab125efba7b2bb4e2335" }
8+
]

.config/nextest.toml

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[store]
2+
dir = "out/tests/nextest"

.dprint.json

+14-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,20 @@
1414
"semi": true
1515
},
1616
"includes": ["**/*.{ts,tsx,js,jsx,cjs,mjs,json,md,toml,svelte}"],
17-
"excludes": ["**/node_modules", "**/*-lock.json", "**/*.{ts,json,md,svelte}", "qt/aqt/data/web/js/vendor/*.js"],
17+
"excludes": [
18+
"**/node_modules",
19+
"out/**",
20+
"**/*-lock.json",
21+
"**/*.{ts,toml,svelte}",
22+
"qt/aqt/data/web/js/vendor/*.js",
23+
"ftl/qt-repo",
24+
"ftl/core-repo",
25+
"ftl/usage",
26+
"licenses.json",
27+
".dmypy.json",
28+
"qt/bundle/PyOxidizer",
29+
"target"
30+
],
1831
"plugins": [
1932
"https://plugins.dprint.dev/typescript-0.77.0.wasm",
2033
"https://plugins.dprint.dev/json-0.16.0.wasm",

ts/.eslintrc.js .eslintrc.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
module.exports = {
2+
root: true,
23
extends: ["eslint:recommended", "plugin:compat/recommended"],
34
parser: "@typescript-eslint/parser",
45
plugins: [
@@ -21,6 +22,7 @@ module.exports = {
2122
"simple-import-sort/exports": "warn",
2223
"prefer-const": "warn",
2324
"no-nested-ternary": "warn",
25+
"@typescript-eslint/consistent-type-imports": "error",
2426
},
2527
overrides: [
2628
{
@@ -43,7 +45,7 @@ module.exports = {
4345
},
4446
],
4547
env: { browser: true },
46-
ignorePatterns: ["backend_proto.d.ts", "*.svelte.d.ts"],
48+
ignorePatterns: ["backend_proto.d.ts", "*.svelte.d.ts", "vendor"],
4749
globals: {
4850
globalThis: false,
4951
NodeListOf: false,

.github/ISSUE_TEMPLATE/bug-report.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,17 @@ labels: ""
66
assignees: ""
77
---
88

9-
- Have a question or feature suggestion?
10-
- Problems building/running on your system?
11-
- Not 100% sure you've found a bug?
9+
- Have a question or feature suggestion?
10+
- Problems building/running on your system?
11+
- Not 100% sure you've found a bug?
1212

1313
If so, please post on https://forums.ankiweb.net/ instead. This issue tracker is
1414
intended primarily to track development tasks, and it is easier to provide support
1515
over on the forums. Please make sure you read the following pages before
1616
you post there:
1717

18-
- https://faqs.ankiweb.net/when-problems-occur.html
19-
- https://faqs.ankiweb.net/getting-help.html
18+
- https://faqs.ankiweb.net/when-problems-occur.html
19+
- https://faqs.ankiweb.net/getting-help.html
2020

2121
If you post questions, suggestions, or vague bug reports here, please do not be
2222
offended if we close your ticket without replying. If in doubt, please post on

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ anki.prof
44
target
55
/user.bazelrc
66
.dmypy.json
7-
node_modules
87
/.idea/
98
/.vscode/
109
/.bazel
1110
/windows.bazelrc
11+
/out
12+
node_modules

.gitmodules

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[submodule "ftl/core-repo"]
2+
path = ftl/core-repo
3+
url = https://github.com/ankitects/anki-core-i18n.git
4+
shallow = true
5+
[submodule "ftl/qt-repo"]
6+
path = ftl/qt-repo
7+
url = https://github.com/ankitects/anki-desktop-ftl.git
8+
shallow = true
9+
[submodule "qt/bundle/PyOxidizer"]
10+
path = qt/bundle/PyOxidizer
11+
url = https://github.com/ankitects/PyOxidizer.git

pylib/.isort.cfg .isort.cfg

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
[settings]
2-
skip=aqt/forms,backend_pb2.py,backend_pb2.pyi,fluent_pb2.py,fluent_pb2.pyi,rsbackend_gen.py,generated.py,hooks_gen.py,genbackend.py
3-
profile=black
4-
multi_line_output=3
5-
include_trailing_comma=True
2+
ensure_newline_before_comments=true
63
force_grid_wrap=0
7-
use_parentheses=True
4+
include_trailing_comma=True
5+
known_first_party=anki,aqt,tests
86
line_length=88
9-
ensure_newline_before_comments=true
10-
known_first_party=tests,anki
7+
multi_line_output=3
8+
profile=black
9+
skip=
10+
use_parentheses=True

0 commit comments

Comments
 (0)