bazel-deps icon indicating copy to clipboard operation
bazel-deps copied to clipboard

Cannot redefine repository after any load statement in the WORKSPACE file

Open ramtej opened this issue 7 years ago • 5 comments

Hi, a crosslink to a bazel https://github.com/bazelbuild/bazel/issues/1984 issue that happens when two same maven_jar dependencies are specified. That may happen, when two independent workspaces are included with "local_repository"

See also

https://github.com/ramtej/bazel-multiworkspace-sampler.git

I;m not really sure if the problem can/should be solved within bazel-deps or if that is something that has to be done in bazel "core".

Best Regards, jj

ramtej avatar Jun 26 '17 12:06 ramtej

yeah, I don't think bazel will allow us to compose workspace.bzl loads in this way.

The way we do it at stripe is to merge the yaml files and then regenerate the workspace.bzl from there.

I added a merge command to deal with this case, sadly, I think it can create some errors on merge and not quite ready to use.

johnynek avatar Jun 26 '17 17:06 johnynek

Hello, thank you for your answer.

I don't think bazel will allow us to compose workspace.bzl loads in this way.

Do you mean that conceptually this approach does not makes sense ? My intention was to introduce an "App" based approach following the microservice patterns (share nothing principle). That means that each xyz.app is self-contained and is executed in a docker/kubernetes environment. Further, it is a fully valid case where two apps are using the same maven artifact, but in different versions.

The way we do it at stripe is to merge the yaml files and then regenerate the workspace.bzl from there

Ok, means there is a kind of preprocessing step that parses the different yaml files, generates the "final.yaml" that is then used to generate the bazel 3rdparty dependencies. Hmm, see the point.

I would expect that there is a kind isolation/visibility pattern included in bazel where packages can manage they own dependency, e.g. @a_app//:jar/junit/junit.

Regards, jj

ramtej avatar Jun 26 '17 19:06 ramtej

Hi, I could solve the issue with support given by the bazel community. The simple trick is to verify if a rule (maven_jar) is already defined in the repository.

That means, the one can build two workspaces, e.g. ~/tmp/bazel-multiworkspace-sampler/profile$ bazel build @a_app//... @b_app//... The load.bzl looks as follow:

if native.existing_rule(item["name"]) == None: 
    if sha != None:
      native.maven_jar(name = item["name"], artifact = item["artifact"], sha1 = sha)
    else:
      native.maven_jar(name = item["name"], artifact = item["artifact"])
    native.bind(name = item["bind"], actual = item["actual"])

See full sample at https://github.com/ramtej/bazel-multiworkspace-sampler

The issue is still, that two different maven_jar artifacts can have the same maven_jar rule name. Evtl. the "bind": "jar/junit/junit" definition in the generated dependencies.bzl can contain the version and the maven_jar rule as well, e.g. "bind": "jar/junit/junit_4_12.

That means also that the BUILD files has to reference the version number :

java_test(
    name = "hello",
    srcs = ["TestHello.java"],
    test_class = "com.example.myproject.TestHello",
    deps = [
        "//src/main/java/com/example/myproject:hello-lib",
        "//external:jar/junit/junit_4_12",
    ],
)

alternatively the current workspace name can be used for binding e.g. "//external:a_app_jar/junit/junit",

Cheers, jj

ramtej avatar Jun 28 '17 08:06 ramtej

this is a nice solution!

Thanks for sharing.

johnynek avatar Jun 30 '17 19:06 johnynek

this may have been due to the bug we tried to fix with #51

johnynek avatar Jul 14 '17 19:07 johnynek