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

linkmode = "c-shared" cannot be used with wasm #4200

Closed
leonm1 opened this issue Dec 19, 2024 · 1 comment · Fixed by #4201
Closed

linkmode = "c-shared" cannot be used with wasm #4200

leonm1 opened this issue Dec 19, 2024 · 1 comment · Fixed by #4201

Comments

@leonm1
Copy link
Contributor

leonm1 commented Dec 19, 2024

What version of rules_go are you using?

v0.51.0

What version of gazelle are you using?

N/A

What version of Bazel are you using?

6.5.0

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

Yes.

What operating system and processor architecture are you using?

x86_64-linux

Any other potentially useful information about your toolchain?

Using go toolchain 1.24rc1

What did you do?

.bazelversion:

6.5.0

WORKSPACE:

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

http_archive(
    name = "io_bazel_rules_go",
    sha256 = "0936c9bc3c4321ee372cb8f66dd972d368cb940ed01a9ba9fd7debcf0093f09b",
    urls = [
        "https://mirror.bazel.build/github.com/bazel-contrib/rules_go/releases/download/v0.51.0/rules_go-v0.51.0.zip",
        "https://github.com/bazel-contrib/rules_go/releases/download/v0.51.0/rules_go-v0.51.0.zip",
    ],
)

load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_dependencies")

go_rules_dependencies()

go_register_toolchains(version = "1.24rc1")

BUILD:

load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_cross_binary")

go_binary(
  name = "main.wasm",
  srcs = ["main.go"],
  linkmode = "c-shared",
  goos = "wasip1",
  goarch = "wasm",
)

main.go:

package main

import "fmt"

func main() {}

//go:wasmexport
func hello() {
        fmt.Println("Hello, world!")
}

What did you expect to see?

Go 1.24 newly supports building wasm modules as wasi reactors: https://tip.golang.org/doc/go1.24#wasm.

I expected the module to be built with the go compiler invocation such as:

$ go version
go version go1.24rc1 linux/amd64
$ env GOOS=wasip1 GOARCH=wasm go build -buildmode=c-shared -o main.wasm main.go
### Works

What did you see instead?

rules_go validation causes this to fail:

$ bazel build :main.wasm
ERROR: /usr/local/google/home/mattleon/.cache/bazel/_bazel_mattleon/8b98898d3781492ad7d94d7510d87642/external/io_bazel_rules_go/BUILD.bazel:42:7: in stdlib rule @io_bazel_rules_go//:stdlib: 
Traceback (most recent call last):
        File "/usr/local/google/home/mattleon/.cache/bazel/_bazel_mattleon/8b98898d3781492ad7d94d7510d87642/external/io_bazel_rules_go/go/private/rules/stdlib.bzl", line 33, column 20, in _stdlib_impl
                go = go_context(ctx, include_deprecated_properties = False)
        File "/usr/local/google/home/mattleon/.cache/bazel/_bazel_mattleon/8b98898d3781492ad7d94d7510d87642/external/io_bazel_rules_go/go/private/context.bzl", line 518, column 22, in go_context
                validate_mode(mode)
        File "/usr/local/google/home/mattleon/.cache/bazel/_bazel_mattleon/8b98898d3781492ad7d94d7510d87642/external/io_bazel_rules_go/go/private/mode.bzl", line 69, column 17, in validate_mode
                fail(("linkmode '{}' can't be used when cgo is disabled. Check that pure is not set to \"off\" and that a C/C++ toolchain is configured for " +
Error in fail: linkmode 'c-shared' can't be used when cgo is disabled. Check that pure is not set to "off" and that a C/C++ toolchain is configured for your current platform. If you defined a custom platform, make sure that it has the @io_bazel_rules_go//go/toolchain:cgo_on constraint value.
ERROR: /usr/local/google/home/mattleon/.cache/bazel/_bazel_mattleon/8b98898d3781492ad7d94d7510d87642/external/io_bazel_rules_go/BUILD.bazel:42:7: Analysis of target '@io_bazel_rules_go//:stdlib' failed
@fmeum
Copy link
Member

fmeum commented Dec 19, 2024

Could you try removing the check that causes the failure? If that ends up working, it should be easy to fix properly.

leonm1 added a commit to leonm1/rules_go that referenced this issue Dec 19, 2024
Go 1.24 added support for `GOOS=wasip1 GOARCH=wasm go build -buildmode=c-shared`; however, the rules_go starlark code contained validation against this.

I also suppressed the addition of a `lib` prefix and the `.so` suffix for wasi reactors.

Fixes bazel-contrib#4200

Signed-off-by: Matt Leon <[email protected]>
fmeum pushed a commit to leonm1/rules_go that referenced this issue Dec 24, 2024
Go 1.24 added support for `GOOS=wasip1 GOARCH=wasm go build -buildmode=c-shared`; however, the rules_go starlark code contained validation against this.

I also suppressed the addition of a `lib` prefix and the `.so` suffix for wasi reactors.

Fixes bazel-contrib#4200

Signed-off-by: Matt Leon <[email protected]>
fmeum pushed a commit that referenced this issue Dec 24, 2024
**What type of PR is this?**

 Bug fix

**What does this PR do? Why is it needed?**

[Go 1.24](https://go.dev/doc/go1.24#wasm) added support for `GOOS=wasip1
GOARCH=wasm go build -buildmode=c-shared`; however, the rules_go
starlark code contained validation against this.

* Fixes validation error in newly supported go 1.24 wasip1-wasm tuple
with -buildmode=c-shared.
* Removes lib prefix for wasip1 -buildmode=c-shared
* Adds `.wasm` suffix for wasi reactors built with `-buildmode=c-shared`

**Which issues(s) does this PR fix?**

Fixes #4200

**Other notes for review**

It looks like this could use a test (as simple as adding a new go file
under `tests/core/c_linkmodes/`); however, the support for features
added in this PR requires Go 1.24, which is not out yet. Updating
`go_register_toolchains(version = "1.24rc1")` does allow it to build,
but then `nogo` fails due to lack of support in some of the linters for
go 1.24.

Signed-off-by: Matt Leon <[email protected]>
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

Successfully merging a pull request may close this issue.

2 participants