1

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.

Jianwu Chen
  • 5,336
  • 3
  • 30
  • 35
  • Please add more details. What language/ruleset are you talking about? Maybe give a concrete example of a case where you want to exclude some dependency. – lummax Jul 06 '23 at 06:51

1 Answers1

0

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.

Ondrej K.
  • 8,841
  • 11
  • 24
  • 39