# The checks behind `make check` are split across independent stages so that
# formatting and lint failures surface in seconds, before any compilation:
#
#   lint    - gofmt and golangci-lint (make fmt-check-go, make lint)
#   docs    - prettier check of all markdown (make fmt-check-md)
#   builder - tests (make test) and the release build
#
# The builder stage declares explicit dependencies on lint and docs so
# BuildKit cannot finish the build unless every check passed.

# Lint stage: fast feedback on Go formatting and lint issues
# golangci/golangci-lint:v2.15.0, 2026-10-16 (keep in sync with Makefile)
FROM golangci/golangci-lint@sha256:fbfcfd12a6f8d7f1b97b3362ca80fc2a244d46863148c73cb88e7793af96c481 AS lint
WORKDIR /src
COPY go.mod go.sum ./
RUN go mod download
COPY . .
RUN make fmt-check-go
RUN make lint

# Docs stage: markdown formatting check with the pinned prettier
# node:24-alpine, 2026-10-28
FROM node@sha256:8a32c8cf649a49915a66e81e7a609223c708d0adabf288ee3f4077d765542f85 AS docs
RUN apk add --no-cache make
WORKDIR /src
COPY package.json yarn.lock ./
RUN yarn install --frozen-lockfile
COPY . .
RUN make fmt-check-md

# Build stage
# golang:1.26-alpine, 2026-10-27
FROM golang@sha256:a2012b4a1d1f9f7e9f44e0d39b805a901e856b1dd80b4e6ee4338db547050308 AS builder
RUN apk add --no-cache make
WORKDIR /src

# Force BuildKit to run the lint and docs stages before proceeding
COPY --from=lint /src/go.sum /dev/null
COPY --from=docs /src/package.json /dev/null

COPY go.mod go.sum ./
RUN go mod download
COPY . .
RUN make test

ARG VERSION=dev
RUN CGO_ENABLED=0 go build -trimpath \
    -ldflags="-s -w -X main.Version=${VERSION}" \
    -o /sutil ./cmd/sutil

# Runtime stage: the binary plus the full source it was built from
# alpine:3.24, 2026-10-23
FROM alpine@sha256:be2656061b536c1883c4034790e21d0a09c5aaaf51968b89368a3c0371ea2c9a
COPY --from=builder /sutil /usr/local/bin/sutil
COPY --from=builder /src /usr/local/src/x
ENTRYPOINT ["sutil"]
