bazel-contrib / rules_jvm_external

Bazel rules to resolve, fetch and export Maven artifacts
Apache License 2.0
336 stars 256 forks source link

`override_targets` can create duplicate dependencies #582

Open jdai8 opened 3 years ago

jdai8 commented 3 years ago

Guice multibindings was added to Guice core in 4.2. Our repo only uses Guice 4.2+, so we'd like to point multibindings to guice:

override_targets = {
  "com.google.inject.extensions:guice-multibindings": "@maven//:com_google_inject_guice"
}

For dependencies which depend on both guice and guice multibindings, for example nifty-core, this results in a duplicate dependency in the generated rule:

jvm_import(
    name = "com_facebook_nifty_nifty_core",
    jars = [...],
    srcjar = "...",
    deps = [
        ":io_airlift_log",
        ":io_airlift_units",
        ":com_google_code_findbugs_annotations",
        "@maven//:com_google_inject_guice",
        ":io_netty_netty",
        ":com_google_guava_guava",
        ":javax_inject_javax_inject",
        ":com_google_inject_guice",
    ],
    tags = ["maven_coordinates=com.facebook.nifty:nifty-core:0.23.0"],
)

which fails with an error that looks like:

Label '@maven//:com_google_inject_guice' is duplicated in the 'deps' attribute of rule 'com_facebook_nifty_nifty_core'
jdai8 commented 3 years ago

One solution might be to still generate the "overriden" target (:com_google_inject_extensions_guice_multibindings in this case):

jvm_import(
    name = "com_facebook_nifty_nifty_core",
    jars = [...],
    srcjar = "...",
    deps = [
        ":io_airlift_log",
        ":io_airlift_units",
        ":com_google_code_findbugs_annotations",
        ":com_google_inject_extensions_guice_multibindings",
        ":io_netty_netty",
        ":com_google_guava_guava",
        ":javax_inject_javax_inject",
        ":com_google_inject_guice",
    ],
    tags = ["maven_coordinates=com.facebook.nifty:nifty-core:0.23.0"],
)

And take advantage of this existing alias:

alias(
    name = "com_google_inject_extensions_guice_multibindings",
    actual = "@maven//:com_google_inject_guice",
    visibility = ["//visibility:public"],)