# syntax=docker/dockerfile:1

FROM docker.io/golang:1.25.11

# Image layers go from less likely to most likely to change.
RUN apt-get update && \
  DEBIAN_FRONTEND=noninteractive apt-get install -yq --no-install-recommends npm && \
  rm -rf /var/lib/apt/lists/*

# Pre-cache the `npm exec`ed packages. We execute `true` in the exec environment
# so `npm exec` will download, build and install packages but not actually run
# anything.
#
# The version should be kept in sync with buf-ts.gen.yaml.
RUN npm exec --yes \
  --package=@protobuf-ts/plugin@2.9.3 \
  --package=@protobuf-ts/plugin-framework@2.9.3 \
  --package=@protobuf-ts/protoc@2.9.3 \
  --package=@protobuf-ts/runtime@2.9.3 \
  --package=@protobuf-ts/runtime-rpc@2.9.3 \
  --package=typescript@3.9.10 \
  -- true

# buf version including v, like "v1.26.1"
ARG BUF_VERSION
RUN go install "github.com/bufbuild/buf/cmd/buf@${BUF_VERSION}"

# Pre-cache `go run`ned binaries. We use `true` as the launcher so `go run` will
# cache the final linked binaries but not actually run anything. This can be
# verified to work in the final image by using `go run -n` (dry-run) from the
# teleport workdir to confirm that the binary is immediately available for
# running.
#
# This is meant to be the only step that changes depending on the Teleport
# branch.
COPY go.mod go.sum /teleport-module/
RUN \
  go -C /teleport-module run -exec true connectrpc.com/connect/cmd/protoc-gen-connect-go && \
  go -C /teleport-module run -exec true github.com/gogo/protobuf/protoc-gen-gogofast && \
  go -C /teleport-module run -exec true google.golang.org/grpc/cmd/protoc-gen-go-grpc && \
  go -C /teleport-module run -exec true google.golang.org/protobuf/cmd/protoc-gen-go && \
  rm -rf /teleport-module

# Pre-cache buf plugin
COPY build.assets/tooling /tooling-tmp/
RUN \
  go -C /tooling-tmp  run -exec true ./cmd/buf-plugin-linters &&  \
  rm -rf /tooling-tmp

ARG UID
ARG GID
RUN mkdir -p /.cache /.npm && \
  chown -R $UID:$GID /.cache /.npm /go/pkg/mod/
