Skip to content

Commit

Permalink
chore: rework go dependencies (#321)
Browse files Browse the repository at this point in the history
Reworked the Go dependencies for this repository so that we do not need
to list the transitive dependencies in `MODULE.bazel`. Because
`github.com/ekalinin/github-markdown-toc.go` references its dependencies
in its `main` package, `go get ...` does not add the transitive
dependencies to `go.mod`. The bzlmod-aware `update-repos` does not find
the transitive dependencies. To fix this, we add direct dependency for
`github.com/ekalinin/github-markdown-toc.go` to this repo and use it in
`cmd/go_deps`.

Related to bazel-contrib/bazel-gazelle#1585.
  • Loading branch information
cgrindel authored Jul 21, 2023
1 parent ac737b9 commit 3a902c4
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 87 deletions.
14 changes: 12 additions & 2 deletions BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ gazelle_binary(

# gazelle:prefix github.com/cgrindel/bazel-starlib
gazelle(
name = "gazelle",
name = "update_build_files",
gazelle = ":gazelle_bin",
)

Expand Down Expand Up @@ -58,7 +58,17 @@ tidy(
# Run go mod tidy before we gazelle_update_repos
":go_mod_tidy",
":gazelle_update_repos",
":gazelle",
":update_build_files",
],
)

# This is meant to be fast and convenient to quickly update the source files
# with the latest.
tidy(
name = "update_files",
targets = [
":update_all",
":update_build_files",
],
)

Expand Down
74 changes: 1 addition & 73 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -29,82 +29,10 @@ bazel_dep(
bazel_dep(name = "platforms", version = "0.0.6")

go_deps = use_extension("@bazel_gazelle//:extensions.bzl", "go_deps")

# NOTE: We are not loading the Go modules from go.mod, because we are doing
# something a little weird in that we are building an executable from another
# repository. The `go mod tidy` operation does not fill in the indirect deps.
# Instead, we declare the same repos listed in `go_deps.bzl`.
# go_deps.from_file(go_mod = "//:go.mod")
go_deps.module(
path = "github.com/alecthomas/assert",
sum = "h1:smF2tmSOzy2Mm+0dGI2AIUHY+w0BUc+4tn40djz7+6U=",
version = "v0.0.0-20170929043011-405dbfeb8e38",
)
go_deps.module(
path = "github.com/alecthomas/colour",
sum = "h1:nOE9rJm6dsZ66RGWYSFrXw461ZIt9A6+nHgL7FRrDUk=",
version = "v0.1.0",
)
go_deps.module(
path = "github.com/alecthomas/repr",
sum = "h1:8Uy0oSf5co/NZXje7U1z8Mpep++QJOldL2hs/sBQf48=",
version = "v0.0.0-20210801044451-80ca428c5142",
)
go_deps.module(
path = "github.com/alecthomas/template",
sum = "h1:cAKDfWh5VpdgMhJosfJnn5/FoN2SRZ4p7fJNX58YPaU=",
version = "v0.0.0-20160405071501-a0175ee3bccc",
)
go_deps.module(
path = "github.com/alecthomas/units",
sum = "h1:qet1QNfXsQxTZqLG4oE62mJzwPIB8+Tee4RNCL9ulrY=",
version = "v0.0.0-20151022065526-2efee857e7cf",
)
go_deps.module(
path = "github.com/davecgh/go-spew",
sum = "h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=",
version = "v1.1.1",
)
go_deps.module(
path = "github.com/ekalinin/github-markdown-toc.go",
sum = "h1:6jRFt5qg61XfXZbP3SDaeTX+1OC1EgbHvRceYDmPAUE=",
version = "v1.2.1",
)
go_deps.module(
path = "github.com/mattn/go-isatty",
sum = "h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=",
version = "v0.0.14",
)
go_deps.module(
path = "github.com/sergi/go-diff",
sum = "h1:XU+rvMAioB0UC3q1MFrIQy4Vo5/4VsRDQQXHsEya6xQ=",
version = "v1.2.0",
)
go_deps.module(
path = "github.com/stretchr/testify",
sum = "h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=",
version = "v1.7.0",
)
go_deps.module(
path = "gopkg.in/alecthomas/kingpin.v2",
sum = "h1:CC8tJ/xljioKrK6ii3IeWVXU4Tw7VB+LbjZBJaBxN50=",
version = "v2.2.4",
)

# Declare all of the go modules that we are going to use to build the ekalinin
# github markdown toc.
go_deps.from_file(go_mod = "//:go.mod")
use_repo(
go_deps,
"com_github_alecthomas_assert",
"com_github_alecthomas_colour",
"com_github_alecthomas_repr",
"com_github_alecthomas_template",
"com_github_alecthomas_units",
"com_github_davecgh_go_spew",
"com_github_ekalinin_github_markdown_toc_go",
"com_github_mattn_go_isatty",
"com_github_sergi_go_diff",
"com_github_stretchr_testify",
"in_gopkg_alecthomas_kingpin_v2",
)

Expand Down
5 changes: 4 additions & 1 deletion cmd/go_deps/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ go_library(
srcs = ["main.go"],
importpath = "github.com/cgrindel/bazel-starlib/cmd/go_deps",
visibility = ["//visibility:private"],
deps = ["@com_github_ekalinin_github_markdown_toc_go//:github-markdown-toc_go"],
deps = [
"@com_github_ekalinin_github_markdown_toc_go//:github-markdown-toc_go",
"@in_gopkg_alecthomas_kingpin_v2//:kingpin_v2",
],
)

go_binary(
Expand Down
14 changes: 13 additions & 1 deletion cmd/go_deps/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,17 @@ To ensure that these binary targets are downloaded and built properly, a simple
exists to reference the required packages. [Gazelle](https://github.com/bazelbuild/bazel-gazelle) is
then used to identify the transitive dependencies and saves them to `go_deps.bzl`.

NOTE: If the external Go module has dependencies that are referenced only in its `main` package, you
must update this repository to directly reference those direct dependencies. For instance,
`github.com/ekalinin/github-markdown-toc.go` references `gopkg.in/alecthomas/kingpin.v2` in its
`main` package. Hence, we added the version of `gopkg.in/alecthomas/kingpin.v2` that is referenced
in `github.com/ekalinin/github-markdown-toc.go` and added a usage to `cmd/go_deps`.

For more details, please see the following:

- https://github.com/bazelbuild/bazel-gazelle/issues/1585
- [Slack thread](https://bazelbuild.slack.com/archives/CDBP88Z0D/p1689814617770239)

## To Add a Golang Dependency

Update the `main.go` in this directory to depend upon the desired Golang package. Be sure to use the
Expand All @@ -14,9 +25,10 @@ Execute the following to update the go module files, resolve the Golang dependen
Bazel build files.

```sh
$ bazel run @io_bazel_rules_go//go -- github.com/sweet/go_pkg
# bazel run //:go_mod_tidy
$ bazel run //:gazelle_update_repos
$ bazel run //:gazelle
$ bazel run //:update_build_files
```

Reference the Golang binary target.
2 changes: 2 additions & 0 deletions cmd/go_deps/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import (
"strings"

gmt "github.com/ekalinin/github-markdown-toc.go"
"gopkg.in/alecthomas/kingpin.v2"
)

func main() {
kingpin.Parse()
fmt.Println("Hello, world.")

ghtoc := generateToc()
Expand Down
17 changes: 12 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
module github.com/cgrindel/bazel-starlib

go 1.18
go 1.19

require github.com/ekalinin/github-markdown-toc.go v1.2.1
require (
github.com/ekalinin/github-markdown-toc.go v1.2.1
gopkg.in/alecthomas/kingpin.v2 v2.2.4
)

// require gopkg.in/alecthomas/kingpin.v2 v2.3.1

// replace gopkg.in/alecthomas/kingpin.v2 => github.com/alecthomas/kingpin/v2 v2.3.1
require (
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc // indirect
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/sys v0.10.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
20 changes: 20 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,22 @@
github.com/alecthomas/assert v0.0.0-20170929043011-405dbfeb8e38 h1:smF2tmSOzy2Mm+0dGI2AIUHY+w0BUc+4tn40djz7+6U=
github.com/alecthomas/colour v0.1.0 h1:nOE9rJm6dsZ66RGWYSFrXw461ZIt9A6+nHgL7FRrDUk=
github.com/alecthomas/repr v0.0.0-20210801044451-80ca428c5142 h1:8Uy0oSf5co/NZXje7U1z8Mpep++QJOldL2hs/sBQf48=
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc h1:cAKDfWh5VpdgMhJosfJnn5/FoN2SRZ4p7fJNX58YPaU=
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf h1:qet1QNfXsQxTZqLG4oE62mJzwPIB8+Tee4RNCL9ulrY=
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/ekalinin/github-markdown-toc.go v1.2.1 h1:6jRFt5qg61XfXZbP3SDaeTX+1OC1EgbHvRceYDmPAUE=
github.com/ekalinin/github-markdown-toc.go v1.2.1/go.mod h1:V5aiwoSLm1+er91D4l0AXn8vr4FX07Iu+zgDMFj3FeU=
github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sergi/go-diff v1.2.0 h1:XU+rvMAioB0UC3q1MFrIQy4Vo5/4VsRDQQXHsEya6xQ=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
golang.org/x/sys v0.10.0 h1:SqMFp9UcQJZa+pmYuAKjd9xq1f0j5rLcDIk0mj4qAsA=
golang.org/x/sys v0.10.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/alecthomas/kingpin.v2 v2.2.4 h1:CC8tJ/xljioKrK6ii3IeWVXU4Tw7VB+LbjZBJaBxN50=
gopkg.in/alecthomas/kingpin.v2 v2.2.4/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
38 changes: 33 additions & 5 deletions go_deps.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,8 @@ bazel-starlib repository.

load("@bazel_gazelle//:deps.bzl", "go_repository")

# NOTE: The go_repository declarations must be kept in sync with the go_deps.module() declarations
# in MODULE.bazel.

def bazel_starlib_go_dependencies():
"""Specifies the Golang dependencies for the bazel-starlib repository."""
"""Declare dependencies for bazel-starlib."""
go_repository(
name = "com_github_alecthomas_assert",
build_external = "external",
Expand All @@ -23,6 +20,7 @@ def bazel_starlib_go_dependencies():
sum = "h1:nOE9rJm6dsZ66RGWYSFrXw461ZIt9A6+nHgL7FRrDUk=",
version = "v0.1.0",
)

go_repository(
name = "com_github_alecthomas_repr",
build_external = "external",
Expand Down Expand Up @@ -58,14 +56,20 @@ def bazel_starlib_go_dependencies():
sum = "h1:6jRFt5qg61XfXZbP3SDaeTX+1OC1EgbHvRceYDmPAUE=",
version = "v1.2.1",
)

go_repository(
name = "com_github_mattn_go_isatty",
build_external = "external",
importpath = "github.com/mattn/go-isatty",
sum = "h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=",
version = "v0.0.14",
)
go_repository(
name = "com_github_pmezard_go_difflib",
build_external = "external",
importpath = "github.com/pmezard/go-difflib",
sum = "h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=",
version = "v1.0.0",
)

go_repository(
name = "com_github_sergi_go_diff",
Expand All @@ -74,17 +78,41 @@ def bazel_starlib_go_dependencies():
sum = "h1:XU+rvMAioB0UC3q1MFrIQy4Vo5/4VsRDQQXHsEya6xQ=",
version = "v1.2.0",
)

go_repository(
name = "com_github_stretchr_testify",
build_external = "external",
importpath = "github.com/stretchr/testify",
sum = "h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=",
version = "v1.7.0",
)

go_repository(
name = "in_gopkg_alecthomas_kingpin_v2",
build_external = "external",
importpath = "gopkg.in/alecthomas/kingpin.v2",
sum = "h1:CC8tJ/xljioKrK6ii3IeWVXU4Tw7VB+LbjZBJaBxN50=",
version = "v2.2.4",
)
go_repository(
name = "in_gopkg_check_v1",
build_external = "external",
importpath = "gopkg.in/check.v1",
sum = "h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=",
version = "v0.0.0-20161208181325-20d25e280405",
)

go_repository(
name = "in_gopkg_yaml_v3",
build_external = "external",
importpath = "gopkg.in/yaml.v3",
sum = "h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=",
version = "v3.0.1",
)
go_repository(
name = "org_golang_x_sys",
build_external = "external",
importpath = "golang.org/x/sys",
sum = "h1:SqMFp9UcQJZa+pmYuAKjd9xq1f0j5rLcDIk0mj4qAsA=",
version = "v0.10.0",
)

0 comments on commit 3a902c4

Please sign in to comment.