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

clippy aspect causes go binaries to fail #1517

Closed
duarten opened this issue Aug 15, 2022 · 7 comments
Closed

clippy aspect causes go binaries to fail #1517

duarten opened this issue Aug 15, 2022 · 7 comments
Labels

Comments

@duarten
Copy link
Contributor

duarten commented Aug 15, 2022

When building with the clippy aspect, if there are go targets in the workspaces, then the build fails with

ERROR: Analysis of aspects '[@rules_rust//rust:defs.bzl%rust_clippy_aspect] on //some/go_bin' failed; 
build aborted: no matching toolchains found for types @bazel_tools//tools/cpp:toolchain_type,
@rules_rust//rust:toolchain_type

The go binary is a simple go_binary:

    go_binary(
        name = "go_bin",
        embed = [lib],
        pure = "on",
        goos = "linux",
        goarch = "amd64",
        out = "main",
        visibility = visibility,
    )
@UebelAndre
Copy link
Collaborator

Can you provide a workspace as an example? I’m curious why it seems someone is trying to run clippy without registering any toolchains. Perhaps there’s more going on.

@duarten
Copy link
Contributor Author

duarten commented Aug 15, 2022

Sure, this is a pretty minimal repro:

# WORKSPACE

workspace(
    name = "repro",
)

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

rules_go_version = "0.29.0"

http_archive(
    name = "io_bazel_rules_go",
    sha256 = "2b1641428dff9018f9e85c0384f03ec6c10660d935b750e3fa1492a281a53b0f",
    url = "https://github.com/bazelbuild/rules_go/releases/download/v{}/rules_go-v{}.zip"
        .format(rules_go_version, rules_go_version),
)

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

go_rules_dependencies()

go_register_toolchains(go_version = "1.18")

rules_rust_version = "0.9.0"

http_archive(
    name = "rules_rust",
    sha256 = "6bfe75125e74155955d8a9854a8811365e6c0f3d33ed700bc17f39e32522c822",
    urls = [
        "https://mirror.bazel.build/github.com/bazelbuild/rules_rust/releases/download/{}/rules_rust-v{}.tar.gz".format(
            rules_rust_version,
            rules_rust_version,
        ),
        "https://github.com/bazelbuild/rules_rust/releases/download/{}/rules_rust-v{}.tar.gz".format(
            rules_rust_version,
            rules_rust_version,
        ),
    ],
)
load("@rules_rust//rust:repositories.bzl", "rules_rust_dependencies", "rust_register_toolchains")

rules_rust_dependencies()

rust_register_toolchains(
    edition = "2021",
)
# BUILD.bazel

load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library")
load("@rules_rust//rust:defs.bzl", "rust_binary")

go_library(
    name = "lib",
    srcs = ["main.go"],
    importpath = "main",
)

go_binary(
    name = "bin",
    out = "main",
    embed = ["lib"],
    goarch = "amd64",
    goos = "linux",
    pure = "on",
)

rust_binary(
    name = "rust",
    srcs = ["main.rs"],
)
// main.go

package main

import "fmt"

func main() {
    fmt.Println("hello world")
}
// main.rs

fn main() {
    println!("hello world");
}

.bazelrc

build --aspects=@rules_rust//rust:defs.bzl%rust_clippy_aspect
build --output_groups=+clippy_checks

@UebelAndre
Copy link
Collaborator

Seems you’re missing rust_register_toolchains call in your workspace? Can you double check your workspace file with https://bazelbuild.github.io/rules_rust/#setup ?

@duarten
Copy link
Contributor Author

duarten commented Aug 15, 2022

Sorry, I cut things too short. I've updated the repro with a rust binary too. I've also pushed it here: https://github.com/duarten/clippy-repro.

bzl build //.... fails with the aspect, but succeeds without it.

Something I forgot to mention: the issue happens because I'm cross-compiling the go binary (I'm running on mac, but if you'll notice, the go binary is targeting linux). There's no rust toolchain for that triplet.

@UebelAndre
Copy link
Collaborator

It seems rules_go is doing some forced transitions here? https://github.com/bazelbuild/rules_go/blob/v0.29.0/go/private/rules/transition.bzl

The issue very likely comes form the @rules_cc//cc:toolchain_type/@bazel_tools//tools/cpp:toolchain_type dependency on a @rules_rust//rust:toolchain_type. I suspect if you were to register a C++ toolchain compatible with the target then the issue would be resolved. But perhaps i'm misinterpreting the output of --toolchain_resolution_debug.

INFO: ToolchainResolution:   Type @bazel_tools//tools/cpp:toolchain_type: target platform @io_bazel_rules_go//go/toolchain:linux_amd64: No toolchains found.

This error makes sense to me though since I'm building on my mac and have not registered any compatible toolchains. I'm not sure how relevant the message here is:

ERROR: Analysis of aspects '[@rules_rust//rust:defs.bzl%rust_clippy_aspect] on //:bin' failed; build aborted: No matching toolchains found for types @bazel_tools//tools/cpp:toolchain_type. Maybe --incompatible_use_cc_configure_from_rules_cc has been flipped and there is no default C++ toolchain added in the WORKSPACE file? See https://github.com/bazelbuild/bazel/issues/10134 for details and migration instructions.

since I definitely haven't registered a toolchain and wouldn't expect one to be found on my host. Could you try maybe updating the version of rules_go?

@illicitonion
Copy link
Collaborator

rules_go changed how it does transitions fairly recently to be much more constrained in what it transitions - bazel-contrib/rules_go#3116 should have fixed this :) it was released in 0.34.0

@duarten
Copy link
Contributor Author

duarten commented Aug 15, 2022

Of course the issue was with Go :) Updating rules_go fixed it. Thanks for the help guys!

@duarten duarten closed this as completed Aug 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants