Bazel/Golang: rule 'go_embed_data' doesn't contain declared provider 'GoArchive' - go

I'm trying to create a Bazel rule for my project that just embeds a collection of files. The embed rule is as follows:
go_embed_data(
name = "my_files_go",
src = [
"embedded/src1", "embedded/src2"
],
package = "my_lib",
var = "myFiles",
)
Which I then add in my go_library rule:
go_library(
name = "library",
srcs = [
"library.go",
],
importpath = "github.com/nickfelker/golang-app",
deps = [
":my_files_go"
"//otherLib",
],
)
However when I try to build this, I end up getting an obscure error that I cannot find elsewhere.
Error: <target //library:my_files_go> (rule 'go_embed_data') doesn't contain declared provider 'GoArchive'
ERROR: Analysis of target '//:binary' failed; build aborted: Analysis of target '//library:library' failed
How am I supposed to get around this error?

The rule created for go_embed_data does not go as a dependency to the go_library rule. Instead, it should be considered one of the srcs, as so:
go_embed_data(
name = "my_files_go",
src = [
"embedded/src1", "embedded/src2"
],
package = "my_lib",
var = "myFiles",
)
go_library(
name = "library",
srcs = [
":my_files_go",
"library.go",
],
importpath = "github.com/nickfelker/golang-app",
deps = [
"//otherLib",
],
)

Related

Unable to build bazel - Getting error running bazel with gin package

I'm trying to add the gin package in the BUILD file. When I build the file I get the following error
no such package '#com_github_gin_gonic_gin//go_default_library': The repository '#com_github_gin_gonic_gin' could not be resolved: Repository '#com_github_gin_gonic_gin' is not defined and referenced by
load("#io_bazel_rules_go//go:def.bzl", "go_library")
load("#bazel_gazelle//:def.bzl", "gazelle")
package(default_visibility = ["//visibility:public"])
go_library(
name = "",
srcs = [""],
importpath = "",
visibility = ["//visibility:public"],
deps = [
"#com_github_gin_gonic_gin//go_default_library",
"#io_k8s_client_go//kubernetes",
"#io_k8s_client_go//tools/clientcmd",
"#io_k8s_client_go//util/homedir",
],
)
In your workspace file try calling go_rules_dependencies() first and then load other dependencies with respect to gin packages.

how to add an additional loop over a stringlist within a for_each

