Bazel for test resources - Input path does not exist: - bazel

I am trying to use Bazel for java tests. And need to access data files under resources. My project has standard Maven structure. I am using filegroup for the resources and have it as dependency in java_test.
The issue is when i try to run the test, it fails with following error java.lang.RuntimeException: org.apache.hadoop.mapred.InvalidInputException: Input path does not exist: file:/private/var/tmp/_bazel_username/2c26269e9af968f4ce3e88b22720114e/execroot/myproject/bazel-out/darwin-fastbuild/bin/hadoop/com_company_hadoop_search_join_TermTest.jar!/com/company/hadoop/search/join/names.txt
I have following in my src/test/resources/BUILD:
filegroup(
name = "resources",
testonly = 1,
srcs = glob(["**/*.txt"]),
visibility = ["//visibility:public"],
)
And the following in my hadoop/BUILD:
java_test(
srcs =
["src/test/java/com/company/hadoop/search/join/TermTest.java"],
resources = ["//hadoop/src/test/resources:resources"],
deps = [
"//src/main/java/com/path/to/my/code",
"//:junit_junit",
],
)
I verified the test jar contents and text file does exist as expected.

I believed your test "resources" are not java resources but data files.
Use the data attribute:
java_test(
srcs = [
"src/test/java/com/company/TermTest.java",
],
data = [
"//hadoop/src/test/resources:resources",
],
deps = [
"//src/main/java/com/path/to/my/code"
],
)

Related

Missing required module in ios_dynamic_framework [Bazel]

I am trying to use Bazel to create distributable framework and I have BUILD file like this:
ios_dynamic_framework(
name = "TheFramework",
bundle_name = "TheFramework",
...
deps = [
"TheFrameworkLib",
]
)
swift_library(
name = "TheFrameworkLib",
module_name: "TheFramework"
srcs = [
# publicly available
],
deps = [
":InternalLib"
],
)
swift_library(
name = "InternalLib",
srcs = [
# internal files
],
)
It builds successfully, and I can see by the size of the framework that InternalLib is also included in the binary. However, when I import this TheFramework.framework in the new iOS project (without Bazel), I am getting error: Missing required module "InternalLib". Any ideas how this can work? I guess I am missing some flags?

Deploy py_binary without container bazel

I am attempting to create a py_binary in bazel and then copy that to a remote machine. At the moment I was doing a dummy app to make sure the pipeline would work. I cannot use a docker image as I need lower level control of the hardware than docker can provide.
My goal is to do the following:
build the software with bazel
create a tar package that contains the py_binary
copy and run that binary on another computer not connected to bazel
To do this I made a simple binary(that for context just makes some RPC calls to a server I am working on as a side project) and the build files is as follows:
py_binary(
name="rosecore_cli",
srcs=glob([
"src/*.py"
]),
deps = [
"//rosecore/proto:project_py_pb2_grpc",
"//rosecore/proto:project_py_pb2"
]
)
pkg_files(
name = "binary",
srcs = [
":rosecore_cli",
],
prefix = "/usr/share/rosecore/bin",
)
pkg_filegroup(
name = "arch",
srcs = [
":binary",
],
visibility = ["//visibility:public"],
)
pkg_tar(
name = "rosecore_tar",
srcs = [
":arch"
],
include_runfiles=True
)
When I build, copy the tar file and extract it I get the following error:
AssertionError: Cannot find .runfiles directory for ./usr/share/rosecore/bin/rosecore_cli
Any help would be appreciated :)

`Bazel run` won't find static file dependency in bazel-bin when running nodejs_image

