From cea9eb8fe2d4e5d2bf0a866083f02c3adf7fb932 Mon Sep 17 00:00:00 2001 From: Milan Vukov Date: Mon, 12 Dec 2022 14:34:11 +0100 Subject: [PATCH 1/4] Add failing test for repo-relative imports --- py/tests/repo_relative_imports/lib/BUILD.bazel | 7 +++++++ py/tests/repo_relative_imports/lib/adder.py | 2 ++ py/tests/repo_relative_imports/test/BUILD.bazel | 7 +++++++ py/tests/repo_relative_imports/test/test.py | 3 +++ 4 files changed, 19 insertions(+) create mode 100644 py/tests/repo_relative_imports/lib/BUILD.bazel create mode 100644 py/tests/repo_relative_imports/lib/adder.py create mode 100644 py/tests/repo_relative_imports/test/BUILD.bazel create mode 100644 py/tests/repo_relative_imports/test/test.py 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 From 77963dd82e5495148495783c6f826f575d2d05eb Mon Sep 17 00:00:00 2001 From: Milan Vukov Date: Mon, 12 Dec 2022 14:37:14 +0100 Subject: [PATCH 2/4] Fix repo-relative imports --- py/private/venv/venv.bzl | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/py/private/venv/venv.bzl b/py/private/venv/venv.bzl index da8e6b28..6ccbd54f 100644 --- a/py/private/venv/venv.bzl +++ b/py/private/venv/venv.bzl @@ -56,6 +56,11 @@ def _make_venv(ctx, name = None, main = None, strip_pth_workspace_root = None): # We also need to collect our own "imports" attr. # Can reuse the helper from py_library, as it's the same process imports_depset = _py_library.make_imports_depset(ctx) + # Add the workspace name in the imports such that repo-relative imports work. + imports_depset = depset( + direct = [ctx.workspace_name], + transitive = [imports_depset], + ) pth = ctx.actions.declare_file("%s.pth" % name) From a8cff48bb571660b23768326f6d998f856ddccc5 Mon Sep 17 00:00:00 2001 From: Milan Vukov Date: Wed, 18 Jan 2023 16:55:03 +0100 Subject: [PATCH 3/4] Add workspace root in _make_imports_depset --- py/private/py_library.bzl | 3 +++ py/tests/external-deps/expected_pathing | 1 + py/tests/import-pathing/tests.bzl | 5 +++-- 3 files changed, 7 insertions(+), 2 deletions(-) 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 501cb6f4..e135dfa0 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([".."]) From a5c50f9dcfaa9328c7c8d8496ed99743b7379659 Mon Sep 17 00:00:00 2001 From: Milan Vukov Date: Wed, 18 Jan 2023 16:59:30 +0100 Subject: [PATCH 4/4] rm obsolete addition of workspace root --- py/private/venv/venv.bzl | 5 ----- 1 file changed, 5 deletions(-) diff --git a/py/private/venv/venv.bzl b/py/private/venv/venv.bzl index 6ccbd54f..da8e6b28 100644 --- a/py/private/venv/venv.bzl +++ b/py/private/venv/venv.bzl @@ -56,11 +56,6 @@ def _make_venv(ctx, name = None, main = None, strip_pth_workspace_root = None): # We also need to collect our own "imports" attr. # Can reuse the helper from py_library, as it's the same process imports_depset = _py_library.make_imports_depset(ctx) - # Add the workspace name in the imports such that repo-relative imports work. - imports_depset = depset( - direct = [ctx.workspace_name], - transitive = [imports_depset], - ) pth = ctx.actions.declare_file("%s.pth" % name)