Skip to content

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

@meteorcloudy

Description

@meteorcloudy

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 the 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

Metadata

Metadata

Assignees

Labels

P2We'll consider working on this in future. (Assignee optional)team-DocumentationDocumentation improvements that cannot be directly linked to other team labelsteam-OSSIssues for the Bazel OSS team: installation, release processBazel packaging, websitetype: feature request

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions