Is it possible to list just links/names to go dependencies?
For example in Gopkg.lock I have:
[[projects]]
digest = "x:xxxxxxx"
name = "cloud.google.com/go"
packages = [
"xxxxx",
"xxxxx/xxxxx",
]
I want just name: cloud.google.com/go be listed
Related
I'm using bazel/gazelle to pull in some external git repositories.
For example:
go_repository(
name = "com_github_pkg_errors",
importpath = "github.com/pkg/errors",
sum = "h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=",
version = "v0.8.1",
)
My go file would just import github.com/pkg/errors like normal.
Problem comes when I run go vet on my file. It would complain that no such package exists under GOROOT or GOPATH. This repo is located in my bazel cache. ~/.cache/bazel/.../external/com_github_pkg_errors
How can I resolve this?
You have to specify the dependency in go.mod
require (
github.com/pkg/errors v0.8.1
)
In the BUILD.bazel file of your project, specify go_library with dep
go_library(
name = "project_lib",
srcs = [],
importpath = "github.com/pkg/errors",
visibility = ["//visibility:public"],
deps = [
"#com_github_pkg_errors",
],
)
I'm writing a go test with bazel. The test will build a binary first, and mount the binary to a docker container. So I need to know the path of the binary I built.
The file system structure is like this:
The dir structure looks like the following:
some/of/my/path
├── BUILD.bazel
├── my_test.go
└── my_tool
├── BUILD.bazel
└── my_tool.go
where I'd like to build a go binary with go files at my_tool dir.
From my previous question's answer, I got to know that I can use the data entry of in go_test from some/of/my/path/BUILD.bazel to build the binary:
go_test(
name = "my_test",
srcs = ["my_test.go"],
data = glob(["testdata/**"]) + [
"//some/of/my/path/my_tool:my_tool",
],
gotags = ["my_test"],
deps = [
"//pkg/util/contextutil",
"//pkg/util/log",
...
],
)
which did build the go binary for my_tool. But now the question is, how do I know where is the built binary of my_tool in my_test.go?
In my_test.go, I need to write something like this:
package my_lucky_test
...
pathForMyTool := some_path
hostConfig := container.HostConfig{
Binds := []string {""%s/my_tool-bin:/workding-dir/my_tool-bin", pathForMyTool"}
}
My question is how can I get the absolute path of "my_tool-bin"?
Simply run;
bazel build //some/of/my/path:my_test
It should print out the path of the binary to stdout.
As an alternative to your current approach might I suggest that you make use of bazelbuild/rules_docker#go_image which is a Bazel aware method for building containers.
EDIT: OP has clarified the question, though I am leaving the original answer above as others may find it useful.
When you bundle a binary/test with some data, the files under the data attribute become a set of 'runfiles'. There are various libraries in each language for resolving runfiles. For golang you'll want to use the bazel pkg in rules_go, to resolve your runfiles. e.g.
go_test(
name = "my_test",
srcs = ["my_test.go"],
data = glob(["testdata/**"]) + [
"//some/of/my/path/my_tool:my_tool",
],
gotags = ["my_test"],
deps = [
"//pkg/util/contextutil",
"//pkg/util/log",
# NEW dep
"#rules_go//rules_go/go/tools/bazel",
...
],
)
Your my_test.go;
package my_lucky_test
# NEW
import(
"github.com/bazelbuild/rules_go/go/tools/bazel"
)
#...
pathForMyTool, ok := bazel.FindBinary("some/of/my/path/my_tool", "my_tool")
if !ok {
# Error handling
}
hostConfig := container.HostConfig{
Binds := []string {""%s/my_tool-bin:/workding-dir/my_tool-bin", pathForMyTool}
}
I'm using bazel 4.1.0 to build to project.
Current set up:
go.mod file
...
github.com/mwitkow/go-proto-validators v0.3.2 // indirect
...
WORKSPACE file
load("#bazel_gazelle//:deps.bzl", "gazelle_dependencies", "go_repository")
load("//:repositories.bzl", "go_repositories")
go_repositories()
in repositories.blz file
...
go_repository(
name = "com_github_mwitkow_go_proto_validators",
importpath = "github.com/mwitkow/go-proto-validators",
sum = "h1:qRlmpTzm2pstMKKzTdvwPCF5QfBNURSlAgN/R+qbKos=",
version = "v0.3.2",
)
....
in students/api.v1/api.proto file
import "google/api/annotations.proto";
import "github.com/mwitkow/go-proto-validators/validator.proto";
after running bazel run //:gazelle -- update-repos -from_file=go.mod -to_macro=repositories.bzl%go_repositories -prune=true and bazel run //:gazelle
it generate students/api.v1/BUILD.bazel file
proto_library(
name = "api_proto",
srcs = ["api.proto"],
visibility = ["//visibility:public"],
deps = [
"//github.com/mwitkow/go-proto-validators:validators_proto",
"#go_googleapis//google/api:annotations_proto",
],
)
go_proto_library(
name = "api_go_proto",
compilers = ["#io_bazel_rules_go//proto:go_grpc"],
importpath = "students/generated/api",
proto = ":api_proto",
visibility = ["//visibility:public"],
deps = [
"//github.com/mwitkow/go-proto-validators:validator_proto",
"#go_googleapis//google/api:annotations_go_proto",
],
)
When I run bazel build //..., it produces an error
ERROR: /my-app/students/api.v1/BUILD.bazel:15:17: no such package 'github.com/mwitkow/go-proto-validators': BUILD file not found in any of the following directories. Add a BUILD file to a directory to mark it as a package.
- /my-app/github.com/mwitkow/go-proto-validators and referenced by '/students/api.v1:api_go_proto'
ERROR: Analysis of target '//students/api.v1:api' failed; build aborted: Analysis failed
I would like to make the build work without error. Thank you very much for your help.
I think in BUILD.bazel file, //github.com/mwitkow/go-proto-validators:validators_proto should be #github.com/mwitkow/go-proto-validators:validators_proto, like #go_googleapis//google/api:annotations_proto but it's just a vague idea.
When generating BUILD files with gazelle, bazel run //:gazelle, I'm getting strange dependencies within the protobuf package. I've added comments where the weird deps are in the generated output. Without these deps the build seems to be okay.
go.mod
proto/
BUILD.bazel
user.proto
command.proto
core.proto
load("#rules_proto//proto:defs.bzl", "proto_library")
load("#io_bazel_rules_go//go:def.bzl", "go_library")
load("#io_bazel_rules_go//proto:def.bzl", "go_proto_library")
proto_library(
name = "api_proto",
srcs = [
"command.proto",
"core.proto",
"user.proto",
],
visibility = ["//visibility:public"],
deps = [
"//:root_proto", # THIS DOESN'T EXIST
"#com_google_protobuf//:any_proto",
],
)
go_proto_library(
name = "api_go_proto",
compilers = ["#io_bazel_rules_go//proto:go_grpc"],
importpath = "kubelt.com/kubelt/api",
proto = ":api_proto",
visibility = ["//visibility:public"],
deps = [ # THESE ARE NOT GO DEPS
"//:command.proto",
"//:core.proto",
"//:user.proto",
],
)
go_library(
name = "api",
embed = [":api_go_proto"],
importpath = "kubelt.com/kubelt/api",
visibility = ["//visibility:public"],
)
My project structure
/PROJECT
WORKSPACE
BUILD
third_party
tensorflow <-- cloned repository
my_files
BUILD
In WORKSPACE file i added this
local_repository(
name = "tensorflow",
path = "third_party/tensorflow",
)
load("#tensorflow//tensorflow:workspace3.bzl", "workspace")
workspace()
load("#tensorflow//tensorflow:workspace2.bzl", "workspace")
workspace()
load("#tensorflow//tensorflow:workspace1.bzl", "workspace")
workspace()
load("#tensorflow//tensorflow:workspace0.bzl", "workspace")
workspace()
Initially, the following was already written in the file
#Tensorflow repo should always go after the other external dependencies.
# 2020-10-30
_TENSORFLOW_GIT_COMMIT = "84384703c0d8b502e33ff6fd7eefd219dca5ff8e"
_TENSORFLOW_SHA256= "23fb322fc15a20f7a7838d9a31f8b16f60700a494ea654311a0aa8621769df98"
http_archive(
name = "org_tensorflow",
urls = [
"https://github.com/tensorflow/tensorflow/archive/%s.tar.gz" % _TENSORFLOW_GIT_COMMIT,
],
patches = [
"#//third_party:org_tensorflow_compatibility_fixes.diff",
],
patch_args = [
"-p1",
],
strip_prefix = "tensorflow-%s" % _TENSORFLOW_GIT_COMMIT,
sha256 = _TENSORFLOW_SHA256,
)
load("#org_tensorflow//tensorflow:workspace.bzl", "tf_workspace")
tf_workspace(tf_repo_name = "org_tensorflow")
Than in my_files/BUILD i wrote following
objc_library(
deps = [
"#tensorflow//tensorflow/lite/objc:TensorFlowLite",
],
)
When building, I get the following error
ERROR: file '_middlemen/TensorFlowLiteCMetal-ObjcCppSemantics_build_arch_ios-arm64-min10.0-
applebin_ios-ios_arm64-dbg_with_suffix__non_objc_arc' is generated by these conflicting actions:
Label: #tensorflow//tensorflow/lite/delegates/gpu:metal_delegate,
#org_tensorflow//tensorflow/lite/delegates/gpu:metal_delegate
ERROR: com.google.devtools.build.lib.skyframe.ArtifactConflictFinder$ConflictException:
com.google.devtools.build.lib.actions.MutableActionGraph$ActionConflictException: for
_middlemen/TensorFlowLiteCMetal-ObjcCppSemantics_build_arch_ios-arm64-min10.0-applebin_ios-
ios_arm64-dbg_with_suffix__non_objc_arc, previous action: ObjcCppSemantics_build_arch_ios-
arm64-min10.0-applebin_ios-ios_arm64-dbg_with_suffix__non_objc_arc for #org_tensorflow//tensorflow/lite/delegates/gpu:metal_delegate, attempted action:
ObjcCppSemantics_build_arch_ios-arm64-min10.0-applebin_ios-ios_arm64-
dbg_with_suffix__non_objc_arc for #tensorflow//tensorflow/lite/delegates/gpu:metal_delegate
Maybe I somehow incorrectly add tensorlow, but I do not know how to fix it
This issue solved my problem:
https://github.com/tensorflow/tensorflow/issues/46144