rules_py icon indicating copy to clipboard operation
rules_py copied to clipboard

Demonstrate virtual dependencies

Open hashbrowncipher opened this issue 3 years ago • 1 comments

Virtual dependencies are metadata on a py_library that indicate the need for a given dependency, but do not resolve that dependency to a set of files. The virtual dependency is "resolved" at the terminal rule (e.g. py_binary/py_test), by use of the resolutions parameter.

This way, any number of terminal rules can depend on a single py_library, and each of them can specify its own preferred resolutions. This permits three useful patterns:

  1. monorepos in which many Python projects coexist, but do not want to link their dependency versions together
  2. single Python projects that want to test against a matrix of different Python interpreters and library versions.
  3. mocking of external Python dependencies with simpler internal versions.

In the example given, I added a dependency on six in the django-virtual project. If the dependency is removed from the virtuals list in django-virtual/mysite/BUILD.bazel, the generated virtualenv does not contain the six library, and an ImportError is produced.

This implementation could potentially be extended to include Python requirements specifiers [1] for each virtual dependency (e.g. "six>=1.10"), to allow another tool (e.g. pip-compile) to gather all of the dependencies and resolve them into a requirements.txt file.

[1] https://pip.pypa.io/en/stable/reference/requirement-specifiers/

hashbrowncipher avatar Sep 22 '22 05:09 hashbrowncipher

CLA assistant check
All committers have signed the CLA.

CLAassistant avatar Sep 22 '22 05:09 CLAassistant

Hi, I'm not sure if @mattem has reached out to you already? Thanks for the PR, and I home we can incorporate your work into #201

alexeagle avatar Oct 09 '23 18:10 alexeagle

That's great news! Should we close this one then?

hashbrowncipher avatar Oct 10 '23 06:10 hashbrowncipher

Done in #201

mattem avatar Nov 19 '23 12:11 mattem