I am trying to get a rules docker nodejs_image to run using bazel.
My command is
bazel run :image.binary
Here is my rule:
load("#npm//#bazel/typescript:index.bzl", "ts_project")
load("#io_bazel_rules_docker//nodejs:image.bzl", "nodejs_image")
ts_project(
name = "typescript_build",
srcs = glob([
"src/**/*",
]),
allow_js = True,
out_dir = "build",
deps = ["#npm//:node_modules"],
)
nodejs_image(
name = "image",
data = [
":package.json",
":typescript_build",
"#npm//:node_modules",
],
entry_point = "build/app.js",
)
Basically, I need the package.json file because it includes some important configuration information when Node executes. If I call bazel build :image and then grab/run that image, everything works fine. But if I call bazel run :image it will basically work except that it can't find the package.json.
When I check the bazel-bin/ folder, I've noticed that the package.json isn't included, but the built typescript and node_modules are. I'm guessing because I'm not running any prior rules on the package.json, it doesn't get added to the bin, but I really don't know how to work around this.
So, basically, it seems that if you just use a rule like copy_to_bin or js_library, I think their purpose is to help put static files into your bazel-bin.
https://bazelbuild.github.io/rules_nodejs/Built-ins.html#copy_to_bin
ts_project(
name = "typescript_build",
srcs = glob([
"src/**/*",
]),
allow_js = True,
out_dir = "build",
deps = ["#npm//:node_modules"],
)
js_library(
name = "library",
srcs = ["package.json"],
deps = [":typescript_build"],
)
nodejs_image(
name = "image",
data = [
":library",
"#npm//:node_modules",
],
entry_point = "build/app.js",
)

This Bazel build file has only deps no srcs

I was trying to use google's media pipe but this Bazel build has no srcs only deps. What is supposed to be the main program in this build?
package(default_visibility = ["//mediapipe/examples:__subpackages__"])
cc_binary(
name = "hand_tracking_tflite",
deps = [
"//mediapipe/examples/desktop:simple_run_graph_main",
"//mediapipe/graphs/hand_tracking:desktop_tflite_calculators",
],
)
cc_binary(
name = "hand_tracking_cpu",
deps = [
"//mediapipe/examples/desktop:demo_run_graph_main",
"//mediapipe/graphs/hand_tracking:desktop_tflite_calculators",
],
)
# Linux only
cc_binary(
name = "hand_tracking_gpu",
deps = [
"//mediapipe/examples/desktop:demo_run_graph_main_gpu",
"//mediapipe/graphs/hand_tracking:mobile_calculators",
],
)
As stated in cc_binary docs deps defines list of libraries linked to this binary target. First dependency of each rule defines a library with the main function. You can see this looking at first dependency definition:
mediapipe/examples/desktop/BUILD:
cc_library(
name = "simple_run_graph_main",
srcs = ["simple_run_graph_main.cc"],
deps = [
#... removed for clarity.
],
)
Therefore main is defined in mediapipe/examples/desktop/demo_run_graph_main.cc
Hope it helps ;)

How to specify groupid, artifact and version directly in dependencies section of BUILD file using Bazel?

How to specify groupid, artifact and version directly in dependencies section of BUILD file using Bazel?
I am trying to convert a simple gradle project to bazel project. Can't really use generate_workspace since I have a gradle project (not maven).
I am wondering if there is just a easier way to specify GAV in the dependencies itself in the BUILD file so it would look something like this
java_binary(
name = "HelloWorld",
srcs = glob(["src/main/java/**/*.java"]),
resources = glob(["src/main/resources/**"]),
deps = ["com.fasterxml.jackson.core:jackson-core:2.8.8"],
main_class = "com.hello.sample.Foo"
)
Have you tried using maven_jar() directly?
In WORKSPACE:
maven_jar(
name = "com_google_guava_guava",
artifact = "com.google.guava:guava:18.0",
sha1 = "cce0823396aa693798f8882e64213b1772032b09",
)
In BUILD:
java_binary(
name = "HelloWorld",
srcs = glob(["src/main/java/**/*.java"]),
resources = glob(["src/main/resources/**"]),
deps = ["#com_google_guava_guava//jar"],
main_class = "com.hello.sample.Foo"
)
See https://docs.bazel.build/versions/master/be/workspace.html#maven_jar

Resources