i've setted up multiple github repos within my .tf configuration with a simple for_each on "github" repository resource. In addition i tried to create several branches (a branch per env) for each newly created repo.
My first intention was to use a module (./modules/github_repo/repo.tf) which includes
locals {
environments = var.environments
}
resource "github_branch" "branches" {
for_each = toset(setsubtract(local.environments, ["prod"]))
repository = "orgName/${var.REPO_NAME}"
branch = lookup(var.environment_to_branch_map, each.key, each.key)
source_branch = "master"
}
with following variables
variable "REPO_NAME" {
type = string
}
variable "environments" {
type = list(string)
}
variable "environment_to_branch_map" {
type = map(any)
default = {
"prod" = "master"
"dev" = "develop"
}
calling like this from main.tf
provider "github" {
token = var.GITHUB_TOKEN
owner = "orgName"
}
locals {
environments = ["dev", "prod", "staging", "test"]
microServices = tomap({ "service1" : "fnApp", "service2" : "fnApp" })
default_branch = "master"
}
module "branches_per_microservice" {
for_each = local.microServices
source = "./modules/github_repo"
REPO_NAME = each.key
environments = local.environments
depends_on = [github_repository.microservices]
}
unfortunately i get an 404 for each branch and repo combination like this
Error: Error querying GitHub branch reference /orgName/service1 (refs/heads/master): GET
https://api.github.com/repos//orgName/service1/git/ref/heads/master: 404 Not Found []
with
module.branches_per_microservice["service1"].github_branch.branches["test"]
on modules/github_repo/repo.tf line 23, in resource "github_branch" "branches":
i guess it's a "provider" thing, cause if i try to create a branch directly in the main.tf, it will work. but the problem is, that i only can use one loop within a resource. (i already know that providers are not possible in modules with count or for_each loops, as written in terraform docs)
resource "github_branch" "branches" {
for_each = toset(setsubtract(local.environments, ["prod"]))
repository = github_repository.microservices["service1"].name
branch = lookup(var.environment_to_branch_map, each.key, each.key)
source_branch = "master"
}
in this case, i have to create a resource for each "MicroService" manually, which i want to avoid heavily... Are there any ideas how i could "nest" the second loop over the environments to create my branches for each Micorservice repos?
Many thx in advance for any hint, idea or approach here...
Nested loop can be replaced with a single loop over the setproduct of two sets. The documentation for setproduct can be found here
https://www.terraform.io/language/functions/setproduct

no such target '#maven//:io_lettuce_lettuce_core': target 'io_lettuce_lettuce_core' not declared in package

I am trying to write a very simple example in kotlin to interact with redis.
I want to use bazel to drive the build process.
Here is the WORKSOPACE file
workspace(
name = "com_ahwkong_kotlin",
managed_directories = {},
)
load("#bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
RULES_KOTLIN_VERSION = "legacy-1.4.0-rc3"
RULES_KOTLIN_SHA = "da0e6e1543fcc79e93d4d93c3333378f3bd5d29e82c1bc2518de0dbe048e6598"
http_archive(
name = "io_bazel_rules_kotlin",
urls = ["https://github.com/bazelbuild/rules_kotlin/releases/download/%s/rules_kotlin_release.tgz" % RULES_KOTLIN_VERSION],
sha256 = RULES_KOTLIN_SHA,
)
load("#io_bazel_rules_kotlin//kotlin:kotlin.bzl", "kotlin_repositories", "kt_register_toolchains")
kotlin_repositories() # if you want the default. Otherwise see custom kotlinc distribution below
kt_register_toolchains() # to use the default toolchain, otherwise see toolchains below
# maven
RULES_JVM_EXTERNAL_TAG = "2.8"
RULES_JVM_EXTERNAL_SHA = "79c9850690d7614ecdb72d68394f994fef7534b292c4867ce5e7dec0aa7bdfad"
http_archive(
name = "rules_jvm_external",
strip_prefix = "rules_jvm_external-%s" % RULES_JVM_EXTERNAL_TAG,
sha256 = RULES_JVM_EXTERNAL_SHA,
url = "https://github.com/bazelbuild/rules_jvm_external/archive/%s.zip" % RULES_JVM_EXTERNAL_TAG,
)
load("#rules_jvm_external//:defs.bzl", "maven_install")
maven_install(
artifacts = [
"com.google.code.findbugs:jsr305:1.3.9",
"com.google.errorprone:error_prone_annotations:2.0.18",
"com.google.j2objc:j2objc-annotations:1.1",
],
repositories = [
"https://jcenter.bintray.com/",
"https://repo1.maven.org/maven2",
],
)
This is my BUILD file
load("#io_bazel_rules_kotlin//kotlin:kotlin.bzl", "kt_compiler_plugin", "kt_jvm_library", "kt_jvm_binary")
kt_compiler_plugin(
name = "open_for_testing_plugin",
id = "org.jetbrains.kotlin.allopen",
options = {
"annotation": "plugin.allopen.OpenForTesting",
},
deps = [
"#com_github_jetbrains_kotlin//:allopen-compiler-plugin",
],
)
kt_jvm_library(
name = "ex1_lib",
srcs = glob(["ex1*/src/**/*.kt"]),
deps = [
"#maven//:io_lettuce_lettuce_core",
]
)
java_binary(
name = "ex1",
main_class = "ex1.App",
srcs = glob(["ex1*/src/**/*.kt"]),
visibility = ["//visibility:public"],
runtime_deps = [":ex1_lib"],
)
This is my kotlin file
package ex1
import io.lettuce.core.RedisClient
import io.lettuce.core.RedisReactiveCommandsImpl
import io.lettuce.core.RedisURI
import io.lettuce.core.api.StatefulRedisConnection
import io.lettuce.core.api.reactive.RedisReactiveCommands
import java.time.Duration
class ConnectionFactory {
// https://kotlinlang.org/docs/tutorials/kotlin-for-py/objects-and-companion-objects.html
// If you need a function or a property to be tied to a class rather than to instances of it
// (similar to #staticmethod in Python), you can declare it inside a companion object:
companion object {
fun createConnection(port: Int = 6380, host: String = "127.0.0.1"): RedisClient =
RedisClient.create(RedisURI.builder()
.withTimeout(Duration.ofMinutes(1))
.withHost(host)
.withPort(port)
.build())
}
}
Without the maven dependency I will not be able to compile the above code.
Here is the error message:
$ bazel build ex1
ERROR: /Users/antkong/dev/canva/kongakong-experiments.3.diagrams/tech_talk/kotlin/BUILD:26:1: no such target '#maven//:io_lettuce_lettuce_core': target 'io_lettuce_lettuce_core' not declared in package '' defined by /private/var/tmp/_bazel_antkong/3496a0772a2021d0f0b3c230bf912ab1/external/maven/BUILD and referenced by '//:ex1_lib'
ERROR: Analysis of target '//:ex1' failed; build aborted: Analysis failed
If the issue is "no such target '#maven//:io_lettuce_lettuce_core', how can I define this target?
I need to add this rule to the WORKSPACE
maven_repositories = [
"https://repo.maven.apache.org/maven2/",
"https://s3.amazonaws.com/maven.canva-build.com/release/",
"https://github.com/getdyspatch/dyspatch-java-mvn/raw/master/releases/",
]
maven_install(
artifacts = [
"io.lettuce:lettuce-core:jar:5.3.4.RELEASE",
],
repositories = maven_repositories,
)

Why nogo config is not considered when using bazel coverage

Hello our golang project is using nogo for static code analysis. We also have a nogo-config.json file to exclude certain paths for some of the rules. I found that bazel build //... and bazel test //... all use the config(because they don't complain the shadow variable error configured in the nogo_config.json) however bazel coverage //... will report variable shadow error for code under foo/bar which means the bazel coverage didn't obey the nogo_config.json. Why? (I am using bazel 2.2.0, golang go1.13.6 darwin/amd64)
WORKSPACE file
go_register_toolchains(nogo = "#//:my_nogo")
BUILD.bazel file
load("#io_bazel_rules_go//go:def.bzl", "nogo")
nogo(
name = "my_nogo",
config = "nogo-config.json", # Exclude analysis of 3rd party libraries
visibility = ["//visibility:public"],
deps = [
"#org_golang_x_tools//go/analysis/passes/shadow:go_tool_library",
],
)
nogo_config.json
{
"shadow": {
"exclude_files": {
"external/*": "",
"foo/bar/*": "Some generated model files don't behave by these go rules"
}
},
}

Bazel googleapi gRPC cc_grpc_library output was not created

I am running into Bazel build issues when I try to build googleapis texttospeech_proto and speech_proto with gRPC service. In the root directory of my project, I receive the following error:
ERROR: <>/BUILD:42:1: output 'external/com_google_googleapis/google/cloud/texttospeech/v1/cloud_tts.grpc.pb.h' was not created
ERROR: <>/BUILD:42:1: output 'external/com_google_googleapis/google/cloud/texttospeech/v1/cloud_tts.grpc.pb.cc' was not created
ERROR: <>/BUILD:42:1: not all outputs were created or valid
My BUILD/WORKSPACE files are below:
BUILD
load("#com_github_grpc_grpc//bazel:cc_grpc_library.bzl", "cc_grpc_library")
cc_proto_library(
name = "speech_cc_proto",
deps = [
"#com_google_googleapis//google/cloud/speech/v1p1beta1:speech_proto",
],
visibility = ["//visibility:public"],
)
cc_grpc_library(
name = "cloud_speech_grpc",
srcs = [
"#com_google_googleapis//google/cloud/speech/v1p1beta1:speech_proto",
],
deps = [
":speech_cc_proto",
],
grpc_only = 1,
use_external = 1,
visibility = ["//visibility:public"],
)
cc_proto_library(
name = "texttospeech_cc_proto",
deps = [
"#com_google_googleapis//google/cloud/texttospeech/v1:texttospeech_proto",
],
visibility = ["//visibility:public"],
)
cc_grpc_library(
name = "cloud_texttospeech_grpc",
srcs = [
"#com_google_googleapis//google/cloud/texttospeech/v1:texttospeech_proto",
],
deps = [
":texttospeech_cc_proto",
],
grpc_only = 1,
use_external = 1,
visibility = ["//visibility:public"],
)
WORKSPACE
git_repository(
name = "com_github_grpc_grpc",
remote = "https://github.com/grpc/grpc.git",
tag = "v1.27.0-pre1",
)
load("#com_github_grpc_grpc//bazel:grpc_deps.bzl", "grpc_deps")
grpc_deps()
load("#com_github_grpc_grpc//bazel:grpc_extra_deps.bzl", "grpc_extra_deps")
grpc_extra_deps()
load("#io_bazel_rules_python//python:pip.bzl", "pip_import", "pip_repositories")
pip_import(
name = "grpc_python_dependencies",
requirements = "#com_github_grpc_grpc//:requirements.bazel.txt",
)
load("#grpc_python_dependencies//:requirements.bzl", "pip_install")
pip_repositories()
pip_install()
git_repository(
name = "com_google_googleapis",
remote = "https://github.com/googleapis/googleapis.git",
commit = "3c39a1d6e23c1ef63c7fba4019c25e76c40dfe19",
)
load("#com_google_googleapis//:repository_rules.bzl", "switched_rules_by_language")
switched_rules_by_language(
name = "com_google_googleapis_imports",
cc = True,
grpc = True,
)
If I move my BUILD file out of the root workspace (into a child workspace/folder), I receive a different error saying the cloud_speech.proto / cloud_tts.proto files are not within the child workspace.

Resources