Skip to content

Commit

Permalink
Move away from Bazel (#2202)
Browse files Browse the repository at this point in the history
(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
  • Loading branch information
dae authored Nov 27, 2022
1 parent 88362ba commit 5e0a761
Show file tree
Hide file tree
Showing 1,052 changed files with 10,897 additions and 47,724 deletions.
2 changes: 0 additions & 2 deletions .bazelignore

This file was deleted.

47 changes: 0 additions & 47 deletions .bazelrc

This file was deleted.

1 change: 0 additions & 1 deletion .bazelversion

This file was deleted.

16 changes: 14 additions & 2 deletions .buildkite/linux/docker/Dockerfile.amd64
Original file line number Diff line number Diff line change
Expand Up @@ -44,21 +44,33 @@ RUN apt-get update && apt install --yes gnupg ca-certificates && \
portaudio19-dev \
python3-dev \
rsync \
unzip \
zstd \
&& rm -rf /var/lib/apt/lists/*


RUN curl -L https://github.com/bazelbuild/bazelisk/releases/download/v1.7.4/bazelisk-linux-amd64 \
-o /usr/local/bin/bazel \
&& chmod +x /usr/local/bin/bazel

RUN ln -sf /usr/bin/python3 /usr/bin/python

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

COPY buildkite.cfg /etc/buildkite-agent/buildkite-agent.cfg
COPY environment /etc/buildkite-agent/hooks/environment

RUN curl -LO https://github.com/ninja-build/ninja/releases/download/v1.11.1/ninja-linux.zip \
&& unzip ninja-linux.zip \
&& chmod +x ninja \
&& mv ninja /usr/bin \
&& rm ninja-linux.zip

RUN mkdir /state/rust && chown user /state/rust

USER user

ENV CARGO_HOME=/state/rust/cargo
ENV RUSTUP_HOME=/state/rust/rustup
RUN curl https://sh.rustup.rs -sSf | sh -s -- -y --no-modify-path --default-toolchain none

WORKDIR /code/buildkite
ENTRYPOINT ["/usr/bin/buildkite-agent", "start"]
11 changes: 9 additions & 2 deletions .buildkite/linux/docker/Dockerfile.arm64
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
FROM debian:11-slim

ARG DEBIAN_FRONTEND="noninteractive"
ENV PYTHON_SITE_PACKAGES=/usr/lib/python3/dist-packages/
ENV PYTHONPATH=/usr/lib/python3/dist-packages

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

Expand Down Expand Up @@ -46,13 +46,13 @@ RUN apt-get update && apt install --yes gnupg ca-certificates && \
python3-dev \
rsync \
# -- begin only required for arm64/debian11
ninja-build \
clang-format \
python-is-python3 \
python3-pyqt5.qtwebengine \
# -- end only required for arm64/debian11
&& rm -rf /var/lib/apt/lists/*


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

RUN mkdir /state/rust && chown user /state/rust

USER user

ENV CARGO_HOME=/state/rust/cargo
ENV RUSTUP_HOME=/state/rust/rustup
RUN curl https://sh.rustup.rs -sSf | sh -s -- -y --no-modify-path --default-toolchain none

WORKDIR /code/buildkite
ENTRYPOINT ["/usr/bin/buildkite-agent", "start"]
1 change: 1 addition & 0 deletions .buildkite/linux/docker/buildkite.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ build-path="/state/build"
hooks-path="/etc/buildkite-agent/hooks"
no-plugins=true
no-local-hooks=true
no-git-submodules=true
18 changes: 5 additions & 13 deletions .buildkite/linux/entrypoint
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,11 @@ set -e
echo "--- Checking CONTRIBUTORS"
.buildkite/linux/check_contributors

BAZEL="bazel --output_user_root=/state/bazel --output_base=/state/bazel/anki"
BUILDARGS="--config=ci --disk_cache=/state/bazel/disk --repository_cache=/state/bazel/repo"

echo "+++ Building and testing"
ln -sf out/node_modules .

# move existing node_modules into tree
test -e /state/node_modules && mv /state/node_modules .

$BAZEL test $BUILDARGS ... //rslib/linkchecker

echo "--- Running lints"
python tools/copyright_headers.py
export PATH="$PATH:/state/rust/cargo/bin"
export BUILD_ROOT=/state/build
export ONLINE_TESTS=1

echo "--- Cleanup"
# if tests succeed, back up node_modules folder
mv node_modules /state/
./ninja pylib/anki qt/aqt check
16 changes: 6 additions & 10 deletions .buildkite/linux/release-entrypoint
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,13 @@

set -e

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

if [ $(uname -m) = "aarch64" ]; then
./tools/build
./ninja wheels:anki
else
./tools/bundle
./ninja bundle
fi

rm -rf /state/dist
mv .bazel/out/dist /state

# if tests succeed, back up node_modules folder
mv node_modules /state/
15 changes: 2 additions & 13 deletions .buildkite/mac/entrypoint
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,7 @@ set -e

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

echo "+++ Building and testing"

# move existing node_modules into tree
test -e $STATE/node_modules && mv $STATE/node_modules .

$BAZEL test $BUILDARGS ...

echo "--- Building wheels"
$BAZEL build wheels

# if tests succeed, back up node_modules folder
mv node_modules $STATE/
ln -sf out/node_modules .
BUILD_ROOT=$STATE/build ./ninja pylib/anki qt/aqt wheels check
24 changes: 9 additions & 15 deletions .buildkite/windows/entrypoint.bat
Original file line number Diff line number Diff line change
@@ -1,22 +1,16 @@
set BAZEL=\bazel\bazel.exe --output_user_root=\bazel\ankici --output_base=\bazel\ankici\base
set BUILDARGS=--config=ci
set PATH=c:\cargo\bin;%PATH%

echo +++ Building and testing

if exist \bazel\node_modules (
move \bazel\node_modules .\node_modules
if exist \buildkite\state\out (
move \buildkite\state\out .
)

call %BAZEL% test %BUILDARGS% ...
IF %ERRORLEVEL% NEQ 0 (
echo checking ts build
call %BAZEL% build //ts/... || (
echo ts build failed, cleaning up build products
call %BAZEL% run tools:cleanup_js
)

exit /B 1
if exist \buildkite\state\node_modules (
move \buildkite\state\node_modules .
)

call tools\ninja build pylib/anki qt/aqt check || exit /b 1

echo --- Cleanup
move node_modules \bazel\node_modules
move out \buildkite\state\
move node_modules \buildkite\state\
9 changes: 9 additions & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[env]
STRINGS_JSON = { value = "out/rslib/i18n/strings.json", relative = true }
# build script will append .exe if necessary
PROTOC = { value = "out/extracted/protoc/bin/protoc", relative = true }
PYO3_NO_PYTHON = "1"
MACOSX_DEPLOYMENT_TARGET = "10.13.4"

[term]
color = "always"
8 changes: 8 additions & 0 deletions .config/hakari.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
hakari-package = "workspace-hack"
dep-format-version = "2"
resolver = "2"

[traversal-excludes]
third-party = [
{ name = "reqwest", git = "https://github.com/ankitects/reqwest.git", rev = "7591444614de02b658ddab125efba7b2bb4e2335" }
]
2 changes: 2 additions & 0 deletions .config/nextest.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[store]
dir = "out/tests/nextest"
15 changes: 14 additions & 1 deletion .dprint.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,20 @@
"semi": true
},
"includes": ["**/*.{ts,tsx,js,jsx,cjs,mjs,json,md,toml,svelte}"],
"excludes": ["**/node_modules", "**/*-lock.json", "**/*.{ts,json,md,svelte}", "qt/aqt/data/web/js/vendor/*.js"],
"excludes": [
"**/node_modules",
"out/**",
"**/*-lock.json",
"**/*.{ts,toml,svelte}",
"qt/aqt/data/web/js/vendor/*.js",
"ftl/qt-repo",
"ftl/core-repo",
"ftl/usage",
"licenses.json",
".dmypy.json",
"qt/bundle/PyOxidizer",
"target"
],
"plugins": [
"https://plugins.dprint.dev/typescript-0.77.0.wasm",
"https://plugins.dprint.dev/json-0.16.0.wasm",
Expand Down
4 changes: 3 additions & 1 deletion ts/.eslintrc.js → .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
module.exports = {
root: true,
extends: ["eslint:recommended", "plugin:compat/recommended"],
parser: "@typescript-eslint/parser",
plugins: [
Expand All @@ -21,6 +22,7 @@ module.exports = {
"simple-import-sort/exports": "warn",
"prefer-const": "warn",
"no-nested-ternary": "warn",
"@typescript-eslint/consistent-type-imports": "error",
},
overrides: [
{
Expand All @@ -43,7 +45,7 @@ module.exports = {
},
],
env: { browser: true },
ignorePatterns: ["backend_proto.d.ts", "*.svelte.d.ts"],
ignorePatterns: ["backend_proto.d.ts", "*.svelte.d.ts", "vendor"],
globals: {
globalThis: false,
NodeListOf: false,
Expand Down
10 changes: 5 additions & 5 deletions .github/ISSUE_TEMPLATE/bug-report.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@ labels: ""
assignees: ""
---

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

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

- https://faqs.ankiweb.net/when-problems-occur.html
- https://faqs.ankiweb.net/getting-help.html
- https://faqs.ankiweb.net/when-problems-occur.html
- https://faqs.ankiweb.net/getting-help.html

If you post questions, suggestions, or vague bug reports here, please do not be
offended if we close your ticket without replying. If in doubt, please post on
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ anki.prof
target
/user.bazelrc
.dmypy.json
node_modules
/.idea/
/.vscode/
/.bazel
/windows.bazelrc
/out
node_modules
11 changes: 11 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[submodule "ftl/core-repo"]
path = ftl/core-repo
url = https://github.com/ankitects/anki-core-i18n.git
shallow = true
[submodule "ftl/qt-repo"]
path = ftl/qt-repo
url = https://github.com/ankitects/anki-desktop-ftl.git
shallow = true
[submodule "qt/bundle/PyOxidizer"]
path = qt/bundle/PyOxidizer
url = https://github.com/ankitects/PyOxidizer.git
14 changes: 7 additions & 7 deletions pylib/.isort.cfg → .isort.cfg
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
[settings]
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
profile=black
multi_line_output=3
include_trailing_comma=True
ensure_newline_before_comments=true
force_grid_wrap=0
use_parentheses=True
include_trailing_comma=True
known_first_party=anki,aqt,tests
line_length=88
ensure_newline_before_comments=true
known_first_party=tests,anki
multi_line_output=3
profile=black
skip=
use_parentheses=True
Loading

0 comments on commit 5e0a761

Please sign in to comment.