Skip to content

Commit

Permalink
Make find_versions aspect more robust for rules that don't define a `…
Browse files Browse the repository at this point in the history
…deps` attr

It'll still only propagate down the deps, but at least it'll fall back on not doing any propagation, rather than spuriously aborting.

Fixes #8024.

RELNOTES: None
PiperOrigin-RevId: 243859871
  • Loading branch information
brandjon authored and Copybara-Service committed Apr 16, 2019
1 parent 0a1776d commit e84bb33
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -449,4 +449,28 @@ public void requirementNotPropagated() throws Exception {
"");
assertThat(result).isEqualTo(golden);
}

@Test
public void toleratesTargetsWithoutDepsAttr() throws Exception {
scratch.file(
"pkg/rules.bzl",
"def _dummy_rule_impl(ctx):",
" info = PyInfo(transitive_sources = depset([]))",
" return [info]",
"dummy_rule = rule(",
" implementation = _dummy_rule_impl,",
")");
scratch.file(
"pkg/BUILD",
"load(':rules.bzl', 'dummy_rule')",
"dummy_rule(",
" name = 'lib',",
")",
"py_binary(",
" name = 'bin',",
" srcs = ['bin.py'],",
" deps = [':lib'],",
")");
evaluateAspectFor("//pkg:bin");
}
}
24 changes: 15 additions & 9 deletions tools/python/srcs_version.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -176,10 +176,13 @@ def _introduces_version_requirement(target, target_attr, version):
fail("Illegal state")

# No good, check the direct deps' provider fields.
return not any([
_has_version_requirement(dep, version)
for dep in target_attr.deps
])
if not hasattr(target_attr, "deps"):
return True
else:
return not any([
_has_version_requirement(dep, version)
for dep in target_attr.deps
])

def _empty_depswithpaths():
"""Initializes an empty `_DepsWithPathsInfo` object."""
Expand Down Expand Up @@ -235,11 +238,14 @@ def _find_requirements_impl(target, ctx):
# Determine whether this target introduces a requirement. If so, any deps
# that introduce that requirement are not propagated, though they might
# still be considered top-most if an alternate path exists.
dep_tv_infos = [
d[_TransitiveVersionInfo]
for d in ctx.rule.attr.deps
if _TransitiveVersionInfo in d
]
if not hasattr(ctx.rule.attr, "deps"):
dep_tv_infos = []
else:
dep_tv_infos = [
d[_TransitiveVersionInfo]
for d in ctx.rule.attr.deps
if _TransitiveVersionInfo in d
]

if not _has_version_requirement(target, "PY2"):
new_py2 = _empty_depswithpaths()
Expand Down

0 comments on commit e84bb33

Please sign in to comment.