diff --git a/examples/pytest/BUILD.bazel b/examples/pytest/BUILD.bazel index 41b34215..25889c14 100644 --- a/examples/pytest/BUILD.bazel +++ b/examples/pytest/BUILD.bazel @@ -22,3 +22,21 @@ py_test( "@pypi_pytest//:pkg", ], ) + +py_test( + name = "nested/pytest", + srcs = [ + "foo_test.py", + ":__test__", + ], + env_inherit = ["FOO"], + imports = ["../.."], + main = ":__test__.py", + package_collisions = "warning", + deps = [ + ":__test__", + "@pypi_ftfy//:pkg", + "@pypi_neptune//:pkg", + "@pypi_pytest//:pkg", + ], +) diff --git a/py/private/py_binary.bzl b/py/private/py_binary.bzl index 575ff1ce..73485d26 100644 --- a/py/private/py_binary.bzl +++ b/py/private/py_binary.bzl @@ -32,7 +32,9 @@ def _py_binary_rule_impl(ctx): # each segment from site-packages in the venv to the root of the runfiles tree. # Five .. will get us back to the root of the venv: # {name}.runfiles/.{name}.venv/lib/python{version}/site-packages/first_party.pth - escape = "/".join(([".."] * 4)) + # If the target is defined with a slash, it adds to the level of nesting + target_depth = len(ctx.label.name.split("/")) - 1 + escape = "/".join(([".."] * (4 + target_depth))) # A few imports rely on being able to reference the root of the runfiles tree as a Python module, # the common case here being the @rules_python//python/runfiles target that adds the runfiles helper,