Skip to content

Commit a01a390

Browse files
committed
chore: add deadcode elimination linter
Assert that `machined` build does deadcode elimination. Fixes #11296 Signed-off-by: Andrey Smirnov <andrey.smirnov@siderolabs.com> (cherry picked from commit 721595a)
1 parent 49fad0e commit a01a390

File tree

4 files changed

+25
-5
lines changed

4 files changed

+25
-5
lines changed

Dockerfile

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -325,8 +325,11 @@ WORKDIR /src
325325
FROM build AS build-go
326326
COPY ./go.mod ./go.sum ./
327327
COPY ./pkg/machinery/go.mod ./pkg/machinery/go.sum ./pkg/machinery/
328+
COPY ./tools ./tools
328329
WORKDIR /src/pkg/machinery
329330
RUN --mount=type=cache,target=/.cache,id=talos/.cache go mod download
331+
WORKDIR /src/tools
332+
RUN --mount=type=cache,target=/.cache,id=talos/.cache go mod download
330333
WORKDIR /src
331334
RUN --mount=type=cache,target=/.cache,id=talos/.cache go mod download
332335
RUN --mount=type=cache,target=/.cache,id=talos/.cache go mod verify
@@ -505,6 +508,22 @@ WORKDIR /src
505508
FROM base AS lint-vulncheck
506509
RUN --mount=type=cache,target=/.cache,id=talos/.cache govulncheck ./...
507510

511+
# The lint-deadcode target runs the deadcode elimination check.
512+
FROM base AS lint-deadcode
513+
ARG GO_BUILDFLAGS
514+
ARG GO_LDFLAGS
515+
ARG GO_MACHINED_LDFLAGS
516+
ARG GOAMD64
517+
RUN --mount=type=cache,target=/.cache,id=talos/.cache GOOS=linux GOARCH=amd64 GOAMD64=${GOAMD64} go build ${GO_BUILDFLAGS} -ldflags "${GO_LDFLAGS} ${GO_MACHINED_LDFLAGS} -dumpdep" ./internal/app/machined \
518+
|& go tool -modfile=tools/go.mod github.com/aarzilli/whydeadcode > deadcode.txt
519+
RUN if [[ -s deadcode.txt ]]; then \
520+
echo "Dead code elimination problem found:"; \
521+
cat deadcode.txt; \
522+
exit 1; \
523+
else \
524+
echo "No dead code elimination issues found"; \
525+
fi
526+
508527
# The init target builds the init binary.
509528

510529
FROM base AS init-build-amd64
@@ -892,7 +911,6 @@ ARG NAME
892911
ARG TAG
893912

894913
COPY ./hack/sbom.sh /usr/bin/sbom.sh
895-
COPY ./tools ./tools
896914

897915
RUN mkdir -p /tmp/sbom-src /rootfs/usr/share/spdx
898916
RUN cp go.mod go.sum /tmp/sbom-src/
@@ -953,7 +971,6 @@ COPY --from=vex-generate /talos.grype.yaml /talos.grype.yaml
953971
FROM build-go AS grype-scan
954972
COPY --from=sbom-arm64 /talos-arm64.spdx.json /talos-arm64.spdx.json
955973
COPY --from=vex /talos.vex.json /talos.vex.json
956-
COPY ./tools ./tools
957974
RUN --mount=type=cache,target=/.cache,id=talos/.cache go tool -modfile=tools/go.mod \
958975
github.com/anchore/grype/cmd/grype sbom:/talos-arm64.spdx.json \
959976
--vex /talos.vex.json -vv 2>&1 | tee /grype-scan.log
@@ -965,7 +982,6 @@ FROM build-go AS grype-validate
965982
COPY --from=sbom-arm64 /talos-arm64.spdx.json /talos-arm64.spdx.json
966983
COPY --from=vex /talos.vex.json /talos.vex.json
967984
COPY --from=vex /talos.grype.yaml /talos.grype.yaml
968-
COPY ./tools ./tools
969985
RUN --mount=type=cache,target=/.cache,id=talos/.cache go tool -modfile=tools/go.mod \
970986
github.com/anchore/grype/cmd/grype sbom:/talos-arm64.spdx.json \
971987
--vex /talos.vex.json -vv --fail-on negligible --config /talos.grype.yaml

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -560,8 +560,8 @@ fmt: ## Formats the source code and protobuf files.
560560
lint-%: ## Runs the specified linter. Valid options are go, protobuf, and markdown (e.g. lint-go).
561561
@$(MAKE) target-lint-$* PLATFORM=linux/$(ARCH)
562562

563-
lint: ## Runs linters on go, vulncheck, protobuf, and markdown file types.
564-
@$(MAKE) lint-go lint-vulncheck lint-protobuf lint-markdown
563+
lint: ## Runs linters on go, vulncheck, deadcode, protobuf, and markdown file types.
564+
@$(MAKE) lint-go lint-vulncheck lint-deadcode lint-protobuf lint-markdown
565565

566566
check-dirty: ## Verifies that source tree is not dirty
567567
@if test -n "`git status --porcelain`"; then echo "Source tree is dirty"; git status; git diff; exit 1 ; fi

tools/go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module github.com/siderolabs/talos/tools
33
go 1.24.5
44

55
tool (
6+
github.com/aarzilli/whydeadcode
67
github.com/anchore/grype/cmd/grype
78
github.com/anchore/syft/cmd/syft
89
)
@@ -42,6 +43,7 @@ require (
4243
github.com/OneOfOne/xxhash v1.2.8 // indirect
4344
github.com/ProtonMail/go-crypto v1.2.0 // indirect
4445
github.com/STARRY-S/zip v0.2.1 // indirect
46+
github.com/aarzilli/whydeadcode v0.0.0-20241226171816-ed86f8ea0a6f // indirect
4547
github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d // indirect
4648
github.com/acobaugh/osrelease v0.1.0 // indirect
4749
github.com/adrg/xdg v0.5.3 // indirect

tools/go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -664,6 +664,8 @@ github.com/ProtonMail/go-crypto v1.2.0 h1:+PhXXn4SPGd+qk76TlEePBfOfivE0zkWFenhGh
664664
github.com/ProtonMail/go-crypto v1.2.0/go.mod h1:9whxjD8Rbs29b4XWbB8irEcE8KHMqaR2e7GWU1R+/PE=
665665
github.com/STARRY-S/zip v0.2.1 h1:pWBd4tuSGm3wtpoqRZZ2EAwOmcHK6XFf7bU9qcJXyFg=
666666
github.com/STARRY-S/zip v0.2.1/go.mod h1:xNvshLODWtC4EJ702g7cTYn13G53o1+X9BWnPFpcWV4=
667+
github.com/aarzilli/whydeadcode v0.0.0-20241226171816-ed86f8ea0a6f h1:S9nWL2BVcrC0teJDsnP+HLWqnOlGl9cz/a5rlsUh4cc=
668+
github.com/aarzilli/whydeadcode v0.0.0-20241226171816-ed86f8ea0a6f/go.mod h1:rHV6WbQI4oOtjbl+J0d9UYVs8eBU9bqZIp9UB5QACiQ=
667669
github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d h1:licZJFw2RwpHMqeKTCYkitsPqHNxTmd4SNR5r94FGM8=
668670
github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d/go.mod h1:asat636LX7Bqt5lYEZ27JNDcqxfjdBQuJ/MM4CN/Lzo=
669671
github.com/acobaugh/osrelease v0.1.0 h1:Yb59HQDGGNhCj4suHaFQQfBps5wyoKLSSX/J/+UifRE=

0 commit comments

Comments
 (0)