From c0ca0a83ad0549c54df1ca5860c01b33dde69afa Mon Sep 17 00:00:00 2001 From: LGUG2Z Date: Sat, 31 Oct 2026 19:32:58 -0700 Subject: [PATCH] fix(nix): check the Windows formatting with the nightly rustfmt over the Windows files; add tools/format-all The Windows fmt check ran the stable `cargo fmt` over the stubbed workspace source: it failed on the empty stub lib.rs files of the other platforms, on `mod linux;`/`mod macos;` of the shared entry files (the Windows source does not have those modules), and its stable rustfmt ignores the nightly options of rustfmt.toml. It is now a runCommand running tools/format-all --tree windows with the nightly rustfmt over the files of the Windows source (build.real, no stubs), file by file with skip_children. tools/format-all runs the same check locally (--check, or --apply to format; --tree windows|mac|kde|all; the nightly rustfmt from $RUSTFMT, PATH or the root dev shell). mac is the explicit-file invocation of the kfm check, kde is `cargo fmt -p ...` as the KDE check does. Verified: in a throwaway copy of wip-cand7, --apply formats 17 Windows files (+211 -88; crates/ komorebi-backend, bar-core, core, protocol, bar windows modules, client windows.rs, komorebi-gui, testwin) and 12 KDE files (+22 -29); afterwards --check passes for windows, mac and kde. The formatting itself is not in this revision (other agents are editing those files). The nix attribute evaluates; not built. --- flake.nix | 19 ++++++++---- tools/format-all | 81 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 94 insertions(+), 6 deletions(-) create mode 100755 tools/format-all diff --git a/flake.nix b/flake.nix index 7b867cbe..614f8bdf 100644 --- a/flake.nix +++ b/flake.nix @@ -522,12 +522,19 @@ } ); - komorebi-workspace-fmt = build.craneLib.cargoFmt { - inherit (build) src; - pname = "komorebi-workspace"; - version = "0.1.0"; - cargoToml = "${build.real}/Cargo.toml"; - }; + # the nightly rustfmt over the Windows files, as tools/format-all --tree windows does locally. Not + # `cargo fmt`: it ran the stable rustfmt (which ignores the nightly options of rustfmt.toml) over + # the stub sources of the other platforms and followed `mod linux;` into files the Windows source + # does not have. + komorebi-workspace-fmt = + pkgs.runCommand "komorebi-workspace-fmt" + { + nativeBuildInputs = [ nightlyRustfmt ]; + } + '' + RUSTFMT=rustfmt bash ${./tools/format-all} --check --tree windows --root ${build.real} + touch $out + ''; komorebi-workspace-toml-fmt = build.craneLib.taploFmt { src = pkgs.lib.sources.sourceFilesBySuffices build.real [ ".toml" ]; diff --git a/tools/format-all b/tools/format-all new file mode 100755 index 00000000..eb6c1a30 --- /dev/null +++ b/tools/format-all @@ -0,0 +1,81 @@ +#!/usr/bin/env bash +# format-all: the formatting checks of the nix flakes, runnable locally, and the fix for them. +# +# Usage: tools/format-all [--check | --apply] [--tree windows|kde|mac|all] [--root DIR] +# --check (default) fails if any file is not formatted; prints the diff +# --apply formats the files in place (do it LAST on a candidate: it touches many files) +# --tree which flake's check to reproduce (default all) +# --root the repository (default: the one this script is in; the nix check passes its source) +# +# The checks use the nightly rustfmt (rustfmt.toml has nightly options) and edition 2024, the +# edition of every manifest: +# windows every .rs file of the Windows crates with the root rustfmt.toml, file by file +# (skip_children: the entry files name modules of other platforms that are not in the +# Windows source, which rustfmt would fail to find); the linux and macos modules of the +# shared crates belong to the kde and mac trees +# mac komorebi-for-mac/**/*.rs and the macos modules of the shared crates, with +# komorebi-for-mac/rustfmt.toml +# kde `cargo fmt -p `, as komorebi-for-kde/flake.nix does +# The nightly rustfmt is $RUSTFMT if set, else `rustfmt` if that is a nightly, else the one of the +# dev shell of the root flake (`nix develop`), which is the one the nix checks use (a nightly of the +# pinned rust-overlay; another nightly may format a little differently). +set -uo pipefail + +mode=check +tree=all +root=$(cd "$(dirname "$0")/.." && pwd) +while [ $# -gt 0 ]; do + case $1 in + --check) mode=check ;; + --apply) mode=apply ;; + --tree) tree=$2; shift ;; + --root) root=$2; shift ;; + -h | --help) sed -n 2,23p "$0"; exit 0 ;; + *) echo "format-all: unknown argument $1" >&2; exit 2 ;; + esac + shift +done +root=$(cd "$root" && pwd) + +if [ -z "${RUSTFMT:-}" ]; then + if rustfmt --version 2>/dev/null | grep -q nightly; then + RUSTFMT=rustfmt + elif [ -z "${FORMAT_ALL_IN_SHELL:-}" ] && command -v nix >/dev/null; then + FORMAT_ALL_IN_SHELL=1 exec nix develop "$root" --command "$0" "$@" --root "$root" + else + echo "format-all: no nightly rustfmt (set RUSTFMT)" >&2; exit 2 + fi +fi +export RUSTFMT +cd "$root" || exit 2 + +flag=--check +[ "$mode" = apply ] && flag= +status=0 + +# the linux and macos modules of the shared crates (the Windows source filter of flake.nix leaves them out) +other_platform='(^|/)(komorebic/src/(linux|macos)|komorebic/tests/(linux|macos)_|komorebi-client/src/(linux|macos)\.rs|komorebi-client/tests/(linux|macos)_|komorebi-bar/src/macos|komorebi-daemon/tests/(linux|macos)_)' + +rs_files() { find . -name '*.rs' -not -path '*/target/*' -not -path './.git/*' -not -path './.jj/*' | sed 's|^\./||' | sort; } + +run_rustfmt() { # config-file, files on stdin + local cfg=$1 files + mapfile -t files + [ ${#files[@]} -eq 0 ] && { echo "format-all: no files for $cfg" >&2; return 1; } + "$RUSTFMT" --edition 2024 --config-path "$cfg" --config skip_children=true $flag "${files[@]}" +} + +if [ "$tree" = windows ] || [ "$tree" = all ]; then + echo "format-all: windows" + rs_files | grep -v '^komorebi-for-\(kde\|mac\)/' | grep -Ev "$other_platform" | run_rustfmt rustfmt.toml || status=1 +fi +if [ "$tree" = mac ] || [ "$tree" = all ]; then + echo "format-all: mac" + { rs_files | grep '^komorebi-for-mac/'; rs_files | grep -E "^(komorebic/src/macos|komorebi-bar/src/macos|komorebi-client/src/macos\.rs)"; } \ + | run_rustfmt komorebi-for-mac/rustfmt.toml || status=1 +fi +if [ "$tree" = kde ] || [ "$tree" = all ]; then + echo "format-all: kde" + cargo fmt -p komorebi -p komorebi-linux -p komorebic -p komorebi-client -p komorebi-hotkeys -- $flag || status=1 +fi +exit $status