please-build / go-rules

Golang rules for the Please build system
Apache License 2.0
7 stars 16 forks source link

Upgrading from go_get: mage binary #259

Open sean- opened 2 months ago

sean- commented 2 months ago

I'm struggling mightily with getting the mage tool built using the new regime. I'm coming from:

go_get(
    name = "mage",
    binary = True,
    get = [],
    install = ["github.com/magefile/mage"],
    deps = [":mage_lib"],
)

go_get(
    name = "mage_lib",
    get = "github.com/magefile/mage",
    install = [
        "internal",
        "mage",
        "mg",
        "parse",
        "sh",
        "target",
    ],
    revision = "aedfce64c122eef47009b7f80c9771044753215d",  # v1.8.0
)

And trying to upgrade to:

go_module(
    name = "mage_module",
    module = "github.com/magefile/mage",
    version = "v1.15.0",
    install = [
        "github.com/magefile/mage",
    ],
)

go_binary(
    name = "mage",
    srcs = ["src/github.com/magefile/main.go"],
    deps = [":mage_module"],
)

Results in:

$ plz build
Build stopped after 120ms. 1 target failed:
    //third_party/go:_mage#lib_pkg_info
cannot calculate hash for third_party/go/src/github.com/magefile/main.go: file does not exist

Or:

go_binary(
     name = "mage",
     srcs = ["github.com_magefile_mage/main.go"],
     deps = [
         "///third_party/go/github.com_magefile_mage//main",
         #":mage_module",
     ],
 )

Which results in:

✦ ➜ plz build
Build stopped after 90ms. 1 target failed:
    ///third_party/go/github.com_magefile_mage//main:main
subrepo third_party/go/github.com_magefile_mage is not defined in this package yet. It must appear before it is used by //third_party/go:mage
    ///third_party/go/github.com_magefile_mage//main:main
subrepo third_party/go/github.com_magefile_mage is not defined in this package yet. It must appear before it is used by //third_party/go:mage
    ///third_party/go/github.com_magefile_mage//main:main
subrepo third_party/go/github.com_magefile_mage is not defined in this package yet. It must appear before it is used by //third_party/go:mage

or the other commented-out variant:

 ➜ plz build --shell --debug //third_party/go:mage
11:11:01.203   ERROR: //third_party/go:_mage#lib_pkg_info failed:
cannot calculate hash for third_party/go/github.com_magefile_mage/main.go: file does not exist
Build stopped after 30ms. 1 target failed:
    //third_party/go:_mage#lib_pkg_info
cannot calculate hash for third_party/go/github.com_magefile_mage/main.go: file does not exist

This feels like it should be dead simple, but I'm struggling because I also can't easily investigate the build environment because the --shell is failing to build.

goddenrich commented 2 months ago

I would advise you use go_repo which is the latest way to add third party go deps but if you want to use go_module first I think maybe you want

go_module(
    name = "mage_module",
    module = "github.com/magefile/mage",
    version = "v1.15.0",
    binary = True,
)

To explain in more detail the module argument defines the go module you want so you don't need to repeat that in your install list and binary = True says you want compile it as an executable. you don't need an install list because you are installing the root package of the module aka main.go in github.com/magefile/mage.

If you had wanted to install other packages within that module eg. there was a src/submodulex within it then you would do install = [".", "src/submodulex"], aka the root and the submodule you want.

goddenrich commented 2 months ago

if you wanted to use go_repo you would do

go_repo(
    name = "mage_module",
    module = "github.com/magefile/mage",
    version = "v1.15.0",
)

filegroup(
    name = "mage",
    srcs = ["///third_party/go/github.com_magefile_mage//:mage"],
    binary = True,
)
sean- commented 2 months ago

Drat. I was hoping to avoid that because I wanted to pass in commitHash and the like to properly stamp the mage util. filegroup() doesn't support definitions like go_binary does. e.g.:

filegroup(
    name = "mage",
    srcs = ["///third_party/go/github.com_magefile_mage//:mage"],
    binary = True,
    definitions = {
        "commitHash": git_commit(),
        "timestamp": git_show("%ct"),
        "gitTag": git_branch(),
    },
)

As I said, it seems like this should be easily doable within go-rules. Ideally, the library would be separate from the binary because there are moments when I do want to use mage as a library. Thank you for your help.