Skip to content

Commit 7804e2c

Browse files
authored
Merge pull request moby#3236 from crazy-max/generated-infer-gomod
hack: use tools build constraint for generated-files deps
2 parents 0a377bb + e1b3b6c commit 7804e2c

File tree

58 files changed

+20072
-189
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+20072
-189
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ require (
8686
golang.org/x/time v0.1.0
8787
google.golang.org/genproto v0.0.0-20220706185917-7780775163c4
8888
google.golang.org/grpc v1.50.1
89+
google.golang.org/protobuf v1.28.1
8990
)
9091

9192
require (
@@ -150,7 +151,6 @@ require (
150151
go.opentelemetry.io/otel/internal/metric v0.27.0 // indirect
151152
go.opentelemetry.io/otel/metric v0.27.0 // indirect
152153
golang.org/x/text v0.4.0 // indirect
153-
google.golang.org/protobuf v1.28.1 // indirect
154154
gopkg.in/yaml.v3 v3.0.1 // indirect
155155
)
156156

Lines changed: 49 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,55 @@
11
# syntax=docker/dockerfile-upstream:master
22

3-
# protoc is dynamically linked to glibc to can't use golang:1.10-alpine
4-
FROM golang:1.19-buster AS gobuild-base
5-
6-
RUN apt-get update && apt-get --no-install-recommends install -y \
7-
unzip \
8-
&& true
9-
10-
# https://github.com/golang/protobuf/blob/v1.3.5/.travis.yml#L15
11-
ARG PROTOC_VERSION=3.11.4
12-
ARG TARGETOS TARGETARCH
13-
RUN set -e; \
14-
arch=$(echo $TARGETARCH | sed -e s/amd64/x86_64/ -e s/arm64/aarch_64/); \
15-
wget -q https://github.com/protocolbuffers/protobuf/releases/download/v${PROTOC_VERSION}/protoc-${PROTOC_VERSION}-${TARGETOS}-${arch}.zip && unzip protoc-${PROTOC_VERSION}-${TARGETOS}-${arch}.zip -d /usr/local
16-
17-
ARG GOGO_VERSION=v1.3.2
18-
RUN --mount=target=/root/.cache,type=cache GO111MODULE=on go install \
19-
github.com/gogo/protobuf/protoc-gen-gogo@${GOGO_VERSION} \
20-
github.com/gogo/protobuf/protoc-gen-gogofaster@${GOGO_VERSION} \
21-
github.com/gogo/protobuf/protoc-gen-gogoslick@${GOGO_VERSION}
22-
23-
ARG PROTOBUF_VERSION=v1.3.5
24-
RUN --mount=target=/root/.cache,type=cache GO111MODULE=on go install \
25-
github.com/golang/protobuf/protoc-gen-go@${PROTOBUF_VERSION}
26-
3+
ARG GO_VERSION="1.19"
4+
ARG PROTOC_VERSION="3.11.4"
5+
6+
# protoc is dynamically linked to glibc so can't use alpine base
7+
FROM golang:${GO_VERSION}-buster AS base
8+
RUN apt-get update && apt-get --no-install-recommends install -y git unzip
9+
ARG PROTOC_VERSION
10+
ARG TARGETOS
11+
ARG TARGETARCH
12+
RUN <<EOT
13+
set -e
14+
arch=$(echo $TARGETARCH | sed -e s/amd64/x86_64/ -e s/arm64/aarch_64/)
15+
wget -q https://github.com/protocolbuffers/protobuf/releases/download/v${PROTOC_VERSION}/protoc-${PROTOC_VERSION}-${TARGETOS}-${arch}.zip
16+
unzip protoc-${PROTOC_VERSION}-${TARGETOS}-${arch}.zip -d /usr/local
17+
EOT
2718
WORKDIR /go/src/github.com/moby/buildkit
2819

29-
# Generate into a subdirectory because if it is in the root then the
30-
# extraction with `docker export` ends up putting `.dockerenv`, `dev`,
31-
# `sys` and `proc` into the source directory. With this we can use
32-
# `tar --strip-components=1 generated-files` on the output of `docker
33-
# export`.
34-
FROM gobuild-base AS generated
35-
RUN mkdir /generated-files
36-
RUN --mount=target=/tmp/src \
37-
cp -r /tmp/src/. . && \
38-
git add -A && \
39-
go generate -mod=vendor -v ./... && \
40-
git ls-files -m --others -- **/*.pb.go | tar -cf - --files-from - | tar -C /generated-files -xf -
20+
FROM base AS tools
21+
RUN --mount=type=bind,target=.,rw \
22+
--mount=type=cache,target=/root/.cache \
23+
--mount=type=cache,target=/go/pkg/mod \
24+
go install \
25+
github.com/gogo/protobuf/protoc-gen-gogo \
26+
github.com/gogo/protobuf/protoc-gen-gogofaster \
27+
github.com/gogo/protobuf/protoc-gen-gogoslick \
28+
github.com/golang/protobuf/protoc-gen-go
29+
30+
FROM tools AS generated
31+
RUN --mount=type=bind,target=.,rw <<EOT
32+
set -ex
33+
go generate -mod=vendor -v ./...
34+
mkdir /out
35+
git ls-files -m --others -- ':!vendor' '**/*.pb.go' | tar -cf - --files-from - | tar -C /out -xf -
36+
EOT
4137

4238
FROM scratch AS update
43-
COPY --from=generated /generated-files /generated-files
44-
45-
FROM gobuild-base AS validate
46-
RUN --mount=target=/tmp/src \
47-
cp -r /tmp/src/. . && \
48-
go generate -mod=vendor -v ./... && git diff && ./hack/validate-generated-files check
39+
COPY --from=generated /out /
40+
41+
FROM base AS validate
42+
RUN --mount=type=bind,target=.,rw \
43+
--mount=type=bind,from=generated,source=/out,target=/generated-files <<EOT
44+
set -e
45+
git add -A
46+
if [ "$(ls -A /generated-files)" ]; then
47+
cp -rf /generated-files/* .
48+
fi
49+
diff=$(git status --porcelain -- ':!vendor' '**/*.pb.go')
50+
if [ -n "$diff" ]; then
51+
echo >&2 'ERROR: The result of "go generate" differs. Please update with "make generated-files"'
52+
echo "$diff"
53+
exit 1
54+
fi
55+
EOT

hack/update-generated-files

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,13 @@
33
. $(dirname $0)/util
44
set -eu
55

6-
gogo_version=$(awk '$1 == "github.com/gogo/protobuf" { print $2 }' go.mod)
7-
# protobuf_version=$(awk '$3 == "github.com/golang/protobuf" { print $4 }' go.mod)
86
output=$(mktemp -d -t buildctl-output.XXXXXXXXXX)
97

108
buildxCmd build \
119
--target "update" \
12-
--build-arg "GOGO_VERSION=$gogo_version" \
1310
--output "type=local,dest=$output" \
1411
--file "./hack/dockerfiles/generated-files.Dockerfile" \
1512
.
1613

17-
# --build-arg "PROTOBUF_VERSION=$protobuf_version" \
18-
19-
cp -R "$output/generated-files/." .
14+
cp -R "$output/." .
2015
rm -rf $output

hack/validate-generated-files

Lines changed: 7 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,10 @@
11
#!/usr/bin/env bash
2+
3+
. $(dirname $0)/util
24
set -eu
35

4-
case ${1:-} in
5-
'')
6-
. $(dirname $0)/util
7-
gogo_version=$(awk '$1 == "github.com/gogo/protobuf" { print $2 }' go.mod)
8-
buildxCmd build \
9-
--target validate \
10-
--build-arg "GOGO_VERSION=$gogo_version" \
11-
--file ./hack/dockerfiles/generated-files.Dockerfile \
12-
.
13-
;;
14-
check)
15-
diffs="$(git status --porcelain -- **/*.pb.go 2>/dev/null)"
16-
set +x
17-
if [ "$diffs" ]; then
18-
{
19-
echo 'The result of "go generate" differs'
20-
echo
21-
echo "$diffs"
22-
echo
23-
echo 'Please update with "make generated-files"'
24-
echo
25-
} >&2
26-
exit 1
27-
fi
28-
echo 'Congratulations! All auto generated files are correct.'
29-
;;
30-
esac
6+
buildxCmd build \
7+
--target "validate" \
8+
--output "type=cacheonly" \
9+
--file "./hack/dockerfiles/generated-files.Dockerfile" \
10+
.

