How to add go build -ldflags to bazel compilation
What version of rules_go are you using?
v0.41.0
What version of gazelle are you using?
v0.33.0
What version of Bazel are you using?
bazel 6.3.2
Does this issue reproduce with the latest releases of all the above?
What operating system and processor architecture are you using?
Any other potentially useful information about your toolchain?
What did you do?
Migrate my golang project to compile using bazel My golang compilation script is as follows
COMMIT_HASH=$(shell git rev-parse --verify HEAD | cut -c 1-8) BUILD_DATE=$(shell date +%Y-%m-%d_%H:%M:%S%z) GIT_TAG=$(shell git describe --tags) GIT_AUTHOR=$(shell git show -s --format=%an) SHELL:=/bin/bash BIN_NAME="test_server"
build: mod go build -ldflags "-X main.GitTag=$(GIT_TAG) -X main.BuildTime=$(BUILD_DATE) -X main.GitCommit=$(COMMIT_HASH) -X main.GitAuthor=$(GIT_AUTHOR)" -o ${BIN_NAME} ./cmd/main.go
My bazel compilation command is as follows: go mod tidy bazel run //:gazelle-update-repos bazel run //:gazelle bazel build ...
How to add ldflags information to bazel compilation?
What did you expect to see?
run ./test_server version,You can see the tag, author and commit information, etc.
What did you see instead?
You can use the x_defs attribute on go_binary and go_library.
@fmeum x_defs can indeed be implemented, but I use bazel_gazelle to generate BUILD.bazel. I don’t want to maintain BUILD.bazel separately. Is there any way for gazelle to solve this kind of custom parameter setting?
Gazelle should maintain your local edits