Notes about using bazel
Flaky tests
bazel test --runs_per_test=100 --cache_test_results=no --test_arg=-test.v --test_output=streamed <target_path> --test_filter=Testxxxxx
To run with race detetor
bazel test ... --test_filter=TestRunForever --runs_per_test=100 --@io_bazel_rules_go//go/config:race
Run benchmark
bazel run //target:go_default_test \
--test_sharding_strategy=disabled -- -test.bench=Benchmark
Run with environment variables
- I used this for company-s debugging an grpc GOAWAY error.
bazel test --test_arg=-test.v --test_output=streamed <your-package>/... --test_filter=TestRPC --test_env=GODEBUG=http2debug=2
Test Coverage
bazel coverage --nocache_test_results <your-package-path>:all
go install github.com/wadey/gocovmerge@latest
// The path of the coverage.dat is from Bazel command output.
$GOPATH/bin/gocovmerge <path>/<to>/<your>/<coverage-dat>/coverage.dat > bazel-cover.out
go tool cover -func bazel-cover.out
Cross platform compilation for go
for x86 linux
This is needed in company-s because all Macs are ARM.
bazel build --platforms=@io_bazel_rules_go//go/toolchain:linux_amd64 //your-targets/...
The built will be at bazel-bin/your/target/locaiton
(i.e. bazel-bin/ dir)
Also, when not using bazel, but just go toolchain directly, add env variables as below:
GOOS=linux GOARCH=amd64 go build <package>
Command line Auto completion
For bash + mac, in ~/.bash\_profile
source $(dirname $(readlink -f `which bazel`))/bazel-complete.bash
WARNING
This could be quite slow on big projects!
Reference:
- https://bazel.build/install/completion#bash
- The key is the ‘bazel-complete.bash’ file.
Query dependencies
What are the packages that depend on qtdb lib?
bazel query 'rdeps(..., //vistar/geo/qtdb:go_default_library)' --output package
which packages does qtdb depend on?
bazel query 'deps(//vistar/geo/qtdb:go_default_library)' --output package