From e38733a955f6f6fc8962cf0e669d9cf4696bc14d Mon Sep 17 00:00:00 2001 From: Florian Klink Date: Tue, 17 Oct 2023 11:53:57 +0100 Subject: [PATCH] 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 Tested-by: BuildkiteCI --- nix/bufCheck/default.nix | 1 - ops/modules/www/code.tvl.fyi.nix | 2 +- tvix/castore-go/LICENSE | 21 ++++++++++++ tvix/castore-go/README.md | 10 ++++++ .../{castore/protos => castore-go}/castore.go | 0 .../protos => castore-go}/castore.pb.go | 0 .../protos => castore-go}/castore_test.go | 0 tvix/castore-go/default.nix | 25 ++++++++++++++ tvix/{castore/protos => castore-go}/go.mod | 0 tvix/{castore/protos => castore-go}/go.sum | 0 .../protos => castore-go}/rename_node.go | 0 .../protos => castore-go}/rpc_blobstore.pb.go | 0 .../rpc_blobstore_grpc.pb.go | 0 .../protos => castore-go}/rpc_directory.pb.go | 0 .../rpc_directory_grpc.pb.go | 0 tvix/castore/protos/default.nix | 33 +++++++++++++++++++ tvix/default.nix | 11 +++---- 17 files changed, 94 insertions(+), 9 deletions(-) create mode 100644 tvix/castore-go/LICENSE create mode 100644 tvix/castore-go/README.md rename tvix/{castore/protos => castore-go}/castore.go (100%) rename tvix/{castore/protos => castore-go}/castore.pb.go (100%) rename tvix/{castore/protos => castore-go}/castore_test.go (100%) create mode 100644 tvix/castore-go/default.nix rename tvix/{castore/protos => castore-go}/go.mod (100%) rename tvix/{castore/protos => castore-go}/go.sum (100%) rename tvix/{castore/protos => castore-go}/rename_node.go (100%) rename tvix/{castore/protos => castore-go}/rpc_blobstore.pb.go (100%) rename tvix/{castore/protos => castore-go}/rpc_blobstore_grpc.pb.go (100%) rename tvix/{castore/protos => castore-go}/rpc_directory.pb.go (100%) rename tvix/{castore/protos => castore-go}/rpc_directory_grpc.pb.go (100%) create mode 100644 tvix/castore/protos/default.nix diff --git a/nix/bufCheck/default.nix b/nix/bufCheck/default.nix index 3a6451330..f247e6839 100644 --- a/nix/bufCheck/default.nix +++ b/nix/bufCheck/default.nix @@ -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 diff --git a/ops/modules/www/code.tvl.fyi.nix b/ops/modules/www/code.tvl.fyi.nix index 8d8938bca..6a7e4b250 100644 --- a/ops/modules/www/code.tvl.fyi.nix +++ b/ops/modules/www/code.tvl.fyi.nix @@ -21,7 +21,7 @@ } location = /go-get/tvix/castore/protos { - alias ${pkgs.writeText "go-import-metadata.html" ''''}; + alias ${pkgs.writeText "go-import-metadata.html" ''''}; } location = /go-get/tvix/store/protos { diff --git a/tvix/castore-go/LICENSE b/tvix/castore-go/LICENSE new file mode 100644 index 000000000..2034ada6f --- /dev/null +++ b/tvix/castore-go/LICENSE @@ -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. + diff --git a/tvix/castore-go/README.md b/tvix/castore-go/README.md new file mode 100644 index 000000000..1aee48dfd --- /dev/null +++ b/tvix/castore-go/README.md @@ -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. diff --git a/tvix/castore/protos/castore.go b/tvix/castore-go/castore.go similarity index 100% rename from tvix/castore/protos/castore.go rename to tvix/castore-go/castore.go diff --git a/tvix/castore/protos/castore.pb.go b/tvix/castore-go/castore.pb.go similarity index 100% rename from tvix/castore/protos/castore.pb.go rename to tvix/castore-go/castore.pb.go diff --git a/tvix/castore/protos/castore_test.go b/tvix/castore-go/castore_test.go similarity index 100% rename from tvix/castore/protos/castore_test.go rename to tvix/castore-go/castore_test.go diff --git a/tvix/castore-go/default.nix b/tvix/castore-go/default.nix new file mode 100644 index 000000000..09bbb77f8 --- /dev/null +++ b/tvix/castore-go/default.nix @@ -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; + }; + }; +}) diff --git a/tvix/castore/protos/go.mod b/tvix/castore-go/go.mod similarity index 100% rename from tvix/castore/protos/go.mod rename to tvix/castore-go/go.mod diff --git a/tvix/castore/protos/go.sum b/tvix/castore-go/go.sum similarity index 100% rename from tvix/castore/protos/go.sum rename to tvix/castore-go/go.sum diff --git a/tvix/castore/protos/rename_node.go b/tvix/castore-go/rename_node.go similarity index 100% rename from tvix/castore/protos/rename_node.go rename to tvix/castore-go/rename_node.go diff --git a/tvix/castore/protos/rpc_blobstore.pb.go b/tvix/castore-go/rpc_blobstore.pb.go similarity index 100% rename from tvix/castore/protos/rpc_blobstore.pb.go rename to tvix/castore-go/rpc_blobstore.pb.go diff --git a/tvix/castore/protos/rpc_blobstore_grpc.pb.go b/tvix/castore-go/rpc_blobstore_grpc.pb.go similarity index 100% rename from tvix/castore/protos/rpc_blobstore_grpc.pb.go rename to tvix/castore-go/rpc_blobstore_grpc.pb.go diff --git a/tvix/castore/protos/rpc_directory.pb.go b/tvix/castore-go/rpc_directory.pb.go similarity index 100% rename from tvix/castore/protos/rpc_directory.pb.go rename to tvix/castore-go/rpc_directory.pb.go diff --git a/tvix/castore/protos/rpc_directory_grpc.pb.go b/tvix/castore-go/rpc_directory_grpc.pb.go similarity index 100% rename from tvix/castore/protos/rpc_directory_grpc.pb.go rename to tvix/castore-go/rpc_directory_grpc.pb.go diff --git a/tvix/castore/protos/default.nix b/tvix/castore/protos/default.nix new file mode 100644 index 000000000..e2e2f910e --- /dev/null +++ b/tvix/castore/protos/default.nix @@ -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/ + ''; + }; +} diff --git a/tvix/default.nix b/tvix/default.nix index dfb9f4143..3365fe798 100644 --- a/tvix/default.nix +++ b/tvix/default.nix @@ -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"