tvl-depot/tools/depotfmt.nix
Vincent Ambo d254dbb848 feat(depotfmt): Check & format Rust code with rustfmt
The rustfmt configuration of the depot is moved to `rustfmt.toml` (it
is recognised more reliably from this path than from the hidden
.rustfmt.toml).

Nested configuration is theoretically possible, but detection of
nested config files is flaky. Paths with nested config files need to
be disabled in the top-level check (I've excluded my user directory).

Change-Id: I385ce3ef529bda28fac03bfba86fc204c81b8a61
Reviewed-on: https://cl.tvl.fyi/c/depot/+/5241
Tested-by: BuildkiteCI
Reviewed-by: grfn <grfn@gws.fyi>
2022-02-08 12:06:39 +00:00

60 lines
1.5 KiB
Nix

# Builds treefmt for depot, with a hardcoded configuration that
# includes the right paths to formatters.
{ depot, pkgs, ... }:
let
# terraform fmt can't handle multiple paths at once, but treefmt
# expects this
terraformat = pkgs.writeShellScript "terraformat" ''
echo "$@" | xargs -n1 ${pkgs.terraform}/bin/terraform fmt
'';
config = pkgs.writeText "depot-treefmt-config" ''
[formatter.go]
command = "${pkgs.go}/bin/gofmt"
options = [ "-w" ]
includes = ["*.go"]
[formatter.tf]
command = "${terraformat}"
includes = [ "*.tf" ]
[formatter.nix]
command = "${pkgs.nixpkgs-fmt}/bin/nixpkgs-fmt"
includes = [ "*.nix" ]
excludes = [
"third_party/nix/tests/*",
"third_party/nix/src/tests/*"
]
[formatter.rust]
command = "${pkgs.rustfmt}/bin/rustfmt"
includes = [ "*.rs" ]
excludes = [
"users/tazjin/*",
]
'';
# helper tool for formatting the depot interactively
depotfmt = pkgs.writeShellScriptBin "depotfmt" ''
exec ${pkgs.treefmt}/bin/treefmt ''${@} \
--config-file ${config} \
--tree-root $(${pkgs.git}/bin/git rev-parse --show-toplevel)
'';
# wrapper script for running formatting checks in CI
check = pkgs.writeShellScript "depotfmt-check" ''
${pkgs.treefmt}/bin/treefmt \
--clear-cache \
--fail-on-change \
--config-file ${config} \
--tree-root .
'';
in
depotfmt.overrideAttrs (_: {
passthru.meta.ci.extraSteps.check = {
label = "depot formatting check";
command = check;
alwaysRun = true;
};
})