Running go tools in bazel

147 views
Skip to first unread message

Jonathan Barrios

unread,
Nov 6, 2017, 3:05:17 PM11/6/17
to bazel-go-discuss
I have a tool go swagger which I need to run is there a way to do this in bazel. The main issue is that it needs all the information necessary to compile the code. I am using the external method and there for my build is go build incompatible.

Tom Wilkie

unread,
Nov 7, 2017, 7:32:48 AM11/7/17
to Jonathan Barrios, bazel-go-discuss
Hi Jonathan


We use something similar to build our UI code, although I doubt yours will need to be anyway near as complicated - we had to work around various problems with symlinks and npm.

Cheers

Tom

genrule(
    name = "build",
    srcs = glob([
        "src/**/*",
        "public/*",
    ]) + [
      "node_modules.tgz",
      "package.json",
      ".eslintrc",
    ],
    outs = ["ui.tar"],
    cmd =
      # First cp everything into a tempdir to get around symlink problems.
      "mkdir tmp && cp -LR $$(dirname $(location :package.json))/* tmp/ && " +

      # Then extract the tarball of node_modules into the tmp dir.
      "tar zxf $(location node_modules.tgz) -C tmp/ && " +

      # Finally build the UI.
      "cd tmp && npm run build && " +

      # And tar up the resulting built assets.
      "tar -cf - -C ./build/ . >../$@", # --mtime='1970-01-01'
    visibility = ["//visibility:public"],
)

On Mon, Nov 6, 2017 at 8:05 PM Jonathan Barrios <jonathan...@gmail.com> wrote:
I have a tool go swagger which I need to run is there a way to do this in bazel. The main issue is that it needs all the information necessary to compile the code. I am using the external method and there for my build is go build incompatible.

--
You received this message because you are subscribed to the Google Groups "bazel-go-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bazel-go-discu...@googlegroups.com.
To post to this group, send email to bazel-go...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/bazel-go-discuss/417da64c-3f09-4ffd-9aaa-32e810dcfb90%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Jay Conrod

unread,
Nov 7, 2017, 12:04:16 PM11/7/17
to Tom Wilkie, Jonathan Barrios, bazel-go-discuss
Seems like this is a code generating tool. To run it with Bazel, you'll want to run it with a genrule or define a new Skylark rule for it.

If your sources are already laid out in the format that tool expects, you should just be able to add the sources in a srcs or data attribute as Tom suggested. Note that glob won't cross Bazel package boundaries, so if you need all sources from all packages, you may want to define a filegroup in each package with the sources. Each filegroup can include the filegroup from child packages.

If you need your sources in a GOPATH layout (sounds like this might be the case since you're using external repositories), you can try the experimental go_path rule. See path.bzl. You can load this from @io_bazel_rules_go//go:def.bzl.

To unsubscribe from this group and stop receiving emails from it, send an email to bazel-go-discuss+unsubscribe@googlegroups.com.
To post to this group, send email to bazel-go-discuss@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "bazel-go-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bazel-go-discuss+unsubscribe@googlegroups.com.
To post to this group, send email to bazel-go-discuss@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/bazel-go-discuss/CAB58Z12-naUJuQkJwmbe_3CmuMgrvLS1G7m-0YsCWcSLS6HoMw%40mail.gmail.com.

Jonathan Barrios

unread,
Nov 8, 2017, 9:53:50 AM11/8/17
to bazel-go-discuss
How is go_path supposed to be used? I will need this tool because I use external repos

Jay Conrod

unread,
Nov 8, 2017, 2:13:47 PM11/8/17
to Jonathan Barrios, bazel-go-discuss
See BUILD.bazel for an example of how we're using it in rules_go. Basically, you give it a list of deps; they can be go_library, go_binary, go_test, or anything compatible. The rule will produce a GOPATH-like directory tree.

This rule is experimental, and we may change it in the future, but we'll provide a reasonable alternative for tools like this.

On Wed, Nov 8, 2017 at 2:53 PM, Jonathan Barrios <jonathan...@gmail.com> wrote:
How is go_path supposed to be used? I will need this tool because I use external repos
--
You received this message because you are subscribed to the Google Groups "bazel-go-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bazel-go-discuss+unsubscribe@googlegroups.com.
To post to this group, send email to bazel-go-discuss@googlegroups.com.

Guy BL

unread,
Oct 13, 2021, 12:22:03 PM10/13/21
to bazel-go-discuss
Jonathan, 

Were you able to figure how to use go_path ? I am wondering what was your eventual solution.

Best,

Guy

On Wednesday, November 8, 2017 at 2:13:47 PM UTC-5 jayc...@google.com wrote:
See BUILD.bazel for an example of how we're using it in rules_go. Basically, you give it a list of deps; they can be go_library, go_binary, go_test, or anything compatible. The rule will produce a GOPATH-like directory tree.

This rule is experimental, and we may change it in the future, but we'll provide a reasonable alternative for tools like this.

On Wed, Nov 8, 2017 at 2:53 PM, Jonathan Barrios <jonathan...@gmail.com> wrote:
How is go_path supposed to be used? I will need this tool because I use external repos
--
You received this message because you are subscribed to the Google Groups "bazel-go-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bazel-go-discu...@googlegroups.com.
To post to this group, send email to bazel-go...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages