Skip to content

Commit 721f66c

Browse files
committed
Improve resolving PYTHONPATH in cocotb_test rule
This PR adds all imports from the deps section to the PYTHONPATH to ensure that all transitive dependencies are resolved correctly. Internal-tag: [#46586] Signed-off-by: Robert Winkler <[email protected]>
1 parent 9d5f2a9 commit 721f66c

File tree

1 file changed

+31
-3
lines changed

1 file changed

+31
-3
lines changed

cocotb/cocotb.bzl

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,36 @@ def _collect_verilog_files(ctx):
6868
def _collect_vhdl_files(ctx):
6969
return ctx.files.vhdl_sources
7070

71+
def _collect_python_imports(ctx):
72+
collected_imports = []
73+
for dep in ctx.attr.deps:
74+
if PyInfo in dep:
75+
root = dep.label.workspace_root
76+
name = dep.label.workspace_name
77+
prefix = root.removesuffix(name)
78+
imports = dep[PyInfo].imports.to_list()
79+
collected_imports += [(prefix, imports)]
80+
81+
return collected_imports
82+
83+
def _get_import_paths_for_python_deps(ctx):
84+
collected_paths = _collect_python_imports(ctx)
85+
86+
import_paths = []
87+
for prefix, paths in collected_paths:
88+
for path in paths:
89+
import_paths += [prefix + path]
90+
91+
return _remove_duplicates_from_list(import_paths)
92+
93+
def _get_test_module_paths(ctx):
94+
module_paths = [module.dirname for module in ctx.files.test_module]
95+
return _remove_duplicates_from_list(module_paths)
96+
97+
def _get_pythonpath_for_exec(ctx):
98+
paths = _get_test_module_paths(ctx) + _get_import_paths_for_python_deps(ctx)
99+
return ":".join([str(p) for p in paths])
100+
71101
def _cocotb_test_impl(ctx):
72102
# prepare arguments for the test command
73103
vhdl_files = _collect_vhdl_files(ctx)
@@ -145,9 +175,7 @@ def _cocotb_test_impl(ctx):
145175
)
146176

147177
# specify pythonpath for the script
148-
test_module_paths = _remove_duplicates_from_list([module.dirname for module in ctx.files.test_module])
149-
pypath = ":".join([str(p) for p in test_module_paths])
150-
env = {"PYTHONPATH": pypath}
178+
env = {"PYTHONPATH": _get_pythonpath_for_exec(ctx)}
151179

152180
# return the information about testing script and its dependencies
153181
return [

0 commit comments

Comments
 (0)