Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support embeds with native golang rules #781

Open
mkditto opened this issue Sep 20, 2024 · 0 comments
Open

Support embeds with native golang rules #781

mkditto opened this issue Sep 20, 2024 · 0 comments

Comments

@mkditto
Copy link

mkditto commented Sep 20, 2024

I've been experimenting with building go with buck2, and up to this point I've had a pretty good experience. The one drawback I've had so far is using to go embed package. Right now when building go with buck I have to manually create the embedcfg for my embedded resources, and this comes with a few drawbacks:

  1. The documentation on the format of the embedcfg can be hard to find, so it takes a good bit of time to write one the first time. Since this is something that you don't have to do with standard go tooling, it's not something most regular go devs are familiar with doing.

  2. The embedcfg needs to be a json source which you can either get with a genrule or by writing it to your repo directly.

    1. If you choose to write a genrule, you have to write it for each library that requires embeds. Optionally, you can make this easier by writing your own macro wrapping the genrule, but now you have to maintain your own macro for something that feels like it should be supported by the native rules.

    2. If you choose to write the json sources to your repo directly, you'll pretty quickly run into issues because the embedcfg requires the absolute paths of the embedded sources, which is obviously not portable.

Abstracting away the generation of the embedcfg inside the standard go_* rules would be great and would save devs some effort learning how to write proper embedcfgs and maintaining their own genrules to generate them. This is fairly similar to what bazel already does with the embedsrcs attribute on its go rules, and would probably look very much the same:

go_library(
    name = "testlib",
    srcs = ["testlib.go"],
    embeds = [
        "resource-1.json",
        "resource-2.json",
    ],
    visibility = ["PUBLIC"],
)

Another option would be to add a macro to the prelude called go_embedcfg that can generate the embedcfg for you, which you can then depend on with the current ruleset. It would maybe be little bit more verbose than the above option, but would also work. Ergonomically I'd imagine it would look something like:

filegroup(
    name = "testlib_embeds",
    srcs = [
        "resource-1.json",
        "resource-2.json",
    ],
)

go_embedcfg(
    name = "testlib_embedcfg",
    srcs = [":testlib_embeds"],
)

go_library(
    name = "testlib",
    srcs = [
        "testlib.go",
        ":testlib_embeds",
    ],
    embedcfg = ":testlib_embedcfg",
    visibility = ["PUBLIC"],
)

(I think that by adding a filegroup to the second example it's no longer equivalent to the first, but I think it still demonstrates the point)

@mkditto mkditto changed the title Support embeds with native golang rulescccccbldbtingtedkttjgrtbhccerurcgicbctkkuefc Support embeds with native golang rules Sep 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant