|
1 | 1 | """Implementation of the _py_extension_wrapper rule.""" |
2 | 2 |
|
| 3 | +load("@bazel_skylib//lib:dicts.bzl", "dicts") |
3 | 4 | load("@rules_cc//cc/common:cc_shared_library_info.bzl", "CcSharedLibraryInfo") |
4 | 5 | load("//python/private:attr_builders.bzl", "attrb") |
5 | | -load("//python/private:attributes.bzl", "COMMON_ATTRS") |
| 6 | +load("//python/private:attributes.bzl", "COMMON_ATTRS", "IMPORTS_ATTRS") |
6 | 7 | load("//python/private:builders.bzl", "builders") |
7 | 8 | load("//python/private:py_info.bzl", "PyInfo") |
8 | 9 | load("//python/private:rule_builders.bzl", "ruleb") |
9 | 10 | load("//python/private:toolchain_types.bzl", "PY_CC_TOOLCHAIN_TYPE") |
10 | 11 |
|
11 | 12 | def _py_extension_wrapper_impl(ctx): |
12 | 13 | module_name = ctx.attr.module_name or ctx.label.name |
13 | | - repo_name = ctx.label.workspace_name or ctx.workspace_name |
14 | | - import_path = repo_name |
15 | | - if ctx.label.package: |
16 | | - import_path = repo_name + "/" + ctx.label.package |
17 | 14 |
|
18 | 15 | cc_toolchain = ctx.toolchains["@bazel_tools//tools/cpp:toolchain_type"].cc |
19 | 16 | ext = _get_extension(cc_toolchain) |
@@ -51,29 +48,51 @@ def _py_extension_wrapper_impl(ctx): |
51 | 48 | runfiles_builder.add(csl_target[DefaultInfo].default_runfiles) |
52 | 49 | runfiles = runfiles_builder.build(ctx) |
53 | 50 |
|
| 51 | + # Resolve imports paths relative to the target package and repository: |
| 52 | + # 1. Default (imports = []): No extra search paths are added to sys.path, |
| 53 | + # enforcing clean package-qualified imports (e.g. `from foo.bar import ext`). |
| 54 | + # 2. Relative paths (e.g. imports = ["."]): Resolved relative to `repo_name/package_name` |
| 55 | + # so passing `imports = ["."]` adds the target's package directory to sys.path. |
| 56 | + # 3. Absolute paths (starting with "/"): Stripped of leading "/" and resolved relative to runfiles root. |
| 57 | + imports_list = [] |
| 58 | + repo_name = ctx.label.workspace_name or ctx.workspace_name |
| 59 | + for path in ctx.attr.imports: |
| 60 | + if path.startswith("/"): |
| 61 | + imports_list.append(path[1:]) |
| 62 | + else: |
| 63 | + pkg = ctx.label.package |
| 64 | + full_path = "{}/{}".format(pkg, path) if pkg else path |
| 65 | + if repo_name: |
| 66 | + full_path = "{}/{}".format(repo_name, full_path) |
| 67 | + imports_list.append(full_path) |
| 68 | + |
54 | 69 | return [ |
55 | 70 | DefaultInfo( |
56 | 71 | files = depset([py_dso]), |
57 | 72 | runfiles = runfiles, |
58 | 73 | ), |
59 | 74 | PyInfo( |
60 | 75 | transitive_sources = depset([py_dso]), |
61 | | - imports = depset([import_path]), |
| 76 | + imports = depset(imports_list), |
62 | 77 | ), |
63 | 78 | ] |
64 | 79 |
|
65 | | -PY_EXTENSION_WRAPPER_ATTRS = COMMON_ATTRS | { |
66 | | - "libc": lambda: attrb.String(default = "glibc"), |
67 | | - "module_name": lambda: attrb.String(), |
68 | | - "py_limited_api": lambda: attrb.String( |
69 | | - default = "", |
70 | | - ), |
71 | | - "src": lambda: attrb.Label( |
72 | | - mandatory = True, |
73 | | - providers = [CcSharedLibraryInfo], |
74 | | - doc = "The cc_shared_library target to wrap.", |
75 | | - ), |
76 | | -} |
| 80 | +PY_EXTENSION_WRAPPER_ATTRS = dicts.add( |
| 81 | + COMMON_ATTRS, |
| 82 | + IMPORTS_ATTRS, |
| 83 | + { |
| 84 | + "libc": lambda: attrb.String(default = "glibc"), |
| 85 | + "module_name": lambda: attrb.String(), |
| 86 | + "py_limited_api": lambda: attrb.String( |
| 87 | + default = "", |
| 88 | + ), |
| 89 | + "src": lambda: attrb.Label( |
| 90 | + mandatory = True, |
| 91 | + providers = [CcSharedLibraryInfo], |
| 92 | + doc = "The cc_shared_library target to wrap.", |
| 93 | + ), |
| 94 | + }, |
| 95 | +) |
77 | 96 |
|
78 | 97 | def create_py_extension_wrapper_rule_builder(**kwargs): |
79 | 98 | """Create a rule builder for the wrapper.""" |
|
0 commit comments