Przeglądaj źródła

build: make source generation reproducible (#1119)

James Read 1 dzień temu
rodzic
commit
67a030da79

+ 58 - 0
.github/workflows/generated.yml

@@ -0,0 +1,58 @@
+name: Generated source checks
+
+on:
+  push:
+    branches:
+      - main
+      - next
+    paths:
+      - '.github/workflows/generated.yml'
+      - 'Makefile'
+      - 'frontend/resources/scripts/gen/**'
+      - 'lang/**'
+      - 'proto/**'
+      - 'service/gen/**'
+      - 'service/generate.go'
+      - 'service/go.mod'
+      - 'service/go.sum'
+      - 'service/Makefile'
+      - 'service/tools.go'
+  pull_request:
+    branches:
+      - next
+    paths:
+      - '.github/workflows/generated.yml'
+      - 'Makefile'
+      - 'frontend/resources/scripts/gen/**'
+      - 'lang/**'
+      - 'proto/**'
+      - 'service/gen/**'
+      - 'service/generate.go'
+      - 'service/go.mod'
+      - 'service/go.sum'
+      - 'service/Makefile'
+      - 'service/tools.go'
+
+permissions:
+  contents: read
+
+jobs:
+  generated:
+    runs-on: ubuntu-latest
+    steps:
+      - name: Checkout
+        uses: actions/checkout@v4
+        with:
+          persist-credentials: false
+
+      - name: Setup Go
+        uses: actions/setup-go@v5
+        with:
+          go-version-file: 'service/go.mod'
+          cache: true
+          cache-dependency-path: |
+            service/go.sum
+            lang/go.sum
+
+      - name: Check generated sources
+        run: make generated-check

+ 1 - 2
CONTRIBUTING.adoc

@@ -46,8 +46,7 @@ pre-commit install
 pre-commit install --hook-type pre-push
 
 # Step3: compile binary for current dev env (OS, ARCH)
-# `make proto` will also run `make go-tools`, which installs "buf". This binary
-# will be put in your GOPATH/bin/, which should be on your path. buf is used to
+# `make proto` installs pinned protobuf tools in GOPATH/bin/ and uses buf to
 # generate the protobuf / Connect RPC stubs.
 make proto
 make

+ 13 - 2
Makefile

@@ -38,9 +38,20 @@ it:
 go-tools:
 	$(MAKE) -wC service go-tools
 
-proto: go-tools
+proto-tools:
+	$(MAKE) -wC service proto-tools
+
+proto: proto-tools
 	$(MAKE) -wC proto
 
+lang-generate:
+	$(MAKE) -wC lang
+
+generated-check: proto lang-generate
+	git diff --exit-code -- service/gen frontend/resources/scripts/gen lang/combined_output.json
+	@untracked="$$(git ls-files --others --exclude-standard -- service/gen frontend/resources/scripts/gen lang/combined_output.json)"; \
+	test -z "$$untracked" || { printf 'Untracked generated files:\n%s\n' "$$untracked"; exit 1; }
+
 dist:
 	echo "dist noop"
 
@@ -85,4 +96,4 @@ config-tool:
 devcheck:
 	python3 scripts/devcheck.py $(ARGS)
 
-.PHONY: proto default service windows-resources windows-msi frontend-unittests docs-check it devcheck
+.PHONY: proto proto-tools lang-generate generated-check default service windows-resources windows-msi frontend-unittests docs-check it devcheck

+ 1 - 0
lang/main.go

@@ -34,6 +34,7 @@ func main() {
 		log.Fatalf("Error marshalling combined language content: %v", err)
 	}
 
+	jsonData = append(jsonData, '\n')
 	err = os.WriteFile("combined_output.json", jsonData, 0644)
 
 	if err != nil {

+ 1 - 0
proto/Makefile

@@ -1,4 +1,5 @@
 buf:
 	buf generate
+	python3 normalize_generated.py
 
 .PHONY: buf

+ 28 - 0
proto/normalize_generated.py

@@ -0,0 +1,28 @@
+#!/usr/bin/env python3
+
+from pathlib import Path
+
+ROOT = Path(__file__).resolve().parent.parent
+GENERATED_DIRS = (
+    ROOT / "service/gen",
+    ROOT / "frontend/resources/scripts/gen",
+)
+
+
+def normalize_file(path: Path) -> None:
+    content = path.read_bytes()
+    normalized = content.rstrip(b"\r\n") + b"\n"
+
+    if normalized != content:
+        path.write_bytes(normalized)
+
+
+def main() -> None:
+    for directory in GENERATED_DIRS:
+        for path in directory.rglob("*"):
+            if path.is_file():
+                normalize_file(path)
+
+
+if __name__ == "__main__":
+    main()

+ 13 - 12
service/Makefile

@@ -2,6 +2,10 @@ define delete-files
 	python3 -c "import shutil;shutil.rmtree('$(1)', ignore_errors=True)"
 endef
 
+BUF_VERSION := v1.72.0
+GOLANGCI_LINT_VERSION := v2.13.2
+PROTOC_GEN_GO_VERSION := v1.36.12
+
 compile-currentenv:
 	go build
 
@@ -10,14 +14,10 @@ prep:
 	go generate ./...
 
 compile-armhf:
-	go env -w GOARCH=arm GOARM=6
-	go build -o OliveTin.armhf
-	go env -u GOARCH GOARM
+	GOOS=linux GOARCH=arm GOARM=6 go build -o OliveTin.armhf
 
 compile-x64-lin:
-	go env -w GOOS=linux
-	go build -o OliveTin
-	go env -u GOOS
+	GOOS=linux GOARCH=amd64 go build -o OliveTin
 
 compile-x64-win: windows-resources
 	GOOS=windows GOARCH=amd64 go build -o OliveTin.exe
@@ -55,11 +55,12 @@ find-flakey-tests-inf:
 	go run ./scripts/find-flakey-tests-inf
 
 go-tools:
-	go install "github.com/golangci/golangci-lint/v2/cmd/golangci-lint@v2.13.2"
+	go install "github.com/golangci/golangci-lint/v2/cmd/golangci-lint@$(GOLANGCI_LINT_VERSION)"
+
+proto-tools:
+	go install "github.com/bufbuild/buf/cmd/buf@$(BUF_VERSION)"
+	go install "google.golang.org/protobuf/cmd/protoc-gen-go@$(PROTOC_GEN_GO_VERSION)"
 
-.PHONY: codestyle go-tools unittests unittests-fast unittests-race find-flakey-tests find-flakey-tests-inf
+go-tools-all: go-tools proto-tools
 
-go-tools-all:
-	go install "github.com/bufbuild/buf/cmd/buf"
-	go install "github.com/golangci/golangci-lint/v2/cmd/golangci-lint@v2.13.2"
-	go install "google.golang.org/protobuf/cmd/protoc-gen-go"
+.PHONY: codestyle go-tools proto-tools go-tools-all unittests unittests-fast unittests-race find-flakey-tests find-flakey-tests-inf

+ 1 - 1
service/generate.go

@@ -1,3 +1,3 @@
-//go:generate make -wC ../
+//go:generate make -wC ../ proto
 
 package main