I'm working with a Go monorepo and using Bazel. My issue arises when trying to access Go files located in a nested directory structure during testing.
The project has a directory structure like this:
/src/
/monorepo/
/subproject/
/component/
/nested/
/directory/
- somefile.go
- anotherfile.go
/test/
- test.go
- BUILD.bazel
BUILD.bazel
In the nested/directory/BUILD.bazel, I have a filegroup that encompasses all Go files within that directory:
filegroup(
name = "source_files",
srcs = glob(["**/*.go"]),
visibility = ["//visibility:public"],
)
The test.go located in the test directory should access these Go files for testing. The relevant BUILD.bazel for the test is configured as follows:
go_test(
name = "my_test",
srcs = ["test.go"],
data = ["//monorepo/subproject/component/nested/directory:source_files"],
embed = [":component_lib"],
deps = [
# Other dependencies...
],
)
Despite this setup, during test execution with Bazel, the files/folder in nested/directory are not found, resulting in file not found errors. It seems Bazel is not making these files available in the test's sandbox environment.
How can I correctly configure Bazel so that the Go test can access these files? Is there a specific method to reference and utilize these nested directory files within the test environment?
Tried various approaches via Genrule too. But that didn't work either. However, I am able to expose files from a config/ directory to tests via the similar filegroup glob definition. One this to call out, the /directory/ contains go.mod and main.go in it. Not sure if that can cause problems as /component/ has its own main.go.
To see if the files are in the sandbox, run with
--sandbox_debug, which will print the sandbox directory and leave it. (Be aware that this will leave large amounts of temporary files if you run large and/or repeated builds with it.)I suspect your files are in the sandbox, but you're having trouble finding them. rules_go has a runfiles package designed for this. You may also be able to locate them with relative paths, although that's not as robust to changes in how the data files are being generated.