bazelbuild / rules_go

Go rules for Bazel
Apache License 2.0
1.36k stars 645 forks source link

`nogo` error with Golang 1.20 #3452

Open jarreds opened 1 year ago

jarreds commented 1 year ago

What version of rules_go are you using?

0.38.1

What version of gazelle are you using?

0.26.0

What version of Bazel are you using?

6.0.0

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

Yes

What operating system and processor architecture are you using?

Linux

What did you do?

go_register_toolchains(
    nogo = "@//:nogo",
    version = "1.20",
)

What did you expect to see?

Success

What did you see instead?

ERROR: /private/var/tmp/_bazel_user/nnn/external/io_bazel_rules_go/go/tools/builders/BUILD.bazel:77:10: no such package '@org_golang_x_tools//internal/facts': BUILD file not found in directory 'internal/facts' of external repository @org_golang_x_tools. Add a BUILD file to a directory to mark it as a package. and referenced by '@io_bazel_rules_go//go/tools/builders:nogo_srcs'
jarreds commented 1 year ago

The following patch seems to resolve the issue on Go 1.20, but will cause other Go versions to fail:

diff --git a/go/tools/builders/BUILD.bazel b/go/tools/builders/BUILD.bazel
index 5bbfbd67..fff66315 100644
--- a/go/tools/builders/BUILD.bazel
+++ b/go/tools/builders/BUILD.bazel
@@ -93,8 +93,8 @@ go_source(
     visibility = ["//visibility:public"],
     deps = [
         "@org_golang_x_tools//go/analysis",
+        "@org_golang_x_tools//go/analysis/internal/facts",
         "@org_golang_x_tools//go/gcexportdata",
-        "@org_golang_x_tools//internal/facts",
     ],
 )

diff --git a/go/tools/builders/nogo_main.go b/go/tools/builders/nogo_main.go
index b9423fb4..127dbec2 100644
--- a/go/tools/builders/nogo_main.go
+++ b/go/tools/builders/nogo_main.go
@@ -39,8 +39,8 @@ import (
    "sync"

    "golang.org/x/tools/go/analysis"
+   "golang.org/x/tools/go/analysis/internal/facts"
    "golang.org/x/tools/go/gcexportdata"
-   "golang.org/x/tools/internal/facts"
 )

 const nogoBaseConfigName = "_base"
@@ -346,7 +346,7 @@ func load(packagePath string, imp *importer, filenames []string) (*goPackage, er
    }
    pkg.types, pkg.typesInfo = types, info

-   pkg.facts, err = facts.NewDecoder(pkg.types).Decode(imp.readFacts)
+   pkg.facts, err = facts.Decode(pkg.types, imp.readFacts)
    if err != nil {
        return nil, fmt.Errorf("internal error decoding facts: %v", err)
    }
@@ -559,8 +559,8 @@ func (i *importer) Import(path string) (*types.Package, error) {
    return gcexportdata.Read(r, i.fset, i.packageCache, path)
 }

