Managing package dependencies with Bazel
- Alexander_BernauerOP 2
Hi
I have a mono-repo with Bazel. There are two components A and B, both a DAML project, where A depends on B.
I can
daml damlc buildcomponents B, no problem. The fileB.daris then somewhere inbazel-out/.But how do I make component B available to the build of component A?
The documentation suggests to list
B.darin the dependencies section of thedaml.yamlfile of component A. But this means doing something like../../bazel-out/k8-fastbuild/bin/components/B/B.darugh.I understand I could also just list
Bas an absolute name under A’s dependencies. But then I need to haveB.darbe included in the package db that the build for component A is using. Am I getting this right?If so, how do I create a package db that includes
B.dar? And how would the Bazel build rule look like that accomplishes this?Or is there another way I am not seeing right now?
Thanks and regard,
Alex - bernhard 1
Since your
B.darends up inbazel-out, I assume thedaml buildcommand (no need fordamlc) is run inside a Bazel rule. Probably aGenrule?
And I assume theGenrulehas theB.daras its output as well.If so, the “right” way of using that dependency in
Awould be to also buildAusing aGenrule, which takes the rule ofBas a dependency.Say you have
genrule( name = "B", srcs = glob(sourcepathB/**), outs = ["B.dar"], tools = ["//.../daml"] cmd = """ cd sourcepathB $(execpath //.../daml) build -o $@ """ )Then you can use that dependency in the build rule for
A.genrule( name = "A", srcs = glob(sourcepathA/**) + [ "//pathToB:B" ], outs = ["A.dar"], tools = ["//.../daml"] cmd = """ cd sourcepath mkdir -p lib cp $(location //pathToB:B) lib $(execpath //.../daml) build -o $@ """ )Now you’d have to have to reference
lib/B.darin yourdaml.yamlfile.This doesn’t integrate with the Daml VsCode plugin. You’d have to move the
B.darfile to thelibdirectory after changingB.If you want an even more solid Bazel solution you can write Daml rules in Skylark. You can find examples of this in the Daml repo.
I think the only way to get bazel rules and a working Daml Studio is probably to do what you suggested (dependency path in bazel_out) or at best prettifying that via a symlink.
- Alexander_Bernauer
Thank you very much, @bernhard , for your help.
How does
daml.yamlneed to list the dependency onB.darfor damlc to pick it up from the component A’slibfolder? Or is this a runtime flag fordamlc?For me
damlcis looking forB.darin/usr/local/.daml/sdk/1.9.0/daml-libs/and obviously can’t find it there.I will check out the Skylark implementation. I was just hoping I could get away with genrules.
Regards,
Alex - bernhard 1
The stanza in the
daml.yamlofAshould be something likedependencies: - daml-prim - daml-stdlib - daml-script - ./libs/B.darie if you give it a relative or absolute path, it’ll look there, not in the
daml-libsfolder of the SDK. - Alexander_Bernauer 1
Thank you, @bernhard, this works.
For the records, this is my
BUILDfile for A:genrule( name = "A", srcs = glob(["src/**"]) + ["daml.yaml", "//path/to/B:B"], outs = ["A.dar"], cmd = """ mkdir -p path/to/A/lib cp $(location //path/to/B:B) path/to/A/lib daml build --project-root path/to/A -o $@ )and my
daml.yamllooks just like what you have quoted.dependencies: - daml-prim - daml-stdlib - daml-script - ./libs/B.darThanks again.
Alex - cocreature 1
This is a common problem when using Bazel with off-the shelf, language-specific build systems (I’d count
daml buildas one in this case). If you hand over control fully to Bazel, things like IDE integrations break which is obviously annoying (this is the approach taken byrules_daml@bernhard pointed you to above). If you don’t, then you have to keep things in sync. In your example that means manually updatinglibs/B.darwhen it changes (or try to symlink it from the bazel dir but that gets increasingly hard once you take different operating systems, optimization levels, … into account all of which influence the directory bazel uses).There are a few options to get out of this conundrum that different languages have chosen:
- Instead of pointing your IDE directly at your underlying tool (
damlcin this case) you point it at some wrapper script which queries Bazel to build and locate depedencies and then invokes the underlying tool with some extra flags to make it find dependencies. This is the approach taken by Haskell tooling for example and Scala also does something along those lines.
For Daml, this would mean that you have adaml.yamlwith an empty list ofdata-dependencies/dependencies. You then need your Bazel build setup to produce package databases, e.g., by wrappingdaml damlc initor maybe even go lower-level and construct them manually (unfortunately no docs on this atm). Finally, your wrapper script would find the location of those package databases and then invokedaml damlc ide --package-db=… --package-db= … --package=… --package=…and your IDE should find dependencies. This is probably the approach I’d recommend but it is a fair amount of effort. - Another option taken by
rules_nodejsis to have Bazel step out of its build root and mess around in the actual source tree, e.g., modifynode_modules. In this case, this could mean modifyingdaml.yamlto point to the location of the dependencies. That requires in some sense less effort since you don’t need the wrapper script but it also gives up on Bazel managing your stuff cleanly and you get back to the good old days whererm -rf node_modulessolves your problem (or whatever the equivalent of that in Daml is
) - Lastly, you could also imagine some kind of native support in the compiler to make the location of source files and dependencies configurable. I’m not really aware of any language that has proper support for this atm that works well with Bazel but it has at least been discussed for Go in the context of Expose generated Go files to editors and IDEs · Issue #512 · bazelbuild/rules_go · GitHub.
So unfortunately, while I think making 1 work is definitely feasible, the current situation is not great. I’ve opened Improve UX when compiling Daml code in Bazel or other build systems · Issue #8767 · digital-asset/daml · GitHub to track this on our side and we’ll see what we can do to make the UX here a bit better.
- Instead of pointing your IDE directly at your underlying tool (
- Alexander_Bernauer
Thank you very much, @cocreature, for this extensive response. This is really helpful, since I have been wondering about these things.
From my perspective the native tools should indeed be highly configurable with where they read input from and where they write output to. I guess that would be option 3.
I wonder into which category the Java and C++ toolchains fall. They seem to be rather well supported by Bazel.
Regards,
Alex - cocreature
Java and C++ are a bit special since they are natively supported by Bazel rather than being based on a separate rule set. You still run into the same issue though. Your IDE tooling might expect a Maven project or a CMake project and you have to teach it how to handle Bazel. Not quite sure how that works there.