bazel exclude transitive dependencies

896 Views Asked by At

How can I exclude a transitive dependencies just like Maven exclusions?

In maven, we often use exclusions to exclude them. But I didn't find it in the bazel.

1

There are 1 best solutions below

0
Ondrej K. On

If you wanted optional dependencies in your tree, you would need to model them into the tree using select() to shape said tree at build time. E.g. instead of:

xyz_library(
    name = "libA",
    ...
)

xyz_library(
    name = "libB",
    ...
)

xyz_library(
    name = "intermediate",
    deps = [
        "libA",
        "libB",
    ],
    ...
)

xyz_binary(
    name = "exec",
    deps = [":intermediate"],
    ...
)

You the intermediate target would looks like:

xyz_library(
    name = "intermediate",
    deps = [
        "libA" + select({
            ":condition1": [],
            "//conditions:default": ["libB"],
        }),
    ],
    ...
)

To not include the dependency on libB when config setting or constraint value :conditiona1 is matched in the current config. The whole reading on configurable attributes is in the docs.

If you wanted to extricate transitive dependency already hard-wired into the tree (e.g. pre-canned 3rd party dep you must take as is and have no control over)... and your specific rule used does not implement such behavior (note: "built-in" things like cc_binary, py_binary, ... generally do not), you could write a custom rule that could go through values (dependencies) passed through an attribute and process the list incl. disregarding some labels therein as perhaps passed through another attribute.