chore(tvix): move castore golang bindings to tvix/castore-go

Have `tvix/castore/protos` only contain the protos, no go noise.

Make the `.pb.go` file generation a pure Nix build
at `//tvix/castore/protos:go-bindings`, and have a script at
`//tvix:castore-go-generate` (TBD) that copies the results to
`tvix/castore-go`.

`//tvix:castore-go`, with sources in `tvix/castore-go` now contains the
tooling around the generated bindings, and the generated bindings
themselves (So go mod replace workflows still work).

An additional CI step is added from there to ensure idempotenty of
the .pb.go files.

The code.tvl.fyi webserver config is updated to the new source code
path. I'm still unsure if we want to also update the go.mod name. While
being a backwards-incompatible change, it'll probbaly make it easier
where to find these files, and the amount of external consumers is still
low enough.

Part of b/323.

Change-Id: I2edadd118c22ec08e57c693f6cc2ef3261c62489
Reviewed-on: https://cl.tvl.fyi/c/depot/+/9787
Reviewed-by: Connor Brewster <cbrewster@hey.com>
Tested-by: BuildkiteCI
This commit is contained in:
Florian Klink 2023-10-17 11:53:57 +01:00 committed by flokli
parent 0325ae3ba3
commit e38733a955
17 changed files with 94 additions and 9 deletions

View file

@ -7,7 +7,6 @@ pkgs.writeShellScriptBin "ci-buf-check" ''
(cd $(git rev-parse --show-toplevel) && buf lint .)
# Run buf generate, and bail out if generated files are changed.
(cd $(git rev-parse --show-toplevel) && buf generate --path tvix/castore/protos)
(cd $(git rev-parse --show-toplevel) && buf generate --path tvix/store/protos)
# Check if any files have changed
if [[ -n "$(git status --porcelain -unormal)" ]]; then

View file

@ -21,7 +21,7 @@
}
location = /go-get/tvix/castore/protos {
alias ${pkgs.writeText "go-import-metadata.html" ''<html><meta name="go-import" content="code.tvl.fyi/tvix/castore/protos git https://code.tvl.fyi/depot.git:/tvix/castore/protos.git"></html>''};
alias ${pkgs.writeText "go-import-metadata.html" ''<html><meta name="go-import" content="code.tvl.fyi/tvix/castore/protos git https://code.tvl.fyi/depot.git:/tvix/castore-go.git"></html>''};
}
location = /go-get/tvix/store/protos {

21
tvix/castore-go/LICENSE Normal file
View file

@ -0,0 +1,21 @@
Copyright © The Tvix Authors
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
“Software”), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

10
tvix/castore-go/README.md Normal file
View file

@ -0,0 +1,10 @@
# castore-go
This directory contains generated golang bindings, both for the `tvix-castore`
data models, as well as the gRPC bindings.
They are generated with `mg run //tvix:castore-go-generate`.
These files end with `.pb.go`, and are ensured to be up to date by a CI check.
Additionally, code useful when interacting with these data structures
(ending just with `.go`) is provided.

View file

@ -0,0 +1,25 @@
{ depot, pkgs, ... }:
(pkgs.buildGoModule {
name = "castore-go";
src = depot.third_party.gitignoreSource ./.;
vendorHash = "sha256-ZNtSSW+oCxMsBtURSrea9/GyUHDagtGefM+Ii+VkgCA=";
}).overrideAttrs (_: {
meta.ci.extraSteps = {
check = {
label = ":water_buffalo: ensure generated protobuf files match";
needsOutput = true;
command = pkgs.writeShellScript "pb-go-check" ''
${depot.tvix.castore-go-generate}
if [[ -n "$(git status --porcelain -unormal)" ]]; then
echo "-----------------------------"
echo ".pb.go files need to be updated, run //tvix:castore-go-generate"
echo "-----------------------------"
git status -unormal
exit 1
fi
'';
alwaysRun = true;
};
};
})

View file

@ -0,0 +1,33 @@
{ depot, pkgs, ... }: {
# Produces the golang bindings.
go-bindings = pkgs.stdenv.mkDerivation {
name = "go-bindings";
src = depot.nix.sparseTree {
name = "castore-protos";
root = depot.path.origSrc;
paths = [
./castore.proto
./rpc_blobstore.proto
./rpc_directory.proto
../../../buf.yaml
../../../buf.gen.yaml
];
};
nativeBuildInputs = [
pkgs.buf
pkgs.protoc-gen-go
pkgs.protoc-gen-go-grpc
];
buildPhase = ''
export HOME=$TMPDIR
buf lint
buf generate
mkdir -p $out
cp tvix/castore/protos/*.pb.go $out/
'';
};
}

View file

@ -106,12 +106,10 @@ in
# workspace too.
shell = (import ./shell.nix { inherit pkgs; });
# Builds and tests the code in castore/protos.
castore-protos-go = pkgs.buildGoModule {
name = "castore-golang";
src = depot.third_party.gitignoreSource ./castore/protos;
vendorHash = "sha256-ZNtSSW+oCxMsBtURSrea9/GyUHDagtGefM+Ii+VkgCA=";
};
# Update `.pb.go` files in tvix/castore-go with the generated ones.
castore-go-generate = pkgs.writeShellScriptBin "castore-go-protogen" ''
(cd $(git rev-parse --show-toplevel)/tvix/castore-go && rm *.pb.go && cp ${depot.tvix.castore.protos.go-bindings}/*.pb.go . && chmod +w *.pb.go)
'';
# Builds and tests the code in store/protos.
store-protos-go = pkgs.buildGoModule {
@ -146,7 +144,6 @@ in
};
meta.ci.targets = [
"castore-protos-go"
"store-protos-go"
"shell"
"rust-docs"