bazelbuild / bazelbuild/bazel

docs: give advice on project structure to avoid unnecessary 'load' dependencies

Open
#12,835 16 comments 0 reactions 1 assignee Claimed by @meteorcloudy View on GitHub
P2 team-Documentation team-OSS type: feature request
Dominant language
Java
Stars
25.8k
Forks
4.6k
Avg merge
2d 20h
Merged PRs (30d)
72

Description

Bazel only downloads external dependencies that are needed for building your target? That's not exactly true.

## Problem
Bazel fetches unneeded dependencies due to load statements.

## Reproduce
Considering the following example:

Repo A is your main repo, which depends on some library in repo B. Repo B depends on a test rule in repo C for testing its library.

In A/BUILD:
```
cc_binary(
name = "bin",
srcs = ["main.cc"],
deps = ["@B//:lib"]
)
```

In B/BUILD:
```
load("@C-dev//:def.bzl", "my_cc_test")

cc_library(
name = "lib",
srcs = ["lib.cc"],
)

my_cc_test(
name = "lib_test",
srcs = ["lib_test.cc"],
deps = [
":lib",
"@C-dev//:test",
],
)
```

Although C is only a dev dependency of B, in order to build `//:bin` A has to declare C as a dependency in WORKSPACE and Bazel will fetch C at build time.

The root cause is `load("@C-dev//:def.bzl", "my_cc_test")` being in the same package as `@B//:lib`, in the loading phase, Bazel has to fetch C to correctly parse the BUILD file.

A full repo case is here: https://github.com/meteorcloudy/my_tests/tree/master/dev_deps_test

##

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.