Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion python/pip_install/pip_repository.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,15 @@ def _pip_repository_bzlmod_impl(rctx):
else:
build_contents += _bzlmod_pkg_aliases(repo_name, bzl_packages)

# NOTE: we are using the canonical name with the double '@' in order to
# always uniquely identify a repository, as the labels are being passed as
# a string and the resolution of the label happens at the call-site of the
# `requirement`, et al. macros.
Comment on lines +370 to +373
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the doc. Yeah, this is kind of confusing. We pass the canonical name, but it later gets "reparsed" into a slightly different string by the generated requirements.bzl, so it'll get re-evaluated in (potentially) another context.

Since we're dealing with canonical names, that re-evaluation shouldn't matter much.

resolution happens at the call site of the macro

Well, sort of. IIUC, resolution happens when Label() is called. This occurs when Label() is called manually or when a string is passed to a label attribute (i.e. rule invocation). When it occurs, it'll use the context of the current repo to resolve it. e.g. when repo A calls a macro in repo B, and passes a string label, then the string gets resolved with repo B's mappings. If repo A calls Label() before passing to repo B, then it'll use A's mappings.

if rctx.attr.incompatible_generate_aliases:
macro_tmpl = "@@{name}//{{}}:{{}}".format(name = rctx.attr.name)
else:
macro_tmpl = "@@{name}//:{{}}_{{}}".format(name = rctx.attr.name)

rctx.file("BUILD.bazel", build_contents)
rctx.template("requirements.bzl", rctx.attr._template, substitutions = {
"%%ALL_REQUIREMENTS%%": _format_repr_list([
Expand All @@ -377,7 +386,7 @@ def _pip_repository_bzlmod_impl(rctx):
"@{}//{}:whl".format(repo_name, p) if rctx.attr.incompatible_generate_aliases else "@{}_{}//:whl".format(rctx.attr.name, p)
for p in bzl_packages
]),
"%%NAME%%": rctx.attr.name,
"%%MACRO_TMPL%%": macro_tmpl,
"%%REQUIREMENTS_LOCK%%": str(requirements_txt),
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ def _clean_name(name):
return name.replace("-", "_").replace(".", "_").lower()

def requirement(name):
return "@@%%NAME%%//:" + _clean_name(name) + "_pkg"
return "%%MACRO_TMPL%%".format(_clean_name(name), "pkg")

def whl_requirement(name):
return "@@%%NAME%%//:" + _clean_name(name) + "_whl"
return "%%MACRO_TMPL%%".format(_clean_name(name), "whl")

def data_requirement(name):
return "@@%%NAME%%//:" + _clean_name(name) + "_data"
return "%%MACRO_TMPL%%".format(_clean_name(name), "data")

def dist_info_requirement(name):
return "@@%%NAME%%//:" + _clean_name(name) + "_dist_info"
return "%%MACRO_TMPL%%".format(_clean_name(name), "dist_info")