Go added workspaces in 1.18 and support was subsequently to gazelle with bazel-gazelle#1250.
This example shows how to used go workspaces with gazelle and rules_go.
Dependency versions in go.mod
& go.sum
through the workspace defined in go.work
should be kept
in sync by running go work sync
.
In this example module1
and module2
are both runnable main packages.
You can build & run with regular go tooling from the root of the go_workspaces example directory as follows,
$ go run ./module1
Calling cmd.Execute()!
sunrise 2022-09-26 11:09:12 +0000 UTC sunset 2022-09-26 23:08:26 +0000 UTC
and
$ go run ./module2
sunrise 2022-09-26 11:09:12 +0000 UTC sunset 2022-09-26 23:08:26 +0000 UTC
To update the list of go_repositories()
in deps.bzl
from go.mod
&
corresponding go.sum
files listed in go.work
run:
bazel run //:gazelle-update-repos
To update & create go targets throughout the repository run
bazel run //:gazelle
To run the two go_binary targets generated by gazelle call,
$ bazel run //module1:module1
INFO: Analyzed target //module1:module1 (0 packages loaded, 0 targets configured).
INFO: Found 1 target...
Target //module1:module1 up-to-date:
bazel-bin/module1/module1_/module1
INFO: Elapsed time: 0.167s, Critical Path: 0.00s
INFO: 1 process: 1 internal.
INFO: Build completed successfully, 1 total action
INFO: Build completed successfully, 1 total action
Calling cmd.Execute()!
sunrise 2022-09-26 11:09:12 +0000 UTC sunset 2022-09-26 23:08:26 +0000 UTC
and
$ bazel run //module2:module2
INFO: Analyzed target //module2:module2 (0 packages loaded, 0 targets configured).
INFO: Found 1 target...
Target //module2:module2 up-to-date:
bazel-bin/module2/module2_/module2
INFO: Elapsed time: 0.184s, Critical Path: 0.00s
INFO: 1 process: 1 internal.
INFO: Build completed successfully, 1 total action
INFO: Build completed successfully, 1 total action
sunrise 2022-09-26 11:09:12 +0000 UTC sunset 2022-09-26 23:08:26 +0000 UTC