diff --git a/py/private/py_library.bzl b/py/private/py_library.bzl index 3fc5b0ff..332569ac 100644 --- a/py/private/py_library.bzl +++ b/py/private/py_library.bzl @@ -55,6 +55,9 @@ def _make_imports_depset(ctx): import_paths = [ _make_import_path(ctx.label, ctx.workspace_name, base, im) for im in ctx.attr.imports + ] + [ + # Add the workspace name in the imports such that repo-relative imports work. + ctx.workspace_name ] return depset( diff --git a/py/tests/external-deps/expected_pathing b/py/tests/external-deps/expected_pathing index 10e2ec84..fe482f1a 100644 --- a/py/tests/external-deps/expected_pathing +++ b/py/tests/external-deps/expected_pathing @@ -12,6 +12,7 @@ sys path: (pwd)/bazel-out/host/bin/py/tests/external-deps/pathing.runfiles/pathing.venv/lib/python3.9/site-packages (pwd)/bazel-out/host/bin/py/tests/external-deps/pathing.runfiles (pwd)/bazel-out/host/bin/py/tests/external-deps/pathing.runfiles/aspect_rules_py/py/tests/external-deps +(pwd)/bazel-out/host/bin/py/tests/external-deps/pathing.runfiles/aspect_rules_py Entrypoint Path: (pwd)/bazel-out/host/bin/py/tests/external-deps/pathing.runfiles/aspect_rules_py/py/tests/external-deps/pathing.py diff --git a/py/tests/import-pathing/tests.bzl b/py/tests/import-pathing/tests.bzl index c5580893..a9ed5d9f 100644 --- a/py/tests/import-pathing/tests.bzl +++ b/py/tests/import-pathing/tests.bzl @@ -25,10 +25,11 @@ def _can_resolve_path_in_workspace_test_impl(ctx): imports = py_library.make_imports_depset(fake_ctx).to_list() asserts.equals(env, "aspect_rules_py/foo/bar", imports[0]) - # Empty imports array results in no imports + # Empty imports array results in just the workspace root import fake_ctx = _ctx_with_imports([]) imports = py_library.make_imports_depset(fake_ctx).to_list() - asserts.equals(env, 0, len(imports)) + asserts.equals(env, 1, len(imports)) + asserts.equals(env, "aspect_rules_py", imports[0]) # The import path is the parent package fake_ctx = _ctx_with_imports([".."]) diff --git a/py/tests/repo_relative_imports/lib/BUILD.bazel b/py/tests/repo_relative_imports/lib/BUILD.bazel new file mode 100644 index 00000000..e0592cc0 --- /dev/null +++ b/py/tests/repo_relative_imports/lib/BUILD.bazel @@ -0,0 +1,7 @@ +load("//py:defs.bzl", "py_library") + +py_library( + name = "lib", + srcs = ["adder.py"], + visibility = ["//visibility:public"], +) diff --git a/py/tests/repo_relative_imports/lib/adder.py b/py/tests/repo_relative_imports/lib/adder.py new file mode 100644 index 00000000..4693ad3c --- /dev/null +++ b/py/tests/repo_relative_imports/lib/adder.py @@ -0,0 +1,2 @@ +def add(a, b): + return a + b diff --git a/py/tests/repo_relative_imports/test/BUILD.bazel b/py/tests/repo_relative_imports/test/BUILD.bazel new file mode 100644 index 00000000..5e31c3cb --- /dev/null +++ b/py/tests/repo_relative_imports/test/BUILD.bazel @@ -0,0 +1,7 @@ +load("//py:defs.bzl", "py_test") + +py_test( + name = "test", + srcs = ["test.py"], + deps = ["//py/tests/repo_relative_imports/lib"], +) diff --git a/py/tests/repo_relative_imports/test/test.py b/py/tests/repo_relative_imports/test/test.py new file mode 100644 index 00000000..8e87756b --- /dev/null +++ b/py/tests/repo_relative_imports/test/test.py @@ -0,0 +1,3 @@ +from py.tests.repo_relative_imports.lib import adder + +assert adder.add(2, 3) == 5