hex/scripts/check-nix-git-pins.py
Dax 687ea32d65
Bump Rune to 1899e64: overlays pace frames with the swap chain on Windows (#1832)
* Bump Rune to 1899e64: overlays pace frames with the swap chain on Windows

Windows HUD memory grew and the app could hang after dictation because the
overlay presented frames without waiting for swap chain room after Rune #844;
Rune #985 makes an overlay's frames wait for room in its swap chain, as a
window's do.

nix: vendor each Cargo source in its own directory. Rune's tiny-skia-path is a
path dependency inside Rune (a Git crate at 0.11.4 here), the same name and
version as resvg's from crates.io, and cargo's directory source keys a crate by
name and version alone.

Goldens: Rune's text drawing changed over the 152 commits since d6715352.

* Review: patch accesskit_macos, and make the nix vendor split name what breaks

Rune #975 put accesskit_macos in its [patch.crates-io], so Hex patches it too
(Rune's README asks consumers to); it is the fifth and last crate Rune patches.

The vendor script override now checks each fragment it rewrites and names the
missing one, its comment says crates.io goes to registry/ and Git sources to
git/, nixfmt formats the file, and check-nix-git-pins.py's docstring says the
hashes reach importCargoLock through the cargoDeps override.

* linux: keep Hex running when the window can't start

Rune r399 makes App::run print a startup error and exit 1 on native targets
instead of panicking, so a Linux Hex with no display died, the next launch
became a fresh instance, and Check Linux's single-instance exercise saw
`hex start` hang and `hex app --hidden` reset.

run_app now returns Rune's try_run error and run_window reports it and waits,
as it did for the panic (#979).

* macos, windows: take rune's startup error instead of exiting

Rune r399 prints a startup error and exits the process from `run` on
native targets, which skipped Hex's own stop: macOS's drain (workers,
event-log flush, a recording kept for the next launch) and Windows' GPU
draw-marker disarm, so the launch that failed before drawing was read as
a crash while drawing. Both apps take `try_run`'s error now, log it, and
stop the way Quit does; Windows also reports it from `hex app`. The
Linux window already took the error (#979).

* macos: a launch whose window never started exits 1

Taking Rune's try_run error lets the app stop through the drain, but
process_exit::finish ends with _exit(0), so a failed GUI launch reported
success. finish_with takes the code; the window's failure exits 1 while a
normal quit keeps 0.

* docs: overlays wait for swap-chain room on windows since rune #985

Rune #985 makes every overlay's frames wait for room in its swap chain, so
the notes that said only a window waits and that the HUD's 60 Hz timer worked
around a suspected cause were stale. The timer is a cap on top of the slot
wait now. The macOS and Windows startup-error comments cited #979, which is
the Linux window issue; they describe Rune r399 instead.

* windows: a failed start keeps the GPU draw marker

disarm() means a frame reached the screen or Hex quit cleanly; a start that
never drew is neither, so it keeps the marker and the next launch falls back to
the high-performance GPU.

* docs: say what was measured about overlay pacing

* docs: say what stays bounded on windows, not what shrinks

The meetings map and the dictation map described the fix as memory that no
longer grows, and as Rune holding a frame's slot and skipping it. Rune #985
defers the frame until its swap chain has room, then draws it, which bounds
the frames in flight; the growth itself hasn't been re-measured on Windows
with the fix.
2026-11-14 20:40:20 -05:00

189 lines
6.8 KiB
Python
Executable file

#!/usr/bin/env python3
"""Fail when Cargo.lock's Git revisions and the Nix cargo pins disagree.
`nix/package.nix` records, for every Git dependency, the revision its
`outputHashes` entry was computed from (the table it hands to
`importCargoLock` inside the `cargoDeps` override). Bumping a pin in Cargo.lock
without updating that revision and hash makes `nix build` fail late with a
fixed-output hash mismatch. This check catches the revision half in a pull
request, before Nix downloads anything.
Cargo.lock writes Git sources as `git+URL[?query]#<commit>`, where the query
may name a branch, tag, or short rev, and `<commit>` is always the full
resolved commit. Only the fragment identifies the checked-out code, so the
query is ignored here. Python 3.9 is the floor: `scripts/check` runs with
Apple's interpreter on macOS.
"""
from __future__ import annotations
import argparse
from pathlib import Path
import re
import sys
REV = re.compile(r"[0-9a-f]{40}")
HASH = re.compile(r"sha256-[A-Za-z0-9+/]{43}=")
FAKE_HASH = re.compile(r"sha256-A{43}=")
PACKAGE_HEADER = "[[package]]"
TABLE_HEADER = re.compile(r"(?m)^\[")
NAME = re.compile(r'^name = "([^"]+)"$', re.MULTILINE)
VERSION = re.compile(r'^version = "([^"]+)"$', re.MULTILINE)
SOURCE = re.compile(r'^source = "([^"]+)"$', re.MULTILINE)
PIN_ENTRY = re.compile(r'"([^"]+)"\s*=\s*\{')
PIN_VALUE = re.compile(r'(rev|hash)\s*=\s*"([^"]+)";')
def fail(message: str) -> None:
print(f"nix git pins: {message}", file=sys.stderr)
sys.exit(1)
def parse_pins(path: Path) -> dict[str, dict[str, str]]:
lines = path.read_text().splitlines()
try:
start = next(
index
for index, line in enumerate(lines)
if line.strip() == "cargoGitDependencies = {"
)
except StopIteration:
fail(f"{path} has no cargoGitDependencies table")
pins: dict[str, dict[str, str]] = {}
key = None
for line in lines[start + 1 :]:
stripped = line.strip()
if stripped == "};":
if key is not None:
key = None
continue
break
if not stripped or stripped.startswith("#"):
continue
entry = PIN_ENTRY.fullmatch(stripped)
if entry:
key = entry.group(1)
if key in pins:
fail(f"{path} declares {key} twice")
pins[key] = {}
continue
value = PIN_VALUE.fullmatch(stripped)
if value and key is not None:
pins[key][value.group(1)] = value.group(2)
continue
fail(f"{path} has an unexpected line in cargoGitDependencies: {stripped}")
if not pins:
fail(f"{path} declares no Git dependencies")
for name, pin in pins.items():
if set(pin) != {"rev", "hash"}:
fail(f"{path} needs one rev and one hash for {name}")
if not REV.fullmatch(pin["rev"]):
fail(f"{path} has no full 40-character rev for {name}")
if FAKE_HASH.fullmatch(pin["hash"]):
fail(f"{path} still has lib.fakeHash for {name}")
if not HASH.fullmatch(pin["hash"]):
fail(f"{path} has no sha256 output hash for {name}")
return pins
def parse_git_source(source: str, package: str) -> tuple[str, str]:
"""Return the normalized repository URL and resolved commit in a source."""
if "#" not in source:
fail(
f"Cargo.lock pins {package} to a Git source without a resolved "
f"commit: {source}"
)
url_part, commit = source.rsplit("#", 1)
if not REV.fullmatch(commit):
fail(
f"Cargo.lock pins {package} to a Git source without a full "
f"40-character commit: {source}"
)
url = url_part[len("git+") :].split("?", 1)[0].rstrip("/")
if url.endswith(".git"):
url = url[:-4]
if not url:
fail(f"Cargo.lock pins {package} to an empty Git repository: {source}")
return url, commit
def parse_lock(path: Path) -> dict[str, dict]:
"""One entry per Git repository: its resolved commit and its packages."""
repos: dict[str, dict] = {}
for stanza in path.read_text().split(PACKAGE_HEADER)[1:]:
stanza = TABLE_HEADER.split(stanza, maxsplit=1)[0]
name = NAME.search(stanza)
version = VERSION.search(stanza)
if name is None or version is None:
fail(f"{path} has a package without a name or version")
package = f"{name.group(1)}-{version.group(1)}"
source = SOURCE.search(stanza)
if source is None or not source.group(1).startswith("git+"):
continue
url, commit = parse_git_source(source.group(1), package)
repo = repos.setdefault(url, {"rev": commit, "packages": set()})
if repo["rev"] != commit:
fail(f"Cargo.lock pins more than one commit of {url}")
repo["packages"].add(package)
return repos
def compare(pins: dict[str, dict[str, str]], repos: dict[str, dict]) -> list[str]:
problems = []
consumed = set()
for url, repo in sorted(repos.items()):
named = sorted(repo["packages"] & pins.keys())
if not named:
candidates = ", ".join(sorted(repo["packages"]))
problems.append(
f"{url}: Cargo.lock pins it, but the Nix table has no hash "
f"(pin one of: {candidates})"
)
continue
if len(named) > 1:
problems.append(
f"{url}: the Nix table names several of its packages: "
+ ", ".join(named)
)
consumed.update(named)
for name in named:
if pins[name]["rev"] != repo["rev"]:
problems.append(
f"{name} ({url}): Cargo.lock names {repo['rev']}, "
f"the Nix table names {pins[name]['rev']}"
)
for name in sorted(pins.keys() - consumed):
problems.append(f"{name}: the Nix table pins it, but Cargo.lock does not")
return problems
def main() -> None:
repo = Path(__file__).resolve().parent.parent
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--lock", type=Path, default=repo / "Cargo.lock")
parser.add_argument("--pins", type=Path, default=repo / "nix/package.nix")
args = parser.parse_args()
pins = parse_pins(args.pins)
repos = parse_lock(args.lock)
problems = compare(pins, repos)
if problems:
print("nix git pins do not match Cargo.lock:", file=sys.stderr)
for problem in problems:
print(f" {problem}", file=sys.stderr)
fail(
"update cargoGitDependencies in nix/package.nix with the new rev "
"and the output hash from a failed `nix build .#hex`"
)
print(
f"nix git pins match Cargo.lock "
f"({len(repos)} Git repositories, {len(pins)} pins)"
)
if __name__ == "__main__":
main()