tools/tools.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//go:build tools
2+
// +build tools
3+
4+
// Package tools tracks dependencies on binaries not referenced in this codebase.
5+
// https://github.com/golang/go/wiki/Modules#how-can-i-track-tool-dependencies-for-a-module
6+
// Disclaimer: Avoid adding tools that don't need to be inferred from go.mod
7+
// like golangci-lint and check they don't import too many dependencies.
8+
package tools
9+
10+
import (
11+
_ "github.com/gogo/protobuf/protoc-gen-gogo"
12+
_ "github.com/gogo/protobuf/protoc-gen-gogofaster"
13+
_ "github.com/gogo/protobuf/protoc-gen-gogoslick"
14+
_ "github.com/golang/protobuf/protoc-gen-go"
15+
)

util/grpcerrors/grpcerrors.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ func FromGRPC(err error) error {
182182

183183
for _, s := range stacks {
184184
if s != nil {
185-
err = stack.Wrap(err, *s)
185+
err = stack.Wrap(err, s)
186186
}
187187
}
188188

util/stack/stack.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ func Enable(err error) error {
7979
return err
8080
}
8181

82-
func Wrap(err error, s Stack) error {
82+
func Wrap(err error, s *Stack) error {
8383
return &withStack{stack: s, error: err}
8484
}
8585

@@ -169,7 +169,7 @@ func convertStack(s errors.StackTrace) *Stack {
169169
}
170170

171171
type withStack struct {
172-
stack Stack
172+
stack *Stack
173173
error
174174
}
175175

@@ -178,5 +178,5 @@ func (e *withStack) Unwrap() error {
178178
}
179179

180180
func (e *withStack) StackTrace() *Stack {
181-
return &e.stack
181+
return e.stack
182182
}

0 commit comments

Comments
 (0)