-func (i *importer) readFacts(pkg *types.Package) ([]byte, error) {
-   archive := i.factMap[pkg.Path()]
+func (i *importer) readFacts(packagePath string) ([]byte, error) {
+   archive := i.factMap[packagePath]
    if archive == "" {
        // Packages that were not built with the nogo toolchain will not be
        // analyzed, so there's no opportunity to store facts. This includes
dmateusp commented 1 year ago

I've hit the same issue on go 1.19.5

jschaf commented 1 year ago

jarreds comment didn't fix it for me. I had a different error using v0.70 about package visibility. I fixed it by adding a patch to go_repository:

diff --git a/internal/facts/BUILD.bazel b/internal/facts/BUILD.bazel
index cf2ade5..2640b5e 100644
--- a/internal/facts/BUILD.bazel
+++ b/internal/facts/BUILD.bazel
@@ -7,7 +7,7 @@ go_library(
         "imports.go",
     ],
     importpath = "golang.org/x/tools/internal/facts",
-    visibility = ["//:__subpackages__"],
+    visibility = ["//visibility:public"],
     deps = [
         "//go/analysis",
         "//go/types/objectpath",
@@ -18,7 +18,7 @@ go_library(
 alias(
     name = "go_default_library",
     actual = ":facts",
-    visibility = ["//:__subpackages__"],
+    visibility = ["//visibility:public"],
 )

 go_test(
    go_repository(
        name = "org_golang_x_tools",
        build_extra_args = [
            "-exclude=**/testdata",
            "-exclude=go/packages/packagestest",
        ],
        importpath = "golang.org/x/tools",
        patches = ["@simc//third_party/bazel/go:tools_public_visibility.patch"],
        patch_args = ["-p1"],
        sum = "h1:W4OVu8VVOaIO0yzWMNdepAulS7YfoS3Zabrm8DOXXU4=",
        version = "v0.7.0",
    )
fmeum commented 1 year ago

@jschaf Which versions of Gazelle and rules_go are you on? https://github.com/bazelbuild/bazel-gazelle/blob/4dfcb75883c6ca2441e6ad17173488068adcfb1c/language/go/generate.go#L541 should apply that patch automatically.

jschaf commented 1 year ago

After bazel clean --expunge, I wasn't able to repro and I could remove the patch, so probably a false alarm.

tkinz27 commented 1 year ago

I am running into this issue consistently. I have been trying to upgrade gazelle and rules_go for a while now but this issue keeps me reverting the changes.

❯ git diff WORKSPACE
  diff --git a/WORKSPACE b/WORKSPACE
  index e3e03db66..350dbebec 100644
  --- a/WORKSPACE
  +++ b/WORKSPACE
  @@ -19,19 +19,19 @@ bazel_skylib_workspace()

   http_archive(
       name = "io_bazel_rules_go",
  -    sha256 = "16e9fca53ed6bd4ff4ad76facc9b7b651a89db1689a2877d6fd7b82aa824e366",
  +    sha256 = "6b65cb7917b4d1709f9410ffe00ecf3e160edf674b78c54a894471320862184f",
       urls = [
  -        "https://mirror.bazel.build/github.com/bazelbuild/rules_go/releases/download/v0.34.0/rules_go-v0.34.0.zip",
  -        "https://github.com/bazelbuild/rules_go/releases/download/v0.34.0/rules_go-v0.34.0.zip",
  +        "https://mirror.bazel.build/github.com/bazelbuild/rules_go/releases/download/v0.39.0/rules_go-v0.39.0.zip",
  +        "https://github.com/bazelbuild/rules_go/releases/download/v0.39.0/rules_go-v0.39.0.zip",
       ],
   )

   http_archive(
       name = "bazel_gazelle",
  -    sha256 = "501deb3d5695ab658e82f6f6f549ba681ea3ca2a5fb7911154b5aa45596183fa",
  +    sha256 = "727f3e4edd96ea20c29e8c2ca9e8d2af724d8c7778e7923a854b2c80952bc405",
       urls = [
  -        "https://mirror.bazel.build/github.com/bazelbuild/bazel-gazelle/releases/download/v0.26.0/bazel-gazelle-v0.26.0.tar.gz",
  -        "https://github.com/bazelbuild/bazel-gazelle/releases/download/v0.26.0/bazel-gazelle-v0.26.0.tar.gz",
  +        "https://mirror.bazel.build/github.com/bazelbuild/bazel-gazelle/releases/download/v0.30.0/bazel-gazelle-v0.30.0.tar.gz",
  +        "https://github.com/bazelbuild/bazel-gazelle/releases/download/v0.30.0/bazel-gazelle-v0.30.0.tar.gz",
       ],
   )
go_register_toolchains(
    nogo = "@//:nogo",
    version = "1.19",
)
# gazelle:prefix github.com/braincorp/titanium
# gazelle:proto disable_global
# gazelle:go_generate_proto false
# gazelle:exclude infrastructure-pulumi
# gazelle:exclude node_modules
gazelle(
    name = "gazelle",
    external = "vendored",
)
❯ bazel run //:gazelle
INFO: Invocation ID: 8e905347-7e80-4d37-ad6b-f6dd620637e9
ERROR: /home/tony.kinsley/.cache/bazel/_bazel_tony.kinsley/5b466fe6d0bf121e0d4086ac6c4e9a68/external/io_bazel_rules_go/go/tools/builders/BUILD.bazel:77:10: no such package '@org_golang_x_tools//internal/facts': BUILD file not found in directory 'internal/facts' of external repository @org_golang_x_tools. Add a BUILD file to a directory to mark it as a package. and referenced by '@io_bazel_rules_go//go/tools/builders:nogo_srcs'
ERROR: Analysis of target '//:gazelle' failed; build aborted:
INFO: Elapsed time: 0.207s
INFO: 0 processes.
ERROR: Build failed. Not running target
FAILED: Build did NOT complete successfully (2 packages loaded, 41 targets configured)
    Fetching repository @org_golang_x_sys; starting

I tried the bazel clean --expunge but that didn't really make a difference for me. Any help would be much appreciated.

jschaf commented 1 year ago

What's your version of golang.org/x/tools in go.mod? I'm on 0.7.0, which is the same version used by rules_go. Older versions will break rules_go because x/tools moves around the internal directories.

tkinz27 commented 1 year ago

I do have an older version in go.mod (v0.4.0). At least after running a go get golang.org/x/tools@latest I'm still having issues, but I'll play around with order of operations here to see if I can get it working. Thanks for the tip.

tkinz27 commented 1 year ago

Ahh I think I figured it out finally. We had gazelle dependencies set up despite mostly using vendored dependencies. Had to delete all the gazelle managed dependencies, and now I can get past that issue.

Might have been able to update to v0.7.0 in go.mod then update the dependencies with the older version of gazelle, but I ended up just removing them and only using vendoring.

mashail commented 1 year ago

I am getting the same error!

prestonvanloon commented 1 year ago

I'm having the same issue. What is the resolution? Is it to downgrade golang.org/x/tools to a specific version?

I'm currently using a replace directive for this dependency per this commit https://github.com/bazelbuild/rules_go/issues/3230#issuecomment-1216728711. So I'm on golang.org/x/tools v0.1.12 and experiencing these issues when trying to update to go 1.20