Bazel 构建缺少严格的依赖项
Bazel build missing strict dependencies
我正在尝试使用 brazel 构建 Go 应用程序。这是我正在处理的现有私有 GitHub 存储库(位置:github.xyz.com/repo-name
),我的目标是从 main.go 文件中创建一个二进制文件,该文件依赖于其他文件为它的方法去文件。这是我的 BUILD.bazel 文件,位于名为 e2e 的文件夹中,其中包含所有这些 go 文件:
load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library")
go_binary(
name = "e2e",
embed = [":go_default_library"],
visibility = ["//visibility:public"],
)
go_library(
name = "go_default_library",
srcs = ["main.go"],
data = [
"//cmd/iasd",
"//migrations:migration_files",
"//test/iamd:client-ca-bundle.pem",
] + glob(
["testdata/**"],
),
importpath = "github.xyz.com/repo-name/test/e2e",
visibility = ["//visibility:public"],
deps = [
"//test:go_default_library",
"//vendor/github.com/bazelbuild/rules_go/go/tools/bazel:go_default_library",
"//vendor/github.com/golang-migrate/migrate:go_default_library",
"//vendor/github.com/golang-migrate/migrate/database/cockroachdb:go_default_library",
"//vendor/github.com/golang-migrate/migrate/source/file:go_default_library",
"//vendor/github.com/google/uuid:go_default_library",
"//vendor/github.com/lib/pq:go_default_library",
"//vendor/github.com/pkg/errors:go_default_library",
"//vendor/google.golang.org/grpc:go_default_library",
"//vendor/google.golang.org/grpc/credentials:go_default_library",
"//api/grpc/v1/applications:go_default_library",
"//api/grpc/v1/clients:go_default_library",
"//pkg/oauth2/mtls:go_default_library",
"//vendor/github.com/coreos/go-oidc:go_default_library",
"//vendor/github.com/dgrijalva/jwt-go:go_default_library",
"//vendor/golang.org/x/oauth2:go_default_library",
],
)
这就是我的 main.go 文件中的导入内容:
import (
"context"
"crypto/tls"
"encoding/json"
"io/ioutil"
"net/http"
"net/url"
"strings"
"time"
"log"
"fmt"
"io"
"os"
"github.com/pkg/errors"
"github.com/coreos/go-oidc"
"github.com/dgrijalva/jwt-go"
"github.xyz.com/repo-name/api/grpc/v1/applications"
"github.xyz.com/repo-name/api/grpc/v1/clients"
"github.xyz.com/repo-name/pkg/oauth2/mtls"
"github.xyz.com/repo-name/test/e2e"
"github.xyz.com/repo-name/test"
"golang.org/x/oauth2"
"google.golang.org/grpc"
)
现在执行命令 bazel run //test/e2e:e2e
,我得到以下错误:
compilepkg: missing strict dependencies:
/private/var/tmp/_bazel_<myname>/a9b32769d59e90b5398bc5d8a988c877/sandbox/darwin-sandbox/394/execroot/github_xyz_com_repo_name/test/e2e/main.go: import of "github.xyz.com/repo-name/test/e2e"
No dependencies were provided.
这是因为在我的主文件中导入 github.xyz.com/repo-name/test/e2e
。要包含导入,如果我将其添加到 deps 中的 BUILD.bazel 文件中:"//test/e2e:go_default_library"
它最终会出现此错误:
"//test/e2e:go_default_library: cycle in dependency graph:
//test/e2e:e2e
.-> //test/e2e:go_default_library [self-edge]"
我明白错误的来龙去脉,但我想不出合适的方法来解决它。我应该在 BUILD.bazel 文件中进行哪些更改才能正确包含依赖项?
我通过将我的 main.go 文件添加到 e2e 文件夹内的新文件夹中解决了这个问题(同时将帮助程序文件保留在 e2e 文件夹中)。这样,当我将 main.go 文件的依赖项添加到辅助文件时,它不会导致循环依赖性错误。
我正在尝试使用 brazel 构建 Go 应用程序。这是我正在处理的现有私有 GitHub 存储库(位置:github.xyz.com/repo-name
),我的目标是从 main.go 文件中创建一个二进制文件,该文件依赖于其他文件为它的方法去文件。这是我的 BUILD.bazel 文件,位于名为 e2e 的文件夹中,其中包含所有这些 go 文件:
load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library")
go_binary(
name = "e2e",
embed = [":go_default_library"],
visibility = ["//visibility:public"],
)
go_library(
name = "go_default_library",
srcs = ["main.go"],
data = [
"//cmd/iasd",
"//migrations:migration_files",
"//test/iamd:client-ca-bundle.pem",
] + glob(
["testdata/**"],
),
importpath = "github.xyz.com/repo-name/test/e2e",
visibility = ["//visibility:public"],
deps = [
"//test:go_default_library",
"//vendor/github.com/bazelbuild/rules_go/go/tools/bazel:go_default_library",
"//vendor/github.com/golang-migrate/migrate:go_default_library",
"//vendor/github.com/golang-migrate/migrate/database/cockroachdb:go_default_library",
"//vendor/github.com/golang-migrate/migrate/source/file:go_default_library",
"//vendor/github.com/google/uuid:go_default_library",
"//vendor/github.com/lib/pq:go_default_library",
"//vendor/github.com/pkg/errors:go_default_library",
"//vendor/google.golang.org/grpc:go_default_library",
"//vendor/google.golang.org/grpc/credentials:go_default_library",
"//api/grpc/v1/applications:go_default_library",
"//api/grpc/v1/clients:go_default_library",
"//pkg/oauth2/mtls:go_default_library",
"//vendor/github.com/coreos/go-oidc:go_default_library",
"//vendor/github.com/dgrijalva/jwt-go:go_default_library",
"//vendor/golang.org/x/oauth2:go_default_library",
],
)
这就是我的 main.go 文件中的导入内容:
import (
"context"
"crypto/tls"
"encoding/json"
"io/ioutil"
"net/http"
"net/url"
"strings"
"time"
"log"
"fmt"
"io"
"os"
"github.com/pkg/errors"
"github.com/coreos/go-oidc"
"github.com/dgrijalva/jwt-go"
"github.xyz.com/repo-name/api/grpc/v1/applications"
"github.xyz.com/repo-name/api/grpc/v1/clients"
"github.xyz.com/repo-name/pkg/oauth2/mtls"
"github.xyz.com/repo-name/test/e2e"
"github.xyz.com/repo-name/test"
"golang.org/x/oauth2"
"google.golang.org/grpc"
)
现在执行命令 bazel run //test/e2e:e2e
,我得到以下错误:
compilepkg: missing strict dependencies:
/private/var/tmp/_bazel_<myname>/a9b32769d59e90b5398bc5d8a988c877/sandbox/darwin-sandbox/394/execroot/github_xyz_com_repo_name/test/e2e/main.go: import of "github.xyz.com/repo-name/test/e2e"
No dependencies were provided.
这是因为在我的主文件中导入 github.xyz.com/repo-name/test/e2e
。要包含导入,如果我将其添加到 deps 中的 BUILD.bazel 文件中:"//test/e2e:go_default_library"
它最终会出现此错误:
"//test/e2e:go_default_library: cycle in dependency graph:
//test/e2e:e2e
.-> //test/e2e:go_default_library [self-edge]"
我明白错误的来龙去脉,但我想不出合适的方法来解决它。我应该在 BUILD.bazel 文件中进行哪些更改才能正确包含依赖项?
我通过将我的 main.go 文件添加到 e2e 文件夹内的新文件夹中解决了这个问题(同时将帮助程序文件保留在 e2e 文件夹中)。这样,当我将 main.go 文件的依赖项添加到辅助文件时,它不会导致循环依赖性错误。