问题描述
我正在尝试使用brazel构建Go应用程序。这是我正在使用的现有私有GitHub存储库(位于此位置:github.xyz.com/repo-name
),我的目标是从main.go文件中创建一个二进制文件,该文件依赖于其他Go文件作为其方法。这是我的BUILD.bazel文件,位于所有这些go文件都存在的名为e2e的文件夹中:
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",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 "//test/e2e:go_default_library"
中的BUILD.bazel文件中,它将以以下错误结束:
"//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文件的依赖项添加到帮助程序文件中时,它不会导致周期性的依赖项错误。