gpt4 book ai didi

go - Bazel Gazelle 错误 : no such package '@org_golang_x_tools//go/analysis/internal/facts' : BUILD file not found in directory

转载 作者:行者123 更新时间:2023-12-03 10:08:58 35 4
gpt4 key购买 nike

我正在使用 bazel + gazelle 构建一个非常简单的 go 项目。

遵循此规则:https://github.com/bazelbuild/bazel-gazelle#running-gazelle-with-bazel .

package main

import (
"net/http"
"time"

"github.com/gin-gonic/gin"
)

func main() {
router := gin.Default()

router.GET("/", func(c *gin.Context) {
c.String(http.StatusOK, "Hello")
})

router.Run(":8080")
}

我的工作区:

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

http_archive(
name = "io_bazel_rules_go",
sha256 = "207fad3e6689135c5d8713e5a17ba9d1290238f47b9ba545b63d9303406209c6",
urls = [
"https://mirror.bazel.build/github.com/bazelbuild/rules_go/releases/download/v0.24.7/rules_go-v0.24.7.tar.gz",
"https://github.com/bazelbuild/rules_go/releases/download/v0.24.7/rules_go-v0.24.7.tar.gz",
],
)

http_archive(
name = "bazel_gazelle",
sha256 = "b85f48fa105c4403326e9525ad2b2cc437babaa6e15a3fc0b1dbab0ab064bc7c",
urls = [
"https://mirror.bazel.build/github.com/bazelbuild/bazel-gazelle/releases/download/v0.22.2/bazel-gazelle-v0.22.2.tar.gz",
"https://github.com/bazelbuild/bazel-gazelle/releases/download/v0.22.2/bazel-gazelle-v0.22.2.tar.gz",
],
)

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

# gazelle:repository_macro deps.bzl%go_dependencies
go_dependencies()

go_rules_dependencies()

go_register_toolchains()

gazelle_dependencies()

我的构建

load("@bazel_gazelle//:def.bzl", "gazelle")

# gazelle:prefix github.com/myprojectXYZ/go-bazel
gazelle(name = "gazelle")

当我用 gazelle 生成文件时出现以下错误:

bazel run //:gazelle -- update-repos -from_file=go.mod -to_macro=deps.bzl%go_dependencies

...
...
ERROR: /private/var/tmp/_bazel_ch/c341ee081eec158c6db2e8b32b60c79e/external/io_bazel_rules_go/go/tools/builders/BUILD.bazel:48:10: no such package '@org_golang_x_tools//go/analysis/internal/facts': BUILD file not found in directory 'go/analysis/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: Analysis failed

有什么想法吗?

最佳答案

对 SO 很陌生,我遇到了同样的问题,通过调用 go_rules_dependencies() 后加载 gazelle 解决了这个问题

WORKSPACE 文件如下所示:

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

http_archive(
name = "io_bazel_rules_go",
sha256 = "6f111c57fd50baf5b8ee9d63024874dd2a014b069426156c55adbf6d3d22cb7b",
urls = [
"https://mirror.bazel.build/github.com/bazelbuild/rules_go/releases/download/v0.25.0/rules_go-v0.25.0.tar.gz",
"https://github.com/bazelbuild/rules_go/releases/download/v0.25.0/rules_go-v0.25.0.tar.gz",
],
)

http_archive(
name = "bazel_gazelle",
sha256 = "b85f48fa105c4403326e9525ad2b2cc437babaa6e15a3fc0b1dbab0ab064bc7c",
urls = [
"https://mirror.bazel.build/github.com/bazelbuild/bazel-gazelle/releases/download/v0.22.2/bazel-gazelle-v0.22.2.tar.gz",
"https://github.com/bazelbuild/bazel-gazelle/releases/download/v0.22.2/bazel-gazelle-v0.22.2.tar.gz",
],
)

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

load("@bazel_gazelle//:deps.bzl", "gazelle_dependencies")
load("//:go_third_party.bzl", "go_deps")

# gazelle:repository_macro go_third_party.bzl%go_deps
go_deps()


go_register_toolchains(version = "1.15.5")

gazelle_dependencies()

这里是 github 帖子,解释了为什么会发生,比我没有正确解释要好:https://github.com/bazelbuild/bazel-gazelle/issues/561#issuecomment-506047843

关于go - Bazel Gazelle 错误 : no such package '@org_golang_x_tools//go/analysis/internal/facts' : BUILD file not found in directory,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64929217/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com