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

Can't use rules_go toolchain as toolchains attribute in genrule #2255

Closed
gkousik opened this issue Oct 23, 2019 · 5 comments
Closed

Can't use rules_go toolchain as toolchains attribute in genrule #2255

gkousik opened this issue Oct 23, 2019 · 5 comments

Comments

@gkousik
Copy link

gkousik commented Oct 23, 2019

What version of rules_go are you using?

0.20.1

What version of gazelle are you using?

0.17.0

What version of Bazel are you using?

1.1.0

Does this issue reproduce with the latest releases of all the above?

Yes

What operating system and processor architecture are you using?

Linux x86_64

Any other potentially useful information about your toolchain?

None

What did you do?

I have a genrule like:

genrule(
    name = "foo",
    srcs = [],
    outs = ["foo.txt"],
    cmd = "touch foo.txt > $@",
    toolchains = [
        "@io_bazel_rules_go//go:toolchain",
    ],
)

What did you expect to see?

That this toolchain is successfully provided to the execution of the genrule.

What did you see instead?

'@io_bazel_rules_go//go:toolchain' does not have mandatory providers: 'TemplateVariableInfo'
ERROR: Analysis of target '//foo:bar' failed; build aborted: Analysis of target '//foo:bar' failed; build aborted
INFO: Elapsed time: 0.662s
INFO: 0 processes.
FAILED: Build did NOT complete successfully (6 packages loaded, 9 targets configured)
@jayconrod
Copy link
Contributor

Could you explain more about the problem you're trying to solve? The example you gave reproduces the error, but I don't understand why you'd want to use a Go toolchain in a genrule.

This is the first time I've heard of the toolchains attribute of genrule. The documentation says this:

The set of toolchains that supply "Make variables" that this target can use in some of its attributes. Some rules have toolchains whose Make variables they can use by default.

The Go toolchain does not provide any variables like this. I guess TemplateVariableInfo is how they would be provided. What variables do you need? Why not consume the toolchain in a regular rule instead of a genrule?

@gkousik
Copy link
Author

gkousik commented Oct 23, 2019

Sure, I'm trying to build a go-plugin based on some C++ libraries (using SWIG). I also build some C++ libraries as part of that, and after the C++ libraries are built, I want to use the same go_sdk used by bazel to call go build -buildmode=plugin <sourcefiles>.

I need to use the same go_sdk, so that the plugin can be built with the same version of go-compiler that all of our other go-tooling uses.

@gkousik
Copy link
Author

gkousik commented Oct 23, 2019

The actual genrule looks something like this:

genrule(
name = "buildwithswig",
cmd = "run-build.sh > $@",
srcs = ["foo.go"],
toolchains = [ "@io_bazel_rules_go//go:toolchain", ],
outputs = ["foo.so"],

and in the run-build.sh file:

#!/bin/bash

<...
code to clone and build C++ libraries
...>

go build -buildmode=plugin foo.go

@jayconrod
Copy link
Contributor

I can't speak for building C/C++, but running go build in a genrule is not supported.

For building a plugin, your best option is likely go_binary with linkmode = "plugin". You can set cgo = True, and pull in any C/C++ files to build directly in srcs and C/C++ libraries to link against in cdeps.

Writing a custom rule that uses the toolchain API directly is also an option.

I'd still advise against using go build in either situation though. The go command is an entirely separate build system with its own cache and dependency system which both will need to be set up in the sandbox and which tend to change from release to release. Use the toolchain functions like go.archive, go.link, or use the compiler and linker directly.

@gkousik
Copy link
Author

gkousik commented Oct 23, 2019

I'm using swig to make go talk to C++ classes, and unfortunately go_binary does not support swig currently (based on caveats here https://github.com/bazelbuild/rules_go#overview).

I'll try writing a custom rule and using the toolchain API.

fmeum added a commit to fmeum/rules_go that referenced this issue Mar 10, 2023
Allows developers to build their own local convenience scripts around
`go`, e.g. to run `go mod tidy` backed by a hermetic Go toolchain.

This requires getting rid of the `env` attribute as it is not evaluated
if the binary is run as a dependency of another target.

Since `//go` select a Go SDK suitable for the host, not the exec
platform, we forbid its use in the exec configuration. As remarked in
bazelbuild#2255, using raw `go` in
a genrule is not a good idea to start with.
fmeum added a commit to fmeum/rules_go that referenced this issue Mar 10, 2023
Allows developers to build their own local convenience scripts around
`go`, e.g. to run `go mod tidy` backed by a hermetic Go toolchain.

This requires getting rid of the `env` attribute as it is not evaluated
if the binary is run as a dependency of another target.

Since `//go` select a Go SDK suitable for the host, not the exec
platform, we forbid its use in the exec configuration. As remarked in
bazelbuild#2255, using raw `go` in
a genrule is not a good idea to start with.
fmeum added a commit to fmeum/rules_go that referenced this issue Mar 10, 2023
Allows developers to build their own local convenience scripts around
`go`, e.g. to run `go mod tidy` backed by a hermetic Go toolchain.

This requires getting rid of the `env` attribute as it is not evaluated
if the binary is run as a dependency of another target.

Since `//go` select a Go SDK suitable for the host, not the exec
platform, we forbid its use in the exec configuration. As remarked in
bazelbuild#2255, using raw `go` in
a genrule is not a good idea to start with.
fmeum added a commit to fmeum/rules_go that referenced this issue Mar 10, 2023
Allows developers to build their own local convenience scripts around
`go`, e.g. to run `go mod tidy` backed by a hermetic Go toolchain.

This requires getting rid of the `env` attribute as it is not evaluated
if the binary is run as a dependency of another target.

Since `//go` select a Go SDK suitable for the host, not the exec
platform, we forbid its use in the exec configuration. As remarked in
bazelbuild#2255, using raw `go` in
a genrule is not a good idea to start with.
fmeum added a commit to fmeum/rules_go that referenced this issue Mar 10, 2023
Allows developers to build their own local convenience scripts around
`go`, e.g. to run `go mod tidy` backed by a hermetic Go toolchain.

This requires getting rid of the `env` attribute as it is not evaluated
if the binary is run as a dependency of another target.

Since `//go` select a Go SDK suitable for the host, not the exec
platform, we forbid its use in the exec configuration. As remarked in
bazelbuild#2255, using raw `go` in
a genrule is not a good idea to start with.
fmeum added a commit to fmeum/rules_go that referenced this issue Mar 10, 2023
Allows developers to build their own local convenience scripts around
`go`, e.g. to run `go mod tidy` backed by a hermetic Go toolchain.

This requires getting rid of the `env` attribute as it is not evaluated
if the binary is run as a dependency of another target.

Since `//go` select a Go SDK suitable for the host, not the exec
platform, we forbid its use in the exec configuration. As remarked in
bazelbuild#2255, using raw `go` in
a genrule is not a good idea to start with.
fmeum added a commit that referenced this issue Mar 11, 2023
Allows developers to build their own local convenience scripts around
`go`, e.g. to run `go mod tidy` backed by a hermetic Go toolchain.

This requires getting rid of the `env` attribute as it is not evaluated
if the binary is run as a dependency of another target.

Since `//go` selects a Go SDK suitable for the host, not the exec
platform, we forbid its use in the exec or host configuration. As remarked in
#2255, using raw `go` in
a genrule is not a good idea to start with.
tingilee pushed a commit to tingilee/rules_go that referenced this issue Jul 19, 2023
Allows developers to build their own local convenience scripts around
`go`, e.g. to run `go mod tidy` backed by a hermetic Go toolchain.

This requires getting rid of the `env` attribute as it is not evaluated
if the binary is run as a dependency of another target.

Since `//go` selects a Go SDK suitable for the host, not the exec
platform, we forbid its use in the exec or host configuration. As remarked in
bazelbuild#2255, using raw `go` in
a genrule is not a good idea to start with.
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

2 participants